Rev 1550 | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1085 | pj | 1 | /* |
2 | * Project: S.Ha.R.K. |
||
3 | * |
||
4 | * Coordinators: |
||
5 | * Giorgio Buttazzo <giorgio@sssup.it> |
||
6 | * Paolo Gai <pj@gandalf.sssup.it> |
||
7 | * |
||
8 | * Authors : |
||
9 | * Paolo Gai <pj@gandalf.sssup.it> |
||
10 | * (see the web pages for full authors list) |
||
11 | * |
||
12 | * ReTiS Lab (Scuola Superiore S.Anna - Pisa - Italy) |
||
13 | * |
||
14 | * http://www.sssup.it |
||
15 | * http://retis.sssup.it |
||
16 | * http://shark.sssup.it |
||
17 | */ |
||
18 | |||
19 | /** |
||
20 | ------------ |
||
1550 | pj | 21 | CVS : $Id: ptest4.c,v 1.4 2005-01-08 14:36:11 pj Exp $ |
1085 | pj | 22 | |
23 | File: $File$ |
||
1550 | pj | 24 | Revision: $Revision: 1.4 $ |
25 | Last update: $Date: 2005-01-08 14:36:11 $ |
||
1085 | pj | 26 | ------------ |
27 | |||
28 | Posix test 4: |
||
29 | |||
30 | the main task create 3 tasks, J1, J2, J3 |
||
31 | at t = 1 sec. it raise a signal to J1 |
||
32 | at t = 2 sec. it kills J2 |
||
33 | |||
34 | J1,J2,J3: it simply calls nanosleep |
||
35 | |||
36 | non standard function used: |
||
37 | cprintf |
||
38 | sys_gettime |
||
39 | keyboard stuffs |
||
40 | |||
41 | **/ |
||
42 | |||
43 | /* |
||
44 | * Copyright (C) 2000 Paolo Gai |
||
45 | * |
||
46 | * This program is free software; you can redistribute it and/or modify |
||
47 | * it under the terms of the GNU General Public License as published by |
||
48 | * the Free Software Foundation; either version 2 of the License, or |
||
49 | * (at your option) any later version. |
||
50 | * |
||
51 | * This program is distributed in the hope that it will be useful, |
||
52 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
53 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
54 | * GNU General Public License for more details. |
||
55 | * |
||
56 | * You should have received a copy of the GNU General Public License |
||
57 | * along with this program; if not, write to the Free Software |
||
58 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
||
59 | * |
||
60 | */ |
||
61 | |||
62 | #include <sys/types.h> |
||
63 | #include <pthread.h> |
||
64 | #include <signal.h> |
||
65 | #include <time.h> |
||
66 | |||
67 | #include <kernel/kern.h> |
||
68 | |||
69 | void uscitaJ(void *arg) |
||
70 | { |
||
71 | cprintf("J: (pid=%d) AAAARRRRGGGHHH!!! killed by someone...\n", exec_shadow); |
||
72 | } |
||
73 | |||
74 | void *J(void *arg) |
||
75 | { |
||
76 | struct timespec t1, t2; |
||
77 | int err; |
||
78 | |||
79 | cprintf("J (pid=%d) starts and call nanosleep\n",exec_shadow); |
||
80 | |||
81 | t1.tv_sec = 3; |
||
82 | t1.tv_nsec = 0; |
||
83 | NULL_TIMESPEC(&t2); |
||
84 | pthread_cleanup_push(uscitaJ,NULL); |
||
85 | err = nanosleep(&t1, &t2); |
||
86 | pthread_cleanup_pop(0); |
||
87 | |||
88 | cprintf("J (pid=%d) ending, nanosleep returns errno=%d, t2=%ld.%ld\n", |
||
89 | exec_shadow, err, t2.tv_sec, t2.tv_nsec/1000); |
||
90 | |||
91 | return 0; |
||
92 | } |
||
93 | |||
94 | void signal_handler(int signo, siginfo_t *info, void *extra) |
||
95 | { |
||
96 | cprintf("SIGNAL HANDLER: pid=%d\n",exec_shadow); |
||
97 | } |
||
98 | |||
99 | |||
100 | int main(int argc, char **argv) |
||
101 | { |
||
102 | int err; |
||
103 | pthread_t j1, j2, j3; |
||
104 | struct sigaction sig_act; |
||
105 | |||
106 | sig_act.sa_sigaction = (void *) signal_handler; |
||
107 | sig_act.sa_flags = SA_SIGINFO; |
||
108 | sigemptyset(&sig_act.sa_mask); |
||
109 | |||
110 | sigaction(31, &sig_act, NULL); |
||
111 | |||
112 | cprintf("main: creating J1\n"); |
||
113 | err = pthread_create(&j1, NULL, J, NULL); |
||
114 | if (err) cprintf("Error creating J1\n"); |
||
115 | |||
116 | cprintf("main: creating J2\n"); |
||
117 | err = pthread_create(&j2, NULL, J, NULL); |
||
118 | if (err) cprintf("Error creating J2\n"); |
||
119 | |||
120 | cprintf("main: creating J3\n"); |
||
121 | err = pthread_create(&j3, NULL, J, NULL); |
||
122 | if (err) cprintf("Error creating J3\n"); |
||
123 | |||
124 | cprintf("main: waiting 1 sec\n"); |
||
125 | while (sys_gettime(NULL) < 1000000); |
||
126 | |||
127 | cprintf("main: pthread_kill on j1, then wait until t=2 sec \n"); |
||
128 | pthread_kill(j1, 31); |
||
129 | |||
130 | while (sys_gettime(NULL) < 2000000); |
||
131 | cprintf("main: pthread_cancel(J2)\n"); |
||
132 | pthread_cancel(j2); |
||
133 | |||
134 | cprintf("main: ending...\n"); |
||
135 | |||
136 | return 0; |
||
137 | } |