1,fork() Is the creation process function .

2,c At the beginning of the procedure , It's going to happen A process , When the process is executed to fork() When , A child process is created .

3, At this point, the parent process and the child process coexist , They both go down together c Program code .

4, Attention is needed !!! After the child process is successfully created ,fork Yes, two values are returned , One represents the parent process , One represents the subprocess : The value representing the parent process is a string of numbers , This string of numbers belongs to the subprocess ID( address ); One represents the subprocess , The value is 0.

Let's write a passage to understand ( Notes are important )

The result of running is the following parent process printf What comes out is the subprocess Of ID That's the number , Subprocess printf And 0:

<> Deep learning

1-1.c
#include<unistd.h> #include<stdio.h> int main() { int p1,p2; if(p1=fork())
/* Child process created successfully */ // The parent process returns a string of numbers >1, Meet conditions , Execute the following statement putchar('b');// First output b, Then the father killed himself ( end ) else
// This is due to the subprocess fork by 0, The child process is executed here { if(p2=fork())
// Here, the child process creates its own child process ( Let's call it sun process first ), At this point, the child process fork Return to sun process's ID>1, You can execute the following statement
putchar('c');// Subprocess execution , output c else putchar('a'); /* Parent process execution */
// I think the parent process in the teacher's note above is “ Sun Jincheng ”, Instead of the parent process in the beginning , Parent process at the beginning putchar Out b after , It's been killed , Don't believe it
printf("%d",p2);, See if it's output 0,fork() by 0, It is sun Jincheng } printf("\n"); }
results of enforcement ( Pay attention to the code comments , Generally speaking, it is the output of the parent process b, Child process output c, Sun process output a . In fact, there is no such statement as sun Cheng Cheng .)

1-2.c
#include<stdio.h> #include<sys/types.h> #include<unistd.h> int main() { int
p1,p2;
while((p1=fork())==-1)// If the child process creation fails, the fork return -1=-1,while The loop doesn't jump out , Until the creation is successful ,fork For child processes ID perhaps 0 You can jump out while loop
printf("111"); // There is no output 111. explain p1 Not equal to -1, That means the child process was created successfully , Jump straight out while loop // printf("%d\n",p1);
// Both parent and child processes execute this sentence , Who comes first and who comes after is random , So it's possible to show it first 0 Redisplay ID, Otherwise, the opposite is true if (p1==0) // Of subprocesses fork by 0, This condition is met
putchar('b'); // Output by child process ‘b’, Then the sub process committed suicide else // Because the parent process just did not satisfy the if condition , So come here else This is the next step {
while((p2=fork())==-1); // Rebuild child process ( Second child ) if (p2==0) // The second child is satisfied fork=0 putchar('c');
// The second child performs the output “c” else // The parent process eventually comes here unsatisfied with nothing {putchar('a'); // Parent process output “a”
//printf("\n%d\n",p2); } } }// I guess the correct order is acb perhaps bac. This depends on the speed of the father son process , It's like a race .
results of enforcement

2-1.c
#include<stdio.h> #include<unistd.h> int main() { int p1,p2,i;
if(p1=fork())// The fork It's a string of numbers , Subprocess fork by 0; Of the parent process fork>0, Meet conditions for(i=0;i<5;i++)
printf("child %d\n",i);// Indicates that the creation process is successful , Whether it's a boy or a girl else// The child process is not satisfied with the above if condition , To come here {
if(p2=fork())// The child process is here to create “ Grandchildren ” process , After that, the subprocess fork Is a string of numbers “ Grandchildren ” Process ID; Zesun process fork by 0; Subprocess fork>0, Meet conditions
for(i=0;i<5;i++) printf("son %d\n",i); else//“ Grandchildren ” The process is not satisfied with the above if condition , To come here
for(i=0;i<5;i++) printf("daughter %d\n",i);// The teacher's understanding is different from mine . I think this is a child of a child process , It's a daughter . }
} // Output the result , It's also random
results of enforcement :

2-2.c
/* author:Szymou explain: lockf(1,1,0) Lock the resource and let the process enjoy it alone lockf(1,0,0) Is to unlock resources , For other processes
Below sleep For this process “ sit back ”, At rest , Other processes can use resources The resource here is what I think is a disk buffer buffer */
#include<stdio.h> #include<unistd.h> int
main()// every time printf When , The system will first need to output the character buffer buffer inside { int p1,p2,i; if(p1=fork()) {
lockf(1,1,0);// Lock it buffer Enjoy yourself for(i=0;i<3;i++) { printf("child %d\n",i);
/* Teacher's default second lockf(1,1,0) It's locked all the time , The result will be all output first son012, Output others , Other processes are the same */
lockf(1,0,0);// Unlock to another child process sleep(2);// Other child processes can be called when the parent is sleeping buffer use } } else {
if(p2=fork()) { lockf(1,1,0);// Lock it buffer Enjoy yourself for(i=0;i<3;i++) { printf("son
%d\n",i); lockf(1,0,0); sleep(2); } } else { lockf(1,1,0);// Lock it buffer Enjoy yourself
// Operation results >>>>> for(i=0;i<3;i++) // Operation results >>>>> { printf("daughter %d\n",i);
lockf(1,0,0); sleep(2); } } }return 0; }
Operation results :

3-1.c
/* author:Szymou explain: The parent process is called a process Down there b,c,d process */ #include<stdio.h>
#include<unistd.h> int main() { int p1,p2,p3,p4;
while((p1=fork())==-1);//a The process is created here b process if(p1!=0)// The parent process is called a process , Come in { printf("process
a(its id=%d,its father pid is %d)\n",getpid(),getppid()); } else//b The process is coming in {
printf("precess b(its id=%d,its father pid is %d)\n",getpid(),getppid());
while((p2=fork())==-1) ;//b Process creation c process if(p2==0)//c The process comes in { printf("precess c(its
id=%d,its father pid is %d)\n",getpid(),getppid()); while((p3=fork())==-1)
;//c Process creation d process if(p3==0)//d The process comes in printf("precess d(its id=%d,its father pid is
%d)\n",getpid(),getppid()); } } return 0; }
Observe the implementation results , You will find the parent process of some processes ID Display is not its own parent process ID
This is because the father process is dead , And she became an orphan process , Adopted by a process , So the parent process displayed is ID It was in the process of adopting it ID, You can use the command ps
x View current process , Find process ID, That's clear .

How to make the parent process of each process ID Is the display correct ?
Then the parent process should not die , Let it wait for its child process to die . At this point, we should use a function , This function is wait().
The function is to execute the parent process to wait(), It's going to hang up , After waiting for its child process to finish , It's over by myself .

code :
/* author:Szymou explain: The parent process is called a process Down there b,c,d process ;
There is a situation : Some processes display their parents ID Is not a parent process ID, It's a system process ID, This is because after the death of the parent process , Subprocess get Parent process not reached ID, And became an orphan , Under ubantu will be upstart adoption , stay CentOS I will be init adoption . It can be used ps
x To see ;
wait() Function allows the current parent process to wait for the child process to finish , The parent process ends . This function is introduced here , Because , Avoid the death of the parent process , There's no father in the child process , I can't find dad ID Namely getppid */
/* These two header files are used to support wait() function */ #include <sys/types.h> #include <sys/wait.h>
#include<stdio.h> #include<unistd.h> int main() { int p1,p2,p3,p4;
while((p1=fork())==-1);//a The process is created here b process if(p1!=0)// The parent process is called a process , Come in { printf("process
a(its id=%d,its father pid is %d)\n",getpid(),getppid()); wait(NULL); }
else//b The process is coming in { printf("precess b(its id=%d,its father pid is
%d)\n",getpid(),getppid()); while((p2=fork())==-1) ;//b Process creation c process if(p2!=0)
wait(NULL); //sleep(1); else if(p2==0)//c The process comes in { printf("precess c(its id=%d,its
father pid is %d)\n",getpid(),getppid()); while((p3=fork())==-1) ;//c Process creation d process
if(p3!=0) wait(NULL); //sleep(1); else if(p3==0)//d The process comes in printf("precess d(its
id=%d,its father pid is %d)\n",getpid(),getppid()); } } return 0; }
Operation results ( There will no longer be orphan processes )

<> Any questions can be discussed in the comments section ! Learn together

Technology