Subversion Repositories shark

Rev

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