A
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
int main(int argc, const char *argv[])
{
//创建管道fifo1
if(mkfifo("./zuoye_fifo1", 0664) < 0)
{
if(errno != EEXIST)
{
perror("mkfifo");
return -1;
}
}
printf("fifo1创建成功!\n");
//创建管道fifo2
if(mkfifo("./zuoye_fifo2", 0664) < 0)
{
if(errno != EEXIST)
{
perror("mkfifo");
return -1;
}
}
printf("fifo2创建成功!\n");
//写
int fo = open("./zuoye_fifo1", O_WRONLY);
if(fo < 0)
{
perror("open");
return -1;
}
printf("open wronly success!!!\n");
//读
int fo2 = open("./zuoye_fifo2", O_RDONLY);
if(fo < 0)
{
perror("open");
return -1;
}
printf("open rdonly success!!!\n");
char buf[200] = "";
ssize_t res = 0;
while(1)
{
//写
printf("请输入数据:");
fgets(buf, sizeof(buf), stdin); //从终端获取数据
buf[strlen(buf)-1] = '\0';
if(write(fo, buf, sizeof(buf)) < 0)
{
perror("write");
return -1;
}
printf("写入成功\n");
if(strcmp(buf, "quit") == 0)
break;
//读
bzero(buf, sizeof(buf));
//读写端均存在,管道中没有数据,read函数阻塞
res = read(fo2, buf, sizeof(buf));
if(res < 0)
{
perror("read");
return -1;
}
else if(res == 0)
{
printf("写端关闭,且管道中没有数据\n");
break;
}
printf("%ld:%s \n", res, buf);
if(strcmp(buf, "quit") == 0)
break;
}
close(fo);
return 0;
}
B
#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdlib.h>
#include <string.h>
//MSG_EXCEPT的宏需要追加复制过来
#define MSG_EXCEPT 020000
struct msgbuf
{
long mtype;
char mtext[100];
};
int main(int argc, const char *argv[])
{
key_t key1 = ftok("/home/ubuntu/",2);
if(key1<0)
{
perror("ftok");
return-1;
}
printf("key1 = %#x\n",key1);
int msqid1 = msgget(key1,IPC_CREAT|0664);
if(msqid1<0)
{
perror("msgget");
return -1;
}
printf("msqid = %d\n",msqid1);
key_t key2 = ftok("/home/ubuntu/",2);
if(key2<0)
{
perror("ftok");
return-1;
}
printf("key = %#x\n",key2);
int msqid2 = msgget(key2,IPC_CREAT|0664);
if(msqid2<0)
{
perror("msgget");
return -1;
}
printf("msqid = %d\n",msqid2);
ssize_t res = 0;
struct msgbuf rcv;
while(1)
{
system("ipcs -q");
//A接收
//第三个参数,msgtyp==0,读取队列中第一条消息,先进先出
//>0指定消息类型读取
//第四个参数,0为阻塞方式接收,IPC_NOWAIT非阻塞方式接收
//读取消息队列中的第一条消息
res = msgrcv(msqid1,&rcv,sizeof(rcv.mtext),0,0);
/* //读取消息队列中的第一条消息类型为100的消息
res = msgrcv(msqid,&rcv,sizeof(rcv.mtext),100,0);
//读取消息队列中的第一条消息类型不为100的消息
res = msgrcv(msqid,&rcv,sizeof(rcv.mtext),100,MSG_EXCEPT);
*/
if(strcmp(rcv.mtext,"quit") == 0)
{
break;
}
if(res < 0)
{
perror("msgrcv");
return -1;
}
printf("res=%ld | mtype=%ld:mtext=%s\n",res,rcv.mtype,rcv.mtext);
//B发送
printf("请输入消息类型>>>");
scanf("%ld",&rcv.mtype);
getchar();
printf("请输入消息内容>>>");
fgets(rcv.mtext,sizeof(rcv.mtext),stdin);
if(strcmp(rcv.mtext,"quit") == 0)
{
break;
}
rcv.mtext[strlen(rcv.mtext)-1] = '\0';
if(msgsnd(msqid2,(void*)&rcv,sizeof(rcv.mtext),0)<0)
{
perror("msgsnd");
return -1;
}
printf("发送消息成功!\n");
system("ipcs -q");
}
//删除消息队列
if(msgctl(msqid1,IPC_RMID,NULL) < 0)
{
perror("msgctl");
return-1;
}
if(msgctl(msqid2,IPC_RMID,NULL) < 0)
{
perror("msgctl");
return-1;
}
printf("删除消息队列成功!\n");
system("ipcs -q");
return 0;
}