Rev | Line | |
---|
[9] | 1 | /*
|
---|
| 2 | * Test name: test13a.c
|
---|
| 3 | *
|
---|
| 4 | * Objective: The purpose of this tests is to show how a select can
|
---|
| 5 | * get into a race condition when dealing with signals.
|
---|
| 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 | char data[1024];
|
---|
| 31 |
|
---|
| 32 | /* Init read fd_set */
|
---|
| 33 | FD_ZERO(&read_fds);
|
---|
| 34 | FD_SET(0, &read_fds);
|
---|
| 35 |
|
---|
| 36 | /* set the HUP signal handler to 'catch_hup' */
|
---|
| 37 | signal(SIGHUP, catch_hup);
|
---|
| 38 |
|
---|
| 39 | /* Get proc_id and print it */
|
---|
| 40 | printf("Send a signal from other terminal with: kill -1 %d\n", getpid());
|
---|
| 41 | printf("Going to sleep for 5 seconds, if the signal arrives meanwhile\n");
|
---|
| 42 | printf("the process will be blocked until there is input in the keyboard\n");
|
---|
| 43 | printf("if the signal arrives after the timeout and while in select, it will\n");
|
---|
| 44 | printf("behave as it should.\n");
|
---|
| 45 | printf("Sleeping for 5 secs\n");
|
---|
| 46 | sleep(5);
|
---|
| 47 |
|
---|
| 48 | printf("Blocking now on select...\n");
|
---|
| 49 | ret = select(1, &read_fds, NULL, NULL, NULL);
|
---|
| 50 | if (got_sighup) {
|
---|
| 51 | printf("We have a sighup signal so exit the program\n");
|
---|
| 52 | exit(0);
|
---|
| 53 | }
|
---|
| 54 | gets(data);
|
---|
| 55 | printf("Got entry for terminal then, bye\n");
|
---|
| 56 | }
|
---|
Note:
See
TracBrowser
for help on using the repository browser.