multiple kills system calls into a proc

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.

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

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:
1
2
3
4
5
6
7
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);
 }
}



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<unistd.h>
#include<sys/wait.h>
#include<signal.h>
int i,j,k;
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]);
        if((proc_pid[i] = fork()) < 0){
            perror("fork");
        }
        else if(proc_pid[i] == 0){
            proc_pid[i]=getpid();
           
            signal(SIGINT, handler);
            
           // for(j=0;j<4;j++){
            printf("Child%d %d is executed (%d)\n",i, proc_pid[i], k);
            //k++;
            
            //}
            //sleep(3);
            pause();
            exit(0);
            //sleep(3);
        }
        else{   
           
              sleep(1);  
              // for(j-0;j<4;j++){
               // for(i=0;i<=argc-2;i++){
              kill(proc_pid[i], SIGINT);   
              printf("%d\n",i);
              k++;
              //sleep(3);
               // }
            //}   
        }     
    } 
    return 0;
}

void handler(){
    printf("ok\n");
    //sleep(3);
    //printf("%d\n",i);    
}



Last edited on
http://man7.org/linux/man-pages/man2/signal.2.html
A few things to note in the portability section.

1. Signal handlers are not a message queue. If you blast rapid signals, expect things to not happen.

2. Watch out for your signal handler being reset to SIG_DFL on the first signal. A second signal might not be caught as you expect. Look at using sigaction() for a much more defined approach to signal handling.

3. You have a very limited set of functions you can safely call inside a signal handler.
http://man7.org/linux/man-pages/man7/signal-safety.7.html
printf() isn't one of them.
Whilst sleep() is valid, it's a very bad idea to needlessly delay a signal handler.
Topic archived. No new replies allowed.