Linux中的进程间通信

来源:互联网 发布:健身 年轻 知乎 编辑:程序博客网 时间:2024/05/16 11:11

在Linux中进程间通信有很多种方法,可以使用普通的文件,或者命名管道,以及共享内存都可以实现进程间通信,下面是几种实现方式的代码。


一. 使用普通文件实现进程间通信

服务端实现:

#include <stdio.h>#include <stdlib.h>#include <fcntl.h>#include <string.h>int main(int argc, char *argv[]){    int fd;    char message[20] = "hello world!";    if (argc != 2){        printf("error");        exit(1);    }    if ((fd = open(argv[1], O_WRONLY|O_CREAT, 0644)) == -1){        perror("open");        exit(1);    }    while (1){        write(fd, message, strlen(message));        sleep(1);    }    return 0;}

客户端实现:

#include <stdio.h>#include <stdlib.h>#include <fcntl.h>int main(int argc, char *argv[]){    int fd, len;    char message[20] = "hello world!";    if (argc != 2){        printf("error");        exit(1);    }    if ((fd = open(argv[1], O_RDONLY)) == -1){        perror("open");        exit(1);    }    while ((len = read(fd, message, 20)) > 0){        printf("%s\n", message);        sleep(1);    }    close(fd);    return 0;}

二. 使用管道实现

服务端代码:

#include <stdio.h>#include <stdlib.h>#include <fcntl.h>#include <stat.h>#include <string.h>int main(int argc, char *argv[]){    int fd;    char message[20] = "hello world!";    if (argc != 2){        printf("error");        exit(1);    }    if ((fd = mkfifo(argv[1], O_CREAT, 0644)) == -1){        perror("mkfifo");        exit(1);    }    if ((fd = open(argv[1], O_WRONLY)) == -1){        perror("open");        exit(1);    }    while (1){        write(fd, message, strlen(message));        sleep(1);    }    return 0;}

客户端代码:

#include <stdio.h>#include <stdlib.h>#include <fcntl.h>int main(int argc, char *argv[]){    int fd, len;    char message[20] = "hello world!";    if (argc != 2){        printf("error");        exit(1);    }    if ((fd = open(argv[1], O_RDONLY)) == -1){        perror("open");        exit(1);    }    while ((len = read(fd, message, 20)) > 0){        printf("%s\n", message);        sleep(1);    }    close(fd);    return 0;}

三. 共享内存实现

服务端代码:

#include <stdio.h>#include <stdlib.h>#include <shm.h>#include <string.h>#define SHM_KEY 99int main(void){    int shmid;    char *shm_ptr, message[20] = "hello world!";    //创建共享内存,参数分别是共享内存的键值,尺寸,以及权限    if ((shmid = shmget(SHM_KEY, 100, IPC_CREAT|0777)) == -1){        perror("shmget");        exit(1);    }    /*将共享内存附加到进程的地址空间,与进程建立联系,返回共享内存区的指针,第一个参数为共享内存的id,第二个参数为NULL表示让内核分配共享内存区间,第三个为存取标志位,为0表示从0号位置开始存取*/    if ((shm_ptr = shmat(shmid, NULL, 0)) == NULL){        perror("shmat");        exit(1);    }    while (1){        //将信息拷贝到共享内存区        strcpy(shm_ptr, message);        sleep(1);    }    //删除共享内存区    shmctl(shmid, IPC_RMID, NULL);    return 0;}

客户端代码:

#include <stdio.h>#include <stdlib.h>#include <shm.h>#include <string.h>//此key和服务端相同,不然不能代表同一块内存区域#define SHM_KEY 99int main(void){    int shmid;    char *shm_ptr;    if ((shmid = shmget(SHM_KEY, 100, IPC_STAT)) == -1){        perror("shmget");        exit(1);    }    if ((shm_ptr = shmat(shmid, NULL, 0)) == NULL){        perror("shmat");        exit(1);    }    printf("%s\n", shm_ptr);    //断开与共享内存区的联系    shmdt(shm_ptr);    return 0;}

以上这三种为最基本的通信方式,希望对需要的同学们有帮助。

原创粉丝点击