进程间通信机制--消息队列--信号灯集

用消息队列 实现AB进程对话,要求AB进程能够随时收发。

#include "/home/ubuntu/li/IO/head.h"
//消息包格式
struct msgbuf
{
	long mtype;//消息类型  >0
	char mtext[128];//内容,根据需求改变
};
int main(int argc, const char *argv[])
{
	key_t key = ftok("/home/ubuntu/",1);
	key_t key1 = ftok("/home/ubuntu/",2);
	if(key<0||key1<0)
	{
		ERR_MSG("ftok");
		return -1;
	}
	int msqid = msgget(key,IPC_CREAT|0664);
	int msqid1 = msgget(key1,IPC_CREAT|0664);
	if(msqid<0||msqid1<0)
	{
		ERR_MSG("msgget");
		return -1;
	}

	pid_t cpid = fork();
	if(cpid >0)
	{
		struct msgbuf snd;
		while(1)
		{
			printf("请输入消息类型:");
			scanf("%ld",&snd.mtype);
			while(getchar()!=10);
			if(snd.mtype==0)
				break;
			printf("请输入消息内容:");
			scanf("%s",snd.mtext);
			//阻塞方式发送
			if(msgsnd(msqid,&snd,sizeof(snd.mtext),0)<0)
			{
				ERR_MSG("msgsnd");
				break;
			}
			if(!strcmp(snd.mtext,"quit"))
				break;
		}
		kill(cpid,9);

	}
	else if(cpid == 0)
	{
		struct msgbuf rcv;
		while(1)
		{
			if(msgrcv(msqid1,&rcv,sizeof(rcv.mtext),0,0)<0)
			{
				ERR_MSG("msgrcv");
				break;
			}
			printf("mtype:%ld,mtext:%s\n",rcv.mtype,rcv.mtext);
			if(!strcmp(rcv.mtext,"quit"))
				break;
		}
		kill(0,9);
	}
	else
	{
		ERR_MSG("fork");
		return -1;
	}
	if(msgctl(msqid, IPC_RMID, NULL) < 0)
	{
		perror("msgctl");
		return -1;
	}
	if(msgctl(msqid1, IPC_RMID, NULL) < 0)
	{
		perror("msgctl");
		return -1;
	}


	system("ipcs -q");
	return 0;
}
#include "/home/ubuntu/li/IO/head.h"
//消息包格式
struct msgbuf
{
	long mtype;//消息类型  >0
	char mtext[128];//内容,根据需求改变
};
int main(int argc, const char *argv[])
{
	key_t key = ftok("/home/ubuntu/",2);
	key_t key1 = ftok("/home/ubuntu/",1);
	if(key<0||key1<0)
	{
		ERR_MSG("ftok");
		return -1;
	}
	int msqid = msgget(key,IPC_CREAT|0664);
	int msqid1 = msgget(key1,IPC_CREAT|0664);
	if(msqid<0||msqid1<0)
	{
		ERR_MSG("msgget");
		return -1;
	}

	pid_t cpid = fork();
	if(cpid >0)
	{
		struct msgbuf snd;
		while(1)
		{
			printf("请输入消息类型:");
			scanf("%ld",&snd.mtype);
			while(getchar()!=10);
			if(snd.mtype==0)
				break;
			printf("请输入消息内容:");
			scanf("%s",snd.mtext);
			//阻塞方式发送
			if(msgsnd(msqid,&snd,sizeof(snd.mtext),0)<0)
			{
				ERR_MSG("msgsnd");
				break;
			}
			if(!strcmp(snd.mtext,"quit"))
				break;
		}
		kill(cpid,9);

	}
	else if(cpid == 0)
	{
		struct msgbuf rcv;
		while(1)
		{
			if(msgrcv(msqid1,&rcv,sizeof(rcv.mtext),0,0)<0)
			{
				ERR_MSG("msgrcv");
				break;
			}
			printf("mtype:%ld,mtext:%s\n",rcv.mtype,rcv.mtext);
			if(!strcmp(rcv.mtext,"quit"))
				break;
		}
		kill(0,9);
	}
	else
	{
		ERR_MSG("fork");
		return -1;
	}
	if(msgctl(msqid, IPC_RMID, NULL) < 0)
	{
		perror("msgctl");
		return -1;
	}
	if(msgctl(msqid1, IPC_RMID, NULL) < 0)
	{
		perror("msgctl");
		return -1;
	}
	system("ipcs -q");
	return 0;
}

进程间通信机制--消息队列--信号灯集_第1张图片

创建两个进程,定义一个共享内存,内存中存储char str[10]= “1234567”;要求如下:
A循环打印str;
B循环倒置str; 不能使用辅助数组;
要求出现的结果没有乱序,只能出现 1234567 7654321
不允许使用sleep函数
尽量满足同步互斥

#include "/home/ubuntu/li/IO/head.h"
void strrev(char *p,int len)
{
	char *q = p+len-1;
	char temp;
	while(p<q)
	{
		temp = *p;
		*p = *q;
		*q = temp;
		p++;
		q--;
	}
}
int main(int argc, const char *argv[])
{
	key_t key = ftok("/home/ubuntu/li/IO/Process/IPC/sem/homework/",10);
	if(key<0)
	{
		ERR_MSG("fork");
		return -1;
	}
	//共享内存
	int shmid = shmget(key,100,IPC_CREAT|0664);
	if(shmid<0)
	{
		ERR_MSG("shmget");
		return -1;
	}
	void *add = shmat(shmid,NULL,0);
	if((void *)-1 == add)
	{
		ERR_MSG("shmat");
		return -1;
	}
	strcpy((char *)add,"1234567");
	//信号灯集
	int semid = semget(key,2,IPC_CREAT|0664);
	if(semid<0)
	{
		ERR_MSG("semget");
	}
	struct sembuf sops;
	struct sembuf sops1;
	struct sembuf sops2;
	struct sembuf sops3;
	struct sembuf sops4;
	sops.sem_num = 0;
	sops.sem_op = +1;
	sops.sem_flg = 0;
	if(semop(semid,&sops,1)<0)
	{
		ERR_MSG("semop");
		return -1;
	}
	pid_t cpid = fork();
	if(cpid>0)
	{	
		struct sembuf sops1;
		struct sembuf sops4;
		while(1)
		{ 	//p操作  0号减一
			sops4.sem_num = 0;
			sops4.sem_op = -1;
			sops4.sem_flg = 0;	
			if(semop(semid,&sops4,1)<0)
			{
				ERR_MSG("semop");
				return -1;
			}

			printf("打印:%s\n",(char *)add);
			//v操作  1号加一
			sops1.sem_num = 1;
			sops1.sem_op = +1;
			sops1.sem_flg = 0;	
			if(semop(semid,&sops1,1)<0)
			{
				ERR_MSG("semop");
				return -1;
			}
		}
	}
	else if(0 == cpid)
	{
		struct sembuf sops2;
		struct sembuf sops3;
		while(1)
		{
			//p操作,1号减一
			sops2.sem_num = 1;
			sops2.sem_op = -1;
			sops2.sem_flg = 0;	
			if(semop(semid,&sops2,1)<0)
			{
				ERR_MSG("semop");
				return -1;
			}
			strrev((char *)add,strlen((char *)add));

			//v操作  0号加一
			sops3.sem_num = 0;
			sops3.sem_op = +1;
			sops3.sem_flg = 0;	
			if(semop(semid,&sops3,1)<0)
			{
				ERR_MSG("semop");
				return -1;
			}
		}

	}
	else
	{
		ERR_MSG("fork");
		return -1;
	}
	//删除灯集
	if(semctl(semid,18,IPC_RMID)<0)
	{
		ERR_MSG("semctl");
		return -1;
	}

	//先断开链接,才能删除共享内存
	if(shmdt(add)<0)
	{
		ERR_MSG("shmdt");
		return -1;
	}
	if(shmctl(shmid,IPC_RMID,NULL)<0)
	{
		ERR_MSG("shmctl");
		return -1;
	}
	return 0;
}

进程间通信机制--消息队列--信号灯集_第2张图片

你可能感兴趣的:(IO进程线程,ubuntu,linux)