Previous questions
1.
int main () {
int i;
for (i = 0; i<5;i++) {
int pid = fork ();
if (PID = = 0) {
Do something
} else {
Do something
}
}
Do Somthing,sleep
return 0;
}
5
10
15
32
2
2.Linux how many "-" will be printed:
int main (void) {
int i;
for (i=0;i<4;i++) {
Fork ();
printf ("-\n");
}
return 0;
}
16
30
32
15
3. How many "-" of the following program output?
int main (void)
{
int i;
for (i = 0; i < 2; i++) {
Fork ();
printf ("-");
}
return 0;
}
2
4
6
8
Analysis: Problem 1, only need to make a process diagram (similar to a binary tree) to draw out, calculate the last layer of nodes can be,
Question 2: Calculate the number of prints, not the number of creation processes
Question 3: The biggest difference with the title 2 is that the title 2 is printed with a newline character, the title 3 does not.
Properties of the fork () system call
- The fork () system call is a system call that creates a child process in its own process under UNIX, one call, two returns, and if the return is 0, it is a child process, and if the return value >0, it is the parent process (the PID of the child process is the return value), which is known to the public.
- At the fork () call, the entire parent process space is copied to the child process as it is: directives, variable values, program call stacks, environment variables, buffers, and so on.
About the fork () function under Linux