我在使用fork创建的子项下使用execp
我为其设置了新的进程组并使用tcsetpgrp
设置了前台进程组
以下是main
int main()
{
signal(SIGINT,SIG_IGN);
signal(SIGTSTP,SIG_IGN);
pid_t pid=fork();
if(pid==-1)
{
perror("Error in forking");
}
else if(pid == 0)
{
signal(SIGTTOU, SIG_IGN);
setpgid(getpid(),getpid());
//check denotes if process needs to be run in background or not
if(!check)
{
tcsetpgrp(STDOUT_FILENO,getpgid(getpid()));
tcsetpgrp(STDIN_FILENO,getpgid(getpid()));
}
signal(SIGTSTP, tstphandler);
signal(SIGINT,inthandler);
if(strlen(token)>0)
{
execer=execlp(token,token,args,(char*)NULL);
if (execer==-1)
{
printf("Error in execution);
}
exit(0);
}
}
else
{
//processes is a data structure which contains pid of
//processes and current status :- 0 = running in background
//, 1= dead , 2 = stopped in background , -1= in foreground.
if(check)
{
processes[pid_counter].pid=pid;
processes[pid_counter].dead=0;
}
if(!check && strlen(token)>0)
{
processes[pid_counter].pid=pid;
processes[pid_counter].dead=-1;
waitpid(pid,&status,0);
tcsetpgrp(STDOUT_FILENO,globalpgid);
tcsetpgrp(STDIN_FILENO,globalpgid);
processes[pid_counter].dead=1;
}
}
}
这些是我的处理程序
//SIGINT handler
void inthandler(int sig)
{
printf("int handler\n");
int i;
for(i=0;i<=pid_counter;i++)
{
if(processes[i].dead==-1)
{
kill(processes[i].pid,SIGKILL);
break;
}
}
}
//SIGTSTP handler
void tstphandler(int sig)
{
int i;
for(i=0;i<=pid_counter;i++)
{
if(processes[i].dead==-1)
{
processes[i].dead=2;
kill(processes[i].pid, SIGSTOP);
break;
}
}
tcsetpgrp(STDOUT_FILENO,globalpgid);
tcsetpgrp(STDIN_FILENO,globalpgid);
}
我的问题是没有从子进程调用这些信号处理程序。使用默认操作调用它们,但不使用我声明的处理程序。