Sending Signal from Child Process to Parent Process : Not getting desired output
Linux - GeneralThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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);
}
}
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/
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.