simple thread signaling question

This is a discussion on simple thread signaling question within the C++ Programming forums, part of the General Programming Boards category; Just messing around with signals and i have a question... in my code i have kill(getppid(),SIGUSR2); 3 times but it ...

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    3

    simple thread signaling question

    Just messing around with signals and i have a question...
    in my code i have kill(getppid(),SIGUSR2); 3 times but it only gets called once. Why and how can it get called more than once?
    thanks

    #include <iostream>
    #include <signal.h>
    #include <sys/types.h>
    #include <signal.h>

    using namespace std;

    static int alarm_fired = 0;
    void ding (int sig)
    {
    alarm_fired = 1;

    }

    void you (int sig)
    {
    cout<<"second signal"<<endl;

    }

    int main(int argc, char *argv[])
    {
    pid_t pid;

    cout<<"Alarm Application"<<endl;
    signal (SIGUSR1,ding);
    signal (SIGUSR2, you);

    pid = fork();

    switch(pid) {
    case -1:
    cout<<"error"<<endl;
    case 0:
    sleep(5);
    kill(getppid(),SIGUSR1);
    kill(getppid(),SIGUSR2);
    kill(getppid(),SIGUSR2);
    kill(getppid(),SIGUSR2);
    exit (0);
    }

    cout<<"waiting for alarm"<<endl;


    pause();
    if(alarm_fired) cout<<"ding"<<endl;

    cout<<"done"<<endl;


    return 0;
    }

  2. #2
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,810
    code tags pls and might want to break out of your switch statement
    Code:
     
    switch(pid) {
    case -1:
    cout<<"error"<<endl;
    break;//need this so one case doesn't go though another
    case 0:
    sleep(5);
    kill(getppid(),SIGUSR1);
    for(int i = 0, i<4,i++)//try this too
    {
    kill(getppid(),SIGUSR2);
    }
    exit (0);
    }
    Woop?

  3. #3
    Registered User
    Join Date
    Jul 2004
    Posts
    3
    What is diffrent about what you wrote than mine other than you call your kill 4 times instead of the 3?

  4. #4
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,810
    Your working with signals but you don't know hoy do break down a simple for loop.
    Code:
    for(int i=0,i<4,i++)
    {
    //i is less than 4 eg 3 cause it is an integer
    }
    Woop?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple Question!!
    By gameuser in forum C++ Programming
    Replies: 2
    Last Post: 06-06-2009, 06:42 PM
  2. Simple question about the C programming tutorial #1
    By elsheepo in forum C Programming
    Replies: 13
    Last Post: 01-19-2009, 08:59 PM
  3. Problem : Threads WILL NOT DIE!!
    By hanhao in forum C++ Programming
    Replies: 2
    Last Post: 04-16-2004, 02:37 PM
  4. simple fgets question
    By theweirdo in forum C Programming
    Replies: 7
    Last Post: 01-27-2002, 06:58 PM
  5. simple input and string manipulation question
    By Stig in forum C Programming
    Replies: 1
    Last Post: 12-15-2001, 01:33 PM

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