1 year ago
#284070

ser356
SIGINT callback prints twice
i'm trying to start a IPC's related project and under my point of view I have to start handling signals. When it's time to press Ctrl
+C
the handler function starts correctly but in console it appears printed twice.
Another curious situation is that if I change the character to an empty string, the parent process sleeps 10 secs properly and then receives the SIGINT
.
#include <errno.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/sem.h>
void handler(int);
/*typedef struct {
int semaforo;
//iremos añadiendo mas a medida que necesitemos, pero para pasar todos como un
solo argumento a la función manejadora es más comodo. }ipces;*/
int semaforo;
int main() {
// ipces ipces;
semaforo = semget(IPC_PRIVATE, 3, IPC_CREAT | 0600);
semctl(semaforo, 0, SETVAL, 1);
struct sigaction newact, oldact;
struct sembuf semoper, semoper1;
sigset_t mask;
newact.sa_handler = handler;
sigemptyset(&mask);
// nueva.sa_mask=mask;
// newact.sa_flags=SA_RESTART;
sigaction(SIGINT, &newact, &oldact);
int pid = fork();
switch (pid) {
case -1:
perror("Error en el forking");
kill(getpid(), SIGINT); // envía sigint a el mismo proceso que lo
// invoca, e inicia la manejadora
break;
case 0: // hijo
while (1) {
printf("*");
}
break;
default:
semoper.sem_op = -1;
semoper.sem_num = 0;
semop(semaforo, &semoper, 0);
sleep(10);
// hacemos que haga cosas y si todo ha ido bien lanzamos SIGINT ->
// LANZARÁ LA MANEJADORA
kill(getpid(), SIGINT);
break;
}
}
void handler(int sig) {
printf("SIGINT");
if (semctl(semaforo, 0, IPC_RMID) == 1) {
printf("Error borrando semaforo;");
}
// system("clear");
exit(3);
}
c
ipc
sigint
0 Answers
Your Answer