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 !!!
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!
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 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.
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 :)
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.
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.
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
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)
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)
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
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.
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
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
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
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.
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)"
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 ????
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)
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
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 "); }
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?
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
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?
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
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
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
@@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; }
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!
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.
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
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
4 years now!..
two 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
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 !!!
I was astounded that it only has 614 views, high quality video
It has a few more 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
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'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!!
Really enjoyed the simplicity in comparison to stackoverflaw and other forums trying to explain fork and wait. Thanks a lot!
bro just saved me litteraly 16 hours of lab sessions in around 5 videos
After I watch your video, I always give same reaction: "Was it really that simple?!" Thank you.
bro your courses are a godsend man, thanks for making them free and easy to understand
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 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.
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 making these videos and making them free, appreciate your work mate.
The simplest explanation of wait which I have seen.Just perfect.
Better explanation than my hours of lectures, thank you!
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 :)
Thank you, i came from you web-site to specially give you a big thumb up and to subscribe. Great explanation!
Incredible how easy you can make C sound.
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!
Thanks a lot!
you know what brother,
it was the hardest thing for me
you made it easy for me.
thanks
Thank you so much for this video..you cleared all my doubts I was facing. Your channel deserves more views 🤞✌
Veryyyyy helpful. Professor went over processes so fast.
Great !! You can make anything complex to so simple, understandable form.
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.
Thank you sooo much, your videos are very clear and inspiring, wish you all the best!
Has just discovered your channel. You are so good in explaining!! Thank you so much 🙏🏻🙏🏻
I am so glad to discover your videos!
Awesome video serie. Thank you so much Teacher. Your narration is so clear.
Your video really helps me understand this wait() function! Thanks a ton!!
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!
Hey man , Just Keep going.
That's amazing !
Appreciation comment because the video is so good and perfectly explained , helped me ! ty
Incredible series. Thank you!
I love this guy already
This video is pure gold, thanks!
thank you so much, from the bottom of my heart!!!
😍🥰😍I love this playlist, Thanks so much!!
Found your videos at the end of my sem. Nevertheless, these are a goldmine
thx you for this video it make C programming much more simple !! :-)
If i get my bachelors degree, it's all thanks to you ;)
thanks for this wonderful explanation!
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 should deserve more subs man :)
awesome series bro
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
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
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
Would adding wait() immediately after n = 6 give the same result?
Yes. I just wanted to be very explicit when showing that function.
Very good video ! thanks 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)
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.
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!
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.
Is a no-go in embedded / real-time of course!
The best teaher , thank you
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
where are you sir? please upload more videos your videos are really very helpful
I will, hopefully soon. Busy with work nowadays
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
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
well explained
from kerala, India
you are very good at explaining thank you bro
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
You should always use wait when creating new child processes. Otherwise you will have a resource leak
what would happen if you didn't youse the fflush?
It would only print until printf finds a new line character (or some other cache flush occurs)
Great and coherent explanation!
i didn't know this n thing was even possible 👏👏thank u
how about calling wait() inside the curly braces for "else", before making n = 6? That way you save the if (id != 0).
For sure you can do that!
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.
This guy is awesome!!
great series!
thank you
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
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 Thanks from Russia!!
you are great teacher !
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
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 ????
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)
Please what will happen when you call the wait function in the else block of the first if condition.
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
That's great !
you are a lifesaver
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!
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 dont undurstand exactly the probleme of print number how
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
");
}
Can’t you put the wait() function in the else-statement? Please tell me if I am wrong
Yes, you can
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!
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?
Possibly. Make sure you call wait(NULL); like this. It's a mistake I made in the video
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
why for child process the n starts at 1 and ends at 5 , and for main process starts at 6 and ends at 10?
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
Bro u r the best ❤
What compiler do you use? Does it give helpful hints like netbeans does for Java?
if i add sys/wait.h header file - im getting error in VS -any suggestions ??
The whole processes is for unix systems only. On Windows you have to use completely different functions
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
I noticed wait uses
#include
#include
and you can just pass it NULL.
Is this a new thing?
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
I see. Thanks for the reply and all you do to make these videos. Really saved me on my last assignment. Cheers!
perfect explanation
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
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
@@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;
}
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!
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.
@@CodeVault oh okayyy. Thanks a lot man!
Very good and entertaining video
Why do you need fflush if you are using printf?
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