SIGINT 回调打印两次
我正在尝试启动 IPC 的相关项目,根据我的观点,我必须开始处理信号。当按下 Ctrl
+C
时,处理程序函数会正确启动,但在控制台中它会显示打印两次。
另一个奇怪的情况是,如果我将字符更改为空字符串,父进程会正常休眠 10 秒,然后接收 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);
}
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);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论