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.
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 !!!
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!
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!
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 :)
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 :)
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.
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
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.
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.
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.
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?
I don't think I understand your question. Can you elaborate on this please? Recurrence relationship regarding what? The time complexity of the program?
@@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?
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
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)
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.
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?
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.
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
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)
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 "); }
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. . .
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)"
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.
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?
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.
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
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
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.
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.
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
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.
difference is most likely your professor didn't actually understand the concept, he just memorized it
I feel your pain
@@robertgabrielzaharie5405 difference is professor went over it in more depth, which can be exhausting for students.
@@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.
literally hahhaaha im in the same boat lmao
a year later your videos are still helping hundreds of students around the world, Thank you sir. you're a Hero!
a year and a half! ;) . Thank you
2 years now
3 years now
For next watchers:
`wait` method comes from ``
to make `wait(int)` waits for the child process pass `0`.
yep needed to do this aswell
thanks a lot stranger
three years later your videos are still helping hundreds of students around the world, Thank you sir. you're a Hero!
He covered a whole lecture in 10 mins
I was astounded that it only has 614 views, high quality video
It has a few more now
bro your courses are a godsend man, thanks for making them free and easy to understand
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 !!!
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!
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.
Thanks for this video!
if your are getting error with wait() like too few argument
then replace wait() with wait(NULL).
Thanks for the tip :D, really helped me out!
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!!
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!
Really enjoyed the simplicity in comparison to stackoverflaw and other forums trying to explain fork and wait. Thanks a lot!
Has just discovered your channel. You are so good in explaining!! Thank you so much 🙏🏻🙏🏻
Three years later your videos are still helping hundreds of students around the world, Thank you sir. you're a Hero!
Thank you so much for this video..you cleared all my doubts I was facing. Your channel deserves more views 🤞✌
Thank you sooo much, your videos are very clear and inspiring, wish you all the best!
thank you so much, from the bottom of my heart!!!
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 :)
Thanks for making these videos and making them free, appreciate your work mate.
Thank you, i came from you web-site to specially give you a big thumb up and to subscribe. Great explanation!
Incredible series. Thank you!
Awesome video serie. Thank you so much Teacher. Your narration is so clear.
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 :)
The simplest explanation of wait which I have seen.Just perfect.
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.
This video is pure gold, thanks!
Better explanation than my hours of lectures, thank you!
Your video really helps me understand this wait() function! Thanks a ton!!
After I watch your video, I always give same reaction: "Was it really that simple?!" Thank you.
Great !! You can make anything complex to so simple, understandable form.
you know what brother,
it was the hardest thing for me
you made it easy for me.
thanks
I am so glad to discover your videos!
thanks for this wonderful explanation!
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!
Appreciation comment because the video is so good and perfectly explained , helped me ! ty
Hey man , Just Keep going.
That's amazing !
😍🥰😍I love this playlist, Thanks so much!!
thx you for this video it make C programming much more simple !! :-)
great series!
thank you
I love this guy already
Incredible how easy you can make C sound.
Veryyyyy helpful. Professor went over processes so fast.
The best teaher , thank you
awesome series bro
Great and coherent explanation!
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
Thanks, will do!
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
you are very good at explaining thank you bro
Very good video ! thanks you :)
What compiler do you use? Does it give helpful hints like netbeans does for Java?
This guy is awesome!!
perfect explanation
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.
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.
you should deserve more subs man :)
i didn't know this n thing was even possible 👏👏thank u
Thanks alot, that is really helpful
thanks teacher
you are great teacher !
My compiler tells me that I'm providing too few arguments for the wait function
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.
i love u save me time in my life
I had to call : wait(NULL) and include header : #include for my code to work, thank you sir great video!
Yep, that was an oversight on my part. My bad. Glad you got it working still
@@CodeVault thank u
Found your videos at the end of my sem. Nevertheless, these are a goldmine
you are a lifesaver
well explained
from kerala, India
Is a no-go in embedded / real-time of course!
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?
I don't think I understand your question. Can you elaborate on this please? Recurrence relationship regarding what? The time complexity of the program?
@@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?
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
God bless you
Thank you CodeVault! What inspires you to do these tutorials?
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)
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.
It's a mistake I correct later in the series. You have to call wait(NULL); and not just wait(). My bad
@@CodeVault Thanks for reply I was searching where did I made mistake in my code
Great video . Could you tell which extension you are you using to display your output in terminal.
No extension. There's an option in the launch.json file: "externalConsole" which I had set to true
Would adding wait() immediately after n = 6 give the same result?
Yes. I just wanted to be very explicit when showing that function.
thank you :)
Thank you xD
Very good and entertaining video
thank you
Great Thanks from Russia!!
thanks bestie
where are you sir? please upload more videos your videos are really very helpful
I will, hopefully soon. Busy with work nowadays
i dont undurstand exactly the probleme of print number how
Great video!
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
Sure thing, go ahead and ask!
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?
You should join our discord and ask there I think: discord.code-vault.net
If i get my bachelors degree, it's all thanks to you ;)
how would things change if i wanted the parent to print the numbers 1-5 and the child 6-10?
Try playing around with the value of n for the processes. That's basically all you need to change
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.
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
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?
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)
@@CodeVault Okay, thanks for the reply! I'm learning OS on my own and your videos are helping immensely.
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?
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
@@CodeVault Thank you for your answer
I follow all the videos with enthusiasm
How do you wait multiples childs? Just wait() alone can wait for them all?
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
");
}
Very nice.....
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. . .
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)"
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?
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.
@@CodeVault I see, I hadn't realized the importance of waiting at the end. Thank you!
hei CodeVault your site is down.
can we get the code the same way you did in thread playlist. it was very convinient
Yeah, I noticed. Some certificate expired. Should be up now! Thanks for letting me know!
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?
Interesting... could be that the buffers for stdout are somehow delayed. Try adding: fflush(stdout); after each printf
Probably not. I got the same thing and I'm not running VM.
Im getting the same result (6 7 8 9 10
, 1 2 3 4 5) even with adding fflush(stdout); after each printf
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.
What if I fork the child process instead, will two process have 0 id ?
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
I have a question tho, what is the difference between wait() and wait(NULL)? Are they the same thing?
The difference is a mistake I made at the beginning of this series. My bad.
wait(NULL) is what you should use
@@CodeVault Thank you for clarification!
8:56 16 string , Need to write - wait(id); on macOS, until I wrote the id in brackets, the program did not compile
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
If you are running this code in FreeBSD include sys/wait.h instead of time.h and wait(NULL) instead of wait()
Yes, this was a mistake I correct later in the series. I'm sorry about that
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 ?
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.
you will create a Zombie. look it up
Or orphan. I forgot which one it was
@@hamids4550 Look it up.
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.
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