ooze said:
In the unix like system, there is a func ptr signal
defined as below at the last line but I don't know how to understand it.
becoz the more general to define a func ptr is the following:
void (*signal)(int);
#include <signal.h>
void ( * signal(int SIG, void(*FUNC)(int)) )(int);
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
void hello1(int signum) {
fprintf(stdout, "%d = signum\n", signum);
}
void hello2(int signum) {
fprintf(stdout, "%d = signum\n", signum);
}
int main(int argc, char* argv[]) {
typedef void (*sighandler_t)(int);
// install new signal handlers
sighandler_t oldUSR1sighandler = signal(SIGUSR1, hello1);
sighandler_t oldUSR2sighandler = signal(SIGUSR2, hello2);
while (EOF != getchar());
// restore old signal handlers
signal(SIGUSR2, oldUSR2sighandler);
signal(SIGUSR1, oldUSR1sighandler);
return EXIT_SUCCESS;
}
> gcc -Wall -std=c99 -pedantic -o main main.c
> ./main
Then at a second terminal:
PID TTY TIME CMD
21080 pts/21 00:00:01 ssh
21217 pts/22 00:00:01 ssh
22016 pts/23 00:00:00 main
22018 pts/24 00:00:00 ps
and then, at the first terminal, I get:
10 = signum
and back at the second terminal:
PID TTY TIME CMD
21080 pts/21 00:00:01 ssh
21217 pts/22 00:00:01 ssh
22027 pts/23 00:00:00 main
22028 pts/24 00:00:00 ps
and back, at the first terminal, I get:
12 = signum