Talk C together (90th back: C language instance -- use pipelines for inter-process communication 3)

Source: Internet
Author: User

Talk C together (90th back: C language instance -- use pipelines for inter-process communication 3)

Hello, everyone. In the previous session, we talked about the example of using pipelines for inter-process communication. This example is as follows:Use MPs queues for inter-process communication, but the MPs queues are used in different ways.. When you leave the rest of your time, your words will go right. Let's talk C chestnuts together!

We introduced three pipelines in the previous chapter.Describes the third MPs queue and Its Usage. The most important thing is to let everyone know how to use pipelines for inter-process communication.

The third type of MPs queue is called a real MPs queue. The MPs queue has another name:Named Pipe (FIFO). Before introducing it, let's first introduce a function:Mkfifo.

Mkfifo function prototype
int mkfifo(const char *filename, mode_t mode)
This function is used to create an MPS queue file. The first parameter of this function is a string used to indicate the name of the MPs queue file. The second parameter of this function is the file access permission, used to indicate the permissions of the MPs queue file, for example, 0764. This function returns a file descriptor that can be used to operate on the MPs queue. If the function is successfully executed, 0 is returned, otherwise,-1 is returned.

After understanding the usage of this function, we will introduce how to use the named pipeline.

Mkfifo function usage 1. use the mkfifo function to create a named pipe. 2. in process A, use open to open the pipeline (the open mode is write), and A fd is obtained. 3. use write to write data in the pipeline through fd; 4. use close to close the fd obtained in step 2; 5. in process B, use open to open the pipeline (the open mode is read), and a fd is obtained. 6. use read to read data from the pipeline through fd; 7. use close to close the fd obtained in step 5;

We can see that process A writes data to the MPs queue created by mkfifo, and process B reads data from the MPs queue. Process A and process B communicate with each other through the MPs queue. The communication content is data.

Next we will use specific examples to illustrate,The detailed code is as follows::

int main(){    char input[] = "IPC by pipe";    char output[BUFSIZ+1];    char p_name[] = "/tmp/test_fifo";    int count = 0;    int fd;    int stat_value;    pid_t pid,pid_res;    memset(output,'\0',sizeof(output));    if(mkfifo(p_name,0777) == 0) // create pipe    {        pid = fork();        if(pid > 0)        {            printf("father running \n");            fd = open(p_name,O_RDONLY); //open by read mode            if(fd == -1)            {                printf("open pipe file failed \n");                return 1;            }        }        else if(pid == 0)        {            printf("son running \n");            fd = open(p_name,O_WRONLY); //open by write mode            if(fd == -1)            {                printf("open pipe file failed \n");                return 1;            }            count = write(fd,input,strlen(input));    // write the dato into pipe            printf("son process write %d characters,they are : %s \n",count,input);            close(fd);        }        else        {            printf("create process failed \n");            return 1;        }    }    else    {        printf("create fifo failed \n");        return 1;    }        pid_res = wait(&stat_value);        if(pid_res > 0)        {            count = read(fd,output,BUFSIZ);       // read the data from pipe            printf("father process read %d characters,they are: %s \n",count,output);            close(fd);        }    return 0;}

Through the above code, we can find that we first created a named pipeline, then used fork to create a sub-process, and wrote data to the pipeline in the sub-process, then, read data from the parent process. However, the parent process uses the wait function to read data from the pipeline only after the child process writes data. This is an application that is mutually exclusive between processes. If this is not done, when the parent process reads data from the MPs queue, the child process has not written the data to the MPs queue.

The readers will not write the code in the body, and the detailed code will be put into my resources. You can click here to download and use it.

The following is the running result of the program. For details, refer:

. /S // run the compiled program father running // The parent process is running son running // The child process is running son process write 11 characters, they are: IPC by pipe // sub-process writes data to the MPs queue father process read 11 characters, they are: IPC by pipe // The parent process reads data from the MPs queue

We can see from the above program running result that the sub-process writes the data "IPC by pipe" to the pipeline, and the parent process then reads the data from the pipeline, thus implementingData transmission between parent and child processes, that is, communication between processes.

For more information, see the example of using signals for inter-process communication. I want to know what examples will be provided later, and I will try again.


Related Article

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.