My signal problem

This is a discussion on My signal problem within the Linux Programming forums, part of the Platform Specific Boards category; 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) ...

  1. #1
    Microsoft Lover afreedboy's Avatar
    Join Date
    Nov 2003
    Posts
    189

    Question My signal problem

    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

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Why are you jumping around so much? Can't you simplify, something like so:
    Code:
    #include <stdio.h>
    #include <signal.h>
    #include <unistd.h>
    
    static void sig_int(int signo);
    int Running = 10;
    
    int main(void)
    {
      signal(SIGINT,sig_int);
      
      while (Running)
      {
        printf ("%d more goes at hitting interupt\n", Running);
        sleep(2);
      }
      
      return 0;
    }  
      
    static void sig_int(int signo)
    {
      signal(SIGINT,sig_int);
      Running--;
    }
    
    /* Use CTRL+C to interupt */
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,659
    > gets(name);
    Ohhh - no points to you for not reading the FAQ
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with signals
    By kahad in forum C Programming
    Replies: 9
    Last Post: 12-07-2006, 09:42 AM
  2. Signal() problem
    By DarrenY in forum C Programming
    Replies: 8
    Last Post: 06-02-2006, 11:56 AM
  3. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 05:24 PM
  4. NAQ: Everything you never wanted to know about CPP
    By evildave in forum C Programming
    Replies: 21
    Last Post: 12-12-2005, 09:56 AM
  5. Replies: 5
    Last Post: 11-07-2005, 10:34 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21