driver_conductor.c

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

C
79
字号
/* * this program simulated a simple communication between dirver and conductor. * conductor tell diver to begin driving via signal SIGINT(Ctrl + c), and stoping via signal SIGQUIT(Ctrl + \) * dirver tell conductor to notice all the passenger get off the bus when bus arrive at station via signal(SIGHUP) */ #include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <signal.h>#include <sys/types.h>pid_t	pid1;void sig1( int sig_no ){	if( sig_no == 2 )	{		kill( getppid(), 10 );		/* tell driver to drive the bus */		}	else if( sig_no == 3 )	{		kill( getppid(), 12 );		/* tell driver to stop the bus */	}	else if( sig_no == 10)	{		printf( "I have stopped the bus, all the passenger can get off now!\n");	/* recieve the signal 10 from driver */	}}void sig2( int sig_no ){	if( sig_no == 10 )	{		printf( "All the passenger have got on the bus, let's go!\n" );			/* recieve the signal 10 from conductor */		}	else if( sig_no == 12 )	{		printf( "We have arrived at bus station, please stop the car!\n" );		/* recieve the signal 12 from conductor */	}	else if( sig_no == 1 )	{		kill( pid1, 10 );								/* tell conductor the bus has arrivaled at station */	}}int main(){	while( (pid1 = fork()) == -1 )			/* create a child process */                ;	/* conductor is child process */ 	if( pid1 == 0 )			{		while(1)		{			signal( SIGINT, sig1 );			signal( SIGQUIT, sig1 );			signal( 10, sig1 );		}	}	/* driver is parent process */	else	{			printf( "parent process id = %d\n", getpid() );		signal( SIGINT, SIG_IGN	);		signal( SIGQUIT, SIG_IGN );			signal( SIGHUP, sig2 );		signal( 10, sig2 );			/* signal 10 represent signal SIGUSR1 */		signal( 12, sig2 );			/* signal 12 represent signal SIGUSR2 */			while(1)		{			pause();			pause();		}	}}

⌨️ 快捷键说明

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