fd, dup()/dup2() system call tutorial

แชร์
ฝัง
  • เผยแพร่เมื่อ 12 ธ.ค. 2014
  • In this video, we illusrate the basics of dup() and dup2() and how you can use these system calls to modify the file descriptors of your system. Modifying these file descriptors will allow us to change the direction of input and output of our program (e.g: instead of having our program read stdin from the terminal, we can have our program read stdin from our file, instead).

ความคิดเห็น • 69

  • @mohammedmokhtar
    @mohammedmokhtar 3 ปีที่แล้ว +74

    When you don't get it from the man page for 30 minutes and get it from a 4-minute video made in 2014...

    • @unknown_try1
      @unknown_try1 3 ปีที่แล้ว

      It just means u didnt get it at all :)

    • @mrmerfeo4320
      @mrmerfeo4320 2 หลายเดือนก่อน

      @@unknown_try1 It just means that the man page is meant or other people with decade+ experience and not your average person trying to learn

  • @gabrielt6756
    @gabrielt6756 6 ปีที่แล้ว +18

    You're describing this better than the prof. Thank you!

  • @13taras
    @13taras ปีที่แล้ว +2

    You saved me from failing system programming. God bless you both

  • @SoufianeSabiri
    @SoufianeSabiri 8 ปีที่แล้ว +7

    From your videos I learned faster, I can't believe myself, you guys make it look easy !

  • @TheEmbeddedLinuxDude
    @TheEmbeddedLinuxDude 5 ปีที่แล้ว +2

    Was having a hard time understanding dup/dup2 until I saw this video. Thanks guys!

  • @eshgholah
    @eshgholah 9 ปีที่แล้ว +16

    Very well performed guys. Well done.

  • @tal8643
    @tal8643 9 ปีที่แล้ว +3

    Best video of dupp in youtube ! congrats guys !

  • @helene2632
    @helene2632 2 ปีที่แล้ว +2

    From 42Paris, Thank you for this video !! I obviously subscribe and hope you will do more videos (ok, 6 years after this one :( ) . I will talk about your videos in 42! It is so easier to understand by drawings than by code . MERCI

  • @SWAGCOWVIDEO
    @SWAGCOWVIDEO 4 ปีที่แล้ว +6

    I got an assignment involving pipes - and I had no idea what a file descriptor or the dup() system call was. I would've been screwed without this tutorial. Thank you so much!

  • @newtao
    @newtao 5 ปีที่แล้ว +1

    such more i learn, more my loving for programming increases

  • @jamesdouitsis8180
    @jamesdouitsis8180 3 ปีที่แล้ว +1

    Awesome video guys!! Made a very strange concept very easy to grasp!!

  • @alexmathai6245
    @alexmathai6245 5 ปีที่แล้ว +1

    An excellent explanation, thank you very much.

  • @TsumugiFox
    @TsumugiFox 2 หลายเดือนก่อน

    Amazing work. Perfectly clean and succinct

  • @svetameanssun
    @svetameanssun 2 หลายเดือนก่อน

    Thank you! It is a lot more information that I could find in other videos!❤

  • @user-wy5oz5yn9d
    @user-wy5oz5yn9d 6 ปีที่แล้ว

    Thank you so much im gonna go through your whole channel rn maybe ill pass the course

  • @chloeduan8301
    @chloeduan8301 9 ปีที่แล้ว +1

    thank you so much! this was really helpful.

  • @doge-coin
    @doge-coin 6 ปีที่แล้ว

    This is awesome. Thanks a lot!

  • @747knarf
    @747knarf 9 ปีที่แล้ว

    tnx this video really helped with my assignment!!

  • @gabrielgarciamarquez5988
    @gabrielgarciamarquez5988 5 ปีที่แล้ว

    U guys helped me so much! Thanks!!!!

  • @CanardWC008
    @CanardWC008 ปีที่แล้ว

    Really nice and clear video, thank you guys :)

  • @berdanakyurek7804
    @berdanakyurek7804 3 ปีที่แล้ว

    This was like a dream. Thanks.

  • @MrVerao9
    @MrVerao9 8 ปีที่แล้ว

    Very nice helpful video, thanks a lot guys.

  • @cele_stin
    @cele_stin 4 ปีที่แล้ว

    Super useful, thank you for the upload :)

  • @aphasiac
    @aphasiac 6 ปีที่แล้ว

    Very succinct. Well done.

  • @rishiihsir2499
    @rishiihsir2499 7 ปีที่แล้ว

    awsome work guys.... keep up with good work

  • @MontylsHere
    @MontylsHere 9 ปีที่แล้ว +1

    awesome video thanks a lot

  • @garyablett9284
    @garyablett9284 2 ปีที่แล้ว

    Awesome bro thank you both

  • @reljapesic988
    @reljapesic988 3 ปีที่แล้ว

    Thanks guys a lot!

  • @hannahleah5875
    @hannahleah5875 ปีที่แล้ว

    The best! Thank youu

  • @yosipipi
    @yosipipi 8 ปีที่แล้ว

    thanks a lot guys!

  • @smitadeshpande6525
    @smitadeshpande6525 7 ปีที่แล้ว

    thank you so much!!:)

  • @tessacarstairs5998
    @tessacarstairs5998 ปีที่แล้ว

    9 months later I'm back at this video again. Still begging you two to become profs

  • @lennyatomz8389
    @lennyatomz8389 5 ปีที่แล้ว

    Great job, Gentz! :c)

  • @AvinashKumar-up3sh
    @AvinashKumar-up3sh 9 ปีที่แล้ว +2

    very very thnx.

  • @misagh9
    @misagh9 6 ปีที่แล้ว

    keep up the good work

  • @hankzhou279
    @hankzhou279 7 ปีที่แล้ว

    Thank you!

  • @depdoprogramming2750
    @depdoprogramming2750 3 ปีที่แล้ว

    very helpful. Thanks

  • @g4b3r901
    @g4b3r901 4 ปีที่แล้ว +3

    Your videos are quite good. Why don't you make more? Do you still code?

  • @waleedikram452
    @waleedikram452 ปีที่แล้ว

    well choreographed

  • @jeff2573
    @jeff2573 6 ปีที่แล้ว

    Awesome vid

  • @shikharjoshi267
    @shikharjoshi267 5 ปีที่แล้ว

    its awesome

  • @chenxiyuan5184
    @chenxiyuan5184 8 ปีที่แล้ว

    Thanks

  • @1Man2Go
    @1Man2Go 8 ปีที่แล้ว +11

    This was a lot of good information. But I'd like it to better explain the part where we open a file into the fd[0] slot. and how that's done.

    • @matthewhook4197
      @matthewhook4197 7 ปีที่แล้ว +5

      Like this:
      close(0);
      dup(newfd);
      It works because dup() will always use the lowest numbered unused descriptor for the new descriptor and since we closed stdin (0) it'll use that.
      The above is almost equivalent to:
      dup2(newfd, 0);

    • @notker88
      @notker88 7 ปีที่แล้ว

      So dub2() also calls close() ?

    • @guy7329
      @guy7329 2 ปีที่แล้ว +1

      @@notker88 Yes, however it isn't recommended to do the step yourself
      It is preferable for you to use dup2() alone rather than close() -> dup2() (with dup() you can't really control what file descriptor ot will be copied to) because of race conditions

  • @christopherwitt6283
    @christopherwitt6283 9 ปีที่แล้ว

    thanks

  • @its_adit
    @its_adit ปีที่แล้ว

    damnnn so cool

  • @Avadakedvr
    @Avadakedvr 4 ปีที่แล้ว +1

    U didn't explain how u replace stdin with your own file, u just wrote "ourfile".

  • @Rockeru03
    @Rockeru03 8 ปีที่แล้ว +2

    Does anybody know how did he actually fill slot 0 with ourfile

    • @magnuseckenfels8207
      @magnuseckenfels8207 8 ปีที่แล้ว

      u dont have to say the Programm to read from Slot 0. this is a Default Setting. it reads whatever it finds in Slot 0.

    • @blasttrash
      @blasttrash 8 ปีที่แล้ว

      I think something like this
      int file_descriptor_new = open("OurFile", .., .. ) // give option flags and mode of your choice
      close(0); // you are closing stdin
      dup(file_descriptor_new);
      here dup will make file_descriptor_new to point to "OurFile"
      P.S. I am not sure if this is 100% right. Correct me if I am wrong. Thanks

  • @magicalskifilms
    @magicalskifilms 2 ปีที่แล้ว

    goated

  • @user-hl5rn8sy1g
    @user-hl5rn8sy1g 4 ปีที่แล้ว

    Why did you guy's stop uploading videos?

  • @zeuspolancosalgado3385
    @zeuspolancosalgado3385 3 ปีที่แล้ว

    How did you learn this?

  • @user-rt6cl5pb9f
    @user-rt6cl5pb9f 9 ปีที่แล้ว

    well done , but next tutorial take your time and talk please more slowly , because you understand it already , but we have to follow you . and the last words in every sentence you say are low in voice like a song when it ends , be more clear. please. but thank you its a very helpfull tutorial. but take more time , we have time thats what learning is about , taking time ..... thanks

  • @ChrisAthanas
    @ChrisAthanas 3 หลายเดือนก่อน

    Unix is kinda hacky but better than what was available at the time which was nothing

  • @halilibrahimustun50
    @halilibrahimustun50 2 ปีที่แล้ว

    I miss Brendon and Paul 🥺

  • @BradenBest
    @BradenBest 8 ปีที่แล้ว +7

    It's kinda misleading to describe files like that, because that's not how they appear in memory.
    When you say
    FILE *f = fopen(...);
    you are initializing a struct _IO_FILE in memory, and saving a pointer to the resulting block. The file descriptor described in the video is the member (struct _IO_FILE *)->_fileno.
    pastebin.com/raw.php?i=78tpKe2G

    • @EvilSapphireR
      @EvilSapphireR 6 ปีที่แล้ว

      Braden Best this was very helpful especially with the commented code at pastebin. I don't know why this comment didn't receive any attention.

  • @skylarzhao5343
    @skylarzhao5343 ปีที่แล้ว

    牛逼

  • @LittleDUP2
    @LittleDUP2 3 ปีที่แล้ว

    Everytime mfs search up my name they get this shit and it’s his last upload 6 years ago. HE LEFT ME WITH A CURSE😂

  • @Nyllet1221
    @Nyllet1221 7 ปีที่แล้ว

    it would've probably been a good idea to mention the assert function to test if the closes and dups actually worked. it can at least be used while youre coding the program and removed at a later stage just for ease of debugging.

  • @martininorway4113
    @martininorway4113 5 ปีที่แล้ว

    because he's asian doesn't mean he cant narrate. Give my man some confidence cmon

    • @pimdoodle
      @pimdoodle 5 ปีที่แล้ว

      Pretty sure they were previous students at my University, as my professor links to these videos and says so. And the projects are partner projects, so these two probably both had to participate for EC points.

  • @tessacarstairs5998
    @tessacarstairs5998 2 ปีที่แล้ว +2

    if you two choose to become profs youll have 5/5 on ratemyprofs ;)