The following program is supposed to do this:
- The main process should create a child process,which I will refer to as main child from now on for convenience,then wait for a SIGUSR2 signal, then send a SIGUSR1 signal to his child and grandchildren, then wait for his child and finally end.
- The main child should do the following 4 times: create a child and wait for SIGUSR2. After that the process should send a SIGUSR2 to his father,then wait for SIGUSR1, then wait for his children to end and finally end.
- The main child's children should print Proceso "PID" listo,then send a SIGUSR2 to the main child,then wait for SIGUSR1,then print Señal capturada and finally end.
However it just prints one PID and then it never ends unless I use CTRL^C.I've tried changing the order of the pause() fucntions but to no avail. Also this is homework for college and they said we can't use semaphores yet.
If you know Spanish here are the instructions:
• El proceso padre generará un proceso hijo, que será el proceso gestor.
• El proceso gestor creará N_PROC procesos hijos (los participantes en la competición) en un bucle, esperando tras crear cada proceso a que éste le notifique que está preparado enviándole la señal SIGUSR2 .
• Cada participante en la carrera, una vez activo y con la señal armada, imprimirá un mensaje y avisará al gestor mediante la señal SIGUSR2 .
• El proceso gestor, cuando haya creado los N_PROC procesos hijos y éstos estén listos para la competición, avisará al proceso padre de que está todo listo enviándole la señal SIGUSR2 .
• El proceso padre mandará al grupo entero de procesos la señal de la competición).
• Cuando los participantes en la carrera reciban la señal ya han capturado la señal, y terminarán.
• Cuando el proceso gestor reciba SIGUSR1 , terminará su ejecución sin dejar hijos huérfanos.
• El proceso padre esperará a que el proceso gestor termine y acabará él también.
Thanks in advance.
#include <stdio.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#define N_PROC 4
void manejador_SIGUSR1(int sig) {
printf("Señal capturada\n");
exit(EXIT_SUCCESS);
}
void manejador_SIGUSR2(int sig) {
}
int main(void){
int i,pid[N_PROC+1];
struct sigaction act;
sigemptyset(&(act.sa_mask));
act.sa_flags = 0;
/* Se arma la señal SIGUSR1. */
act.sa_handler = manejador_SIGUSR1;
if (sigaction(SIGUSR1, &act, NULL) < 0) {
perror("sigaction");
exit(EXIT_FAILURE);
}
act.sa_handler = manejador_SIGUSR2;
if (sigaction(SIGUSR2, &act, NULL) < 0) {
perror("sigaction");
exit(EXIT_FAILURE);
}
if((pid[0] = fork()) == -1) {
perror("fork");
exit(EXIT_FAILURE);
}
if(pid[0] == 0) {
for(i=0;i<N_PROC;i++){
if((pid[i+1] = fork()) == -1) {
perror("fork");
exit(EXIT_FAILURE);
}
if(pid[i+1]==0){
printf("Proceso %d listo\n",getpid());
kill(SIGUSR2,getppid());
pause();
}
else{
pause();
}
}
kill(SIGUSR2,getppid());
pause();
while(wait(NULL)>0);
exit(EXIT_SUCCESS);
}
pause();
kill(SIGUSR1,0);
while(wait(NULL)>0);
exit(EXIT_SUCCESS);
}