Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <stdio.h>
- #include <pthread.h>
- #include <signal.h>
- int global_gotsig = 0;
- void *gotsig(int sig, siginfo_t *info, void *ucontext)
- {
- global_gotsig++;
- return NULL;
- }
- void *reader(void *arg)
- {
- char buf[32];
- int i;
- int hdlsig = (int)arg;
- struct sigaction sa;
- sa.sa_handler = NULL;
- sa.sa_sigaction = gotsig;
- sa.sa_flags = SA_SIGINFO;
- sigemptyset(&sa.sa_mask);
- if (sigaction(hdlsig, &sa, NULL) < 0) {
- perror("sigaction");
- return (void *)-1;
- }
- i = read(fileno(stdin), buf, 32);
- if (i < 0) {
- perror("read");
- } else {
- printf("Read %d bytesn", i);
- }
- return (void *)i;
- }
- main(int argc, char **argv)
- {
- pthread_t tid1;
- void *ret;
- int i;
- int sig = SIGUSR1;
- if (argc == 2) sig = atoi(argv[1]);
- printf("Using sig %dn", sig);
- if (pthread_create(&tid1, NULL, reader, (void *)sig)) {
- perror("pthread_create");
- exit(1);
- }
- sleep(5);
- printf("killing threadn");
- pthread_kill(tid1, sig);
- i = pthread_join(tid1, &ret);
- if (i < 0)
- perror("pthread_join");
- else
- printf("thread returned %ldn", (long)ret);
- printf("Got sig? %dn", global_gotsig);
- }
- while (!_finished)
- {
- pthread_cond_wait(&cond);
- handleio();
- }
- cleanup();
- struct pollfd pfd;
- pfd.fd = socket;
- pfd.events = POLLIN | POLLHUP | POLLERR;
- pthread_lock(&lock);
- while(thread_alive)
- {
- int ret = poll(&pfd, 1, 100);
- if(ret == 1)
- {
- //handle IO
- }
- else
- {
- pthread_cond_timedwait(&lock, &cond, 100);
- }
- }
- pthread_unlock(&lock);
Add Comment
Please, Sign In to add comment