# 进程间通信

进程间通信的几种方式

匿名消息

main.c

#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<string.h>
int main() {
    int _pipe[2] = {0,0};
    int ret=pipe(_pipe);
    if(ret == -1){
        perror("create pipe error");
        return 1;
    }
    printf("_pipe[0 is %d, _pipe[1] is %d\n",_pipe[0],_pipe[1]);
    pid_t id=fork();
    if(id<0){
        perror("fork error");
        return 2;
    }else if(id == 0){
        printf("child writting\n");
        close(_pipe[0]);
        int count=5;
        const char *msg="i am from xatu";
        while(count--){
            write(_pipe[1],msg,strlen(msg));
            sleep(1);
        }
        close(_pipe[1]);
        exit(1);
    }else{
        printf("father reading\n");
        close(_pipe[1]);
        char msg[1024];
        int count=5;
        while(count--){
            ssize_t s = read(_pipe[0],msg,sizeof(msg)-1);
            if(s>0){
                msg[s] = '\0';
                printf("client# %s\n",msg);
            }else{
                perror("read error");
                exit(1);
            }
        }
        if(waitpid(id,0,NULL) != -1){
            printf("wait success\n");
        }
    }
    return 0;
}


命名消息

server.c

#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
void testserver(){
    int namepipe = mkfifo("myfifo", S_IFIFO|0666);
    if(namepipe == -1){
        perror("mkfifo error");
        exit(1);
    }
    int fd=open("./myfifo",O_RDWR);
    if(fd == -1){
        perror("open error");
        exit(2);
    }
    char buf[1024];
    while(1){
        printf("sendto#");
        fflush(stdout);
        ssize_t s = read(0,buf,sizeof(buf)-1);
        if(s>0){
            buf[s-1] = '\0';
            if(write(fd,buf,s)==-1){
                perror("write error");
                exit(3);
            }
        }

    }
    close(fd);
}
int main(){
    testserver();
    return 0;
}

client.c

#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
void testclient(){
    int fd=open("./myfifo",O_RDWR);
    if(fd == -1){
        perror("open error");
        exit(1);
    }
    char buf[1024];
    while(1){
        ssize_t s = read(fd,buf,sizeof(buf)-1);
        if(s>0){
            printf("client# %s\n",buf);
        }else{
            perror("read error");
            exit(2);
        }

    }
    close(fd);
}
int main(){
    testclient();
    return 0;
}

消息队列

read.c

#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/msg.h>
#include<string.h>
typedef struct _msg{
    long mtype;
    char mtext[50];
}MSG;
int main(int argc, char *argv[]){
    key_t key;
    int msgqid;

    key = ftok("./",2015);
    msgqid = msgget(key, IPC_CREAT|0666);
    if(msgqid == -1){
        perror("msgget");
        exit(-1);
    }
    MSG msg;
    memset(&msg,0,sizeof (msg));

    msgrcv(msgqid,&msg,sizeof(msg)-sizeof(long),(long)10,0);
    printf("msg.mtext=%s\n",msg.mtext);
    msgctl(msgqid,IPC_RMID,NULL);
    return 0;
}


write.c

#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/msg.h>
#include<string.h>
typedef struct _msg{
    long mtype;
    char mtext[50];
}MSG;
int main(int argc, char *argv[]){
    key_t key;
    int msgqid;
    MSG msg;
    key = ftok("./",2015);
    msgqid = msgget(key, IPC_CREAT|0666);
    if(msgqid == -1){
        perror("msgget");
        exit(-1);
    }
    msg.mtype = 10;
    strcpy(msg.mtext,"hello hc");
    msgsnd(msgqid,&msg,sizeof(msg)-sizeof(long),0);
    return 0;
}


共享内存

IPC中最快最有效率的方式,要注意多个进程对于给定存储区的访问是互斥的
常用的函数有ftok(分配键值)和shmget

#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<string.h>
#include<sys/shm.h>
#define BUFSZ 1024
int main(int argc, char *argv[]){
    int shmid;
    key_t key;
    key = ftok("./",2015);
    if(key==-1){
        perror("ftok");
    }
    shmid = shmget(key, BUFSZ, IPC_CREAT|0666);
    if(shmid<0){
        perror("shmget");
        exit(-1);
    }
    return 0;

}

常用函数shmat、shmdt、shmctl
write.c

#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<string.h>
#include<sys/shm.h>
#define BUFSZ 512
int main(int argc,char* argv[]){
    int shmid;
    int ret;
    key_t key;
    char *shmadd;
    key = ftok(".",2015);
    if(key == -1){
        perror("ftok");
    }
    shmid = shmget(key,BUFSZ,IPC_CREAT|0666);
    if(shmid<0){
        perror("shmget");
        exit(-1);
    }
    shmadd = shmat(shmid, NULL, 0);
    if(shmadd<0){
        perror("shmat");
        _exit(-1);
    }
    printf("copy data to shared-memory\n");
    bzero(shmadd,BUFSZ);
    strcpy(shmadd,"how are you,hc\n");
    
    return 0;
}


read.c

#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<string.h>
#include<sys/shm.h>
#define BUFSZ 512
int main(int argc,char *argv[]){
    int shmid;
    int ret;
    key_t key;
    char *shmadd;

    key = ftok("./",2015);
    if(key==-1){
        perror("ftok");
    }
    system("ipcs -m");
    shmid = shmget(key, BUFSZ,IPC_CREAT|0666);
    if(shmid<0){
        perror("shmget");
        exit(-1);
    }
    shmadd = shmat(shmid, NULL, 0);
    if(shmadd<0){
        perror("shmat");
        exit(-1);
    }
    printf("data = [%s]\n",shmadd);
    ret = shmdt(shmadd);
    if(ret<0){
        perror("shmdt");
        exit(1);
    }else{
        printf("deleted shared-memory\n");
    }
    shmctl(shmid,IPC_RMID,NULL);
    system("ipcs -m");
    return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
该资源内项目源码是个人的课程设计、毕业设计,代码都测试ok,都是运行成功后才上传资源,答辩评审平均分达到96分,放心下载使用! ## 项目备注 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用! 2、本项目适合计算机相关专业(如计科、人工智能、通信工程、自动化、电子信息等)的在校学生、老师或者企业员工下载学习,也适合小白学习进阶,当然也可作为毕设项目、课程设计、作业、项目初期立项演示等。 3、如果基础还行,也可在此代码基础上进行修改,以实现其他功能,也可用于毕设、课设、作业等。 下载后请首先打开README.md文件(如有),仅供学习参考, 切勿用于商业用途。 该资源内项目源码是个人的课程设计,代码都测试ok,都是运行成功后才上传资源,答辩评审平均分达到96分,放心下载使用! ## 项目备注 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用! 2、本项目适合计算机相关专业(如计科、人工智能、通信工程、自动化、电子信息等)的在校学生、老师或者企业员工下载学习,也适合小白学习进阶,当然也可作为毕设项目、课程设计、作业、项目初期立项演示等。 3、如果基础还行,也可在此代码基础上进行修改,以实现其他功能,也可用于毕设、课设、作业等。 下载后请首先打开README.md文件(如有),仅供学习参考, 切勿用于商业用途。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值