Instance learning Linux Process Communication (1)-Change

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 stops or stops, send the SIGCHLD signal to its parent process .) 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): # I NcLude # Include # Include Vo IdSigchld_handler (int sig) {pid_t pid; int StatUs; for (; (pid = waitpid (-1, & status, WNOHANG)> 0;) {printf ("child % d di Ed: % D \ n ", pid, status); printf (" parent process completed 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);} e LsE if (p_id> 0) {/* parent process * // * call the signal processing function and 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 TrIng. h> # 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: Named Pipe communication Description: Named Pipe exists 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 the named pipe. 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
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.