Subversion Repositories shark

Rev

Rev 1450 | Go to most recent revision | 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: ptest1.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 1:
29
 
30
   the main task create 4 tasks, J1, J2, J3, J4
31
     at t = 0.2 sec. it raise a signal to J1
32
     at t = 0.4 sec. it raise a signal to J2
33
     at t = 0.8 sec. it kill J4
34
 
35
   J1: it simply calls sigwait
36
 
37
   J2: it simply calls sigwaitinfo
38
 
39
   J3: it simply calls sigtimedwait with a timeout of 0.5 sec.
40
 
41
   J4: it simply calls sigtimedwait with a -long- timeout
42
       (J4 will be killed by main)
43
 
44
 non standard function used:
45
   cprintf
46
   sys_gettime
47
   keyboard stuffs
48
 
49
**/
50
 
51
/*
52
 * Copyright (C) 2000 Paolo Gai
53
 *
54
 * This program is free software; you can redistribute it and/or modify
55
 * it under the terms of the GNU General Public License as published by
56
 * the Free Software Foundation; either version 2 of the License, or
57
 * (at your option) any later version.
58
 *
59
 * This program is distributed in the hope that it will be useful,
60
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
61
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
62
 * GNU General Public License for more details.
63
 *
64
 * You should have received a copy of the GNU General Public License
65
 * along with this program; if not, write to the Free Software
66
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
67
 *
68
 */
69
 
70
#include <sys/types.h>
71
#include <pthread.h>
72
#include <signal.h>
73
 
74
#include <kernel/kern.h>
75
 
76
void *J1(void *arg)
77
{
78
  int sig, err;
79
  sigset_t mask;
80
 
81
  cprintf("J1 starts and call sigwait(31)\n");
82
 
83
  sigemptyset(&mask);
84
  sigaddset(&mask,31);
85
  err = sigwait(&mask,&sig);
86
 
87
  cprintf("J1 exit from sigwait(), err=%d, sig=%d\n", err, sig);
88
 
89
  return 0;
90
}
91
 
92
void *J2(void *arg)
93
{
94
  int err;
95
  siginfo_t info;
96
  sigset_t mask;
97
 
98
  cprintf("J2 starts and call sigwaitinfo(30)\n");
99
 
100
  sigemptyset(&mask);
101
  sigaddset(&mask,30);
102
  err = sigwaitinfo(&mask,&info);
103
 
104
  cprintf("J2 exit from sigwaitinfo(), err=%d, signo=%d code=%d value=%d\n",
105
              err, info.si_signo, info.si_code, info.si_value.sival_int);
106
 
107
  return 0;
108
}
109
 
110
void *J3(void *arg)
111
{
112
  int err;
113
  siginfo_t info;
114
  sigset_t mask;
115
  struct timespec t;
116
 
117
  cprintf("J3 starts and call sigtimedwait(29)\n");
118
 
119
  sigemptyset(&mask);
120
  sigaddset(&mask,29);
121
  t.tv_sec = 0;
122
  t.tv_nsec = 300000000;
123
  err = sigtimedwait(&mask,&info,&t);
124
 
125
  cprintf("J3 exit from sigtimedwait(), err=%d, signo=%d code=%d value=%d\n",
126
              err, info.si_signo, info.si_code, info.si_value.sival_int);
127
 
128
  return 0;
129
}
130
 
131
void uscitaJ4(void *arg)
132
{
133
  cprintf("J4: AAAARRRRGGGHHH!!! killed by someone...\n");
134
}
135
 
136
void *J4(void *arg)
137
{
138
  int err;
139
  siginfo_t info;
140
  sigset_t mask;
141
  struct timespec t;
142
 
143
  cprintf("J4 starts and call sigtimedwait(28)\n");
144
 
145
  sigemptyset(&mask);
146
  sigaddset(&mask,28);
147
  t.tv_sec = 10;
148
  t.tv_nsec = 0;
149
 
150
  pthread_cleanup_push(uscitaJ4,NULL);
151
  err = sigtimedwait(&mask,&info,&t);
152
  pthread_cleanup_pop(0);
153
 
154
  cprintf("J4 exit from sigtimedwait(), err=%d, signo=%d code=%d value=%d\n",
155
              err, info.si_signo, info.si_code, info.si_value.sival_int);
156
 
157
  return 0;
158
}
159
 
160
int main(int argc, char **argv)
161
{
162
  int err;
163
  sigset_t mask;
164
  pthread_t j1, j2, j3, j4;
165
  union sigval value;
166
 
167
 
168
  /* main blocks signals for all the tasks */
169
  sigfillset(&mask);
170
  pthread_sigmask(SIG_BLOCK, &mask, NULL);
171
 
172
  cprintf("main: creating J1\n");
173
  err = pthread_create(&j1, NULL, J1, NULL);
174
  if (err) cprintf("Error creating J1\n");
175
 
176
  cprintf("main: creating J2\n");
177
  err = pthread_create(&j2, NULL, J2, NULL);
178
  if (err) cprintf("Error creating J2\n");
179
 
180
  cprintf("main: creating J3\n");
181
  err = pthread_create(&j3, NULL, J3, NULL);
182
  if (err) cprintf("Error creating J3\n");
183
 
184
  cprintf("main: creating J4\n");
185
  err = pthread_create(&j4, NULL, J4, NULL);
186
  if (err) cprintf("Error creating J4\n");
187
 
188
  cprintf("main: waiting 0.2 sec\n");
189
  while (sys_gettime(NULL) < 200000);
190
 
191
  cprintf("main: kill(31), then wait until t=0.4 sec \n");
192
  kill(0, 31);
193
 
194
  while (sys_gettime(NULL) < 400000);
195
  cprintf("main: sigqueue(30), then wait until t=0.8 sec \n");
196
  value.sival_int = 300;
197
  sigqueue(0, 30, value);
198
 
199
  while (sys_gettime(NULL) < 800000);
200
  cprintf("main: kill(J4)\n");
201
  pthread_cancel(j4);
202
 
203
  cprintf("main: ending...\n");
204
 
205
  return 0;
206
}