vendredi 6 mars 2015

Receiving SIGUSR1 from multiple child processes



I am writing a program that creates child processes with fork() and execl() and then waits for a SIGUSR1 signal indicating that the processes are finished and then reads from pipes that the child processes's stdout has been redirected to. I am running into an issue where I do not know how to check for SIGUSR1 from multiple processes and then perform different functions, in the parent, based on which child sent the signal. Both children are the same function and return the total size of files given to it in a list but one will return the size of files accessed within the past week and the other returns the size of the files that have not been accessed within the past week. I need to print a different string from each child (i.e. parent gets SIGUSR1 signal from child 1, parent prints "There are %s bytes in files that have not been accessed recently\n", and when the parent gets SIGUSR1 from child 2, parent prints "There are %s bytes in files that have been accessed recently\n"), the number of bytes is stored in a string not a integer. P.S. I know this is not an efficient way to get this done. Also I noticed that after the first time a child sends the signal, I cannot tell which one, the second child never will, although they are the same program invoked with different parameters. Here is a code snippet of how the children are called.



if(fork() == 0){//First totalSize
close(a2t1[1]);close(t2r1[0]);
close(r2a1[1]);close(r2a1[0]);
close(r2a2[1]);close(r2a2[0]);
close(a2t2[1]);close(a2t2[0]);
close(t2r2[1]);close(t2r2[0]);

close(0); dup(a2t1[0]); close(a2t1[0]);
close(1); dup(t2r1[1]); close(t2r1[0]);
execl("totalSize","totalSize", argf[1], (char *)0);
fprintf(stderr, "Unable to exec 3\n");
exit(-1);
}
if(fork() == 0){//Second totalSize
close(a2t2[1]);close(t2r2[0]);
close(r2a1[1]);close(r2a1[0]);
close(a2t1[1]);close(a2t1[0]);
close(r2a2[1]);close(r2a2[0]);
close(t2r1[1]);close(t2r1[0]);

close(0); dup(a2t2[0]); close(a2t2[0]);
close(1); dup(t2r2[1]); close(t2r2[0]);
execl("totalSize","totalSize", argf[1], (char *)0);
fprintf(stderr, "Unable to exec 4\n");
exit(-1);
}


And how I write to them



write(a2t1[1], input, curLen);
close(a2t1[1]);
write(a2t2[1], input, curLen);
close(a2t2[1]);



Aucun commentaire:

Enregistrer un commentaire