• 国庆day1


    国庆day1

    #include
    #include
    #include
    struct msgbuf
    {
    	long mtype;      //消息类型
    	char mtext[128]; //消息内容
    };
     
    //线程1函数
    void *task1(void *arg)
    {
    	int msqid = *(int *)arg;//获取消息队列id号
    	struct msgbuf msbuf;//声明消息结构体
    	printf("A:\n\t");
    	fflush(stdout);
    	while (1)
    	{
    		msbuf.mtype = 1;//消息类型
    		fgets(msbuf.mtext,sizeof(msbuf.mtext),stdin);
    		msbuf.mtext[strlen(msbuf.mtext)-1]='\0';
    		if(msgsnd(msqid,&msbuf,sizeof(msbuf)-sizeof(long),0) == -1)
    		{
    			perror("msgsnd");
    			return NULL;
    		}
    		//printf("线程%d:发送成功\n",getpid());
    		printf("A:\n\t");
    		fflush(stdout);
    		if (!strcmp(msbuf.mtext,"quit"))
    		{
    			system("clear");
    			exit(0);
    		}
    	}
    	pthread_exit(NULL);
    }
     
    //线程2函数
    void *task2(void *arg)
    {
    	int msqid = *(int *)arg;
    	struct msgbuf buf;
    	ssize_t num = 0;
    	while (1)
    	{
    		bzero(&buf,sizeof(buf));
    		if ((num = msgrcv(msqid,&buf,sizeof(buf.mtext),2,0))<0)
    		{
    			//perror("msgrcv");
    			return NULL;
    		}
    		printf("\nB:\n\t%s\n",buf.mtext);
    		printf("A:\n\t");
    		fflush(stdout);
     
    		if (!strcmp(buf.mtext,"quit"))
    		{
    			msgctl(msqid,IPC_RMID,NULL);
    			system("clear");
    			exit(0);
    		}
    	}
    	pthread_exit(NULL);
    }
    int main(int argc, const char *argv[])
    {
    	key_t key = ftok("./",0);
    	if (key == -1)
    	{
    		perror("ftok");
    		return -1;/* code */
    	}
    	umask(0);
    	int msqid = msgget(key,IPC_CREAT|0664);
    	if (msqid == -1)
    	{
    		perror("msgget");
    		return -1;
    	}
     
    	pthread_t tid1,tid2;
    	if (pthread_create(&tid1,NULL,task1,&msqid) != 0)
    	{
    		printf("线程1创建失败\n");
    	}
    	if (pthread_create(&tid2,NULL,task2,&msqid) != 0)
    	{
    		printf("线程1创建失败\n");
    	}
    	
     
    	pthread_join(tid1,NULL);
    	pthread_join(tid2,NULL);
     
    	
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98

    =====================================

    #include
    struct msgbuf
    {
    	long mtype;      //消息类型
    	char mtext[128]; //消息内容
    };
     
    //线程1函数
    void *task1(void *arg)
    {
    	int msqid = *(int *)arg;
    	struct msgbuf msbuf;
    	printf("B:\n\t");
    	fflush(stdout);
    	while (1)
    	{
    		msbuf.mtype = 2;
    		fgets(msbuf.mtext,sizeof(msbuf.mtext),stdin);
    		msbuf.mtext[strlen(msbuf.mtext)-1]='\0';
    		if(msgsnd(msqid,&msbuf,sizeof(msbuf)-sizeof(long),0) == -1)
    		{
    			perror("msgsnd");
    			return NULL;
    		}
    		//printf("线程%d:发送成功\n",getpid());
    		printf("B:\n\t");
    		fflush(stdout);
    		if (!strcmp(msbuf.mtext,"quit"))
    		{
    			system("clear");
    			exit(0);
    		}
    	}
    	pthread_exit(NULL);
    }
     
    //线程2函数
    void *task2(void *arg)
    {
    	int msqid = *(int *)arg;
    	struct msgbuf buf;
    	ssize_t num = 0;
    	while (1)
    	{
    		bzero(&buf,sizeof(buf));
    		if ((num = msgrcv(msqid,&buf,sizeof(buf.mtext),1,0))<0)
    		{
    			//perror("msgrcv");
    			return NULL;
    		}
    		printf("\nA:\n\t%s\n",buf.mtext);
    		printf("B:\n\t");
    		fflush(stdout);
     
    		if (!strcmp(buf.mtext,"quit"))
    		{
    			msgctl(msqid,IPC_RMID,NULL);
    			system("clear");
    			exit(0);
    		}
    	}
    	pthread_exit(NULL);
    }
    int main(int argc, const char *argv[])
    {
    	key_t key = ftok("./",0);
    	if (key == -1)
    	{
    		perror("ftok");
    		return -1;/* code */
    	}
    	umask(0);
    	int msqid = msgget(key,IPC_CREAT|0664);
    	if (msqid == -1)
    	{
    		perror("msgget");
    		return -1;
    	}
     
    	pthread_t tid1,tid2;
    	if (pthread_create(&tid1,NULL,task1,&msqid) != 0)
    	{
    		printf("线程1创建失败\n");
    	}
    	if (pthread_create(&tid2,NULL,task2,&msqid) != 0)
    	{
    		printf("线程1创建失败\n");
    	}
    	
     
    	pthread_join(tid1,NULL);
    	pthread_join(tid2,NULL);
     
    	
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
  • 相关阅读:
    IOC和DI入门案例
    【AI数学】交叉熵损失函数CrossEntropy
    【错误记录】安装 Hadoop 运行环境报错 ( Error: JAVA_HOME is incorrectly set. Please update xxx\hadoop-env.cmd )
    Python 中 Selenium 的 getAttribute() 函数
    【常用代码15】文字单词超出强制分割换行,word-break: break-all;和word-wrap: break-word;的区别
    谈谈.NET Core下如何利用 AsyncLocal 实现共享变量
    剑指Offer 05.替换空格
    【UE5】创建蓝图
    【数据分享】2023年我国上市公司数据(Excel格式/Shp格式)
    880. 索引处的解码字符串
  • 原文地址:https://blog.csdn.net/weixin_45790676/article/details/133620624