L-1.8: Fork System call with Example | Fork() system call questions

1.02M views1494 WordsCopy TextShare
Gate Smashers
👉Subscribe to our new channel:https://www.youtube.com/@varunainashots In this video Fork system c...
Video Transcript:
Hello Friends, Welcome to Gate Smashers Today's topic is fork system call We're going to discuss about fork system call in this video And in the last of this video you'll get two questions based on everything that we're going to discuss in this video Whose answer you can share with me in the comment box So first we are talking about fork, which is our system call We use fork system call to create a child process Means let's say there's a process Let's say I wrote a program in C. . .
print F "Hello World" So print F "Hello World" is a main program which we can call a parent program But in the parent program if I would write fork anywhere, then a child of that parent will get created Let's say I want to place this marker from here to here So let's say I'm the parent process If parent process is busy somewhere Then we have to create a child process Child process means it'll be my exact clone Child process will be parent's clone which will have its own ID Means Child have its own ID and Parent have its own ID And that child will pick it up from here and will place here Or we have one more method We can use the method of thread What we do in thread is We don't have to make full clone Let's say My both hands are busy then I can create a third hand here Third hand means that rest of the area is doing sharing, means maximum things are getting shared just an extra thing is performing this task But when we do fork then everything is new Although we are doing little bit sharing there also, There also we are sharing code but. . .
Still it has it's own process ID, I'll have its own registers, there are a lot of values, that it does separately That we'll discuss in the video where we'll talk about differences between fork and thread But here we're talking about what is a fork system call, then fork system call simply means to create a child process So the moment we creates a child process then what happens is, fork system call generally returns a value 0 + 1 or -1 You can say +1 or positive value also Zero means child process Zero denotes child, it returns a child value Positive means that process itself which you can call parent process We denotes -1, if child process didn't get created because of some reason Let's say operating system and kernel is busy, so child process didn't get created But we do not consider this case because we are assuming that child process will surely create means you just have to keep only two values in mind, zero means denotes child process And positive value means we're denoting parent process So let's say If I would write here If we'd write a simple program Main(). . .
If I'll write in main, fork() And in fork system call I'll write PrintF ("Hello") PrintF ("Hello"). . .
Now what is the meaning of this The moment I wrote fork. . .
Let's say if I won't write fork here then main(). . .
printF ("Hello") so hello will get printed once because this is a parent process You can call main process, parent process also That process will get execute And it'll print hello on the monitor But, the moment you wrote fork here Fork means. . .
If you wrote fork inside parent process So a child process of parent process will create Means it's already a parent, child process will generate and run in it's parallel and congruently So the child process will create which I'm denoting from C1 So this child process and parent process will execute parallelly Means how many times hello will print here? Two times Reason. .
. Child process will print hello and parent will definitely do it So Hello will print 2 times here So that's the important point Whenever you write fork and we are talking about child process in fork so you can denote child with 0 and we generally denotes parent with positive number If I would write two times fork here. .
. let's say if I would write the fork statement two times Writing fork statement two times means that one is parent process that's complete main process, that's the general process or main process The moment I wrote fork in that parent then fork means parent got a child But parent is still there It didn't get killed. .
. It's alive A child process congruent to it has created here Now next fork, this was the output of writing first fork But the moment you wrote second fork second fork means that this C1 is like a parent for second fork Parent means C1 will create a further child process C2 and C1 is there already. .
. and this parent P will also create its own child process. .
. let's say C3, and P is already there So It means C2, C1, C3 and P All four will print Hello Means these are four are running congruent, out of which these three are child process and one parent process is also here So there're three and one parent This is a very very Important point And talking from GATE and UGC NET point of view, or any other competitive exam in computer science then fork in operating system is a frequently asked topic Many times question comes on fork Let's check once more. .
. Let's say if I will take fork three times here Fork. .
. Fork. .
. Fork And below it let's say I wrote printF. .
. Hello So what it means is. .
. It's a very simple thing Again what we did here. .
. P is a parent process The moment I wrote fork in parent first time Then a child process of parent i. e.
C1 got created and parent is already there Now in child I wrote fork once again The other fork will create a child of this child i. e. C2 And C1 will already get executed Now this P which was running parallel to it this fork will create another child of that P, let's say C3 and P is already there as we saw Now when you'll write fork third time, what will happen is.
. . This C2 will create its child, Let's say C4 And this C2 is already there This C1 will create its own child process let's say C5 and C1 is already there Now C3 will create its own child process let's say C6 and C3 is already there And this P will have its own child Let's say C7 and P is already there So if we'll talk at leaf then 7 child processes are there and one parent process that was the main program So there's one main program that you can call parent, and 7 others are child So how many times Hello will print It'll print total 8 times So how can you calculate total times hello will be printed.
. . 2^n Means the no.
of times fork is written, that's the number "n" And many times they asks how many child process will be generated Total no. of child processes are 2^n - 1 You can see when we used 3 Then these 7 are child processes from C1 to C7 But P is parent process, here if we'll say we wrote fork two times So 2^2 - 1 i. e.
3. . .
C1, C2, C3 which is child and P is parent, but totally how many times hello will get print is 2^n Means total execution will be 2^n and total child process will be 2^n - 1 This is a very important point guys, here first you have to remember this very important point in the video Second, It always returns the ID of child process The moment you'll write fork, it'll return child process's identity as zero We write parent process's ID as positive You can get it as positive 1 at many places then it's fine Here number is not important, here it's positive, negative or zero So negative means child didn't even created Don't consider this case, because we are using fork so child will definitely create But in the worst case it may be possible that kernel is busy somewhere If there's is any problem or error then maybe it won't create child process But if this comes in question, then they'll mention it definitely And in actual we have used fork to create child so in this type of question you never have to use -1 Just remember these main two values and these two formulas over this you'll get many questions that you can try just by putting direct value you'll easily get the question solved So guys if you liked this video then please hit the like button Share it as much as possible And please subscribe my channel Thank You! !
Copyright © 2025. Made with ♥ in London by YTScribe.com