【无标题】

 建立两个.c 建立子父进程,父进程发送消息到队列,子进程读取队列,另一个同样。

 send.c

#include<stdio.h>
#include<string.h>
#include <sys/msg.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <unistd.h>
       #include <sys/stat.h>
       #include <fcntl.h>
#include<stdlib.h>
#include <sys/wait.h>
#if 1
struct msgbuf {
               long mtype;       /* message type, must be > 0 */
               char mtext[1000];    /* message data */
           };
#endif
int main(int argc, const char *argv[])
{

	pid_t pid;	
	pid=fork();
	if(pid>0)
	{
		
		key_t key=ftok("./",'B');
		if(key==-1)
		{
			perror("ftok");
			return -1;
		}
		int msgID=msgget(key,IPC_CREAT|0664);
		if(msgID==-1)
		{
			perror("msgID");
			return -1;
		}
		struct msgbuf data1;
		while(1)
		{
			printf("请输入消息类型:");
			fflush(stdout);
			read(0,&data1.mtype,sizeof(data1.mtype));
			printf("请输入数据:");
			fflush(stdout);
			read(0,data1.mtext,sizeof(data1.mtext));
			data1.mtext[strlen(data1.mtext)-1]='\0';
			int len= sizeof(struct msgbuf)-sizeof(long);

			msgsnd(msgID,&data1,len,0);
			if(strcmp(data1.mtext,"quit")==0)
			{
				break;
			}

		}
		if(msgctl(msgID,IPC_RMID,NULL)==-1)
		{
			printf("rcv.c文件父进程消息删除失败\n");
			return -1;
		}
		printf("rcv.c文件父进程消息删除成功\n");
	}
	else if(pid==0)
	{

		key_t key=ftok("./",'A');
		if(key==-1)
		{
			perror("ftok");
			return -1;
		}
		int msgID=msgget(key,IPC_CREAT|0664);
		if(msgID==-1)
		{
			perror("msgID");
			return -1;
		}
		struct msgbuf data2_rcv;
		while(1)
		{
			int len= sizeof(struct msgbuf)-sizeof(long);
			msgrcv(msgID,&data2_rcv,len,0,0);
			printf("%s\n",data2_rcv.mtext);
	fflush(stdout);
			if(strcmp(data2_rcv.mtext,"quit")==0)
			{
				break;
			}

		}
		
		printf("rcv.c文件子进程消息删除成功\n");
		exit(EXIT_SUCCESS);
	}
	else
	{
		perror("fork");
		return -1;
	}


	wait(NULL);
	return 0;
}

rcv.c 

#include<stdio.h>
#include<string.h>
#include <sys/msg.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include<stdlib.h>
#include <sys/wait.h>
#if 1
struct msgbuf {
	long mtype;       /* message type, must be > 0 */
	char mtext[1000];    /* message data */
};
#endif


int main(int argc, const char *argv[])
{
	pid_t pid;	
	pid=fork();
	if(pid>0)
	{
		key_t key=ftok("./",'A');
		if(key==-1)
		{
			perror("ftok");
			return -1;
		}
		int msgID=msgget(key,IPC_CREAT|0664);
		if(msgID==-1)
		{
			perror("msgID");
			return -1;
		}
		struct msgbuf data1;
		while(1)
		{
			printf("请输入消息类型:");
			scanf("%ld",&data1.mtype);
			getchar();
			//fflush(stdout);
			//read(0,&data1.mtype,sizeof(data1.mtype));
			printf("请输入数据:");
			fflush(stdout);
			read(0,data1.mtext,sizeof(data1.mtext));
			data1.mtext[strlen(data1.mtext)-1]='\0';
			int len= sizeof(struct msgbuf)-sizeof(long);
			msgsnd(msgID,&data1,len,0);
			if(strcmp(data1.mtext,"quit")==0)
			{
				break;
			}

		}
		if(msgctl(msgID,IPC_RMID,NULL)==-1)
		{
			printf("send.c文件父进程消息删除失败\n");
			return -1;
		}
		printf("send.c文件父进程消息删除成功\n");
	}
	else if(pid==0)
	{
		key_t key=ftok("./",'B');
		if(key==-1)
		{
			perror("ftok");
			return -1;
		}
		int msgID=msgget(key,IPC_CREAT|0664);
		if(msgID==-1)
		{
			perror("msgID");
			return -1;
		}
		struct msgbuf data2_rcv;
		while(1)
		{
			int len= sizeof(struct msgbuf)-sizeof(long);
			msgrcv(msgID,&data2_rcv,len,0,0);
			printf("%s",data2_rcv.mtext);
			fflush(stdout);

			if(strcmp(data2_rcv.mtext,"quit")==0)
			{
				break;
			}

		}

		printf("send.c文件子进程消息删除成功\n");
		exit(EXIT_SUCCESS);
	}
	else
	{
		perror("fork");
		return -1;
	}


	wait(NULL);
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值