MPs queue: the parent process sends messages to the child process!
Source: Internet
Author: User
MPs queue: the parent process sends messages to the child process! -- Linux general technology-Linux programming and kernel information. The following is a detailed description. # Include
# Include
# Include
# Include
# Include
Int main (int argc, char * argv [])
{
Int pfd [2];
Pid_t cpid;
Char buf;
Pipe (pfd); // Create an MPs queue
Cpid = fork (); // create a process
If (cpid = 0)
{
Close (pfd [1]); // close the Write file descriptor
While (read (pfd [0], & buf, 1)> 0) // read
Write (STDOUT_FILENO, & buf, 1 );
Write (STDOUT_FILENO, "\ n", 1 );
Close (pfd [0]);
Exit (0 );
}
Else if (cpid> 0)
{
Close (pfd [0]);
Write (pfd [1], argv [1], strlen (argv [1]);
Close (pfd [1]);
Wait (NULL );
Exit (0 );
}
Return 0;
}
Input:./pipetest abc
Output: abc
Question: After creating a child process, it is generally because the child process starts to execute (cpid = 0), but the parent process has not been written yet. if the while process in the child process ends, it exits first, after the parent process is executed, no results will be output because the child process has exited. why?
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.