client1.c

来自「源码包中是我在学习Linux进程间通信时所写的关于五种通信方式(管道」· C语言 代码 · 共 54 行

C
54
字号
/* client1 */#include <stdio.h>#include <stdlib.h>#include <sys/types.h>#include <sys/ipc.h>#include <sys/msg.h>#include <string.h>#include <unistd.h>#include <signal.h>#define KEY_MSG         0x101#define MSGSIZE         128int main(){		int     msgid;        struct  msgbuf        {                long    mtype;		int	subtype;                char    mtext[MSGSIZE];        }bufin, bufout;	msgid = msgget( KEY_MSG, 0666 );		/* open message queue created by server */	if( fork() == 0 )	{		printf( "send message to client2 :" );		while(1)		{			fgets( bufout.mtext, MSGSIZE, stdin );			bufout.mtype = 1L;				bufout.subtype = 2;			msgsnd( msgid, &bufout, MSGSIZE+4, 0 );		}	}	else	{		while(1)		{			msgrcv( msgid, &bufin, MSGSIZE+4, 2L, 0 );			if( bufin.mtext[0] == 'x' || bufin.mtext[0] == 'X' )                	{				kill( fork(), SIGINT );                        	break;                	}			printf("receive message from client2 :%s", bufin.mtext );		}	}}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?