Code:
#include<signal.h>
#include<unistd.h>
#include<stdio.h>
#include<termios.h>
#include<unistd.h>
#include<setjmp.h>
#include<sys/types.h>
#include<sys/wait.h>
#include<string.h>
#include<sys/times.h>
jmp_buf tml,tm2;
static void sig_int(int signo);
int main (void)
{
//signal(SIGINT,sig_int);
struct termios term, old_term;
char c;
char name[50];
pid_t pid;
struct tms start,end;
clock_t c_st,c_ed;
int jj=0;
static long clktck=0;
if(clktck==0)
if((clktck=sysconf(_SC_CLK_TCK))<0)
printf("sysconf error\n");
//signal(SIGINT,sig_int);
if((c_st=times(&start))==-1)printf("times start error\n");
pid=vfork();
signal(SIGINT,sig_int);
if(pid==0)
{
signal(SIGINT,sig_int);
tcgetattr(STDIN_FILENO, &term);
old_term=term;
term.c_lflag &=~ICANON;
term.c_cc[VMIN]=1;
term.c_cc[VTIME]=0;
term.c_cc[VINTR]=5;
tcsetattr(STDIN_FILENO,TCSANOW,&term);
setjmp(tml);
printf("command # ");
signal(SIGINT,sig_int);
int ii=alarm(6);
printf("remain alarm %d\n",ii);
gets(name);c=' ';
if(strcmp(name,"exit")!=0)
{
jj++;
if(system(name)==-1)
printf("command error\n");
longjmp(tml,1);
}
setjmp(tm2);
printf("Do you really want to exit?(y/n)\n");
while((c&=255)!='y' && (c&=255)!='n')
{
read(STDIN_FILENO,&c,1);
if(c=='y')
{jj++;
exit(0);
}
if(c=='n')longjmp(tml,1);
}
tcsetattr(STDIN_FILENO,TCSANOW,&old_term);
}
else
{
wait(NULL);
printf("world");
if((c_ed=times(&end))==-1)printf("times end error\n");
printf("real : %7.2f\n", (c_ed-c_st)/(double)clktck);
printf("user: %7.2f\n", end.tms_utime-start.tms_utime);
printf("Finish parent\n");
printf("total commands %d\n",jj);
}
}
static void sig_int(int signo)
{
printf("hello");
longjmp(tm2,1);
}
In that program, I want to do whenever I press Ctrl+E, I want that program to print hello and ask do you really want to exit? But when I run that program and press Ctrl+E it exit instantaneously. I try to change the position of
Code:
signal ( SIGINT, sig_int);
to so many places : before fork, after fork, etc..
But still don't work.
Anyone could help me?
Thanks