1

The father forks as many children as the parameter given from the command line.The children after birth are paused and the father awakens them by sending signal. However, in the way I wrote it the children never seem to get the awaking signal. Do I get the concept of children pids wrong and that's why they never get the signal?

./test 4 5 1 3 2

as posted above 4, 5, .... stand for child 4, child 5, ... I save them consecutively as they are posted from the command line in array for saving their indexes (for later usage...) and their pids in another array . I want to send the signal (the father wants) consecutively as cited in the command line

#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<unistd.h>
#include<sys/wait.h>
#include<signal.h>

void handler();

int main(int argc, char **argv){

    pid_t proc_pid[argc-1], order[argc-1];
    int i;

    for(i=0;i<=argc-2;i++){
        order[i]=atoi(argv[i+1]);
        //printf("%d\n",order[i]);
        //printf("%d\n",i);
        if((proc_pid[i] = fork()) < 0){
            perror("fork");
        }
        else if(proc_pid[i] == 0){
           // proc_pid[i]=getpid();
            printf("%d\n",proc_pid[i]);
            pause();
            signal(SIGINT, handler);
            exit(0);
        }
        else{     
            kill(proc_pid[i], SIGINT);     
            //kill(0, SIGINT);
           // exit(0); 
        }     
    } 
    return 0;
}

void handler(){
    printf("ok\n");
}
mariatsamp
  • 49
  • 7
  • Before you're using `argc` and `argv`, always make sure that they are valid. For example, what happens with your arrays if the user doesn't provide any arguments? *Always* do validation (especially of user input) and error checking. By the way, the way you have written the code, there's really no need for your arrays actually. – Some programmer dude Mar 08 '19 at 09:17
  • As for your problem, think about what you're doing in the child processes. Think about what the [`pause`](http://man7.org/linux/man-pages/man2/pause.2.html) function does, and in what order you do things. And remember that you have no control over which process will run first, maybe the parent process will send `SIGINT` before the child process even runs. – Some programmer dude Mar 08 '19 at 09:19
  • Are pipes the only way to control which process will run first? – mariatsamp Mar 08 '19 at 09:21

1 Answers1

2

The order of father-process and child-process is uncertain.

You can add sleep(1) in your code to let father-process run after child-process, like this:

...

   sleep(1);
   kill(proc_pid[i], SIGINT);     
   //kill(0, SIGINT);
   // exit(0); 

...
wonter
  • 415
  • 2
  • 14
  • I would also like to ask why, even after that correct change, the children do not get into a signal handler function – mariatsamp Mar 08 '19 at 09:34
  • 1
    @maritsamp Add the `signal` call before `pause` so that signal handler is installed – Karthick Mar 08 '19 at 09:57
  • @maritsamp plz see: https://stackoverflow.com/questions/9547949/printf-is-not-working-in-c-signal-handler/ – wonter Mar 08 '19 at 10:01
  • Thank you all for your help. I did fix all the things you mentioned. I find signals a bit confusing, so I have one last question. When in the fathers code I send kill(....) I "activate" each process one time. If i want to activate each process mutiple times for example 2, do I include the kill statment in a loop? I did try something like that: for(j=0;j<4;j++){ for(i=0;i<=argc-2;i++){ kill(proc_pid[i], SIGINT); k++; //sleep(3); kill(proc_pid[i], SIGINT); } – mariatsamp Mar 08 '19 at 12:27
  • @maritsamp yes. – wonter Mar 08 '19 at 13:01