Linux学习笔记---进程间通信(上)(管道和消息队列)

一、管道通信
1.无名管道

 #include <unistd.h>
 int pipe(int pipefd[2]);
 #define _GNU_SOURCE     /* See feature_test_macros(7) */
 #include <unistd.h>
 int pipe2(int pipefd[2], int flags);

pipe() 创建一个管道,一个可以使用的单向数据通道
用于进程间通信。 数组 pipefd 用于返回两个引用管道末端的文件描述符。 pipefd[0] 指到管道的读取端。 pipefd[1] 是指管道。 写入管道写入端的数据由内核,直到它从管道的读取端读取。

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
int main()
{
    char readbuf[128]={0};
    int pid;
    int fd[2];
    if(pipe(fd)<0){                   //创建管道
        printf("creat pipe failed\n");      
    }
    pid=fork();      //创建进程
    if(pid<0){
        printf("creat failed\n");
    }else if(pid>0){
        close(fd[0]);       //无名管道是半双工,写的时候关闭读
        int n_write=write(fd[1],"pipe practise",strlen("pipe    practise"));
        wait();   //等待子进程退出
    }else{
        close(fd[1]);
        int n_read=read(fd[0],readbuf,128);  //管道中的数据读走就没了,相当于水管,只流动,不储存。
        printf("read data:%s\n",readbuf);
        exit(0);
    }
}

2、命名管道
FIFO,也称为命名管道。

#include <sys/types.h>
#include <sys/stat.h>
//函数原型,第一个参数为文件路径,第二个参数为权限;
int mkfifo(const char *pathname, mode_t mode);

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <string.h>
#include <fcntl.h>

int main()
{
    int n_read;
    char readbuf[128]={0};
    //int mkfifo(const char *pathname, mode_t mode);
    //创建命名管道
    if((mkfifo("./file1",0600)==-1)&&errno!=EEXIST){
        printf("creat fifo failed\n");
        perror("why");
    }
    int fd=open("./file1",O_RDONLY);   //只读方式打开,默认为阻塞模式;
    while(1){
        n_read=read(fd,readbuf,128);
        printf("read %d byte,context:%s\n",n_read,readbuf);

    }
    close(fd);
    return 0;
}

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <string.h>
#include <fcntl.h>
#include <stdlib.h>
int main()
{
    int cnt=0;
    char *str="Wednesday today, Thursday tomorrow";
    int fd=open("./file1",O_WRONLY);   //以只写方式打开
    while(1){
        write(fd,str,strlen(str));
        sleep(1);
        cnt++;
        if(cnt==20){
            exit(-1);
        }
    }
    close(fd);
    return 0;
}

只读open会阻塞到其他进程为写而打开此FIFO。
在这里插入图片描述
二、消息队列
消息队列,是消息的链接表,存放在内核中。一个消息队列由一个标识符(即队列ID)来标识。
用到的API:

#include <sys/msg.h>
// 创建或打开消息队列:成功返回队列ID,失败返回-1
int msgget(key_t key, int flag);
// 添加消息:成功返回0,失败返回-1
int msgsnd(int msqid, const void *ptr, size_t size, int flag);
// 读取消息:成功返回消息数据的长度,失败返回-1
int msgrcv(int msqid, void *ptr, size_t size, long type,int flag);
// 控制消息队列:成功返回0,失败返回-1
int msgctl(int msqid, int cmd, struct msqid_ds *buf);

编程实现消息队列的信息传输:
1.先读取消息,再添加消息

#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <string.h>
#include <stdlib.h>
struct msgbuf {
   long mtype;       /* message type, must be > 0 */
   char mtext[128];    /* message data */
};

int main()
{
    key_t key;
    if((key=ftok(".",'m'))<0){   //获取key值,ftok为0-255之间自定义的数;
        printf("ftok error\n");
        exit(1);
    }
    struct msgbuf readbuf;
    int msgID=msgget(key,IPC_CREAT|0777);   //创建队列
    if(msgID==-1){
        printf("creat msgget failed\n");
    }
    msgrcv(msgID,&readbuf,128,888,0);   //读取消息
    printf("read msg:%s\n",readbuf.mtext);
    struct msgbuf sndbuf={988,"Anything you want can come true"};
    msgsnd(msgID,&sndbuf,strlen(sndbuf.mtext),0);   //添加
    return 0;
}
~  

2.先添加消息再读取

#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <string.h>
#include <stdlib.h>
struct msgbuf {
   long mtype;       /* message type, must be > 0 */
   char mtext[128];    /* message data */
};

int main()
{
    key_t key;
    if((key=ftok(".",'m'))<0){
        printf("ftok error\n");
        exit(1);
    }
    struct msgbuf readbuf;
    struct msgbuf str={888,"today is a good day"};
    int msgID=msgget(key,IPC_CREAT|0777);
    if(msgID==-1){
        printf("creat msgget failed\n");
    }
    msgsnd(msgID,&str,strlen(str.mtext),0);
    msgrcv(msgID,&readbuf,128,988,0);
    printf("read from get:%s\n",readbuf.mtext);
    return 0;
}

运行结果:
在这里插入图片描述
进程2写消息到队列中,进程1读取到后打印输出,再添加消息到队列中,进程2读取后输出。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值