Waiting for processes to finish (using the wait function) in C

แชร์
ฝัง
  • เผยแพร่เมื่อ 10 ก.ค. 2024
  • Check out our Discord server: / discord

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

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

    idk how my professor managed to make this sound so impossibly confusing while you can explain the same concept clearly and with 1/3 of the time he used. Thank you sir.

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

      difference is most likely your professor didn't actually understand the concept, he just memorized it

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

      I feel your pain

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

      ​@@robertgabrielzaharie5405 difference is professor went over it in more depth, which can be exhausting for students.

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

      @@oualid9486 you re right, i was probably frustrated at something that day, which is why I left that comment, it was bold of me to assume things.

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

      literally hahhaaha im in the same boat lmao

  • @AyoubProds
    @AyoubProds 3 ปีที่แล้ว +131

    a year later your videos are still helping hundreds of students around the world, Thank you sir. you're a Hero!

  • @ceo-s
    @ceo-s 6 หลายเดือนก่อน +5

    three years later your videos are still helping hundreds of students around the world, Thank you sir. you're a Hero!

  • @Hgh38
    @Hgh38 3 ปีที่แล้ว +23

    He covered a whole lecture in 10 mins

  • @kekw8379
    @kekw8379 4 ปีที่แล้ว +44

    I was astounded that it only has 614 views, high quality video

  • @pandasoli6581
    @pandasoli6581 10 หลายเดือนก่อน +15

    For next watchers:
    `wait` method comes from ``
    to make `wait(int)` waits for the child process pass `0`.

    • @Yanik490
      @Yanik490 7 หลายเดือนก่อน

      yep needed to do this aswell

    • @deletoblue6059
      @deletoblue6059 2 หลายเดือนก่อน +1

      thanks a lot stranger

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

    Thanks for this video!
    if your are getting error with wait() like too few argument
    then replace wait() with wait(NULL).

  • @shivamsaini4758
    @shivamsaini4758 3 ปีที่แล้ว +16

    MAN!!!! you've just literally saved my day through this video cause I've been searching a lot to get the exact logic behind this parent child executing manner .
    you just simply nailed it !!!

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

    I'm so glad I found your channel. You make really good programming videos. I like the way you explain things. Please keep up the good work. Thanks.

  • @starsky9832
    @starsky9832 3 ปีที่แล้ว +26

    I'm preparing for my OS exam in a month and I find your videos in this series very straight forward and interesting. Keep it up bro!!

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

    Has just discovered your channel. You are so good in explaining!! Thank you so much 🙏🏻🙏🏻

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

    Really enjoyed the simplicity in comparison to stackoverflaw and other forums trying to explain fork and wait. Thanks a lot!

  • @marshallhank891
    @marshallhank891 9 หลายเดือนก่อน +1

    Three years later your videos are still helping hundreds of students around the world, Thank you sir. you're a Hero!

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

    Thank you so much for this video..you cleared all my doubts I was facing. Your channel deserves more views 🤞✌

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

    Super well explained and right to the point. I live in Spain and this whole corona thing has made it kinda difficult for us to find teachers for certain classes. We are forced to learn some things on our own and I gotta say, your videos have been really really useful. Thanks a lot man!

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

    Your video really helps me understand this wait() function! Thanks a ton!!

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

    Incredible series. Thank you!

  • @albanec4702
    @albanec4702 9 หลายเดือนก่อน

    Thank you sooo much, your videos are very clear and inspiring, wish you all the best!

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

    Dude your videos are the best. I have a exam in 8 hours and you explained in 10 minutes what my professor took 2 lectures to and it makes a ton more sense. Keep it up!

  • @a.lae2
    @a.lae2 3 หลายเดือนก่อน

    bro your courses are a godsend man, thanks for making them free and easy to understand

  • @kevinzebb
    @kevinzebb 5 หลายเดือนก่อน

    Thanks for making these videos and making them free, appreciate your work mate.

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

    Thank you, i came from you web-site to specially give you a big thumb up and to subscribe. Great explanation!

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

    Better explanation than my hours of lectures, thank you!

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

    Awesome video serie. Thank you so much Teacher. Your narration is so clear.

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

    I am so glad to discover your videos!

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

    I need to only understand the execution line and I find it in your video. I understand wait and exit function completely now. Thank you so much. You cannot know how much you help to me :)

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

    We have a lesson about that at school 4 hour per week, but I could not understand, sooo I watched Your videos and I get it in 2h, really good job. Thank you for help :)

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

    thanks for this wonderful explanation!

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

    The simplest explanation of wait which I have seen.Just perfect.

  • @haytam1583
    @haytam1583 9 หลายเดือนก่อน

    This video is pure gold, thanks!

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

    😍🥰😍I love this playlist, Thanks so much!!

  • @anneyshas
    @anneyshas 10 หลายเดือนก่อน

    thank you so much, from the bottom of my heart!!!

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

    Great !! You can make anything complex to so simple, understandable form.

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

    Very good video ! thanks you :)

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

    Appreciation comment because the video is so good and perfectly explained , helped me ! ty

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

    Hey man , Just Keep going.
    That's amazing !

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

    After I watch your video, I always give same reaction: "Was it really that simple?!" Thank you.

  • @nunomiguel2203
    @nunomiguel2203 7 หลายเดือนก่อน

    thx you for this video it make C programming much more simple !! :-)

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

    great series!
    thank you

  • @yash_renaissance_athlete
    @yash_renaissance_athlete 8 หลายเดือนก่อน

    dude you are a freaking legend. I am studying at University of Waterloo and my OS professor here took a three hours lectures to miserably at explaining this, what you just explained in 10 mins. and last 2 videos.

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

    Great and coherent explanation!

  • @alt-f4666
    @alt-f4666 3 ปีที่แล้ว +1

    awesome series bro

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

    you should deserve more subs man :)

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

    you are very good at explaining thank you bro

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

    Veryyyyy helpful. Professor went over processes so fast.

  • @ceopeter5145
    @ceopeter5145 8 หลายเดือนก่อน

    The best teaher , thank you

  • @omarabbas6541
    @omarabbas6541 11 หลายเดือนก่อน

    you know what brother,
    it was the hardest thing for me
    you made it easy for me.
    thanks

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

    Thanks alot, that is really helpful

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

    I love this guy already

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

    thanks last year i didnt take linux course in uni cause i didnt have some external sources now i have some idea how to navigate thanks

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

    i didn't know this n thing was even possible 👏👏thank u

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

    you are great teacher !

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

    This guy is awesome!!

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

    Incredible how easy you can make C sound.

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

    Just subscribed, I love the content of your channel i prefer to program in C language it keep things well grounded and i like that
    Please can you make a video to explain the popen() function and how it works

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

      Thanks, will do!

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

    perfect explanation

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

    you are a lifesaver

  • @tusharbarman1924
    @tusharbarman1924 8 หลายเดือนก่อน

    Found your videos at the end of my sem. Nevertheless, these are a goldmine

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

    Very good and entertaining video

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

    thank you :)

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

    Great video!

  • @SingleSidedFilms
    @SingleSidedFilms 9 หลายเดือนก่อน

    i love u save me time in my life

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

    well explained
    from kerala, India

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

    Thank you xD

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

    thanks bestie

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

    If i get my bachelors degree, it's all thanks to you ;)

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

    thank you

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

    God bless you

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

    What compiler do you use? Does it give helpful hints like netbeans does for Java?

  • @louisuchihatm2556
    @louisuchihatm2556 10 หลายเดือนก่อน

    The way my guy is explaining, I feel like I am the rubber duck the dude is using to debug his code😭!
    A happy rubber duck!

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

    I had to call : wait(NULL) and include header : #include for my code to work, thank you sir great video!

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

      Yep, that was an oversight on my part. My bad. Glad you got it working still

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

      @@CodeVault thank u

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

    My compiler tells me that I'm providing too few arguments for the wait function

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

      Yeah... That is my bad. I made a slight mistake here that I corrected later on. You have to call wait like this: wait(NULL);
      That argument is for getting details on how the processes that ended finished its execution.

  • @jeancarlosgutierrez9256
    @jeancarlosgutierrez9256 9 หลายเดือนก่อน

    thanks teacher

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

    Great video, thank you!
    Isn't the fork() function typically used to run processes in parallel, that don't depend on each other? So in this case there clearly seems to be a dependency for the first 5 digits to be printed out first, until the second 5 can be printed.

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

      Yea... that's the idea. For many of these programs that I explain in the processes playlist, it would be better to use threads instead. But then again, it's just a recommendation, you can use them however you want.

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

    Thank you CodeVault! What inspires you to do these tutorials?

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

      Back in university, when I was first learning about these concepts I had a hard time to understand because the teachers weren't explaining too well. I was able to learn on my own but most of my colleagues couldn't thus, I realized it was because of the lack of good tutorials (especially in this area of programming). So that's when I decided I'd try my hand at explaining these concepts (even though I don't have as much experience as some of my teachers from university)

  • @pedroruiz193
    @pedroruiz193 28 วันที่ผ่านมา

    how about calling wait() inside the curly braces for "else", before making n = 6? That way you save the if (id != 0).

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

    Is a no-go in embedded / real-time of course!

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

    Very nice.....

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

    Would adding wait() immediately after n = 6 give the same result?

    • @CodeVault
      @CodeVault  4 ปีที่แล้ว +8

      Yes. I just wanted to be very explicit when showing that function.

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

    thx

  • @user-rg4lj5yq2i
    @user-rg4lj5yq2i 2 ปีที่แล้ว

    Great Thanks from Russia!!

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

    Great video . Could you tell which extension you are you using to display your output in terminal.

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

      No extension. There's an option in the launch.json file: "externalConsole" which I had set to true

  • @user-tp8lq4cn9u
    @user-tp8lq4cn9u 3 ปีที่แล้ว

    Hi, I just want to say that you explain amazing and I would like to ask you some questions if you could . I will thanks you so much

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

      Sure thing, go ahead and ask!

    • @user-tp8lq4cn9u
      @user-tp8lq4cn9u 3 ปีที่แล้ว

      I have an important work in my degree and I have to pass it.
      I want to ask you because you explain really good and I understand you properly.
      I don't understand what we really should do in the main function at the work, Can I attach the file with the question?

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

      You should join our discord and ask there I think: discord.code-vault.net

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

    Hey buddy, your videos are fantastic, thank you so much.
    Are you of the opinion that there is a recurrence relationship between the parent-child process in the context of processes?

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

      I don't think I understand your question. Can you elaborate on this please? Recurrence relationship regarding what? The time complexity of the program?

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

      @@CodeVault Thanks, and yes I see that the question needed some expansion. I meant to say recursion, not recurrence, I tend to use them interchangeably.
      My reason for asking stems from my interested in complexity, yes, but more in the sense of networks, chaos etc.
      What I was asking was, seeing as the child seems to be defined in terms of the parent, main, can it be regarded as a recurrence, or recursive relationship?

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

      In general parent-child processes don't need to have this recursive relationship (you could simply run a completely different program as the child process) so it wouldn't even execute from the same code-base

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

    If you are running this code in FreeBSD include sys/wait.h instead of time.h and wait(NULL) instead of wait()

    • @CodeVault
      @CodeVault  11 หลายเดือนก่อน

      Yes, this was a mistake I correct later in the series. I'm sorry about that

  • @xndr1105
    @xndr1105 4 หลายเดือนก่อน

    god

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

    I get an error when using wait(), implicit declaration. I had to #include and pass in NULL as well. How does it work for you without the NULL?

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

      Yes I made a mistake there which I correct later on. My bad. I intended to use wait(NULL). wait() worked because my Linux distro defined the wait function without parameters too (somewhere in its code)

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

      @@CodeVault Okay, thanks for the reply! I'm learning OS on my own and your videos are helping immensely.

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

    where are you sir? please upload more videos your videos are really very helpful

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

      I will, hopefully soon. Busy with work nowadays

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

    On MacOS I noticed that you have to add an argument of (0) to wait for the child process. I think it works that way because the result is similar. Otherwise it wouldn't compile in gcc.

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

      It's a mistake I correct later in the series. You have to call wait(NULL); and not just wait(). My bad

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

      @@CodeVault Thanks for reply I was searching where did I made mistake in my code

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

    i dont undurstand exactly the probleme of print number how

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

    i would have put wait() right after the else{ wait()....}... really learning a lot for my OS exam, only usually they use wait(NULL) in class

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

      Yeah, that's a mistake I made early on. The proper usage is wait(NULL). My bad

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

    8:56 16 string , Need to write - wait(id); on macOS, until I wrote the id in brackets, the program did not compile

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

      Yes, that's right. I'm sorry, I made a mistake in the video that I later correct in future videos in the same playlist. You could use wait(id) or wait(NULL) there, and both would have worked

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

    how would things change if i wanted the parent to print the numbers 1-5 and the child 6-10?

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

      Try playing around with the value of n for the processes. That's basically all you need to change

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

    greetings

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

    Does "wait()" work in c++? gcc doesn't have any problems with forking, but no matter what i do, gcc doesn't recognize wait(NULL). I think it's handled automatically in c++, although I'm not sure. . .

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

      I've got it! Turns out in C, NULL and nullptr is the same, so you can put NULL into the function that expects a pointer, but in C++ treating NULL as a pointer is illegal. All i had to do is call "wait(nullptr)" instread of "wait(NULL)"

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

    How do you wait multiples childs? Just wait() alone can wait for them all?

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

      No. wait(NULL) only waits for one child. You'd have to call wait multiple times. I sometimes use this loop:
      while (wait(NULL) != -1 || errno != ECHILD) {
      printf("Waited for a child to finish
      ");
      }

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

    I run the exact same code as you however i always get 6 7 8 9 10
    , followed by 1 2 3 4 5 , without using the wait() function .Could this be because i am running linux in a virtual machine?

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

      Interesting... could be that the buffers for stdout are somehow delayed. Try adding: fflush(stdout); after each printf

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

      Probably not. I got the same thing and I'm not running VM.

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

      Im getting the same result (6 7 8 9 10
      , 1 2 3 4 5) even with adding fflush(stdout); after each printf

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

      I also get this output whether I use 'fflush' or not.
      Also, I don't understand why he got "6 1 7 8 2 9 3 1 0 4 5" the first time he ran it, and then "1 6 2 7 3 8 4 9 5 10" the second time, when the only change he made was to add "
      ".
      Shouldn't the second one have been split over two lines? Is it possible in VS code to accidentally run your previous code without saving changes you've just made? Is that what happened? so the first time he ran it, it was without fflush, and then the second time was with fflush but without
      ?
      Sorry for being dumb.
      edit: also, I'm not being critical. this whole series is amazingly helpful.

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

    i am bit confused since wait(), on my machine, expects a pointer so i went with wait(&id)

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

      Yes. That was a small mistake I made. My call wait() is the same as wait(NULL) but for some reason my machine has the definition without parameters. Later in the playlist I only use the wait call with 1 parameter.

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

      @@CodeVault is this related to your compiler? afaik wait() is a system call isn't it? (not a library function)

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

    I have a question tho, what is the difference between wait() and wait(NULL)? Are they the same thing?

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

      The difference is a mistake I made at the beginning of this series. My bad.
      wait(NULL) is what you should use

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

      @@CodeVault Thank you for clarification!

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

    Great video, but why do we use two processes if one has to wait for the other? Wouldn't it be easier to use just one process in that case?

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

      Well, if you want to take advantage of the multiple cores of your CPU, it would be faster with multiple processes. Since the main process waits at the end of its execution.

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

      @@CodeVault I see, I hadn't realized the importance of waiting at the end. Thank you!