Summary of Linux inter-process communication methods. Is there any class library so?

General discussion for topics related to the FreeBASIC project or its community.
Post Reply
xiaoyao
Posts: 138
Joined: May 05, 2020 2:01

Summary of Linux inter-process communication methods. Is there any class library so?

Post by xiaoyao »

pipeline
named pipe
Signal
message queue
Shared memory
signal
socket
The main methods of Linux inter-process communication are as follows:
FROM URL:https://www.cnblogs.com/adam-ma/p/18083 ... 1%E9%81%93

Pipe: Pipe is the simplest IPC method, used to implement communication between parent and child processes. One process can write data to the pipe, and another process can read data from the pipe.
Named Pipe (Named Pipe) or FIFO (First In First Out): Similar to a pipe, but named pipes allow communication between unrelated processes. It has a corresponding file name in the file system and is accessed through a file descriptor.
Signal: A signal is a simulation of an interrupt mechanism at the software level, used to notify the process that a certain event has occurred. After a process receives a signal, it can perform different operations based on the different meanings of the signal.
Message Queue: A message queue is a linked list of messages that allows processes to communicate by sending and receiving messages. The message queue is maintained by the kernel, ensuring the independence and security of messages.
Shared Memory: Shared memory allows multiple processes to access the same memory area, and the processes can directly read and write the memory area for communication. This method communicates quickly, but requires a synchronization mechanism to avoid conflicts.
Semaphore: Semaphore is a mechanism for inter-process synchronization and mutual exclusion, which can control access to shared resources and avoid problems such as competition and deadlock.
Socket: Socket can be used not only for local inter-process communication, but also for network communication. It provides an inter-process communication mechanism across network platforms, allowing processes on different hosts to communicate.

Below is a simple Linux C example code that demonstrates how to use pipes to communicate between parent and child processes. In this example, the parent process writes a string to the pipe, and the child process reads the string from the pipe and prints it.

Code: Select all

#include <stdio.h>  
#include <stdlib.h>  
#include <string.h>  
#include <unistd.h>  
#include <sys/types.h>  
#include <sys/wait.h>  
  
int main() {  
    int fd[2]; // 文件描述符数组,用于管道的读写  
    pid_t pid; // 子进程的进程ID  
    char buf[1024]; // 用于存储从管道读取的数据的缓冲区  
    const char *message = "Hello from parent process!"; // 父进程要发送的消息  
  
    // 创建管道  
    if (pipe(fd) == -1) {  
        perror("pipe");  
        exit(EXIT_FAILURE);  
    }  
  
    // 创建子进程  
    pid = fork();  
    if (pid == -1) {  
        perror("fork");  
        exit(EXIT_FAILURE);  
    }  
  
    // 父进程  
    if (pid > 0) {  
        close(fd[0]); // 关闭读端,因为父进程要写入  
  
        // 写入消息到管道  
        write(fd[1], message, strlen(message) + 1);  
        close(fd[1]); // 写入完毕后关闭写端  
  
        // 等待子进程结束  
        waitpid(pid, NULL, 0);  
        printf("Parent process has finished.\n");  
    }  
    // 子进程  
    else {  
        close(fd[1]); // 关闭写端,因为子进程要读取  
  
        // 从管道读取消息  
        ssize_t bytesRead = read(fd[0], buf, sizeof(buf) - 1);  
        if (bytesRead > 0) {  
            buf[bytesRead] = '\0'; // 在读取到的字符串末尾添加空字符  
            printf("Child process received: %s\n", buf);  
        }  
        close(fd[0]); // 读取完毕后关闭读端  
  
        exit(EXIT_SUCCESS);  
    }  
  
    return 0;  
}
Post Reply