sigactdemo.c

来自「understanding unix/linux programming sou」· C语言 代码 · 共 43 行

C
43
字号
/* sigactdemo.c *               purpose: shows use of sigaction() *               feature: blocks ^\ while handling ^C *                        does not reset ^C handler, so two kill */#include	<stdio.h>#include	<signal.h>#define	INPUTLEN	100main(){	struct sigaction newhandler;            /* new settings        */	sigset_t         blocked;               /* set of blocked sigs */	void		 inthandler();          /* the handler         */	char		 x[INPUTLEN];	/* load these two members first */	newhandler.sa_handler = inthandler;      /* handler function    */	newhandler.sa_flags = SA_RESETHAND | SA_RESTART;  /* options    */	/* then build the list of blocked signals */	sigemptyset(&blocked);                  /* clear all bits      */	sigaddset(&blocked, SIGQUIT);		/* add SIGQUIT to list */	newhandler.sa_mask = blocked;		/* store blockmask     */	if ( sigaction(SIGINT, &newhandler, NULL) == -1 )		perror("sigaction");	else		while( 1 ){			fgets(x, INPUTLEN, stdin);			printf("input: %s", x);		}}void inthandler(int s){	printf("Called with signal %d\n", s);	sleep(s);	printf("done handling signal %d\n", s);}

⌨️ 快捷键说明

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