LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (http://www.linuxquestions.org/questions/linux-general-1/)
-   -   Sending Signal from Child Process to Parent Process : Not getting desired output (http://www.linuxquestions.org/questions/linux-general-1/sending-signal-from-child-process-to-parent-process-not-getting-desired-output-830971/)

thelink123 09-08-2010 07:36 AM

Sending Signal from Child Process to Parent Process : Not getting desired output
 
Hi LQ,

Please see the code below.

Code:

#include <stdio.h>
#include <unistd.h>
#include <signal.h>
#include <sys/types.h>


typedef struct
{
  int cpid;
  char *s;
}message;

void sig_action_function(int sig, siginfo_t *info, void *ptr)
{
  union sigval value = info->si_value;

  message *msg = (message *)(value.sival_ptr);
  printf("%d\n",msg->cpid);
  //printf("%d\n",value.sival_int);
 
}

int main()
{

  struct sigaction act;
  pid_t pid;
  message msg;
  union sigval value;


  act.sa_sigaction = sig_action_function;
  act.sa_flags = SA_SIGINFO;
  act.sa_mask  = 0;

  sigaction(SIGALRM, &act, 0);

  pid = fork();

  switch(pid)
  {

      case -1:
        printf("Fork Failed\n");
        break;

      case 0:

        msg.cpid = getpid();
        msg.s = "I am ur child with pid = ";

        value.sival_ptr = &msg;
        //value.sival_int = 43;

        sleep(5);
        sigqueue(getppid(), SIGALRM, value);
        exit(0);

      default:
        pause();
        exit(0);


  }

}


Description of what the code does or what i intended to do:

1. Created a child process from parent process using 'fork()'

2. Sent a signal 'SIGALRM' from child process to parent process using 'sigqueue' function.

(The Third parameter of 'siqueue' function contains the message (message msg) which the child process wants to send to the parent process.'msg' is a stucture instance containing a) pid of child and b) string)

5. Print the 'msg' sent by child process inside the signal handler function 'sig_action_function' of the parent process

My Problem
----------

I am getting some junk value when this line is executed

Code:

printf("%d\n",msg->cpid);
. I expected to get the pid of child process, which the child process sent to parent process through the signal.

Kindly help me out on this and tell me the cause of the problem.

Thanks,
TheLink

Guttorm 09-08-2010 01:02 PM

Hi

Your child process sends a pointer to a message struct to the parent. But you set up that struct after doing the fork. Since processes don't share any data, the parent gets a pointer to the message struct, but it's not initialized.

I modified your program a little:

Code:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <sys/types.h>

void sig_action_function(int sig, siginfo_t *info, void *ptr)
{
  union sigval value = info->si_value;
  printf("Got a signal from %d. The message was: %s\n", info->si_pid, (char*) value.sival_ptr);
}

int main()
{

  struct sigaction act;
  pid_t pid;
  union sigval value;
  char *messageText = "Hello parent";

  memset (&act, '\0', sizeof(act));
  act.sa_sigaction = sig_action_function;
  act.sa_flags = SA_SIGINFO;

  sigaction(SIGALRM, &act, 0);

  pid = fork();

  switch(pid)
  {

      case -1:
        printf("Fork Failed\n");
        break;

      case 0:
        value.sival_ptr = messageText;
        sleep(5);
        sigqueue(getppid(), SIGALRM, value);
        exit(0);

      default:
        pause();
        exit(0);


  }

}


thelink123 09-08-2010 11:17 PM

Quote:

Originally Posted by Guttorm (Post 4091647)
Hi

Your child process sends a pointer to a message struct to the parent. But you set up that struct after doing the fork. Since processes don't share any data, the parent gets a pointer to the message struct, but it's not initialized.

I modified your program a little:

Code:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <sys/types.h>

void sig_action_function(int sig, siginfo_t *info, void *ptr)
{
  union sigval value = info->si_value;
  printf("Got a signal from %d. The message was: %s\n", info->si_pid, (char*) value.sival_ptr);
}

int main()
{

  struct sigaction act;
  pid_t pid;
  union sigval value;
  char *messageText = "Hello parent";

  memset (&act, '\0', sizeof(act));
  act.sa_sigaction = sig_action_function;
  act.sa_flags = SA_SIGINFO;

  sigaction(SIGALRM, &act, 0);

  pid = fork();

  switch(pid)
  {

      case -1:
        printf("Fork Failed\n");
        break;

      case 0:
        value.sival_ptr = messageText;
        sleep(5);
        sigqueue(getppid(), SIGALRM, value);
        exit(0);

      default:
        pause();
        exit(0);


  }

}


Thanks for the quick reply.

The child process has created a message and it sends the pointer to the message to the parent using "sigqueue". So the parent should be able to receive the pointer to the message. Then the parent should be able to get the content of the message as well i think.

If we are creating the message before the fork, the message gets duplicated in both the processes. So the child do not have to send that message explicitly to the parent, because parent will be able to access the message before that.

What i intented was to create a message in child and send that message to parent through signal/

Guttorm 09-09-2010 01:28 AM

Hi

I don't think you can send more than a pointer or an int with a signal. But there are lots of other ways processes can communicate:

http://tldp.org/LDP/lpg/node7.html

slacktroll 10-26-2012 09:05 PM

......................
...........
case 0;
mesasageText="fork";

works fine here!


All times are GMT -5. The time now is 01:14 PM.