Thread: Signal Handling

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    54

    Signal Handling

    This is a program which is exec by a execvp function, its purpose is to read anything that is in the "pipe" which is created in the primary program. Before the exec, I have "written" something that goes into the pipe. So it will write the information of the pipe into the outgoing file.

    The signal handling code is suppose to catch a signal that is sent out by another 3rd program which is exec by the primary program. The 3rd program is reading a source file and writting it into the pipe.

    So this code is suppose to read the pipe which contains new information and into the outgoing file again.

    Question, does the pause() really work? Can one background process be put to sleep while waiting for the signal from the 3rd program? When I run the program, it seems to stop at the while loop and not write into the outgoing file.

    Code:
    #include <sys/types.h>
    #include <sys/wait.h>
    #include <sys/stat.h>
    #include <unistd.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <fcntl.h>
    #include <stdio.h>
    #include <errno.h>
    #include <signal.h>
    
    #define BUF_SIZE 255
    #define OUTPUT_MODE 0700
    
    typedef void Sigfunc(int);
    
    Sigfunc *Signal(int signum, Sigfunc *signalHandler){
       
       struct sigaction newAction, oldAction;
       newAction.sa_handler = signalHandler;
       sigemptyset(&newAction.sa_mask);
       newAction.sa_flags = 0;
    
       if (sigaction(signum, &newAction, &oldAction) < 0){
          return(SIG_ERR);
       }
       else{
          return(oldAction.sa_handler);
       }
    }
    
    
    static void mySIGUSR1Handler(int);
    
    int main(int argc, char *argv[]){
    
      int out_fd;
      struct stat buf;
      char c;
      char buffer[BUF_SIZE+1];
      int counter;
      
      fprintf(stderr, "What from writer : %s\n", argv[0]);
    
      if(argc != 1){
         perror("Insufficient files to write from main\n");
      }
    
      if(Signal(SIGUSR1, SIG_IGN) != SIG_IGN){
        if(Signal(SIGUSR1, mySIGUSR1Handler) != SIG_IGN){
           perror("ERROR! Signal handler not established!\n");
           exit(EXIT_FAILURE);
        }
      }
    
      out_fd = creat(argv[0], OUTPUT_MODE);
      if(out_fd < 0){
        perror("Unable to create output file\n");
      }
      else{
        fprintf(stderr, "Opened my outgoing file\n");
      }
      
      if(lstat(argv[0], &buf) < 0){
        fprintf(stderr,"lstat error");
        exit(EXIT_FAILURE);
      }
    
      counter = 0;
      while((c=getchar())){
         fprintf(stderr,"%c",c);
         buffer[counter] = c; 
         counter++;
      }
      /**Program stops here***/
      
      write(out_fd, buffer, counter); 
      pause();
    
    
      return EXIT_SUCCESS;
    }
    
    static void mySIGUSR1Handler(int signum){
    
      Signal(SIGUSR1, mySIGUSR1Handler);
    
      fprintf(stderr, "Received to write from SIGUSR1\n");
    }

  2. #2
    Registered User
    Join Date
    Mar 2005
    Posts
    54
    My handler seems to be doing something else. I'll correct it now.

  3. #3
    Registered User
    Join Date
    Mar 2005
    Posts
    54
    Code:
    #include <sys/types.h>
    #include <sys/wait.h>
    #include <sys/stat.h>
    #include <unistd.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <fcntl.h>
    #include <stdio.h>
    #include <errno.h>
    #include <signal.h>
    
    #define BUF_SIZE 255
    #define OUTPUT_MODE 0700
    
    static void mySIGUSR1Handler(int);
    
    int main(int argc, char *argv[]){
    
      int out_fd;
      struct stat buf;
      char c;
      char buffer[BUF_SIZE+1];
      int counter;
    
      if(argc != 1){
         perror("Insufficient files to write from main\n");
      }
    
      signal(SIGUSR1, mySIGUSR1Handler);
    
      out_fd = creat(argv[0], OUTPUT_MODE);
      if(out_fd < 0){
        perror("Unable to create output file\n");
        exit(EXIT_FAILURE);
      }
      else{
        fprintf(stderr, "Opened my outgoing file\n");
      }
      
      if(lstat(argv[0], &buf) < 0){
        fprintf(stderr,"lstat error");
        exit(EXIT_FAILURE);
      }
    
      pause();
    
      counter = 0;
      while((c=getchar())){
         fprintf(stderr,"%c",c);
         buffer[counter] = c; 
         counter++;
      }
      /**Program stops here***/
      
      write(out_fd, buffer, counter); 
    
      return EXIT_SUCCESS;
    }
    
    static void mySIGUSR1Handler(int signum){
    
      fprintf(stderr, "Received to write from SIGUSR1\n");
      return;
    }
    it seems that the program wants a keyboard input to terminate, but it still doesn't write anything to the outgoing file. I've changed my signal handler to just wake up the sleeping process.

    Is there any explanation of my problem?

    *EDIT
    The problem should be because of the pipe I created in the primary program and I should close it after reading the input through. However, since this program doesn't recognize the pipe but only through "STDOUT_FILENO", how do I make it stop reading?
    Last edited by DarrenY; 06-04-2006 at 10:31 AM.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > while((c=getchar()))
    What's this supposed to do?
    If getchar() returns EOF, that's still true as far as the while loop is concerned.

    Also, c should be an int.

    while ( count < BUF_SIZE && (c=getchar()) != EOF )
    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.

  5. #5
    Registered User
    Join Date
    Mar 2005
    Posts
    54
    True, my mistake, I don't know what made me write that in the first place. Prolly while I was implementing one feature at a time till I assumed that its working. I've fixed it and it seems that I'm able to solve my problem along with other changes in the codes as well.

    Thanks alot for your attention and your great help!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. signal handling and exception handling
    By lehe in forum C++ Programming
    Replies: 2
    Last Post: 06-15-2009, 10:01 PM
  2. Signal and exception handling
    By nts in forum C++ Programming
    Replies: 23
    Last Post: 11-15-2007, 02:36 PM
  3. Signal Handling - Are they part of ANSI C?
    By Stanley S in forum C Programming
    Replies: 3
    Last Post: 12-21-2005, 07:49 AM
  4. signal handling
    By trekker in forum C Programming
    Replies: 2
    Last Post: 07-05-2002, 02:52 AM
  5. error signal handling
    By bizzu in forum C Programming
    Replies: 5
    Last Post: 04-29-2002, 03:12 PM