Thread: Problem with signals

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    17

    Problem with signals

    I'm having trouble with two programs that work with pipes, processes, and signals. The first program, prompt.c, takes the path of the executable of the second program, toupper.c, as its command-line argument. It then creates a child process that runs the second program. The parent process should prompt the user to enter a line of text, which is read and sent via a pipe to standard input of the child process. The parent process should then send a SIGUSR1 signal to the child process and pause until it receives a SIGUSR2 signal from the child, after which it resumes execution and prompts the user to enter the next line. The toupper.c program, run by the child process, should contain a loop, within which it should wait until it receives a SIGUSR1 signal from the parent process, then it should read a line of text from standard input, convert the line to uppercase, print the resulting line on the terminal, and send a SIGUSR2 signal back to the parent process to indicate that it has finished processing that line. If the user does not enter any line one minute after a prompt, the parent process should print a helpful message on the terminal and continue to wait for a line.

    Here's prompt.c

    Code:
    #include <unistd.h>
    #include <stdlib.h>
    #include <stdio.h>
    #include <signal.h>
    #include <sys/wait.h>
    
    
    #define Max_characters 1000
    
    
    static int alarm_off = 0;
    static int stat_val;
    
    
    void int_quit_handler(int signal)
    {
     printf("Goodbye.\n"); //Prints out Goodbye to screen.
     kill (getppid(), SIGTERM);
     wait(&stat_val);
    }
    
    
    void handler(int signal)
    {
    }
    
    
    void ding(int sig)
    {
     alarm_off = 1;
    }
    
    
    int main(int argc, char * argv[])
    {
    
    
     char *filepath; //Makes a char variable named filepath,
     char msg[Max_characters+2]; //Makes a char named msg.
     int pipedes[2]; //Makes variable for pipes.
     pid_t pid;
     int stat_val;
     pid_t child_pid;
     struct sigaction int_handler;
     struct sigaction quit_handler;
     struct sigaction usr2_handler;
    
    
     if (argc != 2)
     {
      printf("Not enough arguments. Next time, remember to enter the filepath of Toupper.c.\n"); //Prints error message if
      //user does not enter enough arguments.
      return (EXIT_FAILURE); //Exits on failure.
     }
    
    
     child_pid = wait(&stat_val);
     filepath = argv[1];
     int_handler.sa_handler = int_quit_handler;
     quit_handler.sa_handler = int_quit_handler;
     usr2_handler.sa_handler = handler;
     int_handler.sa_flags = 0;
     quit_handler.sa_flags = 0;
     usr2_handler.sa_flags = 0;
     sigemptyset(&int_handler.sa_mask);
     sigemptyset(&quit_handler.sa_mask);
     sigemptyset(&usr2_handler.sa_mask);
     sigaction(SIGINT, &int_handler, 0);
     sigaction(SIGQUIT, &quit_handler, 0);
     sigaction(SIGUSR2, &usr2_handler, 0);
    
    
     pipe(pipedes); //Makes pipes.
    
    
     switch(fork())
     {
     case -1:
        perror("Error with fork.\n"); //Prints out error message if the fork fails.
        return (EXIT_FAILURE); //Exits on failure.
     case 0:
        close(pipedes[1]);
        dup2(pipedes[0], 0);
        close(pipedes[0]);
        execl("Toupper", "Toupper", 0);
        kill(getppid(), SIGUSR2);
        exit(1);
     }
    
    
      printf("Please enter a line of text: "); //Asks user to enter a line of text.
      fgets(msg, sizeof(msg), stdin); //Reads in the line.
      write(pipedes[1], msg, strlen(msg)); //Writes line to the pipe.
      //sleep(60);
      //printf("Please enter a line of text as soon as possible.\n");
      //kill(getppid(), SIGALRM);
      //signal(SIGALRM, ding);
      //signal(SIGUSR2, handler);
      //signal(SIGINT, int_quit_handler);
      //signal(SIGQUIT, int_quit_handler);
      close(pipedes[1]);
      return (EXIT_SUCCESS); //Exits on success.
    }
    And here's Toupper.c

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <signal.h>
    #include <sys/wait.h>
    #include <ctype.h>
    #include <sys/wait.h>
    
    
    void handler(int signal)
    {
    }
    
    
    int main()
    {
    
    
     int ch; //Makes an int variable named ch.
     sigset_t mask1, mask2; //Makes sigset_t variables named mask1 and mask2.
     struct sigaction usr1_handler; //Makes a sigaction variable named usr1_handler.
    
    
     usr1_handler.sa_handler = handler;
     usr1_handler.sa_flags = 0;
     sigemptyset(&usr1_handler.sa_mask);
     sigaction(SIGUSR1, &usr1_handler, 0);
    
    
     sigemptyset(&mask1);
     sigaddset(&mask1, SIGINT);
     sigaddset(&mask1, SIGQUIT);
     sigprocmask(SIG_BLOCK, &mask1, 0);
    
    
     while((ch = getchar()) != EOF)
     {
      putchar(toupper(ch)); //Prints the standard input to the screen in all uppercase.
      fflush(stdin); //Flushes the standard input.
     }
    
    
     kill(getppid(), SIGUSR2);
     return(EXIT_SUCCESS); //Returns success.
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > execl("Toupper", "Toupper", 0);
    > kill(getppid(), SIGUSR2);
    If the execl succeeds, then the kill will never happen.

    > fflush(stdin);
    This isn't going to work on any OS which has signals.
    At least not if you're expecting the input stream to be flushed in some way.
    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.

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    17
    [QUOTE=Salem]> execl("Toupper", "Toupper", 0);
    > kill(getppid(), SIGUSR2);
    If the execl succeeds, then the kill will never happen.
    I thought that was needed to send the SIGUSR2 signal to Toupper.c

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Maybe so, but then you
    a) need to do it from the parent process, since after the execl(), you're running the toupper code, not that code.
    b) use the pid of the child you created, if you want to send a signal to it.

    > switch(fork())
    Like by doing
    pid = fork();
    switch ( pid )

    ...

    kill ( pid, SIGUSR2 );
    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
    Nov 2006
    Posts
    17
    Updating post with current code to reflect changes made.

    The thing I currently focusing on is the "If the user does not enter any line one minute after a prompt, the parent process should print a helpful message on the terminal and continue to wait for a line" part. I'm using SIGALRM to do this, but it's currently not working.

    Also, the compiler is giving me some weird message about "conflicting types for 'alarm'" for my alarm method.

    Prompt.c

    Code:
    #include <unistd.h>
    #include <stdlib.h>
    #include <stdio.h>
    #include <signal.h>
    #include <sys/wait.h>
    
    
    #define Max_characters 1000 //Defines the maximum number of characters.
    
    
    static int stat_val;
    
    //Method to handle SIGINT and SIGQUIT signals.
    void int_quit_handler(int signal)
    {
     printf("Goodbye.\n"); //Prints out Goodbye to screen.
     kill (getppid(), SIGTERM);
     wait(&stat_val);
    }
    
    
    //Method to handle SIGUSR2 signals.
    void handler(int signal)
    {
    }
    
    
    //Method to handle SIGALRM
    void alarm(int sig)
    {
     printf("Please enter a line of text as soon as possible.\n"); //Prints out a message reminding the user to enter a line.
    }
    
    
    int main(int argc, char * argv[])
    {
    
    
     char *filepath; //Makes a char variable named filepath,
     char msg[Max_characters+2]; //Makes a char named msg.
     int pipedes[2]; //Makes variable for pipes.
     pid_t pid;
     int stat_val;
     pid_t child_pid;
     struct sigaction int_handler;
     struct sigaction quit_handler;
     struct sigaction usr2_handler;
    
    
     if (argc != 2)
     {
      printf("Not enough arguments. Next time, remember to enter the filepath of Toupper.c.\n"); //Prints error message if
      //user does not enter enough arguments.
      return (EXIT_FAILURE); //Exits on failure.
     }
    
    
     child_pid = wait(&stat_val);
     filepath = argv[1];
     int_handler.sa_handler = int_quit_handler;
     quit_handler.sa_handler = int_quit_handler;
     usr2_handler.sa_handler = handler;
     int_handler.sa_flags = 0;
     quit_handler.sa_flags = 0;
     usr2_handler.sa_flags = 0;
     sigemptyset(&int_handler.sa_mask);
     sigemptyset(&quit_handler.sa_mask);
     sigemptyset(&usr2_handler.sa_mask);
     sigaction(SIGINT, &int_handler, 0);
     sigaction(SIGQUIT, &quit_handler, 0);
     sigaction(SIGUSR2, &usr2_handler, 0);
    
    
     pipe(pipedes); //Makes pipes.
     pid = fork();
    
    
     switch(pid)
     {
     case -1:
        perror("Error with fork.\n"); //Prints out error message if the fork fails.
        return (EXIT_FAILURE); //Exits on failure.
     case 0:
        close(pipedes[1]); //Closes pipe
        dup2(pipedes[0], 0); //Duplicates pipe descriptors
        close(pipedes[0]); //Closes pipe.
        execl("Toupper", "Toupper", 0);
        exit(1);
     }
    
    
      printf("Please enter a line of text: "); //Asks user to enter a line of text.
      alarm(60);
      fgets(msg, sizeof(msg), stdin); //Reads in the line.
      alarm(0);
      write(pipedes[1], msg, strlen(msg)); //Writes line to the pipe.
      kill(pid, SIGUSR2);
      raise(SIGALRM);
      signal(SIGALRM, alarm);
      close(pipedes[1]); //Closes pipe.
      return (EXIT_SUCCESS); //Exits on success.
    }
    Toupper.c

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <signal.h>
    #include <sys/wait.h>
    #include <ctype.h>
    #include <sys/wait.h>
    
    
    void handler(int signal)
    {
    }
    
    
    int main()
    {
     int ch; //Makes an int variable named ch.
     sigset_t mask1, mask2; //Makes sigset_t variables named mask1 and mask2.
     struct sigaction usr1_handler; //Makes a sigaction variable named usr1_handler.
    
    
     usr1_handler.sa_handler = handler;
     usr1_handler.sa_flags = 0;
     sigemptyset(&usr1_handler.sa_mask);
     sigaction(SIGUSR1, &usr1_handler, 0);
    
    
     sigemptyset(&mask1);
     sigaddset(&mask1, SIGINT);
     sigaddset(&mask1, SIGQUIT);
     sigprocmask(SIG_BLOCK, &mask1, &mask2);
    
    
     while((ch = getchar()) != EOF)
     {
      putchar(toupper(ch)); //Prints the standard input to the screen in all uppercase.
      fflush(stdin); //Flushes the standard input.
     }
    
    
     kill(getppid(), SIGUSR2);
     return(EXIT_SUCCESS); //Returns success.
    }

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > child_pid = wait(&stat_val);
    You haven't forked any children yet, what are you waiting for?

    > fflush(stdin);
    You're still doing this, you shouldn't.

    > void alarm(int sig)
    Alarm is also the name of a function in unistd.h, pick another name

    > kill (getppid(), SIGTERM);
    But this is in the parent right? So why are you sending to it's parent?

    > printf("Please enter a line of text as soon as possible.\n");
    You're very limited as to what functions you can call inside a signal handler.
    printf() isn't usually one of them.
    http://www.die.net/doc/linux/man/man2/signal.2.html
    http://www-128.ibm.com/developerwork...nxw03Reentrant

    You should be looking to do the minimum amount of work you can possibly get away with inside a signal handler. It's nowhere near like a regular function call.
    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.

  7. #7
    Registered User
    Join Date
    Nov 2006
    Posts
    17
    Quote Originally Posted by Salem
    >
    > kill (getppid(), SIGTERM);
    But this is in the parent right? So why are you sending to it's parent?
    That line is there to send the SIGTERM signal to the child to kill it. My logic is that if the code gets to that function, I have enter either Control - D, Control - C, or Control - /.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > That line is there to send the SIGTERM signal to the child to kill it
    But getppid() gets the parent process ID, not the child process ID (as returned by fork() )
    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.

  9. #9
    Registered User
    Join Date
    Nov 2006
    Posts
    17
    Now it;s not typing back the text in capital letters. It was doing that.

    Prompt.C

    Code:
    #include <unistd.h>
    #include <stdlib.h>
    #include <stdio.h>
    #include <signal.h>
    #include <sys/wait.h>
    
    
    //Typed by Chris Beech
    
    
    #define Max_characters 1000 //Defines the maximum number of characters.
    
    
    static int stat_val;
    pid_t pid;
    
    
    //Method to handle SIGINT and SIGQUIT signals.
    void int_quit_handler(int signal)
    {
     printf("Goodbye.\n"); //Prints out Goodbye to screen.
     kill (pid, SIGTERM); //Sends SIGTERM to child.
     wait(&stat_val); //Waits for child to terminate.
    }
    
    
    //Method to handle SIGUSR2 signals.
    void handler(int signal)
    {
    }
    
    
    //Method to handle SIGALRM
    void ding(int sig)
    {
     printf("Please enter a line of text as soon as possible.\n"); //Prints out a message reminding the user to enter a line.
     fflush(stdout); //Flushes standard output
     alarm(60);
    }
    
    
    int main(int argc, char * argv[])
    {
    
    
     char *filepath; //Makes a char variable named filepath,
     char msg[Max_characters+2]; //Makes a char named msg.
     int pipedes[2]; //Makes variable for pipes.
     pid_t pid;
     int stat_val;
     pid_t child_pid = wait(&stat_val)
     struct sigaction int_handler;  //Makes a sigaction variable named int_handler.
     struct sigaction quit_handler; //Makes a sigaction variable named quit_handler.
     struct sigaction usr2_handler; //Makes a sigaction variable named usr2_handler.
     struct sigaction alrm_handler; //Makes a sigaction variable named alrm_handler.
    
    
     if (argc != 2)
     {
      printf("Not enough arguments. Next time, remember to enter the filepath of Toupper.c.\n"); //Prints error message if
      //user does not enter enough arguments.
      return (EXIT_FAILURE); //Exits on failure.
     }
    
    
     filepath = argv[1]; //Gets the filepath of Toupper.c from the arguments.
    
    
     //SIGINT
     int_handler.sa_handler = int_quit_handler;
     int_handler.sa_flags = 0;
     sigemptyset(&int_handler.sa_mask);
     sigaction(SIGINT, &int_handler, 0);
    
    
     //SIGQUIT
     quit_handler.sa_handler = int_quit_handler;
     quit_handler.sa_flags = 0;
     sigemptyset(&quit_handler.sa_mask);
     sigaction(SIGQUIT, &quit_handler, 0);
     
    
     //SIGUSR2
     usr2_handler.sa_handler = handler;
     usr2_handler.sa_flags = 0;
     sigemptyset(&usr2_handler.sa_mask);
     sigaction(SIGUSR2, &usr2_handler, 0);
    
    
     //SIGALRM
     alrm_handler.sa_handler = ding;
     alrm_handler.sa_flags = SA_RESTART;
     sigemptyset(&alrm_handler.sa_mask);
     sigaction(SIGALRM, &alrm_handler, 0);
    
    
     pipe(pipedes); //Makes pipes.
     pid = fork();
    
    
     switch(pid)
     {
     case -1:
        perror("Error with fork.\n"); //Prints out error message if the fork fails.
        return (EXIT_FAILURE); //Exits on failure.
     case 0:
        close(pipedes[1]); //Closes pipe
        dup2(pipedes[0], 0); //Duplicates pipe descriptors
        close(pipedes[0]); //Closes pipe.
        execl("Toupper", "Toupper", 0);
        exit(1);
     }
    
    
      kill(pid, SIGUSR1); //Sends SIGUSR1 signal to child process.
      printf("Please enter a line of text: "); //Asks user to enter a line of text.
      fgets(msg, sizeof(msg), stdin); //Reads in the line.
      //alarm(0);
      write(pipedes[1], msg, strlen(msg)); //Writes line to the pipe.
      pause(); //Waits until the child sends SIGUSR2 signal.
      //raise(SIGALRM);
      //signal(SIGALRM, ding);
      close(pipedes[1]); //Closes pipe.
      return (EXIT_SUCCESS); //Exits on success.
    }
    Toupper.c

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <signal.h>
    #include <sys/wait.h>
    #include <ctype.h>
    #include <sys/wait.h>
    
    
    //Method to handler SIGUSR1 signals.
    void handler(int signal)
    {
    }
    
    
    int main()
    {
     int ch; //Makes an int variable named ch.
     sigset_t mask1, mask2; //Makes sigset_t variables named mask1 and mask2.
     struct sigaction usr1_handler; //Makes a sigaction variable named usr1_handler.
    
    
     //SIGUSR1
     usr1_handler.sa_handler = handler;
     usr1_handler.sa_flags = 0;
     sigemptyset(&usr1_handler.sa_mask);
     sigaction(SIGUSR1, &usr1_handler, 0);
    
    
    //Blocks SIGINT and SIGQUIT signals.
     sigemptyset(&mask1);
     sigaddset(&mask1, SIGINT);
     sigaddset(&mask1, SIGQUIT);
     sigprocmask(SIG_BLOCK, &mask1, &mask2);
    
    
     //pause(); //Waits for SIGUSR1 signal from parent.
    
    
     while((ch = getchar()) != EOF)
     {
      putchar(toupper(ch)); //Prints the standard input to the screen in all uppercase.
      kill(getppid(), SIGUSR2);//Sends SIGUSR2 signal to parent.
     }
    
    
     return(EXIT_SUCCESS); //Returns success.
    }
    Edit: It seems that it's the kill(pid, SIGUSR1) line that's causing the trouble.

    Also, having trouble with finding a way to make the program to keep accepting lines. Currently, it accepts one line, prints out in uppercase, then quits. I want it to ask for another line after it prints out the first one in uppercase. I thought about using a do while loop, but can't seem to make it work. Also trying to get the alarm thing to work. It should print out a message reminding the user to enter a line if a line in not entered within 60 seconds.
    Last edited by kahad; 12-07-2006 at 09:28 AM.

  10. #10
    Registered User
    Join Date
    Nov 2006
    Posts
    17
    Updating code to most recent version. I think the only porlbems I am having is that my alarm doesn't work and I can't seem to make it keep asking for a line after it prints out the first line.

    Promopt.c

    Code:
    #include <unistd.h>
    #include <stdlib.h>
    #include <stdio.h>
    #include <signal.h>
    #include <sys/wait.h>
    
    
    //Typed by Chris Beech.
    //This program will take the filepath of Toupper.c as an argument. It creates a child
    //process, which runs Toupper.c. It also creates a pipe between itself and Toupper.c.
    //It asks for the user to enter a line and then sends that line to Toupper.c via the pipe.
    
    
    #define Max_characters 1000 //Defines the maximum number of characters.
    
    
    static int stat_val; //Makes an static int variable named stat_val.
    pid_t pid; //Makes a pid_t variable named pid
    
    
    //Method to handle SIGINT and SIGQUIT signals.
    void int_quit_handler(int signal)
    {
     printf("Goodbye.\n"); //Prints out Goodbye to screen.
     kill (pid, SIGTERM); //Sends SIGTERM signal to child.
     wait(&stat_val); //Waits for child to terminate.
    }
    
    
    //Method to handle SIGUSR2 signals.
    void handler(int signal)
    {
    }
    
    
    //Method to handle SIGALRM
    void ding(int sig)
    {
     printf("Please enter a line of text as soon as possible.\n"); //Prints out a message 
     //reminding the user to enter a line.
     fflush(stdout); //Flushes standard output.
     alarm(60); //Waits 60 seconds.
    }
    
    
    int main(int argc, char * argv[])
    {
    
    
     char *filepath; //Makes a char variable named filepath,
     char msg[Max_characters+2]; //Makes a char named msg.
     int pipedes[2]; //Makes variable for pipes.
     pid_t pid; //Makes a pid_t variable named pid
     int stat_val; //Makes an int variable named stat_val.
     pid_t child_pid = wait(&stat_val); //Makes a pid_t variable named child_pid and sets it
               
                             //equal to wait.
     struct sigaction int_handler;  //Makes a sigaction variable named int_handler.
     struct sigaction quit_handler; //Makes a sigaction variable named quit_handler.
     struct sigaction usr2_handler; //Makes a sigaction variable named usr2_handler.
     struct sigaction alrm_handler; //Makes a sigaction variable named alrm_handler.
    
    
     if (argc != 2)
     {
      printf(stderr, "Not enough arguments. Please remember to enter the filepath of Toupper.c."
             + "\n"); 
      //Prints error message if user does not enter enough arguments.
      return (EXIT_FAILURE); //Exits on failure.
     }
    
    
     filepath = argv[1]; //Gets the filepath of Toupper.c from the arguments.
    
    
     //SIGINT
     int_handler.sa_handler = int_quit_handler;
     int_handler.sa_flags = 0;
     sigemptyset(&int_handler.sa_mask);
     sigaction(SIGINT, &int_handler, 0);
    
    
     //SIGQUIT
     quit_handler.sa_handler = int_quit_handler;
     quit_handler.sa_flags = 0;
     sigemptyset(&quit_handler.sa_mask);
     sigaction(SIGQUIT, &quit_handler, 0);
     
    
     //SIGUSR2
     usr2_handler.sa_handler = handler;
     usr2_handler.sa_flags = 0;
     sigemptyset(&usr2_handler.sa_mask);
     sigaction(SIGUSR2, &usr2_handler, 0);
    
    
     //SIGALRM
     alrm_handler.sa_handler = ding;
     alrm_handler.sa_flags = SA_RESTART;
     sigemptyset(&alrm_handler.sa_mask);
     sigaction(SIGALRM, &alrm_handler, 0);
    
    
     pipe(pipedes); //Makes pipes.
     pid = fork();  //Makes a fork.
    
    
     switch(pid)
     {
     case -1:
        printf("Error with fork.\n", stderr); //Prints out error message if the fork fails.
        return (EXIT_FAILURE); //Exits on failure.
     case 0:
        close(pipedes[1]); //Closes pipe
        dup2(pipedes[0], 0); //Duplicates pipe descriptors
        close(pipedes[0]); //Closes pipe.
        execl("Toupper", "Toupper", 0); //Runs to Toupper.c program.
        return(EXIT_SUCCESS); //Exits on success.
     }
    
    
     while(1)
     {
     printf("Please enter a line of text: "); //Asks user to enter a line of text.
     alarm(60); //Sets off alarm method if no line has been entered in 60 seconds.
     fgets(msg, sizeof(msg), stdin); //Reads in the line.
    
    
     if(FEOF(stdin) != 0)
     {
      break; //Breaks out of loop.
     }
    
    
     alarm(0); //Cancels the previous alarm request.
     write(pipedes[1], msg, strlen(msg)); //Writes line to the pipe.
     kill(pid, SIGUSR1); //Sends SIGUSR1 signal to child.
     pause(); //Waits until the child sends SIGUSR2 signal.
     }
    
    
     close(pipedes[1]); //Closes pipe.
     return (EXIT_SUCCESS); //Exits on success.
    }
    Toupper.c

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <signal.h>
    #include <sys/wait.h>
    #include <ctype.h>
    #include <sys/wait.h>
    
    
    //Typed by Chris Beech.
    //This program accepts standard input from Prompt.c via a pipe. It then converts the line
    //to uppercase and prints it out to the screen. It blocks the signals SIGINT and SIGQUIT.
    
    
    //Method to handler SIGUSR1 signals.
    void handler(int signal)
    {
    }
    
    
    int main()
    {
     int ch; //Makes an int variable named ch.
     sigset_t mask1, mask2; //Makes sigset_t variables named mask1 and mask2.
     struct sigaction usr1_handler; //Makes a sigaction variable named usr1_handler.
    
    
     //SIGUSR1
     usr1_handler.sa_handler = handler;
     usr1_handler.sa_flags = 0;
     sigemptyset(&usr1_handler.sa_mask);
     sigaction(SIGUSR1, &usr1_handler, 0);
    
    
     //Blocks SIGINT and SIGQUIT signals.
     sigemptyset(&mask1);
     sigaddset(&mask1, SIGINT);
     sigaddset(&mask1, SIGQUIT);
     sigprocmask(SIG_BLOCK, &mask1, &mask2);
    
    
     //pause(); //Waits till the parent sends the SIGUSR1 signal.
    
    
     while((ch = getchar()) != EOF)
     {
      putchar(toupper(ch)); //Prints the standard input to the screen in all uppercase.
     }
    
    
     kill(getppid(), SIGUSR2);//Sends SIGUSR2 signal to parent.
     return(EXIT_SUCCESS); //Returns success.
    }
    Last edited by kahad; 12-07-2006 at 10:48 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  2. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  3. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  4. problem with signals
    By cnchybrid in forum C Programming
    Replies: 8
    Last Post: 04-05-2007, 04:01 PM
  5. beginner problem
    By The_Nymph in forum C Programming
    Replies: 4
    Last Post: 03-05-2002, 05:46 PM