The result of the program is running is such a
, for the first time./a.out the parent process is done, then enter the command line, and at this point, the child process is not over yet, was behind the command line to print out the content of the child process,
Excuse me, is because the init process is a background process, so the Linux after detection to the end of the * * parent, then print out the command line? * *
But, my classmate said, in his Linux, did not appear the phenomenon,
This is due to what reason? And the number of kernel virtual machine?
The following is the source code
#include
#include
#include
#include
Int main () {
//declare: int pipe (int pipefd [2]).
int fd[2];
Char buf [128].
If (pipe (fd) & lt; 0 {
Printf (" creat pipe failed \ n ");
The exit - 1;
}
//printf (" it is ok to \ n ");
Int pid;
pid=fork();//the fork () to create a process
If (pid & lt; 0 {
Printf (" creat son failed \ n ");
}
Else if (pid & gt; 0 {
Printf (" into the father \ n ");
Close (fd [0]);
Write (fd [1], "write the from father", strlen (" write the from father "));
//printf (" write is over \ n ");
}
The else {
Printf (" pid=% d, into son \ n ", pid);
Close (fd [1]);
Read (fd [0], buf, 128);
Printf (" read the from father: % s \ n ", buf);
}
return 0;
}
CodePudding user response:
I think, may be a little bit slow your system to deal with, you let the students in the child process (pid==0) are added in the processing of sleep (1), should also can appear the same phenomenon with you,The command line when to take back control?
The shell itself is a process,
Via the shell after the caller, equivalent to shell is the parent process, called program is the child;
After the child process, control back to the parent process;
However, a parent only know their children, don't know their grandchildren,
If the process of the shell with A said the a.out process with B, said a.out, said the child process in C
So, A knows B, and at the end of the B, control back to A hand.
A don't know C, will not wait for A C ever end,
But if we want to make A waiting for B and C, can also, calling wait (man 2) functions, such as
# include
#include
#include
#include
#include//wait () requires this header file
Int main ()
{
int fd[2];
Char buf [128]={0};
If (pipe (fd) & lt; 0)
{
Printf (" creat pipe failed \ n ");
The exit (1);
}
Int pid;
pid=fork();
If (pid & lt; 0)
{
Printf (" creat son failed \ n ");
}
Else if (pid & gt; 0)
{
Int the status;
Printf (" pid=% d, into father \ n ", pid);
Close (fd [0]);
Write (fd [1], "write the from father", strlen (" write the from father "));
Wait (& amp; Status).//call wait in calling process
}
The else
{
Sleep (1);//no matter how long it sleeps
Printf (" pid=% d, into son \ n ", pid);
Close (fd [1]);
Read (fd [0], buf, 128);
Printf (" read the from father: % s \ n ", buf);
}
return 0;
}
Finally,
If the process is not over yet, B C end ahead of schedule, and B without wait (),
C process is marked as a zombie, this is not good, because a zombie process will take up the kernel resources, much less a zombie a can produce process,
But if B process is finished, zombie process will adopt to the init process (pid=1), the init automatically calling wait () to end the zombie,
Above, conforming to the kernel 2.6 and POSIX. 1-2001