溫馨提示×

Ubuntu如何實現進程間通信

小樊
56
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
亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女