Linux Environment Programming - Process Communication

  

Experimental Content

Write a program to implement pipeline communication. Use the system call pipe() to create a pipe. The two child processes P1 and P2 write a sentence to the pipe:

Child 1 is sending a message!

Child 2 is sending a message!

The parent process reads out two information from the child process from the pipeline and displays it (requires receiving P1 first, then P2).


Experimental guidance

1. What is a pipeline

One of the most important contributions of the UNIX system in the development of OS is the system's first initiative. A pipe. This is also a major feature of the UNIX system.

A pipe is a shared file that can be connected to a write process and a read process and allows them to communicate in the producer — consumer mode, also known as a pipe file. The write process writes data to the pipe from the write end of the pipe (handle 1), while the read process reads data from the read end of the pipe (handle 0).


handle fd[0]





Handle fd[1]

Reader





Write

Second, the type of pipeline:

1, named pipeline

A file with a path name that can exist in the file system for a long time. Created with the system call mknod( ). It overcomes the limitations of the use of anonymous pipes, allowing more processes to communicate using pipes. Thus other processes can know its existence and can use the path name to access the file. Access to a named pipe is the same as accessing other files, first open with open().

2, anonymous pipe

A temporary file. An unnamed file (no path name) created with pipe( ). Only the file descriptor returned by the system call is used to identify the file, so only the process calling pipe() and its descendants can recognize the file descriptor in order to communicate with the file (pipe). When these processes no longer use this pipeline, the core reclaims its index nodes.

The two pipes are read and written in the same way. This article only talks about anonymous pipes.

3, the establishment of the pipe file

allocate disk and memory index nodes, allocate file entries for the read process, assign file entries for the write process, assign user file descriptors

4, read/write process mutual exclusion

The kernel sets a read pointer and a write pointer for the address, and reads and writes in the first-in first-out order.

In order for the read and write processes to access the pipe file in a mutually exclusive manner, each process must be mutually exclusive accessing the direct address entry in the index node of the pipe file. Therefore, each time the process accesses the pipe file, it is necessary to check whether the index file has been locked. If so, the process sleeps, otherwise it is locked and read/written. Unlock after the operation ends, and wake up the process that sleeps because the index node is locked.

Three, the system call involved

1, pipe()

Create an anonymous pipe.

System Call Format

pipe(filedes)

Parameter Definition

int pipe(filedes);

int filedes[2] ;

where filedes[1] is the write end and filedes[0] is the read end.

The function uses the header file as follows:

#include <unistd.h>

#inlcude <signal.h>

#include < ;stdio.h>

2, read( )

System Call Format

read(fd,buf,nbyte)

Function: From fd The nbyte bytes of data are read out from the indicated file and sent to the buffer indicated by the pointer buf. If the file is locked, wait until the lock is open.

Parameter definition

int read(fd,buf,nbyte);

int fd;

char *buf;

unsigned Nbyte;

3, write()

system call format

read(fd,buf,nbyte)

function: put nbyte bytes The data is written from the buffer pointed to by buf to the file pointed to by fd. If the file is locked, the write is suspended until the lock is unlocked.

The parameter definition is the same as read().




Compile a program to implement pipeline communication for the process. Use the system call pipe() to create a pipe line. The two child processes p1 and p2 write a sentence to the channel:

child1 process is sending message!

child2 process is sending message!

the parent process is from the pipeline The information from the two processes is read out and displayed on the screen.


Program 1:
#include<unistd.h>#include<sys/types.h>#include<errno.h>#include<stdio.h>#include< ;stdlib.h>#include<string.h>int main(){int pipe_fd[2];pid_t pid;char buf_r[100];char* p_wbuf;int r_num;memset(buf_r,0,sizeof(buf_r)) ;if(pipe(pipe_fd)<0){printf("pipe create error\ ");return -1;}if((pid=fork())==0){printf("\ " Close;pipe(fd[1]);sleep(2);if((r_num=read(pipe_fd[0],buf_r,100))>0){ printf("%d numbers read from the pipe is % s\ ",r_num,buf_r);}close(pipe_fd[0]);exit(0);}else if(pid>0){close(pipe_fd[0]);if(write(pipe_fd[1], "Hello",5)!=-1)printf("parent writel success!\ ");if(write(pipe_fd[1],"pipe",5)!=-1) printf(" Parent write2 success!\ ");close(pipe_fd[1]);sleep(3);waitpid(pid,NULL,0);exit(0);}}


Copyright © Windows knowledge All Rights Reserved