sisi.c

Madars, 09/14/2018 11:21 AM

Download (1.73 KB)

 
1
#include <errno.h>
2
#include <signal.h>
3
#include <stdio.h>
4
#include <string.h>
5
#include <sys/select.h>
6
#include <unistd.h>
7
#include <pthread.h>
8

    
9
#define SIG SIGALRM
10

    
11
void signal_action(int signal)
12
{
13
//    fprintf(stderr, "%s caught\n", strsignal(signal));
14
}
15

    
16
static pthread_mutex_t foo_mutex = PTHREAD_MUTEX_INITIALIZER;
17

    
18
void* thread_routine(void* arg)
19
{
20
        volatile double d;
21
        long i, j;
22
        long t = (long)arg;
23

    
24
    struct sigaction sa;
25
    sa.sa_handler = signal_action;
26
    //sa.sa_handler = NULL;
27

    
28
    if(sigaction(SIG, &sa, NULL) < 0)
29
    {
30
        fprintf(stderr, "sigaction failed with %d (%s)\n", errno, strerror(errno));
31
        return NULL;
32
    }
33
        /* seems signal is dumped while doing this work*/
34
        for (i=0; i<299999000; i++)
35
        {
36
                d = i/3 * 0.14f + t;
37
        }
38

    
39
        int rc = select(0, NULL, NULL, NULL, NULL);
40
        if(rc < 0)
41
        {
42
            fprintf(stderr, "Select error: %d (%s)\n", errno, strerror(errno));
43
        }
44
        else
45
        {
46
            fprintf(stderr, "Select return %d\n", rc);
47
        }
48
        fprintf(stderr, "got result: %lf\n", d);
49
}
50

    
51
int main(int argc, char** argv)
52
{
53
    pthread_t handle;
54

    
55
struct timeval tv;
56

    
57
gettimeofday(&tv, NULL);
58

    
59
    int rc = pthread_create(&handle, NULL, thread_routine, (void *)tv.tv_sec);
60
    if(rc != 0)
61
    {
62
        fprintf(stderr, "pthread_create failed with %d (%s)\n", rc, strerror(rc));
63
        return 1;
64
    }
65

    
66
    sleep(1);
67
        fprintf(stderr, "done sleep\n");
68

    
69
    rc = pthread_kill(handle, SIG);
70
    if(rc != 0)
71
    {
72
        fprintf(stderr, "pthread_kill failed with %d (%s)\n", rc, strerror(rc));
73
        return 1;
74
    }
75

    
76
    rc = pthread_join(handle, NULL);
77
    if(rc != 0)
78
    {
79
        fprintf(stderr, "pthread_join failed with %d (%s)\n", rc, strerror(rc));
80
        return 1;
81
    }
82

    
83
    return 0;
84
}
85