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

Поделиться
HTML-код
  • Опубликовано: 2 май 2024
  • Check out our Discord server: / discord

Комментарии • 246

  • @rayliu4351
    @rayliu4351 2 года назад +184

    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

  • @AyouBChiCha
    @AyouBChiCha 3 года назад +130

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

  • @pandasoli6581
    @pandasoli6581 8 месяцев назад +13

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

    • @Yanik490
      @Yanik490 4 месяца назад

      yep needed to do this aswell

    • @deletoblue6059
      @deletoblue6059 15 дней назад +1

      thanks a lot stranger

  • @ceo-s
    @ceo-s 4 месяца назад +4

    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 3 года назад +44

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

  • @a.lae2
    @a.lae2 Месяц назад

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

  • @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 !!!

  • @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!

  • @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.

  • @venkatasaimaddisetty3284
    @venkatasaimaddisetty3284 2 года назад +8

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

  • @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!!

  • @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!

  • @MSVInkognito
    @MSVInkognito 3 года назад +9

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

  • @milajoy945
    @milajoy945 3 года назад

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

  • @marshallhank891
    @marshallhank891 6 месяцев назад +1

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

  • @shreyasom1738
    @shreyasom1738 2 года назад

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

  • @albanec4702
    @albanec4702 7 месяцев назад

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

  • @anneyshas
    @anneyshas 8 месяцев назад

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

  • @yusufokutan9081
    @yusufokutan9081 9 месяцев назад

    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 :)

  • @kevinzebb
    @kevinzebb 2 месяца назад

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

  • @bama2619
    @bama2619 2 года назад

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

  • @loganjensen5404
    @loganjensen5404 2 года назад

    Incredible series. Thank you!

  • @EnesARAT
    @EnesARAT 3 года назад

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

  • @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 :)

  • @shreyasjoshi3742
    @shreyasjoshi3742 2 года назад

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

  • @yash_renaissance_athlete
    @yash_renaissance_athlete 6 месяцев назад

    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.

  • @haytam1583
    @haytam1583 7 месяцев назад

    This video is pure gold, thanks!

  • @ytwuuuuuuu
    @ytwuuuuuuu 3 года назад +2

    Better explanation than my hours of lectures, thank you!

  • @suwin1051
    @suwin1051 2 года назад

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

  • @cankoban
    @cankoban 2 года назад +1

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

  • @skmplanet9591
    @skmplanet9591 27 дней назад

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

  • @omarabbas6541
    @omarabbas6541 8 месяцев назад

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

  • @jetsonearth4819
    @jetsonearth4819 2 года назад

    I am so glad to discover your videos!

  • @sukritkapil9816
    @sukritkapil9816 3 года назад +1

    thanks for this wonderful explanation!

  • @louisuchihatm2556
    @louisuchihatm2556 8 месяцев назад

    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!

  • @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 !

  • @yazou1307
    @yazou1307 Год назад +1

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

  • @nunomiguel2203
    @nunomiguel2203 4 месяца назад

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

  • @GameSmilexD
    @GameSmilexD 3 года назад

    great series!
    thank you

  • @samuelvalentine7846
    @samuelvalentine7846 Год назад

    I love this guy already

  • @pedrodonda2067
    @pedrodonda2067 3 года назад +4

    Incredible how easy you can make C sound.

  • @Deleted__
    @Deleted__ 2 года назад

    Veryyyyy helpful. Professor went over processes so fast.

  • @ceopeter5145
    @ceopeter5145 5 месяцев назад

    The best teaher , thank you

  • @alt-f4666
    @alt-f4666 3 года назад +1

    awesome series bro

  • @DJndOfficial
    @DJndOfficial 2 года назад

    Great and coherent explanation!

  • @yassine321
    @yassine321 3 года назад

    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

  • @ryldy787gekerz9
    @ryldy787gekerz9 27 дней назад

    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

  • @georgebot447
    @georgebot447 Год назад

    you are very good at explaining thank you bro

  • @sley664
    @sley664 2 года назад +2

    Very good video ! thanks you :)

  • @beef2244
    @beef2244 3 года назад

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

  • @pedromedeiros8104
    @pedromedeiros8104 Месяц назад

    This guy is awesome!!

  • @zakarialahmouz3902
    @zakarialahmouz3902 2 года назад

    perfect explanation

  • @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.

  • @domulko44
    @domulko44 3 года назад +1

    you should deserve more subs man :)

  • @abdelghanimoumen2524
    @abdelghanimoumen2524 Год назад

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

  • @cezzar5233
    @cezzar5233 2 года назад

    Thanks alot, that is really helpful

  • @jeancarlosgutierrez9256
    @jeancarlosgutierrez9256 7 месяцев назад

    thanks teacher

  • @oussemamhiri9713
    @oussemamhiri9713 3 года назад +1

    you are great teacher !

  • @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.

  • @SingleSidedFilms
    @SingleSidedFilms 7 месяцев назад

    i love u save me time in my life

  • @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

  • @tusharbarman1924
    @tusharbarman1924 6 месяцев назад

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

  • @nobi_nose9469
    @nobi_nose9469 3 года назад

    you are a lifesaver

  • @ismail8973
    @ismail8973 3 года назад

    well explained
    from kerala, India

  • @zeus1117
    @zeus1117 Год назад

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

  • @SeaHorseNSparrow
    @SeaHorseNSparrow Год назад

    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?

    • @SeaHorseNSparrow
      @SeaHorseNSparrow Год назад

      @@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

  • @maximilienbernede3191
    @maximilienbernede3191 Год назад

    God bless you

  • @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)

  • @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

  • @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

  • @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.

  • @TheKunalingam
    @TheKunalingam 3 года назад +1

    thank you :)

  • @davidkudo9731
    @davidkudo9731 2 года назад

    Thank you xD

  • @universalponcho
    @universalponcho 2 года назад

    Very good and entertaining video

  • @sakolh
    @sakolh Год назад

    thank you

  • @user-rg4lj5yq2i
    @user-rg4lj5yq2i 2 года назад

    Great Thanks from Russia!!

  • @marsmcfly
    @marsmcfly 2 года назад

    thanks bestie

  • @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

  • @yacinefadili4094
    @yacinefadili4094 3 года назад

    i dont undurstand exactly the probleme of print number how

  • @lavredisgoume
    @lavredisgoume 3 года назад

    Great video!

  • @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

  • @leonardvanvlierberghe8835
    @leonardvanvlierberghe8835 2 года назад +1

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

  • @antwniaaax4022
    @antwniaaax4022 Месяц назад

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

    • @CodeVault
      @CodeVault  27 дней назад

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

  • @Dante-ot8xg
    @Dante-ot8xg 2 года назад

    How can I implement this for more than two processes? For instance, let's say I created three processes (parent, child, and grandchild) and I want all the processes to wait until the grandchild has finished its execution then for the child, and lastly the parent.

    • @CodeVault
      @CodeVault  2 года назад

      Well, you just call wait on the child and parent processes. This will cause the parent to also wait for the grandchild since the child process doesn't finish until the grandchild finishes execution

  • @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.

  • @mostafakhastkhodaei9517
    @mostafakhastkhodaei9517 Год назад

    hi thank you for your video
    it's awesome
    when i use wait() in ubuntu, its need argument but you're using that
    why i can't use that same as you?

    • @CodeVault
      @CodeVault  Год назад

      I made a mistake in the video that is later corrected. You can call wait(NULL); and it would have the same result as in the video

    • @mostafakhastkhodaei9517
      @mostafakhastkhodaei9517 Год назад

      @@CodeVault Thank you for your answer
      I follow all the videos with enthusiasm

  • @kiseijuu7420
    @kiseijuu7420 Месяц назад

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

    • @CodeVault
      @CodeVault  27 дней назад

      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
      ");
      }

  • @shaiktahir7755
    @shaiktahir7755 3 года назад

    Very nice.....

  • @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)"

  • @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!

  • @kaal_bhairav_23
    @kaal_bhairav_23 2 года назад

    hei CodeVault your site is down.
    can we get the code the same way you did in thread playlist. it was very convinient

    • @CodeVault
      @CodeVault  2 года назад

      Yeah, I noticed. Some certificate expired. Should be up now! Thanks for letting me know!

  • @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.

  • @-zero-
    @-zero- 2 года назад +1

    What if I fork the child process instead, will two process have 0 id ?

    • @CodeVault
      @CodeVault  2 года назад +1

      I see this misconception a lot lately. What fork() returns is NOT the process is. Only in the parent process fork() returns the child's process id but in the child process fork() always returns 0 (but that 0 doesn't represent the process id). The process id can be found out using the getpid() function.
      So calling fork() inside the child process would create another process with a different process id than the rest. But the return value of that fork() call inside that inner child process will also be 0

  • @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!

  • @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

  • @poincareelcartografo5406
    @poincareelcartografo5406 9 месяцев назад

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

    • @CodeVault
      @CodeVault  9 месяцев назад

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

  • @jabuci
    @jabuci 4 года назад +1

    Crystal clear :) Thanks! Can the child process also wait for the parent? What if we want the numbers in this order 6 7 8 9 10 1 2 3 4 5 ?

    • @CodeVault
      @CodeVault  4 года назад +5

      Technically you could but it's not recommended. There's a wait function that waits for specific process IDs. The problem is that, usually, you shouldn't have child processes without a parent process. So, if the child process waits for the parent process to finish that child process becomes what is called, a zombie process.
      We'll get into more details later on regarding this situation.

    • @hamids4550
      @hamids4550 3 года назад

      you will create a Zombie. look it up

    • @hamids4550
      @hamids4550 3 года назад

      Or orphan. I forgot which one it was

    • @jabuci
      @jabuci 3 года назад +1

      @@hamids4550 Look it up.

  • @poincareelcartografo5406
    @poincareelcartografo5406 9 месяцев назад

    I'm running your code with Vim in FreeBSD and it never mess up the order of the numbers. It doesn't matter if I use fflush(), wait() or whatever other function in between. It seems that by default waits to end one process to start the next one.

    • @duarteincio2838
      @duarteincio2838 Месяц назад

      i got the same issue and was scrolling through the comments o see if anyone had the same problem. Have you found any answers for that or is it just a thing that vim do? Thanks