-
Notifications
You must be signed in to change notification settings - Fork 0
/
SO_Tema4_Diapos_Señales.c
121 lines (94 loc) · 2.76 KB
/
SO_Tema4_Diapos_Señales.c
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include <stdio.h>
#include <signal.h>
#include <stdlib.h>
// EJEMPLO 1
/*
void manejador(int sig) {
printf("Recibida señal %d!\n",sig);
}
int main() {
// SIGINT interrumpe el proc (ctrl+c) Interactive attention signal. Generally generated by the application user.
signal(SIGINT, manejador);
printf("Esperando señal.\n");
pause();
printf("Saliendo.\n");
return 0;
}
*/
/* PROGRAM OUTPUT
➜ c_programming_files git:(studying_t4) ✗ ./signal
Esperando señal.
^C (ctrl+c)
Recibida señal 2!
Saliendo.
*/
// FIN EJEMPLO 1
// EJEMPLO 2
int pid;
void manejador(int sig) {
printf("Manejador - Entro en el manejador: \n");
printf("Manejador - PID: %d\n", pid);
// Si pid == 0, significa que es un proceso hijo
if(pid == 0){
printf("Manejador - IF PID == 0: \n");
printf("Manejador - Hijo: Recibida señal %d\n", sig);
printf("Manejador - PID es: %d\n", getppid());
kill(getppid(), SIGUSR1);
printf("Manejador - KILL \n");
exit(0);
} else {
printf("Manejador - IF PID != 0: \n");
printf("Manejador - Padre: Recibida señal %d\n", sig);
}
}
int main() {
printf("MAIN - PID: %d\n", pid);
printf("MAIN - SIGNAL ()\n");
signal(SIGUSR1, manejador);
printf("MAIN - Siguiente instrucción es el FORK\n");
pid = fork();
printf("MAIN - Tras el fork, PID es ahora: %d\n", pid);
if ( pid == 0 ) { // HIJO
while(1);
}
else { // Padre
kill(pid, SIGUSR1);
wait(NULL);
printf("MAIN - El hijo terminó\n");
}
return 0;
}
/* PROGRAM OUTPUT
MAIN - PID: 0
MAIN - SIGNAL ()
MAIN - Siguiente instrucción es el FORK
MAIN - Tras el fork, PID es ahora: 4601
Manejador - Entro en el manejador:
Manejador - PID: 0
Manejador - IF PID == 0:
Manejador - Hijo: Recibida señal 30
Manejador - PID es: 4600
Manejador - KILL
Manejador - Entro en el manejador:
Manejador - PID: 4601
Manejador - IF PID != 0:
Manejador - Padre: Recibida señal 30
MAIN - El hijo terminó
*/
// FIN EJEMPLO 2
/*
SIGNAL()
https://www.tutorialspoint.com/c_standard_library/c_function_signal.htm
The C library function void (*signal(int sig, void (*func)(int)))(int) sets a function to handle signal
i.e. a signal handler with signal number sig.
void (*signal(int sig, void (*func)(int)))(int)
sig − This is the signal number to which a handling function is set.
func − This is a pointer to a function. This can be a function defined by the programmer or one of the following predefined functions
This function returns the previous value of the signal handler, or SIG_ERR on error.
signal(int signum, sighandler_t handler)
asigna a la señal de número signum el manejador handler. El manejador puede ser:
§ Una función a implementar void handler(int sig)
§ Una acción a realizar como:
• SIG_DFL: Acción por defecto de la señal
• SIG_IGN: Ignora la señal
*/