Linux Process Communication-MPs queue

Source: Internet
Author: User

Pipeline (PIPE ):Used for communication between kinship processes (such as parent and child processes and sibling processes)
A process writes data to the pipeline: the number of bytes written is smaller than pipe_buf, which is an atomic operation. The write operation is blocked when the buffer in the pipeline is not read in time.
One process read pipeline: Read operations are blocked when there is no data in the pipeline buffer.

Main functions:
Int pipe (INT filedes [2]);

The Code instance creates a parent-child process, a parent process write pipeline, and a child process read pipeline.
The Code verifies the write blocking: the sub-process will sleep for 1 second after reading the pipeline, and the parent process will be blocked after a write operation until the sub-process takes data away.
The Code verifies read blocking: the parent process sleeps for 1 second after writing a pipeline, and the child process blocks after one read operation until the parent process writes data again.
If the actual data in the MPs queue is less than that of the requested data, the read is not blocked.

However, it cannot be verified that the number of written bytes is greater than pipe_buf, which is not an atomic operation. I cannot come up with a method to verify it.

Code attachment

 

# Include <unistd. h>
# Include <sys/types. h>
# Include <errno. h>
# Include <stdlib. h>
Main ()
{
Int pipe_fd [2];
Pid_t PID;
Int Len = 4096*2;
Char r_buf [Len];
Char w_buf [Len];
Char * p_wbuf;
Int r_num;
Int w_num;
Int cmd;
Memset (r_buf, 0, sizeof (r_buf ));
Memset (w_buf, 0, sizeof (r_buf ));
P_wbuf = w_buf;
If (pipe (pipe_fd) <0)
{
Printf ("pipe create Error
");
Return-1;
}

If (pid = fork () = 0)
{
Close (pipe_fd [1]);
While (1)
{
R_num = read (pipe_fd [0], r_buf, sizeof (r_buf ));
If (r_num = 0)
Break;
Printf ("Number of read Bytes: % d
", R_num );
// Sleep (1); // verify write Blocking
}

Close (pipe_fd [0]);
Printf ("the sub-process exits...
");
}
Else if (pid> 0)
{
Close (pipe_fd [0]); // close read
// Sleep (5 );
While (1)
{
// W_num = write (pipe_fd [1], w_buf, sizeof (w_buf ));
W_num = write (pipe_fd [1], w_buf, 111 );
If (w_num! =-1)
Printf ("Number of writing nodes: % d
", W_num );
Else
Printf ("error
");
Sleep (1); // verify read Blocking
}
Close (pipe_fd [1]); // close the write
Printf ("the parent process exits...
");
}

}

Famous pipeline (FIFO ):Different from pipelines, communication can be performed between multiple unrelated processes.

After the FIFO is created, the read or write FIFO process must first enable the FIFO:
Read-first-in-first-out processes are opened in blocking mode. If no other process writes, the operation is blocked;
The write FIFO process is opened in blocking mode, and the open operation is blocked if no other process is read.

A process reads the FIFO in blocking mode. If the FIFO does not have data or has data but other processes are reading the data, blocking occurs.
A process writes data in FIFO in blocking mode, which is the same as that in the write pipeline. If the number of written bytes is smaller than pipe_buf, it is an atomic operation,
A blocking occurs when a write operation does not read data in the buffer zone of the MPs queue in time.

Main functions:
Int mkfifo (const char * pathname, mode_t mode );

Code attachment

 

# Include <unistd. h>
# Include <sys/types. h>
# Include <errno. h>
# Include <stdlib. h>
# Include <stdio. h>
# Include <sys/STAT. h>
# Include <fcntl. h>
# Define FIFO "FIFO"
Main ()
{
Pid_t PID;
Int fd_r, fd_w;
Int num_r, num_w;
Int Len = 40;
Char buf_r [Len];
Char buf_w [Len];
// You can also create a FIFO using mkfifo in Shell
If (mkfifo (FIFO, o_creat | o_excl) <0) & (errno! = Eexist ))
{
Printf ("create FIFO fail!
");
Return;
}

If (pid = fork () = 0)
{
Sleep (1 );
Printf ("process (% d): Prepare to enable FIFO
", Getpid ());
Fd_r = open (FIFO, o_rdonly, 0 );
If (fd_r =-1) // read in blocking mode. If no other process writes, the open operation is blocked.
{
Printf ("process (% d): failed to enable FIFO...
", Getpid ());
Return;
};
Printf ("process (% d): FIFO enabled successfully.
", Getpid ());
While (1)
{
Num_r = read (fd_r, buf_r, sizeof (buf_r ));
Printf ("Number of read Bytes: % d
", Num_r );
Sleep (1 );
// Write blocking will be verified here, And the write process will not sleep, and the pipeline will soon be filled up, and the writing will be blocked again.
}

}
Else if (pid> 0)
{

Printf ("process (% d): Prepare to enable FIFO
", Getpid ());
// If (open (FIFO, o_wronly | o_nonblock, 0) =-1)
Fd_w = open (FIFO, o_wronly, 0 );
If (fd_w =-1) // Enable write in blocking mode. If no other process reads data, the enable operation is blocked.
{
Printf ("process (% d): failed to enable FIFO...
", Getpid ());
Return;
}
Printf ("process (% d): FIFO enabled successfully.
", Getpid ());
While (1)
{
Num_w = write (fd_w, buf_w, sizeof (buf_w ));
Printf ("Number of writing nodes: % d
", Num_w );

}
}
}

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.