Instance learning Linux process communication (1)

Source: Internet
Author: User
1. Experiment 1: Signal Communication description: use signal communication to communicate between parent and child processes and brother processes. solution: Send a SIGCHLD signal to the parent process at the end of the sub-process, the parent process responds to the end of a child process through the signal () or sigaction () functions. (When a process is terminated or...

 

1. Experiment 1: Signal Communication description: use signal communication to communicate between parent and child processes and brother processes. solution: Send a SIGCHLD signal to the parent process at the end of the sub-process, the parent process responds to the end of a child process through the signal () or sigaction () functions. (When a process stops or stops, send the SIGCHLD signal to its parent process .) Source code 1 (blocking communication): # include # Include # Include Void sigchld_handler (int sig) {pid_t pid; int status; for (; (pid = waitpid (-1, & status, WNOHANG)> 0 ;) {printf ("child % d died: % d \ n", pid, status); printf ("parent process has ed SIGHLD signal success \ n");} return ;} void main () {/* create sub-process */int p_id = fork (); if (p_id = 0) {/* if it is a sub-process * // * output sub-process information, sleep for a period of time, exit */printf ("This is child process with pid of % d \ n ", getpid (); sleep (1);} else if (p_id> 0) {/* parent process * // * call the signal processing function , Pause */signal (SIGCHLD, sigchld_handler); sleep (5);} else {/* print the creation process failure information, and exit */printf ("Error! "); Return-1;} return 0;} 2. tutorial 2: Anonymous pipeline communication description: an anonymous pipeline can only be used for communication between two processes with kinship. Source code: # include # Include # Include # Define MAX_LINE 80 void main () {/* create an anonymous pipeline */int thePipe [2], ret; char buf [MAX_LINE + 1]; const char * testbuf = "a test string"; if (pipe (thePipe) = 0) {/* The pipeline is created successfully. the file descriptor in the first element of the array is supplied to the program for reading, the file descriptor in the second element of the array is supplied to the program for writing */if (fork () = 0) {/* The process is created successfully, and the sub-process * // * closes the write end, read data from the read end of the pipeline and put it into the buffer */close (thePipe [1]); ret = read (thePipe [0], buf, MAX_LINE); buf [ret] = 0; /* output the buffer data, close the Read end, and exit */printf ("Read: % s \ n", buf); close (thePipe [0]);} else {/* process created successfully * // * Close the read end and write data to the write end of the pipeline */close (thePipe [0]); ret = write (thePipe [1], testbuf, strlen (testbuf); wait (NULL);/* wait until the sub-process ends */close (thePipe [1]) ;}} 3. Experiment 3: Description of named pipe communication: named pipelines exist in the file system in the form of first-in-first-out. Therefore, as long as you can access the file path, you can communicate with each other through named pipelines. Source code: This code is always problematic after it is written, so it will not be posted. However, it is obvious that the named pipe is not much different from the anonymous pipeline, but one can only communicate between the parent and child, one can communicate with each other

This article is from the "Change" blog

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.