[9] | 1 | /*
|
---|
| 2 | * Test name: test13b.c
|
---|
| 3 | *
|
---|
| 4 | * Objective: The purpose of this tests is to show how pselect()
|
---|
| 5 | * solves the situation shown in test13a.c
|
---|
| 6 | *
|
---|
| 7 | * Description: The program waits for SIGHUP or input in the terminal
|
---|
| 8 | */
|
---|
| 9 |
|
---|
| 10 | #include <sys/types.h>
|
---|
| 11 | #include <sys/select.h>
|
---|
| 12 | #include <stdlib.h>
|
---|
| 13 | #include <unistd.h>
|
---|
| 14 | #include <time.h>
|
---|
| 15 | #include <stdio.h>
|
---|
| 16 | #include <signal.h>
|
---|
| 17 | #include <errno.h>
|
---|
| 18 |
|
---|
| 19 | int got_sighup = 0;
|
---|
| 20 |
|
---|
| 21 | void catch_hup(int sig_num)
|
---|
| 22 | {
|
---|
| 23 | printf("Received a SIGHUP, set global vble \n");
|
---|
| 24 | got_sighup = 1;
|
---|
| 25 | }
|
---|
| 26 |
|
---|
| 27 | int main(void) {
|
---|
| 28 | int ret; /* return value */
|
---|
| 29 | fd_set read_fds;
|
---|
| 30 | sigset_t sigmask, orig_sigmask;
|
---|
| 31 | char data[1024];
|
---|
| 32 |
|
---|
| 33 | /* Init read fd_set */
|
---|
| 34 | FD_ZERO(&read_fds);
|
---|
| 35 | FD_SET(0, &read_fds);
|
---|
| 36 |
|
---|
| 37 | /* set the signal masks */
|
---|
| 38 | sigemptyset(&sigmask);
|
---|
| 39 | sigaddset(&sigmask, SIGHUP);
|
---|
| 40 | sigprocmask( SIG_BLOCK, &sigmask, &orig_sigmask);
|
---|
| 41 |
|
---|
| 42 | /* set the HUP signal handler to 'catch_hup' */
|
---|
| 43 | signal(SIGHUP, catch_hup);
|
---|
| 44 |
|
---|
| 45 | /* Get proc_id and print it */
|
---|
| 46 | printf("Send a signal from other terminal with: kill -1 %d\n", getpid());
|
---|
| 47 | printf("Going to sleep for 5 seconds, if the signal arrives meanwhile\n");
|
---|
| 48 | printf("the process will be blocked until there is input in the keyboard\n");
|
---|
| 49 | printf("if the signal arrives after the timeout and while in select, it will\n");
|
---|
| 50 | printf("behave as it should.\n");
|
---|
| 51 | printf("Sleeping for 5 secs\n");
|
---|
| 52 | sleep(5);
|
---|
| 53 |
|
---|
| 54 | printf("Blocking now on select...\n");
|
---|
| 55 | #if 0
|
---|
| 56 | ret = pselect(1, &read_fds, NULL, NULL, NULL, &orig_sigmask);
|
---|
| 57 | #else
|
---|
| 58 | ret = -1;
|
---|
| 59 | #endif
|
---|
| 60 | if (got_sighup) {
|
---|
| 61 | printf("We have a sighup signal so exit the program\n");
|
---|
| 62 | exit(0);
|
---|
| 63 | }
|
---|
| 64 | gets(data);
|
---|
| 65 | printf("Got entry for terminal then, bye\n");
|
---|
| 66 |
|
---|
| 67 | }
|
---|