|
/*
这个程序是我看信号的时候写的,可是在提示语句后我输入CTRL+C(产生中段),结果出现了问题。。谢谢!!
*/
/*****************************************
signal.c ---used to test the catch a signal and
proceed with it.
Writer: Longwen Date: 08/06/04
*****************************************/
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <signal.h>
#define MAXLINE 20
static void sig_int(int);
int main(void)
{
char buf[MAXLINE];
pid_t pid;
int status;
/* void (*signal(int signo, void (*func)(int)))(int);
Return: the pre set of the signal,or SIG_ERR if error */
if(signal(SIGINT,sig_int)==SIG_ERR)
{
fprintf(stderr,"Signal error.\n");
exit(1);
}
printf("NOW input a shell command(an empty line to quit):");
while(fgets(buf,MAXLINE,stdin)!=NULL && buf[0]!='\n')
{
buf[strlen(buf)-1]='\0';
if((pid=fork())<0)
{
fprintf(stderr,"ERROR in fork()\n");
exit(2);
}
else if(pid==0) /* child */
{
execlp(buf,buf,(char *) 0);
fprintf(stderr,"couldn't execute %s\n",buf);
exit(127);
}
/***************************************
pid_t waitpid(pid_t pid,int *statlog, int options)
options: 0,wnohang,wuntraced
Return: the process ID ,-1 if error
<sys/types.h>
<sys/wait.h>
****************************************/
if((pid=waitpid(pid,&status,0)) <0) /* parent*/
{
fprintf(stderr,"waitpid error!\n");
exit(12;
}
printf("NOW input a shell command(an empty line to quit):");
}
return 0;
}
void sig_int(int signo)
{
printf("interrupt\nNOW input a shell command(an empty line to quit):");
} |
|