Using signals between unrelated processes (C/Linux)

This is a discussion on Using signals between unrelated processes (C/Linux) within the C Programming forums, part of the General Programming Boards category; Hi, I wasn't sure where to post this, here or in the Linux section, but I guess it's more related ...

  1. #1
    Registered User
    Join Date
    Jun 2009
    Posts
    3

    Using signals between unrelated processes (C/Linux)

    Hi, I wasn't sure where to post this, here or in the Linux section, but I guess it's more related to C

    I'm having quite a headache trying to communicate two processes which have no relation (one is not the child of the other). I use SIGUSR1 for this purpose. i just want to send one signal from process 1 to process 2 so that this last process prints something in the console. Instead, it prints "User defined signal 1" and exits. I know this is the default behaviour but i'm installing a signal handler which supposedly just prints something. Here is part of the code:

    This is the signal handler function:

    Code:
    void signalHandler (int sig) {
    printf ("PRINT\n");
    }
    This is where I install the signal handler (I do this in main):

    Code:
    signal (SIGUSR1, signalHandler);
    And now this is where I try to send a signal to the other process (id is that process id):
    Code:
    kill (id, SIGUSR1);
    All of this is written in the process which sends the signal. The other process has nothing related to signals. Of this I am doubting, perhaps it should have something. Anyway, my objective is to have the program which receives the signal print "PRINT" as shown in signalHandler.

    Thanks

  2. #2
    Making mistakes
    Join Date
    Dec 2008
    Posts
    476
    You should use sigaction instead of signal. Signal can vary across platforms.

  3. #3
    cas
    cas is offline
    Registered User
    Join Date
    Sep 2007
    Posts
    983
    You're calling signal() and kill() from the same program? It sounds like it, which would be the source of your problem. The program that's being signalled (ie, the one whose pid you're passing to kill()) needs to install a signal handler.

    You can't control another process's reaction to signals; only that process itself may do so. Install a handler in one process, kill from the other.

    And yes, sigaction() is better than signal().

  4. #4
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by cas View Post
    You can't control another process's reaction to signals; only that process itself may do so. Install a handler in one process, kill from the other.

    Well... unless you ptrace it... But I agree it's unlikely he'll want to do that.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 34
    Last Post: 05-27-2009, 12:26 PM
  2. Processes not dying
    By Elkvis in forum Linux Programming
    Replies: 12
    Last Post: 04-23-2008, 08:59 AM
  3. Stopping Processes Question
    By brett in forum Linux Programming
    Replies: 3
    Last Post: 06-24-2007, 10:15 PM
  4. binary tree of processes
    By gregulator in forum C Programming
    Replies: 1
    Last Post: 02-27-2005, 11:59 PM
  5. Computer Processes.... Which can be stopped?
    By Sevrin in forum Tech Board
    Replies: 3
    Last Post: 06-08-2003, 08:13 PM

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