进程间通信小练习

一、要求用消息队列实现AB进程对话:
1)A进程发送一句话,B进程接收后打印;
2)B进程接着再发送一句话,A进程接收打印;
3)重复上述步骤,当A进程或者B进程接收到quit后退出AB进程。

#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
 
struct msgbuf
{
	long mtype;
	char mtext[128];
};
int main(int argc, const char *argv[])
{
	key_t key = ftok("./", 4);
	if(key<0)
	{
		perror("ftok");
		return -1;
	}
	printf("key=%#x\n", key);
 
	key_t key1=ftok("./", 5);
	if(key1<0)
	{
		perror("ftok");
		return -1;
	}
	printf("key1=%#x\n", key1);
	int msqid= msgget(key, IPC_CREAT|0777);
	if(msqid < 0)
	{
		perror("msgget");
		return -1;
	}
	int msqid1=msgget(key1, IPC_CREAT|0777);
	if(msqid1<0)
 
	{
		perror("msgge");
		return -1;
	}
	printf("msqid1=%d", msqid1);
	printf("msqid=%d\n", msqid);
 
	//
	struct msgbuf snd;
	struct msgbuf rcv;
	ssize_t res;
	
//	strcmp(snd.mtext, "abcd");
	while(1)
	{
		printf("请输入类型:");
		scanf("%ld", &snd.mtype);
		while(getchar()!='\n');
		if(0==snd.mtype)
		{
			break;
		}
		printf("请输入内容:");
		fgets(snd.mtext, sizeof(snd.mtext), stdin);
		snd.mtext[strlen(snd.mtext)-1]=0;
		if(msgsnd(msqid, &snd, sizeof(snd.mtext), 0)<0)
		{
			perror("msgsnd"	);
			return -1;
		}
		res=msgrcv(msqid1, &rcv, sizeof(rcv.mtext), 0, 0);
		if(res<0)
		{
			perror("msgrcv");
			return -1;
		}
		printf("res=%ld %ld %s\n", res, rcv.mtype, rcv.mtext);
	
	//	system("ipcs -q");
	}
	msgctl(msqid,IPC_RMID, NULL);
	msgctl(msqid1,IPC_RMID, NULL);
	return 0;
}
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
 
struct msgbuf
{
	long mtype;
	char mtext[128];
};
int main(int argc, const char *argv[])
{
	key_t key = ftok("./", 4);
	if(key<0)
	{
		perror("ftok");
		return -1;
	}
	key_t key1=ftok("./", 5);
	if(key<0)
	{
		perror("ftok");
		return -1;
	}
	printf("key=%#x\n", key);
	printf("key1=%#x\n", key1);
 
 
	int msqid= msgget(key, IPC_CREAT|0777);
	if(msqid < 0)
	{
		perror("msgget");
		return -1;
	}
	int msqid1=msgget(key1, IPC_CREAT|0777);
	if(msqid1<0)
	{
		perror("msgget");
		return -1;
	}
	printf("msqid=%d\n", msqid);
	printf("msqid=%d\n", msqid1);
 
	//
	struct msgbuf rcv;
	struct msgbuf snd;
	ssize_t res;
 
	while(1)
	{
		res=msgrcv(msqid, &rcv, sizeof(rcv.mtext), 0,0);
		if(res<0)
		{
			perror("msgrcv");
			return -1;
		}
		printf("res:%ld %ld %s\n", res, rcv.mtype, rcv.mtext);
		printf("请输类型>>");
		scanf("%ld", &snd.mtype);
		while(getchar()!='\n');
		if(0==snd.mtype)
		{
			break;
		}
		printf("请输入内容>>");
		fgets(snd.mtext, sizeof(snd.mtext), stdin);
		snd.mtext[strlen(snd.mtext)-1]=0;
		if(msgsnd(msqid1, &snd, sizeof(snd.mtext), 0)<0)
		{
			perror("msfsnd");
			return -1; 
		}
	}
	msgctl(msqid,IPC_RMID, NULL);
	msgctl(msqid1,IPC_RMID, NULL);
	return 0;
}

二、A进程 循环 打印str字符串。
2. B进程 循环 倒置str字符串,不使用辅助数组。注意是循环倒置,要把字符串倒过来,倒回
去。
3. 要求A进程打印出来的结果是有序的,例如:"123456" 或者 "654321",不允许出
现"623451",,,等无序情况

#include <stdio.h>
#include <sys/ipc.h>
#include <stdlib.h>
#include <string.h>
#include <sys/shm.h>
#include <unistd.h>
int main(int argc, const char *argv[])
{
	//创建k值
	key_t key=ftok("./", 1);
	if(key<0)
	{
		perror("ftok");
		return -1;
	}
 
	printf("key = %#x\n", key);
 
	int shmid=shmget(key, 32, IPC_CREAT|0777);
	if(shmid<0)
	{
		perror("shmget");
		return -1;
	}
	printf("shid= %d\n", shmid);
 
	void *addr=shmat(shmid, NULL, 0);
	if((void *)-1==addr)
	{
		perror("shmat");
 
		return -1;
	}
	int *pa=(int*)addr;
	*pa=0;
	char buf[]="hello world";
	char* str =(char *)addr+4;
	strcpy(str, "123456");
	while(1)
	{
		if(0==*pa)
		{
			printf("%s\n", str);
			sleep(1);
		}
		*pa=1;
	}
//	printf("addr=%p\n", addr);
//	system("ipcs -m");
	
	return 0;
}
#include <stdio.h>
#include <sys/ipc.h>
#include <stdlib.h>
#include <string.h>
#include <sys/shm.h>
#include <unistd.h>
int main(int argc, const char *argv[])
{
	//创建k值
	key_t key=ftok("./", 1);
	if(key<0)
	{
		perror("ftok");
		return -1;
	}
 
	printf("key = %#x\n", key);
 
	int shmid=shmget(key, 32, IPC_CREAT|0777);
	if(shmid<0)
	{
		perror("shmget");
		return -1;
	}
	printf("shid= %d\n", shmid);
 
	void *addr=shmat(shmid, NULL, 0);
	if((void *)-1==addr)
	{
		perror("shmat");
 
		return -1;
	}
	int *pa=(int*)addr;
	char *str=(char *)addr+4;
	int i;
	int temp;
	while(1)
	{
		if(1==*pa)
		{
			for(i=0; i<strlen(str)/2; i++)
		
			{
				temp=str[i];
				str[i]=str[strlen(str)-1-i];
				str[strlen(str)-1-i]=temp;
			}
			printf("%s\n", str);
			sleep(1);
			*pa=0;
		}
	}
	printf("addr=%p\n", addr);
//	system("ipcs -m");
	
	return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值