Hi, all.
I have the following test code:
1 #include <stdio.h>
2 #include <pthread.h>
3 #include <signal.h>
4 #include <unistd.h>
5 #include <sys/types.h>
6
7 pthread_t thread1;
8 pthread_t thread2;
9 typedef void (*sig_action)(int, siginfo_t *, void *);
10 void* thread_fun1(void *);
11 void* thread_fun2(void *);
12
13 void reintegration(int signo, siginfo_t *info, void *context)
14 {
15 printf ("get SIGUSR1 signal,enter reintegration thread id is %ul\n.....\n",pthread_self());
16 sleep(6);
17 printf("reintegration completed\n");
18 return;
19 }
20
21 int main()
22 {
23 int err = 0;
24 printf("The parent pid = %d\n",getpid());
25
26 err = pthread_create(&thread1,NULL,thread_fun1,NULL);
27 if (err){
28 printf("create thread 1 error %d\n",err);
29 }
30
31 err = pthread_create(&thread2,NULL,thread_fun2, &thread1);
32 if (err){
33 printf("create thread 2 error %d\n",err);
34 }
35
36 pthread_join(thread1,NULL);
37 pthread_join(thread2,NULL);
38 return 0;
39 }
40
41 void* thread_fun1(void *ptr)
42 {
43 struct sigaction act;
44 int last;
45 pthread_t pthread_id1;
46 sigset_t newmask;
47
48
49 pthread_id1 = pthread_self();
50 printf ("The child thread1 = %lu\n", pthread_id1);
51
52 sigemptyset(&act.sa_mask);
53 sigemptyset(&newmask);
54 sigaddset(&newmask, SIGUSR1);
55 act.sa_sigaction = (sig_action)reintegration;
56
57 act.sa_flags = SA_SIGINFO;
58 if (sigaction (SIGUSR1, &act, NULL) < 0)
59 {
60 printf ("Install signal error\n");
61 return NULL;
62 }
63 last = sleep(5);
64 printf ("The time unsleeped is %d\n", last);
65 }
66
67 void* thread_fun2(void *ptr)
68 {
69 pthread_t pthread_id;
70 pthread_id2 = pthread_self();
71 printf ("The child thread2 = %lu\n",pthread_id2);
72 sleep (3);
73 pthread_kill( *(pthread_t *)ptr,SIGUSR1);
74 printf("thread 2 exit\n");
75 }
The running result is :
76 The parent pid = 14306
77 The child thread1 = 1082132832
78 The child thread2 = 1090525536
79 thread 2 exit
80 get SIGUSR1 signal,enter reintegration thread id is 1082132832l
81 .....
82 reintegration completed
83 The time unsleeped is 2
I have the following questions:
1. In line 58 when I call sigaction to associate a action handler(act.sa_handler or act.sa_sigaction)
with a signal SIGUSR1, how can I pass a parameter(for example:pthread_id1) to the handler?
2. In thread1 when it receives a signal SIGUSR1, it will call the handler reintegration. From the running
result we can find that the thread id of thread1 is 1082132832 (from line 77) and the thread id of
reintegration is 1082132832l (from line 80). Is there some relation between these two thread id ?
Wish I describe the problem clearly~
Thank you!
--
National Research Center for Intelligent Computing Systems
Institute of Computing Technology, Chinese Academy of Sciences
I have the following test code:
1 #include <stdio.h>
2 #include <pthread.h>
3 #include <signal.h>
4 #include <unistd.h>
5 #include <sys/types.h>
6
7 pthread_t thread1;
8 pthread_t thread2;
9 typedef void (*sig_action)(int, siginfo_t *, void *);
10 void* thread_fun1(void *);
11 void* thread_fun2(void *);
12
13 void reintegration(int signo, siginfo_t *info, void *context)
14 {
15 printf ("get SIGUSR1 signal,enter reintegration thread id is %ul\n.....\n",pthread_self());
16 sleep(6);
17 printf("reintegration completed\n");
18 return;
19 }
20
21 int main()
22 {
23 int err = 0;
24 printf("The parent pid = %d\n",getpid());
25
26 err = pthread_create(&thread1,NULL,thread_fun1,NULL);
27 if (err){
28 printf("create thread 1 error %d\n",err);
29 }
30
31 err = pthread_create(&thread2,NULL,thread_fun2, &thread1);
32 if (err){
33 printf("create thread 2 error %d\n",err);
34 }
35
36 pthread_join(thread1,NULL);
37 pthread_join(thread2,NULL);
38 return 0;
39 }
40
41 void* thread_fun1(void *ptr)
42 {
43 struct sigaction act;
44 int last;
45 pthread_t pthread_id1;
46 sigset_t newmask;
47
48
49 pthread_id1 = pthread_self();
50 printf ("The child thread1 = %lu\n", pthread_id1);
51
52 sigemptyset(&act.sa_mask);
53 sigemptyset(&newmask);
54 sigaddset(&newmask, SIGUSR1);
55 act.sa_sigaction = (sig_action)reintegration;
56
57 act.sa_flags = SA_SIGINFO;
58 if (sigaction (SIGUSR1, &act, NULL) < 0)
59 {
60 printf ("Install signal error\n");
61 return NULL;
62 }
63 last = sleep(5);
64 printf ("The time unsleeped is %d\n", last);
65 }
66
67 void* thread_fun2(void *ptr)
68 {
69 pthread_t pthread_id;
70 pthread_id2 = pthread_self();
71 printf ("The child thread2 = %lu\n",pthread_id2);
72 sleep (3);
73 pthread_kill( *(pthread_t *)ptr,SIGUSR1);
74 printf("thread 2 exit\n");
75 }
The running result is :
76 The parent pid = 14306
77 The child thread1 = 1082132832
78 The child thread2 = 1090525536
79 thread 2 exit
80 get SIGUSR1 signal,enter reintegration thread id is 1082132832l
81 .....
82 reintegration completed
83 The time unsleeped is 2
I have the following questions:
1. In line 58 when I call sigaction to associate a action handler(act.sa_handler or act.sa_sigaction)
with a signal SIGUSR1, how can I pass a parameter(for example:pthread_id1) to the handler?
2. In thread1 when it receives a signal SIGUSR1, it will call the handler reintegration. From the running
result we can find that the thread id of thread1 is 1082132832 (from line 77) and the thread id of
reintegration is 1082132832l (from line 80). Is there some relation between these two thread id ?
Wish I describe the problem clearly~
Thank you!
--
National Research Center for Intelligent Computing Systems
Institute of Computing Technology, Chinese Academy of Sciences