1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889 |
- #include <pthread.h>
- #include <signal.h>
- #include <unistd.h>
- static int the_sig;
- static void
- eintr_handler (int sig)
- {
- if (sig != the_sig)
- {
- write (STDOUT_FILENO, "eintr_handler: signal number wrong\n", 35);
- _exit (1);
- }
- write (STDOUT_FILENO, ".", 1);
- }
- static void *
- eintr_source (void *arg)
- {
- struct timespec ts = { .tv_sec = 0, .tv_nsec = 500000 };
- if (arg == NULL)
- {
- sigset_t ss;
- sigemptyset (&ss);
- sigaddset (&ss, the_sig);
- pthread_sigmask (SIG_BLOCK, &ss, NULL);
- }
- while (1)
- {
- if (arg != NULL)
- pthread_kill (*(pthread_t *) arg, the_sig);
- else
- kill (getpid (), the_sig);
- nanosleep (&ts, NULL);
- }
-
- return NULL;
- }
- static void
- setup_eintr (int sig, pthread_t *thp)
- {
- struct sigaction sa;
- sigemptyset (&sa.sa_mask);
- sa.sa_flags = 0;
- sa.sa_handler = eintr_handler;
- if (sigaction (sig, &sa, NULL) != 0)
- {
- puts ("setup_eintr: sigaction failed");
- exit (1);
- }
- the_sig = sig;
-
- pthread_t th;
- if (pthread_create (&th, NULL, eintr_source, thp) != 0)
- {
- puts ("setup_eintr: pthread_create failed");
- exit (1);
- }
- }
|