Sponsored Content
Top Forums UNIX for Dummies Questions & Answers Sending signal from child to parent process! Post 302368777 by vkn_1985 on Thursday 5th of November 2009 03:13:08 PM
Old 11-05-2009
Sending signal from child to parent process!

Hi All,
I facing a problem in handling signals between parent process communication. I am trying to send a signal(SIGINT) from child to parent. I am using kill function to do so and I am trying to read the signal using sigaction(). But the program is ending abruptly and I am not able to figure out why! Could anyone of you please help me?
The code is as follows :


Code:
#include<stdio.h>
#include<signal.h>
#include<unistd.h>
#include<stdlib.h>
void sig_usr(int signo){
if(signo == SIGINT)
printf("Signal caught!");
return;
}

int main(void){
pid_t pid, ppid;
ppid = getpid();
printf("%d ", ppid);
if((pid = fork()) == 0){ //Child
kill(ppid, SIGINT);
}
else{
printf("%d %d ",ppid, pid);
struct sigaction sig;
sigemptyset(&sig.sa_mask);
sig.sa_flags = 0;
sig.sa_handler = sig_usr;
if(sigaction(SIGINT,&sig,NULL) == 0)
printf("Signal processed OKay ");
sleep(10);
}
}

 

10 More Discussions You Might Find Interesting

1. Programming

parent and child process question?

Hi everybody, I'm trying to understand how a parent and child processes interact. This function( below) basically measures the fork time from the perspective of the parent only. what i would like to know is how to measure the time from the perspective of parent and child (ie: inserting... (0 Replies)
Discussion started by: tosa
0 Replies

2. UNIX for Advanced & Expert Users

catch SIGCHLD signal in parent process

I want to catch SIGCHLD signal in parent process. I can't use wait() system call to catch SIGCHLD according to project requirment. Operating system linux 3.1 can any one have a solution for this. Thanking you, ranjan (2 Replies)
Discussion started by: ranjan
2 Replies

3. Programming

catching a signal from child process

i am creating children processes using fork system call every child i create goes to sleep for random time. when child stops running how can i catch his signal and turminate the child (2 Replies)
Discussion started by: emil2006
2 Replies

4. Shell Programming and Scripting

How to make the parent process to wait for the child process

Hi All, I have two ksh script. 1st script calls the 2nd script and the second script calls an 'C' program. I want 1st script to wait until the 'C' program completes. I cant able to get the process id for the 'C' program (child process) to make the 1st script to wait for the second... (7 Replies)
Discussion started by: sennidurai
7 Replies

5. Shell Programming and Scripting

[KSH/Bash] Starting a parent process from a child process?

Hey all, I need to launch a script from within 2 other scripts that can run independently of the two parent scripts... Im having a hard time doing this, if anyone knows how please let me know. More detail. ScriptA (bash), ScriptB (ksh), ScriptC (bash) ScriptA, launches ScriptB ScirptB,... (7 Replies)
Discussion started by: trey85stang
7 Replies

6. Programming

how can i make that a process child send a signal?

I'm trying to do a program that makes activate an signal (SINGALARM) when the next child of a son appears but this not works. I have to caught the next child o the other (pid), to send a singnal which inform a menssage. It's anything worng in the code? thanks. the code: #include... (2 Replies)
Discussion started by: marmaster
2 Replies

7. Programming

Parent,child wait,signal

Hello. I want to make a child do some stuff,wait,then the parent does some stuff and then child does some stuff and waits again.I have made the following but it does not work.Can anybody help me? pid1 = fork(); if (pid1 == -1) { perror("Can't create child\n"); ... (18 Replies)
Discussion started by: Cuervo
18 Replies

8. Programming

Parent process starts before the child using signal, in C

Hi, i want that the parent process start before the child, this code doesn't work, if the child start before the parent it wait for signal, then the father send the signal SIGALRM and the child catch it and call printf; else the father call printf and send the signal to the child that call its... (1 Reply)
Discussion started by: blob84
1 Replies

9. Emergency UNIX and Linux Support

signal between parent process and child process

Hello, everyone. Here's a program: pid_t pid = fork(); if (0 == pid) // child process { execvp ...; } I send a signal (such as SIGINT) to the parent process, the child process receive the signal as well as the parent process. However I don't want to child process to receive the... (7 Replies)
Discussion started by: jackliang
7 Replies

10. Shell Programming and Scripting

forking a child process and kill its parent to show that child process has init() as its parent

Hi everyone i am very new to linux , working on bash shell. I am trying to solve the given problem 1. Create a process and then create children using fork 2. Check the Status of the application for successful running. 3. Kill all the process(threads) except parent and first child... (2 Replies)
Discussion started by: vizz_k
2 Replies
sigwait(2)							   System Calls 							sigwait(2)

NAME
sigwait - wait until a signal is posted SYNOPSIS
#include <signal.h> int sigwait(sigset_t *set); Standard conforming cc [ flag ... ] file ... -D_POSIX_PTHREAD_SEMANTICS [ library...] #include <signal.h> int sigwait(const sigset_t *set, int *sig); DESCRIPTION
The sigwait() function selects a signal in set that is pending on the calling thread (see thr_create(3C) and pthread_create(3C).) If no signal in set is pending, sigwait() blocks until a signal in set becomes pending. The selected signal is cleared from the set of signals pending on the calling thread and the number of the signal is returned, or in the standard-conforming version (see standards(5)) placed in sig. The selection of a signal in set is independent of the signal mask of the calling thread. This means a thread can synchronously wait for signals that are being blocked by the signal mask of the calling thread . To ensure that only the caller receives the signals defined in set, all threads should have signals in set masked including the calling thread. If the set argument points to an invalid address, the behavior is undefined and errno may be set to EFAULT. If sigwait() is called on an ignored signal, then the occurrence of the signal will be ignored, unless sigaction() changes the disposition. If more than one thread waits for the same signal, only one is unblocked when the signal arrives. RETURN VALUES
Upon successful completion, the default version of sigwait() returns a signal number; the standard-conforming version returns 0 and stores the received signal number at the location pointed to by sig. Otherwise, -1 is returned and errno is set to indicate an error. ERRORS
The sigwait() function will fail if: EINTR The wait was interrupted by an unblocked, caught signal. EINVAL The set argument contains an unsupported signal number. The sigwait() function may fail if: EFAULT The set argument points to an invalid address. EXAMPLES
Example 1 Creating a thread to handle receipt of a signal The following sample C code creates a thread to handle the receipt of a signal. More specifically, it catches the asynchronously generated signal, SIGINT. /******************************************************************** * * compile with -D_POSIX_PTHREAD_SEMANTICS switch; * required by sigwait() * * sigint thread handles delivery of signal. uses sigwait() to wait * for SIGINT signal. * ********************************************************************/ #include <pthread.h> #include <stdlib.h> #include <stdio.h> #include <string.h> #include <unistd.h> #include <signal.h> #include <synch.h> static void *threadTwo(void *); static void *threadThree(void *); static void *sigint(void *); sigset_t signalSet; void * main(void) { pthread_t t; pthread_t t2; pthread_t t3; sigfillset ( &signalSet ); /* * Block signals in initial thread. New threads will * inherit this signal mask. */ pthread_sigmask ( SIG_BLOCK, &signalSet, NULL ); printf("Creating threads "); pthread_create(&t, NULL, sigint, NULL); pthread_create(&t2, NULL, threadTwo, NULL); pthread_create(&t3, NULL, threadThree, NULL); printf("################## "); printf("press CTRL-C to deliver SIGINT to sigint thread "); printf("################## "); pthread_exit((void *)0); } static void * threadTwo(void *arg) { printf("hello world, from threadTwo [tid: %d] ", pthread_self()); printf("threadTwo [tid: %d] is now complete and exiting ", pthread_self()); pthread_exit((void *)0); } static void * threadThree(void *arg) { printf("hello world, from threadThree [tid: %d] ", pthread_self()); printf("threadThree [tid: %d] is now complete and exiting ", pthread_self()); pthread_exit((void *)0); } void * sigint(void *arg) { int sig; int err; printf("thread sigint [tid: %d] awaiting SIGINT ", pthread_self()); /* /* use standard-conforming sigwait() -- 2 args: signal set, signum */ err = sigwait ( &signalSet, &sig ); /* test for SIGINT; could catch other signals */ if (err || sig != SIGINT) abort(); printf(" SIGINT signal %d caught by sigint thread [tid: %d] ", sig, pthread_self()); pthread_exit((void *)0); } ATTRIBUTES
See attributes(5) for descriptions of the following attributes: +-----------------------------+-----------------------------+ | ATTRIBUTE TYPE | ATTRIBUTE VALUE | +-----------------------------+-----------------------------+ |Interface Stability |Standard | +-----------------------------+-----------------------------+ |MT-Level |Async-Signal-Safe | +-----------------------------+-----------------------------+ SEE ALSO
sigaction(2), signal.h(3HEAD), sigpending(2), sigprocmask(2), sigsuspend(2), pthread_create(3C), pthread_sigmask(3C), thr_create(3C), thr_sigsetmask(3C), attributes(5), standards(5) NOTES
The sigwait() function cannot be used to wait for signals that cannot be caught (see sigaction(2)). This restriction is silently imposed by the system. Solaris 2.4 and earlier releases provided a sigwait() facility as specified in POSIX.1c Draft 6. The final POSIX.1c standard changed the interface as described above. Support for the Draft 6 interface is provided for compatibility only and may not be supported in future releases. New applications and libraries should use the standard-conforming interface. SunOS 5.11 24 Jun 2002 sigwait(2)
All times are GMT -4. The time now is 07:30 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy