IO / day07 作业

1. 使用消息队列完成两个进程之间相互通信

代码

#include <myhead.h>

//define a msg struct type
struct msgbuf
{
	long mtype; //消息类型
	char mtext[1024]; //消息正文大小

};

//macro msg size
#define SIZE (sizeof(struct msgbuf)-sizeof(long))

int recv(int mtype_recv)
{
	//*-----snd-----	

	//1. create key
	key_t key = ftok("/", 'k');
	if(key==-1)
	{
		perror("error");
		return -1;
	}

	printf("key=%#x\n", key);
	
	//2. create msg queue
	int msgid = msgget(key, IPC_CREAT | 0664);
	if(msgid == -1)
	{
		perror("msgget error");
		return -1;
	}

	printf("msgid=%d\n", msgid);


	//3. send msg to msg queue
	//define a msg var
	struct msgbuf buf;

	while(1)
	{

		//将消息从消息队列读取
		msgrcv(msgid, &buf, SIZE, mtype_recv, 0 );
		//param1: msg queue id
		//param2; buffer start address
		//param3: msg text size
		//param4: msg type
		//param5: if block, 0--> block; 

		printf("recv msg=%s\n", buf.mtext);

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

	//4. delete msg queue
      if ((msgctl(msgid, IPC_RMID, NULL)) == -1)
      {
          perror("msgctl error");
          return -1;
      }  

}

void* task_recv(void* arg)
{
	int *pmtype_recv = (int *)arg;
	recv(*pmtype_recv);
}




int main(int argc, const char *argv[])
{

	int mtype_snd = 0;
	int mtype_recv = 0;

	printf("plesae input msg type for sender:\n>");
	scanf("%d", &mtype_snd );

	printf("plesae input msg type for receiver:\n>");
	scanf("%d", &mtype_recv );

	int *pmtype_recv = &mtype_recv;


	pthread_t tid = -1;
	int res = pthread_create(&tid, NULL, task_recv, (void *)pmtype_recv );
	if(res !=0 )
	{
		printf("thread create error\n");
		return -1;
	}

	//*-----snd-----	

	//1. create key
	key_t key = ftok("/", 'k');
	if(key==-1)
	{
		perror("error");
		return -1;
	}

	printf("key=%#x\n", key);
	
	//2. create msg queue
	int msgid = msgget(key, IPC_CREAT | 0664);
	if(msgid == -1)
	{
		perror("msgget error");
		return -1;
	}

	printf("msgid=%d\n", msgid);


	//3. send msg to msg queue
	//define a msg var
	struct msgbuf buf;
	buf.mtype = mtype_snd;

	while(1)
	{

		printf("please input msg to send:");
		scanf("%s", buf.mtext);

		//将消息从消息队列读取
		msgsnd(msgid, &buf, SIZE, 0 );
		//param1: msg queue id
		//param2; buffer start address
		//param3: msg text
		//param4: if block, 0--> block; 
		printf("recv msg=%s\n", buf.mtext);

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

	//4. delete msg queue
      if ((msgctl(msgid, IPC_RMID, NULL)) == -1)
      {
          perror("msgctl error");
          return -1;
      }    	
	return 0;
}

运行结果

2. 思维导图

相关推荐

  1. day01.03.作业•二

    2023-12-11 21:50:02       27 阅读
  2. 网络 / day03 作业

    2023-12-11 21:50:02       59 阅读
  3. 网络 / day06 作业

    2023-12-11 21:50:02       61 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2023-12-11 21:50:02       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2023-12-11 21:50:02       100 阅读
  3. 在Django里面运行非项目文件

    2023-12-11 21:50:02       82 阅读
  4. Python语言-面向对象

    2023-12-11 21:50:02       91 阅读

热门阅读

  1. qt有哪些常用控件

    2023-12-11 21:50:02       54 阅读
  2. 传染病传播速度

    2023-12-11 21:50:02       53 阅读
  3. C语言-链表_基础

    2023-12-11 21:50:02       41 阅读
  4. redis中scan命令详解

    2023-12-11 21:50:02       51 阅读
  5. Redisson的基础使用(2)

    2023-12-11 21:50:02       57 阅读
  6. 【GO】记一次排查 docker virtual size 过大问题

    2023-12-11 21:50:02       57 阅读
  7. 12.11

    12.11

    2023-12-11 21:50:02      61 阅读
  8. bz2 --- 对 bzip2 压缩算法的支持

    2023-12-11 21:50:02       55 阅读
  9. go进阶语法10问

    2023-12-11 21:50:02       59 阅读
  10. 含电热联合系统的微电网运行优化附Matlab代码

    2023-12-11 21:50:02       55 阅读
  11. Holynix

    Holynix

    2023-12-11 21:50:02      49 阅读
  12. PKCS#11及其在车联网中的应用

    2023-12-11 21:50:02       57 阅读