Help with output generated by this C code using fork() - c

I am trying to figure out the output for a block of C code using fork() and I am having some problems understanding why it comes out the way it does. I understand that when using fork() it starts another instance of the program in parallel and that the child instance will return 0. Could someone explain step by step the output to the block of code below? Thank you. EDIT: I FORGOT TO ADD THE EXIT(1) AFTER THE FOR LOOP. MY APOLOGIES.
main() { int status, i;
for (i=0; i<2; ++i){
printf("At the top of pass %d\n", i);
if (fork() == 0){
printf("this is a child, i=%d\n", i);
} else {
wait(&status);
printf("This is a parent, i=%d\n", i);
}
}
exit(1);
}

What happens on the first loop is that the first process forks. In one, fork() returns 0 and in the other it returns the pid of the child process So you'll get one that prints out "this is a child" and one that prints out "this is a parent". Both of those processes continue through the loop, incremement i to 1 and fork() again. Now you've got four processes: two children and two parents. All four processes will increment i to 2 and break out of the loop.
If you increased the loop termination condition to i<3 then the next time around the loop all four processes will execute fork() and you'd have eight processes in total. If there was no limit in the loop, you'd have a fork bomb where you'd just exponentially create more and more processes each loop until the system runs out of resources.

This code can be tricky to explain. The reason why is that the first child does not exit and will itself call fork. Try modifying the code to include the process id on each print line such as:
printf("At the top of pass %d in pid %u\n", i, getpid());
Then notice how the child becomes the parent...

Related

Could someone explain how fork works?

I don't really understand how fork() works.I understand examples with one fork,but when there are more than one call I don't.I have an example like this and it prints 4 lines of hello, how many processes are created?
int main(void)
{
fork();
fork();
printf("hello\n");
return 0;
}
After fork() call, both processes (original and spawned) continue to execute from next line of code. So both processes execute second fork() instruction, so in the end you have 4 processes. Hence you see 4 instances of "hello" lines printed.
One picture is worth a thousand words:
The fork() syscall essentially creates a "clone" of the process executing it. Both "clones" run almost identically (except for the return value of fork()).
The first call to fork() is executed by one process (let's call that one "P"), which creates a second process "C". Now there are two processes, which both execute the second line in your main() function. So both processes, P and C, create a new process. This is why you end up with a total of 4 processes, all of which print "hello" exactly once.
The following example might make that behaviour a bit clearer:
int main() {
printf("process %d: start\n", getpid());
int r1 = fork();
printf("process %d: first fork() returned %d\n", getpid(), r1);
int r2 = fork();
printf("process %d: second fork() returned %d\n", getpid(), r2);
}
On my system, it outputs the following:
process 12953: start
process 12953: first fork() returned 12954
process 12954: first fork() returned 0
process 12953: second fork() returned 12955
process 12955: second fork() returned 0
process 12954: second fork() returned 12956
process 12956: second fork() returned 0

How to take advantage of each process when using two fork()'s in C

So, I've been trying to understand forks, and although I understand the basics (ie. another process gets created with a copy of the original data when we use fork()), I can't really grasp actually doing things with these processes that are created.
For example: I have to write a program that will call fork() twice to create 4 processes in total. When one process is created I have to print out the pid with getpid(). After printing out four ID's, my program is supposed to print a single letter 10 times.
For example, parent 1 will print 'A', child 1 will print 'B', child 2 will print 'C' and the parent of that will print 'D'. To do this, I have to use putchar(ch); and flush the output with fflush(stdout).
This means the output will look like this:
Process created, ID: 3856
Process created, ID: 3857
Process created, ID: 3858
Process created, ID: 3859
AAAAAABBBBBBBCDCDCDCDCDCDCBBBDCDCAAAADCD
So far, I've gotten the four processes to print with this code:
int main(void) {
pid_t child1, child2;
child1 = fork();
child2 = fork();
printf("Process created. ID: %d\n", getpid());
}
But I don't know how to use wait() to have everything print out randomly, after I have printed the ids.
To get everything I need to print out to be a "random mess," what should I do? Should I call functions like this?
// in main
for(int i = 0; i < 10; ++i) {
char_parent1();
char_child1();
char_parent2();
char_child2();
}
return 0;
}
void char_parent1()
{
putchar('A');
fflush(stdout);
}
void char_child1()
{
putchar('B');
fflush(stdout);
}
// and so on for char_parent2() and char_child2()
In that case, if my professor says I have to basically print things out concurrently and randomly, then why should I be using wait()?
Each process needs to know which letter it is supposed to print. That means you have to analyze the values in child1 and child2. For example, one process has two zeros; it might print D.
Arguably, each process needs to know whether it is a parent or not. If it is not a parent, it can simply exit after printing 10 copies of its letter, each followed by a fflush(). If it is a parent, after waiting for its children to die, it should exit. This means that the original process will exit last. It could usefully output a newline after its last child has died. You might or might not print diagnostic information about dead children as you go.

Visually what happens to fork() in a For Loop

I have been trying to understand fork() behavior. This time in a for-loop. Observe the following code:
#include <stdio.h>
void main()
{
int i;
for (i=0;i<3;i++)
{
fork();
// This printf statement is for debugging purposes
// getppid(): gets the parent process-id
// getpid(): get child process-id
printf("[%d] [%d] i=%d\n", getppid(), getpid(), i);
}
printf("[%d] [%d] hi\n", getppid(), getpid());
}
Here is the output:
[6909][6936] i=0
[6909][6936] i=1
[6936][6938] i=1
[6909][6936] i=2
[6909][6936] hi
[6936][6938] i=2
[6936][6938] hi
[6938][6940] i=2
[6938][6940] hi
[1][6937] i=0
[1][6939] i=2
[1][6939] hi
[1][6937] i=1
[6937][6941] i=1
[1][6937] i=2
[1][6937] hi
[6937][6941] i=2
[6937][6941] hi
[6937][6942] i=2
[6937][6942] hi
[1][6943] i=2
[1][6943] hi
I am a very visual person, and so the only way for me to truly understand things is by diagramming. My instructor said there would be 8 hi statements. I wrote and ran the code, and indeed there were 8 hi statements. But I really didn’t understand it. So I drew the following diagram:
Diagram updated to reflect comments :)
Observations:
Parent process (main) must iterate the loop 3 times. Then printf is called
On each iteration of parent for-loop a fork() is called
After each fork() call, i is incremented, and so every child starts a for-loop from i before it is incremented
At the end of each for-loop, "hi" is printed
Here are my questions:
Is my diagram correct?
Why are there two instances of i=0 in the output?
What value of i is carried over to each child after the fork()? If the same value of i is carried over, then when does the "forking" stop?
Is it always the case that 2^n - 1 would be a way to count the number of children that are forked? So, here n=3, which means 2^3 - 1 = 8 - 1 = 7 children, which is correct?
Here's how to understand it, starting at the for loop.
Loop starts in parent, i == 0
Parent fork()s, creating child 1.
You now have two processes. Both print i=0.
Loop restarts in both processes, now i == 1.
Parent and child 1 fork(), creating children 2 and 3.
You now have four processes. All four print i=1.
Loop restarts in all four processes, now i == 2.
Parent and children 1 through 3 all fork(), creating children 4 through 7.
You now have eight processes. All eight print i=2.
Loop restarts in all eight processes, now i == 3.
Loop terminates in all eight processes, as i < 3 is no longer true.
All eight processes print hi.
All eight processes terminate.
So you get 0 printed two times, 1 printed four times, 2 printed 8 times, and hi printed 8 times.
Yes, it's correct. (see below)
No, i++ is executed after the call of fork, because that's the way the for loop works.
If all goes successfully, yes. However, remember that fork may fail.
A little explanation on the second one:
for (i = 0;i < 3; i++)
{
fork();
}
is similar to:
i = 0;
while (i < 3)
{
fork();
i++;
}
So i in the forked processes(both parent and child) is the value before increment. However, the increment is executed immediately after fork(), so in my opinion, the diagram could be treat as correct.
To answer your questions one by one:
Is my diagram correct?
Yes, essentially. It's a very nice diagram, too.
That is to say, it's correct if you interpret the i=0 etc. labels as referring to full loop iterations. What the diagram doesn't show, however, is that, after each fork(), the part of the current loop iteration after the fork() call is also executed by the forked child process.
Why are there two instances of i=0 in the output?
Because you have the printf() after the fork(), so it's executed by both the parent process and the just forked child process. If you move the printf() before the fork(), it will only be executed by the parent (since the child process doesn't exist yet).
What value of i is carried over to each child after the fork()? If the same value of i is carried over, then when does the "forking" stop?
The value of i is not changed by fork(), so the child process sees the same value as its parent.
The thing to remember about fork() is that it's called once, but it returns twice — once in the parent process, and once in the newly cloned child process.
For a simpler example, consider the following code:
printf("This will be printed once.\n");
fork();
printf("This will be printed twice.\n");
fork();
printf("This will be printed four times.\n");
fork();
printf("This will be printed eight times.\n");
The child process created by fork() is an (almost) exact clone of its parent, and so, from its own viewpoint, it "remembers" being its parent, inheriting all of the parent process's state (including all variable values, the call stack and the instruction being executed). The only immediate difference (other than system metadata such as the process ID returned by getpid()) is the return value of fork(), which will be zero in the child process but non-zero (actually, the ID of the child process) in the parent.
Is it always the case that 2^n - 1 would be a way to count the number of children that are forked? So, here n=3, which means 2^3 - 1 = 8 - 1 = 7 children, which is correct?
Every process that executes a fork() turns into two processes (except under unusual error conditions, where fork() might fail). If the parent and child keep executing the same code (i.e. they don't check the return value of fork(), or their own process ID, and branch to different code paths based on it), then each subsequent fork will double the number of processes. So, yes, after three forks, you will end up with 2³ = 8 processes in total.

Fork() command issue

While going thorough fork() command i got struck with a question .
how many no.of processes are created by the end of 12th second, if
time starts from 0th second? Process id's start from 0.
Pseudo code
while(true)
{
sleep 1second;
if( getpid() % 2 == 0 )
{
fork();
printf("Hello\n");
}
}
when i run above code on my system it is not showing output on konsole. Is no . of process at end of 12 sec is dependent on OS ?Need suggestion as i am not good in fork()
Since when do process IDs "start at 0"? Not even when the system boots; the first process has the id 1 :-)
You're only fork()ing when your own process ID is even; so if it happens to be odd then nothing will happen... which means that if you run the program several times, sometimes it will do something and sometimes it won't.
Add this after your printf:
fflush(stdout);
But you have a fundamental problem with your logic. fork() returns 0 in the child, and the child pid in the parent. You don't check, so both the parent and the child continue doing the loop, which happens again, and again, and again, forever. You need to change the loop body to this:
if(fork() == 0)
{
printf("Hello!\n");
fflush(stdout);
}

fork() causes column headers to print for each process

I'm writing a simple C program using fork() to create a binary tree of processes. I am able to get all the output I need (pid's of process, its parent, and its two children). Unfortunately, each forked process wants to print out the column headers. How do I make sure that the printf for the headers is executed only once?
# include <stdio.h>
# include <stdlib.h>
# include <sys/types.h>
# include <unistd.h>
# include <sys/wait.h>
int main(int argc, char *argv[]){
//Declarations
int i;
int child_1_pid, child_2_pid;
int num_levels = atoi(argv[1]);
//Output banners
//execlp("/bin/echo", "echo", "Level\tProcs\tParent\tChild1\tChild2\nNo.\tID\tID\tID\tID", (char *) NULL);
//if(getpid() > 0)
printf("Level\tProcs\tParent\tChild1\tChild2\nNo.\tID\tID\tID\tID");
//Creates binary tree of processes
for(i = 0; i < num_levels; i++){
if((child_1_pid = fork()) && (child_2_pid = fork())){
printf("\n%d\t%d\t%d\t%d\t%d", i, getpid(), getppid(), child_1_pid, child_2_pid);
sleep(2); //prevents parent from terminating before child can get ppid (parent's pid)
break; //why?
}
}//end for
printf("\n"); //EXPLAIN ME!!
exit(0);
}//end main
There's some more code (error checking really), but my real problem is that the printf under the output banners section executes multiple times, giving output like this (but correctly aligned):
Level Procs Parent Child1 Child2
No. ID ID ID ID
No. ID ID ID ID
No. ID ID ID ID
No. ID ID ID ID
No. ID ID ID ID
No. ID ID ID ID
No. ID ID ID ID
0 30796 24743 30797 30798
1 30797 30796 30799 30800
1 30798 30796 30801 30802
I've tried a few ideas (including those commented out under the banner section), but nothing seems to work and most "fixes" make the problem even worse!
First, the if in the for-loop does not behave as you want it to. Remember that after the fork, it returns the child PID in the parent process and 0 in the child. So inside the loop, the first fork assigns a value to child_1_pid in the parent and continues to the second clause. The child does not enter the if but continues to the next for-loop iteration. The very same happens with the second clause. So only the main process should ever be able to enter the body of the if, but no child process. I wonder why the output suggests otherwise.
So to get your "binary tree", you should actually have this:
// COMPLETELY UNTESTED
for(i = 0; i < num_levels; i++){
if (!(child_1_pid = fork()) || !(child_2_pid = fork())) {
printf("\n%d\t%d\t%d\t%d\t%d", i, getpid(), getppid(), child_1_pid, child_2_pid);
// A child process, go on to next iteration.
continue;
}
// A parent process. Wait for children, then stop.
if (child_1_pid) wait();
if (child_2_pid) wait();
break;
}
The strange output of the banners has to do with flushing of streams. Normally, fprintf only flushed on newline (\n), IIRC. So there's still stuff in the buffer after the fork that has not been flushed yet, and each child runs printf("\n"); and thus flushes out the buffer content.
The solution is to either add a "\n" to the end of the very first printf, or call fflush(stdout); before the for loop.
Here's something to try, although I'm a little rusty with this stuff. In the line where you print out your banners:
printf("Level\tProcs\tParent\tChild1\tChild2\nNo.\tID\tID\tID\tID");
It may be that everything after the \n is being left in the ouput buffer, so it's still there when each child is forked. Try adding another \n at the end of that printf, and removing the \n from the beginning of the printf inside the loop.
Replace:
printf("Level\tProcs\tParent\tChild1\tChild2\nNo.\tID\tID\tID\tID");
With:
puts("Level\tProcs\tParent\tChild1\tChild2\nNo.\tID\tID\tID\tID");
Replace:
printf("\n%d\t%d\t%d\t%d\t%d", i, getpid(), getppid(), child_1_pid, child_2_pid);
With:
printf("%d\t%d\t%d\t%d\t%d\n", i, getpid(), getppid(), child_1_pid, child_2_pid);
Remove:
printf("\n");
Read 2.5.1 here:
http://pubs.opengroup.org/onlinepubs/9699919799/functions/V2_chap02.html
Note that after a fork(), two handles exist where one existed before. The application shall ensure that, if both handles can ever be accessed, they are both in a state where the other could become the active handle first. The application shall prepare for a fork() exactly as if it were a change of active handle. (If the only action performed by one of the processes is one of the exec functions or _exit() (not exit()), the handle is never accessed in that process.)
What this means is that, before calling fork you should call fflush on any streams that you intend to use in both processes after the fork.

Resources