Linux中的进程间通信

在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 99

int 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 99

int 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;
}

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值