ubuntu

Ubuntu如何实现进程间通信

小樊
50
2025-05-09 12:48:32
栏目: 智能运维

在Ubuntu中,有多种进程间通信(IPC)机制可供选择。以下是一些常用的IPC方法:

1. 管道(Pipes)

管道是一种半双工的通信方式,数据只能单向流动,且只能在具有亲缘关系的进程之间使用。

匿名管道

匿名管道通常用于父子进程之间的通信。

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main() {
    int pipefd[2];
    pid_t pid;
    char buffer[256];

    if (pipe(pipefd) == -1) {
        perror("pipe");
        exit(EXIT_FAILURE);
    }

    pid = fork();
    if (pid == -1) {
        perror("fork");
        exit(EXIT_FAILURE);
    }

    if (pid == 0) { // 子进程
        close(pipefd[1]); // 关闭写端
        read(pipefd[0], buffer, sizeof(buffer));
        printf("Child received: %s\n", buffer);
        close(pipefd[0]);
    } else { // 父进程
        close(pipefd[0]); // 关闭读端
        write(pipefd[1], "Hello from parent", 20);
        close(pipefd[1]);
    }

    return 0;
}

命名管道(FIFO)

命名管道是一种特殊的文件,可以在不相关的进程之间进行通信。

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

int main() {
    int fd;
    char buffer[256];

    mkfifo("myfifo", 0666);

    fd = open("myfifo", O_RDWR);
    if (fd == -1) {
        perror("open");
        exit(EXIT_FAILURE);
    }

    write(fd, "Hello from FIFO", 20);
    read(fd, buffer, sizeof(buffer));
    printf("Received: %s\n", buffer);

    close(fd);
    unlink("myfifo");

    return 0;
}

2. 消息队列(Message Queues)

消息队列允许进程发送和接收消息,消息队列是系统范围内的资源。

#include <stdio.h>
#include <stdlib.h>
#include <sys/ipc.h>
#include <sys/msg.h>

struct msg_buffer {
    long msg_type;
    char msg_text[100];
};

int main() {
    int msgid;
    key_t key = 1234;
    struct msg_buffer message;

    msgid = msgget(key, IPC_CREAT | 0666);
    if (msgid == -1) {
        perror("msgget");
        exit(EXIT_FAILURE);
    }

    message.msg_type = 1;
    strcpy(message.msg_text, "Hello from message queue");

    msgsnd(msgid, &message, sizeof(message.msg_text), 0);
    printf("Message sent\n");

    msgrcv(msgid, &message, sizeof(message.msg_text), 1, 0);
    printf("Message received: %s\n", message.msg_text);

    msgctl(msgid, IPC_RMID, NULL);

    return 0;
}

3. 共享内存(Shared Memory)

共享内存是最快的IPC机制之一,因为它避免了内核空间的拷贝。

#include <stdio.h>
#include <stdlib.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <unistd.h>

int main() {
    int shmid;
    key_t key = 1234;
    char *shmaddr;
    int *counter;

    shmid = shmget(key, sizeof(int), IPC_CREAT | 0666);
    if (shmid == -1) {
        perror("shmget");
        exit(EXIT_FAILURE);
    }

    shmaddr = shmat(shmid, NULL, 0);
    if (shmaddr == (char *) -1) {
        perror("shmat");
        exit(EXIT_FAILURE);
    }

    *counter = 0;
    printf("Counter initialized to %d\n", *counter);

    while (1) {
        (*counter)++;
        printf("Counter: %d\n", *counter);
        sleep(1);
    }

    shmdt(shmaddr);
    shmctl(shmid, IPC_RMID, NULL);

    return 0;
}

4. 信号(Signals)

信号是一种异步通知机制,用于进程间通信。

#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>

void signal_handler(int signum) {
    printf("Received signal %d\n", signum);
}

int main() {
    signal(SIGINT, signal_handler);

    printf("Waiting for signal...\n");
    while (1) {
        sleep(1);
    }

    return 0;
}

5. 套接字(Sockets)

套接字是一种通用的IPC机制,不仅可以在本地进程间通信,还可以用于网络通信。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>

int main() {
    int server_fd, new_socket;
    struct sockaddr_in address;
    int opt = 1;
    int addrlen = sizeof(address);
    char buffer[1024] = {0};

    if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0) {
        perror("socket failed");
        exit(EXIT_FAILURE);
    }

    if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &opt, sizeof(opt))) {
        perror("setsockopt");
        exit(EXIT_FAILURE);
    }

    address.sin_family = AF_INET;
    address.sin_addr.s_addr = INADDR_ANY;
    address.sin_port = htons(8080);

    if (bind(server_fd, (struct sockaddr *)&address, sizeof(address)) < 0) {
        perror("bind failed");
        exit(EXIT_FAILURE);
    }

    if (listen(server_fd, 3) < 0) {
        perror("listen");
        exit(EXIT_FAILURE);
    }

    if ((new_socket = accept(server_fd, (struct sockaddr *)&address, (socklen_t*)&addrlen)) < 0) {
        perror("accept");
        exit(EXIT_FAILURE);
    }

    read(new_socket, buffer, 1024);
    printf("Message received: %s\n", buffer);

    close(new_socket);
    close(server_fd);

    return 0;
}

这些是Ubuntu中常用的进程间通信机制。根据具体需求选择合适的IPC方法。

0
看了该问题的人还看了