📄 driver_conductor.c
字号:
/* * 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 + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -