LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 09-08-2010, 07:36 AM   #1
thelink123
Member
 
Registered: Aug 2008
Location: India, Kerala
Distribution: openSUSE 11
Posts: 46

Rep: Reputation: 15
Thumbs up 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
 
Old 09-08-2010, 01:02 PM   #2
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,110

Rep: Reputation: 216Reputation: 216Reputation: 216
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);


   }

}
 
Old 09-08-2010, 11:17 PM   #3
thelink123
Member
 
Registered: Aug 2008
Location: India, Kerala
Distribution: openSUSE 11
Posts: 46

Original Poster
Rep: Reputation: 15
Thumbs up

Quote:
Originally Posted by Guttorm View Post
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/
 
Old 09-09-2010, 01:28 AM   #4
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,110

Rep: Reputation: 216Reputation: 216Reputation: 216
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
 
Old 10-26-2012, 09:05 PM   #5
slacktroll
Member
 
Registered: May 2011
Distribution: Slackware 14.0 on Thinkpad X60
Posts: 56

Rep: Reputation: 3
......................
...........
case 0;
mesasageText="fork";

works fine here!
 
  


Reply

Tags
fork, linux, signal


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how a father process know which child process send the signal SIGCHLD icoming Programming 10 07-20-2010 07:26 AM
child process usses same amount of ram as parent process socialjazz Programming 7 10-19-2006 05:48 PM
Sending signal to child process DiAvOl Programming 6 05-16-2006 12:16 PM
Bash Scripting - child process affecting parent process mthaddon Linux - General 1 05-02-2004 01:19 PM
about parent and child process winwar Solaris / OpenSolaris 3 07-23-2003 06:07 AM


All times are GMT -5. The time now is 03:59 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration