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


 
Thread Tools Search this Thread
Top Forums Programming how can i make that a process child send a signal?
# 1  
Old 05-08-2011
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:

Code:
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <signal.h>
#include <unistd.h>
#include <time.h>


void sigalrm_handler(int);


static void sig_usr(int); 
int orden=0;
pid_t padre,hijo,pid,pidPadre,PIDSIG;

int main(int argc, char **argv) 
    {
    pid_t padre,hijo,pid,pidPadre;
    padre=getpid();
    pid=getpid();
    int i=0;
    FILE *arch;//Archivo a verificar.
    
    
    signal(SIGALRM,sigalrm_handler);
    
    
    for(i=0;i<10;i++)
        {
        if((hijo=fork())==0)
            {
            pidPadre=getppid();
            pid=getpid();
            orden=i+1;
            printf("\nNš%d Pid:%d PPid:%d\n",orden,pid,pidPadre);
            }
        else
            {
            PIDSIG=hijo;
            break;
            }
        }

    char cadena[100];
    for(; ;)
        {
        sprintf(cadena,"%d",orden);
        if((arch=fopen(cadena,"r"))==NULL)
            {
            printf("\nno existe\n");
            }
        else
            {
            pid=getpid();
            printf("\nNš%d Pid:%d Archivo Existente\n",orden,pid);
            sleep(1);

            }
  
        sleep(3);
            
            if ( pid == PIDSIG ) 
              alarm( 5 );
        }
    }

static void sig_usr(int signo) 
    {
    return;
    }


void sigalrm_handler(int senial)
{
    printf("\nNš%d Pid:%d Archivo Existente SEŅALIDADO X+1 \n",orden,pid);
              alarm( 5 );
}


Last edited by jim mcnamara; 05-08-2011 at 10:38 PM.. Reason: please use code tags
# 2  
Old 05-08-2011
you are forking loads of child processes - hijos - far more then you think. Your signals are also being created hundreds of times as well. This code cannot work as it is.

What are you trying to do? Please --Not how you think you want to do it, just what your tasks are.
# 3  
Old 05-13-2011
Sorry I didn't understand your requirement. Explain clearly....
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How make parent to wait from child process?

Hi all, I am starting mgen5 for sometime depends on input from a file, in a child process. now I want to make parent to wait in this child process till mgen5 finishes, or timeout happens. could anyone please tell me how to make parent to wait in child process in shell script? thanks... (2 Replies)
Discussion started by: girijajoshi
2 Replies

2. 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

3. 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

4. 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

5. 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

6. Shell Programming and Scripting

Make cron wait for the child process

I am trying to find a list of files and writing it to a text file. Based on the machine performance the file writing will be slow at certain time. The code to find file and redirecting the output to text file is on a shell script /usr/bin/find $SEARCH_DIR -daystart \( \( -name 'KI*' -a... (4 Replies)
Discussion started by: nuthalapati
4 Replies

7. UNIX for Dummies Questions & Answers

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... (4 Replies)
Discussion started by: vkn_1985
4 Replies

8. 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

9. 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

10. UNIX for Advanced & Expert Users

how to make a current running process ignore SIGHUP signal?

I ask this question since sometimes i run a time-consuming ftp in foreground and forget to use nohup ftp.sh & to put this work background and can still running after i log off. Assume this ftp task have run 1 hour, and still 1 hour time to end, i don't want to abort the ftp, first, i use ctrl+Z... (3 Replies)
Discussion started by: stevensxiao
3 Replies
Login or Register to Ask a Question