Sponsored Content
Top Forums Programming how can i make that a process child send a signal? Post 302520626 by marmaster on Sunday 8th of May 2011 07:42:58 PM
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
 

10 More Discussions You Might Find Interesting

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

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

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

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

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

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

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

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

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

10. 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
getpid(2)							   System Calls 							 getpid(2)

NAME
getpid, getpgrp, getppid, getpgid - get process, process group, and parent process IDs SYNOPSIS
#include <unistd.h> pid_t getpid(void); pid_t getpgrp(void); pid_t getppid(void); pid_t getpgid(pid_t pid); DESCRIPTION
The getpid() function returns the process ID of the calling process. The getpgrp() function returns the process group ID of the calling process. The getppid() function returns the parent process ID of the calling process. The getpgid() function returns the process group ID of the process whose process ID is equal to pid, or the process group ID of the calling process, if pid is equal to 0. RETURN VALUES
Upon successful completion, these functions return the process group ID. Otherwise, getpgid() returns (pid_t)-1 and sets errno to indicate the error. ERRORS
The getpgid() function will fail if: EPERM The process whose process ID is equal to pid is not in the same session as the calling process, and the implementation does not allow access to the process group ID of that process from the calling process. ESRCH There is no process with a process ID equal to pid. The getpgid() function may fail if: EINVAL The value of the pid argument is invalid. ATTRIBUTES
See attributes(5) for descriptions of the following attributes: +-----------------------------+-----------------------------+ | ATTRIBUTE TYPE | ATTRIBUTE VALUE | +-----------------------------+-----------------------------+ |Interface Stability |Standard | +-----------------------------+-----------------------------+ |MT-Level |Async-Signal-Safe | +-----------------------------+-----------------------------+ SEE ALSO
intro(2), exec(2), fork(2), getsid(2), setpgid(2), setpgrp(2), setsid(2), signal(3C), attributes(5), standards(5) SunOS 5.10 28 Dec 1996 getpid(2)
All times are GMT -4. The time now is 03:04 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy