1. 程式人生 > >Linux實驗全紀錄之 非阻塞型通訊程式框架

Linux實驗全紀錄之 非阻塞型通訊程式框架

#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
#include<signal.h>
#include<stdlib.h>

void sigint_handler(int sig)
{
	printf("received SIGINT signal successed!\n");
	return;
}
void main()
{
	/*建立子程序*/
	int pc;
	pc=fork();
	if(pc==0)
	{
		printf("this is child process pid%d\n",getpid());
		sleep(3);
		printf("this is child process pid first sleep %d\n",getpid());
		sleep(3);
		printf("this is child process pid second sleep %d\n",getpid());
		exit(1);
	}else
	if(pc>0)
	{
		signal(SIGINT,sigint_handler);
		pause();
	
	}else
	if(pc<0)
	{
		printf("建立程序出錯!");
		exit(1);
	}
}

記得子程序3條資訊列印完後按Ctrl+C