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

Поділитися
Вставка
  • Опубліковано 15 гру 2024

КОМЕНТАРІ • 264

  • @rayliu4351
    @rayliu4351 3 роки тому +225

    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 3 роки тому +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 роки тому +6

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

    • @robertgabrielzaharie5405
      @robertgabrielzaharie5405 2 роки тому +5

      @@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 2 роки тому +1

      literally hahhaaha im in the same boat lmao

  • @AyoubProds
    @AyoubProds 3 роки тому +141

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

  • @lucsanglas8933
    @lucsanglas8933 2 роки тому +17

    two 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

  • @shivamsaini4758
    @shivamsaini4758 4 роки тому +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 !!!

  • @kekw8379
    @kekw8379 4 роки тому +46

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

  • @pandasoli6581
    @pandasoli6581 Рік тому +20

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

    • @Yanik490
      @Yanik490 Рік тому

      yep needed to do this aswell

    • @deletoblue6059
      @deletoblue6059 8 місяців тому +1

      thanks a lot stranger

  • @williamma8367
    @williamma8367 3 роки тому +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!

  • @starsky9832
    @starsky9832 3 роки тому +27

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

  • @MSVInkognito
    @MSVInkognito 4 роки тому +9

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

  • @christiansaab2044
    @christiansaab2044 Місяць тому +1

    bro just saved me litteraly 16 hours of lab sessions in around 5 videos

  • @cankoban
    @cankoban 3 роки тому +1

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

  • @a.lae2
    @a.lae2 8 місяців тому

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

  • @alexmelillo1247
    @alexmelillo1247 4 роки тому +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!

  • @yash_renaissance_athlete
    @yash_renaissance_athlete Рік тому

    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.

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

  • @kevinzebb
    @kevinzebb 10 місяців тому

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

  • @shreyasjoshi3742
    @shreyasjoshi3742 2 роки тому

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

  • @ytwuuuuuuu
    @ytwuuuuuuu 3 роки тому +2

    Better explanation than my hours of lectures, thank you!

  • @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 3 роки тому +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 :)

  • @bama2619
    @bama2619 3 роки тому

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

  • @pedrodonda2067
    @pedrodonda2067 4 роки тому +4

    Incredible how easy you can make C sound.

  • @venkatasaimaddisetty3284
    @venkatasaimaddisetty3284 2 роки тому +13

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

  • @omarabbas6541
    @omarabbas6541 Рік тому

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

  • @shreyasom1738
    @shreyasom1738 3 роки тому

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

  • @Deleted__
    @Deleted__ 3 роки тому

    Veryyyyy helpful. Professor went over processes so fast.

  • @skmplanet9591
    @skmplanet9591 8 місяців тому

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

  • @woofelator
    @woofelator 4 роки тому +2

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

    • @CodeVault
      @CodeVault  4 роки тому +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.

  • @albanec4702
    @albanec4702 Рік тому

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

  • @milajoy945
    @milajoy945 3 роки тому

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

  • @jetsonearth4819
    @jetsonearth4819 2 роки тому

    I am so glad to discover your videos!

  • @EnesARAT
    @EnesARAT 4 роки тому

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

  • @suwin1051
    @suwin1051 3 роки тому

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

  • @louisuchihatm2556
    @louisuchihatm2556 Рік тому +1

    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!

  • @alijafari6422
    @alijafari6422 2 роки тому

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

  • @alintdg9896
    @alintdg9896 3 роки тому

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

  • @loganjensen5404
    @loganjensen5404 3 роки тому

    Incredible series. Thank you!

  • @samuelvalentine7846
    @samuelvalentine7846 2 роки тому

    I love this guy already

  • @haytam1583
    @haytam1583 Рік тому

    This video is pure gold, thanks!

  • @anneyshas
    @anneyshas Рік тому

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

  • @SuzyZou1995
    @SuzyZou1995 2 роки тому +1

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

  • @tusharbarman1924
    @tusharbarman1924 Рік тому

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

  • @nunomiguel2203
    @nunomiguel2203 Рік тому

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

  • @leonardvanvlierberghe8835
    @leonardvanvlierberghe8835 3 роки тому +1

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

  • @sukritkapil9816
    @sukritkapil9816 4 роки тому +1

    thanks for this wonderful explanation!

  • @ryldy787gekerz9
    @ryldy787gekerz9 8 місяців тому

    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

  • @domulko44
    @domulko44 4 роки тому +1

    you should deserve more subs man :)

  • @alt-f4666
    @alt-f4666 4 роки тому +1

    awesome series bro

  • @throwaway8342
    @throwaway8342 2 роки тому

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

    • @CodeVault
      @CodeVault  2 роки тому +1

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

    • @throwaway8342
      @throwaway8342 2 роки тому

      @@CodeVault thank u

  • @rebei
    @rebei 2 роки тому +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  2 роки тому +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 2 роки тому

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

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

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

  • @sley664
    @sley664 3 роки тому +2

    Very good video ! thanks 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)

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

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

  • @athanasiospapazoglou7310
    @athanasiospapazoglou7310 4 роки тому +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  4 роки тому

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

    • @ivandjordjevic00
      @ivandjordjevic00 4 роки тому

      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.

  • @zeus1117
    @zeus1117 2 роки тому

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

  • @ceopeter5145
    @ceopeter5145 Рік тому

    The best teaher , thank you

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

  • @brightlyricsmusic
    @brightlyricsmusic 3 роки тому

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

    • @CodeVault
      @CodeVault  3 роки тому

      I will, hopefully soon. Busy with work nowadays

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

  • @antwniaaax4022
    @antwniaaax4022 9 місяців тому

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

    • @CodeVault
      @CodeVault  8 місяців тому

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

  • @ismail8973
    @ismail8973 4 роки тому

    well explained
    from kerala, India

  • @georgebot447
    @georgebot447 Рік тому

    you are very good at explaining thank you bro

  • @ig.jay_thakkar
    @ig.jay_thakkar 4 роки тому +1

    Without using wait() we can do this way:
    #include
    #include
    #include
    #include
    int main()
    {
    int n;
    int id = fork();
    if(id!=0)
    {
    n=1;
    }
    else
    {
    n=6;
    }
    int i;
    for(int i=n;i

    • @CodeVault
      @CodeVault  4 роки тому +2

      You should always use wait when creating new child processes. Otherwise you will have a resource leak

  • @Andre-ij8ft
    @Andre-ij8ft 2 роки тому +1

    what would happen if you didn't youse the fflush?

    • @CodeVault
      @CodeVault  2 роки тому +1

      It would only print until printf finds a new line character (or some other cache flush occurs)

  • @DJndOfficial
    @DJndOfficial 3 роки тому

    Great and coherent explanation!

  • @abdelghanimoumen2524
    @abdelghanimoumen2524 2 роки тому

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

  • @pedroruiz193
    @pedroruiz193 6 місяців тому

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

    • @CodeVault
      @CodeVault  3 місяці тому

      For sure you can do that!

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

  • @pedromedeiros8104
    @pedromedeiros8104 9 місяців тому

    This guy is awesome!!

  • @GameSmilexD
    @GameSmilexD 3 роки тому

    great series!
    thank you

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

  • @Rojfos
    @Rojfos 2 роки тому

    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 2 роки тому

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

  • @СтепанЗазян
    @СтепанЗазян 2 роки тому

    Great Thanks from Russia!!

  • @oussemamhiri9713
    @oussemamhiri9713 4 роки тому +1

    you are great teacher !

  • @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  Рік тому

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

  • @HimmatSingh-ml1re
    @HimmatSingh-ml1re Місяць тому

    Doubt
    if(id!=0){wait()} => doesn't this mean that if it is child process, then excute the wait(), hence stopping the child process. Also 1-5 is printed by main followed by 6-10 by child ????

    • @CodeVault
      @CodeVault  Місяць тому

      fork() returns 0 in the child process and the child process id in the parent process, therefore this condition is true for the parent process. The order in which the numbers are printed is non-deterministic (it can be in any order)

  • @Kingdomprogrammer
    @Kingdomprogrammer 2 роки тому

    Please what will happen when you call the wait function in the else block of the first if condition.

    • @CodeVault
      @CodeVault  2 роки тому

      That would basically have the same result since we're calling wait(NULL) on the main process... waiting for the child process to finish execution

    • @Kingdomprogrammer
      @Kingdomprogrammer 2 роки тому

      That's great !

  • @nobi_nose9469
    @nobi_nose9469 3 роки тому

    you are a lifesaver

  • @kaal_bhairav_24
    @kaal_bhairav_24 3 роки тому

    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  3 роки тому

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

  • @Dante-ot8xg
    @Dante-ot8xg 3 роки тому

    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  3 роки тому

      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

  • @yacinefadili4094
    @yacinefadili4094 3 роки тому

    i dont undurstand exactly the probleme of print number how

  • @kiseijuu7420
    @kiseijuu7420 8 місяців тому

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

    • @CodeVault
      @CodeVault  8 місяців тому

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

  • @thomasmuller9024
    @thomasmuller9024 Рік тому

    Can’t you put the wait() function in the else-statement? Please tell me if I am wrong

  • @jimmykuma6046
    @jimmykuma6046 2 роки тому

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

    • @CodeVault
      @CodeVault  2 роки тому +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 2 роки тому

      @@CodeVault Thank you for clarification!

  • @irahazda
    @irahazda 2 роки тому

    When I try to run this code on my MacOS, apparently, the parent process will do all the printing first, and only then the child will proceed with the other printings. Is it due to the difference in the OS?

    • @CodeVault
      @CodeVault  2 роки тому

      Possibly. Make sure you call wait(NULL); like this. It's a mistake I made in the video

  • @poincareelcartografo5406
    @poincareelcartografo5406 Рік тому

    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 9 місяців тому

      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

  • @AfonsoP2001
    @AfonsoP2001 3 роки тому

    why for child process the n starts at 1 and ends at 5 , and for main process starts at 6 and ends at 10?

    • @CodeVault
      @CodeVault  3 роки тому +1

      Because of this:
      if (id == 0) {
      n = 1;
      } else {
      n = 6;
      }
      The if statement is true only in the child process and it's false in the parent process

  • @vedprabhat8949
    @vedprabhat8949 14 днів тому

    Bro u r the best ❤

  • @beef2244
    @beef2244 3 роки тому

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

  • @manisrinivas_hyd
    @manisrinivas_hyd Рік тому

    if i add sys/wait.h header file - im getting error in VS -any suggestions ??

    • @CodeVault
      @CodeVault  Рік тому

      The whole processes is for unix systems only. On Windows you have to use completely different functions

  • @גילשוקרון-כ4ל
    @גילשוקרון-כ4ל 4 роки тому

    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  4 роки тому

      Sure thing, go ahead and ask!

    • @גילשוקרון-כ4ל
      @גילשוקרון-כ4ל 4 роки тому

      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  4 роки тому +1

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

  • @goodknight3072
    @goodknight3072 3 роки тому

    I noticed wait uses
    #include
    #include
    and you can just pass it NULL.
    Is this a new thing?

    • @CodeVault
      @CodeVault  3 роки тому +1

      Yes, I made a slight mistake in the video. You should call wait with the NULL parameter: wait(NULL); instead. I correct this in further videos on this playlist

    • @goodknight3072
      @goodknight3072 3 роки тому

      I see. Thanks for the reply and all you do to make these videos. Really saved me on my last assignment. Cheers!

  • @zakarialahmouz3902
    @zakarialahmouz3902 2 роки тому

    perfect explanation

  • @karthikeyans2825
    @karthikeyans2825 3 роки тому

    hey man i just have a doubt. when i used vfork() instead of wait for the parent process to wait till the child process terminates i get the output along with a segmentation fault. why is that so? can u pls explain

    • @CodeVault
      @CodeVault  3 роки тому +1

      vfork and wait have different use cases. Send in the code and maybe I can figure out what exactly you mean. Here's the docs for vfork: linux.die.net/man/2/vfork

    • @karthikeyans2825
      @karthikeyans2825 3 роки тому

      @@CodeVault #include
      #include
      #include
      #include
      int main(int argc, char *argv[])
      {
      int id = vfork();
      int n;
      if (id == 0)
      {
      n = 1;
      }
      else
      {
      n = 6;
      }
      // if (id != 0)
      // {
      // wait(NULL);
      // }
      for (int i = n; i < n + 5; i++)
      {
      printf("%d ", i);
      fflush(stdout);
      }
      if (id != 0)
      {
      printf("
      ");
      }
      return 0;
      }

    • @karthikeyans2825
      @karthikeyans2825 3 роки тому

      vfork() differs from fork(2) in that the calling thread is suspended until the child terminates (either normally, by calling _exit(2), or abnormally, after delivery of a fatal signal)
      This was why i tried vfork() and removed the wait(). I'm just learning so I might be wrong. Just wanted to know what I was doign. Thanks for the quick reply!

    • @CodeVault
      @CodeVault  3 роки тому

      Ahhh, it seems like there are some restrictions on what you can modify with vfork. At the docs under "Standard description": www.man7.org/linux/man-pages/man2/vfork.2.html
      (From POSIX.1) The vfork() function has the same effect as
      fork(2), except that the behavior is undefined if the process
      created by vfork() either modifies any data other than a variable
      of type pid_t used to store the return value from vfork(), or
      returns from the function in which vfork() was called, or calls
      any other function before successfully calling _exit(2) or one of
      the exec(3) family of functions.
      So, because you're modifying that n variable, it sends out a segmentation fault.

    • @karthikeyans2825
      @karthikeyans2825 3 роки тому

      @@CodeVault oh okayyy. Thanks a lot man!

  • @universalponcho
    @universalponcho 3 роки тому

    Very good and entertaining video

  • @ThatGuy68580
    @ThatGuy68580 3 роки тому

    Why do you need fflush if you are using printf?

    • @CodeVault
      @CodeVault  3 роки тому +1

      It's just to make sure the string is shown in the console at that time. Sometimes stdout buffers things and some messages get delayed which I didn't want