How interprocess communication is encountered in the project

Source: Internet
Author: User
Tags sigint signal

1) Socket: Often encountered, not spoken

2) Signal: Use Kill to send signals, Signal,settimer and other system calls can send a signal to another process, to achieve the purpose of inter-process communication.

Kill (p1,16); / * Send interrupt signal to process number P1 process 16*/

Signal (SIGINT,GO); / * After receiving the SIGINT signal, go to the GO function to process it * /

3) Shared Memory: The use of mmap system calls to do the shared memory, mmap using FD as the entry, two processes open a file name, and mmap this FD mapped to their own process environment, using mmap reflected back to the virtual address directly read and write.

Once in the project, the kernel creates a block of memory at system startup, saves the PRINTK buffer, inserts a KO when needed to get the buffer address, and copies it to the user space, and, when needed, uses MMAP to obtain the value in this address for inter-process communication.

fd = open (argv[1], O_RDWR | O_creat, S_IRUSR | S_IWUSR);flength = Lseek (FD, 1, seek_end);Write (FD, "s", 1);/* Add a null character at the end of the file so that printf works correctly */Lseek (FD, 0, seek_set);start_addr = 0x80000;Mapped_mem = mmap (start_addr, Flength, prot_read| Prot_write,//Allow writemap_shared,//Allow other processes to access this memory areaFD, 0);

4) Message Queuing: Communication between processes has not been processed using Message Queuing. An array of similar messages has been used to handle data sharing between threads.

5) Semaphore: no use.

6) Piping: Pipe system call, the data can only flow one way, only in the relationship between the process flow.

A nameless pipe was used in the project to invoke the shell script in the C language to get the return value of the script. Use the system call to execute the script to get the return value, or use the FORK+EXEC system call to create a new child process execution script. The return value in the child process is returned to the parent process through the pipe. This allows the parent process to get the return value of the child process.

int filedes[2];
Char buf[80];
pid_t pid;
Pipe (Filedes);

if ((Pid=fork ()) > 0)
{
printf ("This was in the Father Process,here write a string to the pipe.\n");
Char s[] = "Hello world, this is write by pipe.\n";
Write (Filedes[1], S, sizeof (s));
Close (Filedes[0]);
Close (filedes[1]);
}
Else
{
printf ("This was in the child Process,here read a string from the pipe.\n");
Read (Filedes[0], buf, sizeof (BUF));
printf ("%s\n", buf);
Close (Filedes[0]);
Close (filedes[1]);
}

Waitpid (PID, NULL, 0);

How interprocess communication is encountered in the project

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.