IO进程day7

msg1.c

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <pthread.h>
#include <semaphore.h>
#include <wait.h>
#include <signal.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <semaphore.h>
#include <sys/msg.h>
#include <sys/shm.h>
#include <sys/un.h>
struct msgbuf
{
	long type;
	char buf[128];
};
int id1;
void* run(void* arg)
{
	struct msgbuf msg2;
	while(1)
	{
		memset(&msg2,0,sizeof(msg2));
		msgrcv(id1,&msg2,128,2,020000);
		printf("the message is:%s\n",msg2.buf);
	}
}
int main(int argc, const char *argv[])
{
	int msgtype=atoi(argv[1]);
	pthread_t id;
	pthread_create(&id,0,run,0);
	pthread_detach(id);
	if(argc!=2){printf("wrong argument\n");}
	key_t key=ftok("./ipc1",1);
	if(key==-1){perror("ftok");return 1;}
	id1=msgget(key,IPC_CREAT|0666);
	if(id1==-1){perror("msgget");return 1;}
	struct msgbuf msg1;
	int size=0;
	while(1)
	{
		memset(&msg1,0,sizeof(msg1));
		msg1.type=msgtype;
		printf("enter ur message:");
		scanf("%128s",msg1.buf);
		while(getchar()!=10);
		size=strlen(msg1.buf);
		msgsnd(id1,&msg1,size,0);
	}
	return 0;
}

msg2.c

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <pthread.h>
#include <semaphore.h>
#include <wait.h>
#include <signal.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <semaphore.h>
#include <sys/msg.h>
#include <sys/shm.h>
#include <sys/un.h>
struct msgbuf
{
    long type;
    char buf[128];
};
int id2;
void* run(void* arg)
{
	struct msgbuf msg2;
	int size=0;
	while(1)
	{
    	memset(&msg2,0,sizeof(msg2));
    	msg2.type=2;
    	printf("enter ur message:");
    	scanf("%128s",msg2.buf);
    	while(getchar()!=10);
    	size=strlen(msg2.buf);
    	msgsnd(id2,&msg2,size,0);
	}

}
int main(int argc, const char *argv[])
{
	pthread_t id;
	pthread_create(&id,0,run,0);
	pthread_detach(id);
	int msgtype=atoi(argv[1]);
	if(argc!=2){printf("wrong argument\n");}
	key_t key=ftok("./ipc1",1);
	if(key==-1){perror("ftok");return 1;}
	id2=msgget(key,IPC_CREAT|0666);
	if(id2==-1){perror("msgget");return 1;}
	struct msgbuf msg1;
	int size=0;                             
	while(1)
	{
    	memset(&msg1,0,sizeof(msg1));
    	msgrcv(id2,&msg1,128,msgtype,020000);
    	printf("the message is:%s\n",msg1.buf);
	}

	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值