1 | /* |
---|
2 | * COPYRIGHT (c) 1989-1999. |
---|
3 | * On-Line Applications Research Corporation (OAR). |
---|
4 | * |
---|
5 | * The license and distribution terms for this file may be |
---|
6 | * found in the file LICENSE in this distribution or at |
---|
7 | * http://www.rtems.com/license/LICENSE. |
---|
8 | * |
---|
9 | * $Id$ |
---|
10 | */ |
---|
11 | |
---|
12 | #define CONFIGURE_INIT |
---|
13 | #include "system.h" |
---|
14 | #include <sched.h> |
---|
15 | #include <sys/utsname.h> |
---|
16 | |
---|
17 | void *POSIX_Init( |
---|
18 | void *argument |
---|
19 | ) |
---|
20 | { |
---|
21 | struct timespec tv; |
---|
22 | struct timespec tr; |
---|
23 | int status; |
---|
24 | int priority; |
---|
25 | pthread_t thread_id; |
---|
26 | time_t seconds; |
---|
27 | time_t seconds1; |
---|
28 | time_t remaining; |
---|
29 | struct tm tm; |
---|
30 | struct utsname uts; |
---|
31 | useconds_t useconds; |
---|
32 | |
---|
33 | puts( "\n\n*** POSIX TEST 1 ***" ); |
---|
34 | |
---|
35 | build_time( &tm, TM_FRIDAY, TM_MAY, 24, 96, 11, 5, 0 ); |
---|
36 | |
---|
37 | /* print some system information */ |
---|
38 | |
---|
39 | puts( "Init: uname - EFAULT (invalid uts pointer argument)" ); |
---|
40 | status = uname( NULL ); |
---|
41 | assert( status == -1 ); |
---|
42 | assert( errno == EFAULT ); |
---|
43 | |
---|
44 | status = uname( &uts ); |
---|
45 | assert( !status ); |
---|
46 | printf( "Init: uts.sysname: %s\n", uts.sysname ); |
---|
47 | printf( "Init: uts.nodename: %s\n", uts.nodename ); |
---|
48 | printf( "Init: uts.release: %s\n", uts.release ); |
---|
49 | printf( "Init: uts.version: %s\n", uts.version ); |
---|
50 | printf( "Init: uts.machine: %s\n", uts.machine ); |
---|
51 | puts(""); |
---|
52 | |
---|
53 | /* error cases in clock_gettime and clock_settime */ |
---|
54 | |
---|
55 | puts( "Init: clock_gettime - EINVAL (invalid clockid)" ); |
---|
56 | status = clock_settime( -1, &tv ); |
---|
57 | assert( status == -1 ); |
---|
58 | assert( errno == EINVAL ); |
---|
59 | |
---|
60 | puts( "Init: clock_settime - EINVAL (invalid clockid)" ); |
---|
61 | status = clock_settime( -1, &tv ); |
---|
62 | assert( status == -1 ); |
---|
63 | assert( errno == EINVAL ); |
---|
64 | |
---|
65 | /* exercise clock_getres */ |
---|
66 | |
---|
67 | puts( "Init: clock_getres - EINVAL (invalid clockid)" ); |
---|
68 | status = clock_getres( -1, &tv ); |
---|
69 | assert( status == -1 ); |
---|
70 | assert( errno == EINVAL ); |
---|
71 | |
---|
72 | puts( "Init: clock_getres - EINVAL (NULL resolution)" ); |
---|
73 | status = clock_getres( CLOCK_REALTIME, NULL ); |
---|
74 | assert( status == -1 ); |
---|
75 | assert( errno == EINVAL ); |
---|
76 | |
---|
77 | puts( "Init: clock_getres - SUCCESSFUL" ); |
---|
78 | status = clock_getres( CLOCK_REALTIME, &tv ); |
---|
79 | printf( "Init: resolution = sec (%ld), nsec (%ld)\n", tv.tv_sec, tv.tv_nsec ); |
---|
80 | assert( !status ); |
---|
81 | |
---|
82 | /* set the time of day, and print our buffer in multiple ways */ |
---|
83 | |
---|
84 | tv.tv_sec = mktime( &tm ); |
---|
85 | assert( tv.tv_sec != -1 ); |
---|
86 | |
---|
87 | tv.tv_nsec = 0; |
---|
88 | |
---|
89 | /* now set the time of day */ |
---|
90 | |
---|
91 | empty_line(); |
---|
92 | |
---|
93 | printf( asctime( &tm ) ); |
---|
94 | puts( "Init: clock_settime - SUCCESSFUL" ); |
---|
95 | status = clock_settime( CLOCK_REALTIME, &tv ); |
---|
96 | assert( !status ); |
---|
97 | |
---|
98 | printf( asctime( &tm ) ); |
---|
99 | printf( ctime( &tv.tv_sec ) ); |
---|
100 | |
---|
101 | /* use sleep to delay */ |
---|
102 | |
---|
103 | remaining = sleep( 3 ); |
---|
104 | assert( !remaining ); |
---|
105 | |
---|
106 | /* print new times to make sure it has changed and we can get the realtime */ |
---|
107 | |
---|
108 | status = clock_gettime( CLOCK_REALTIME, &tv ); |
---|
109 | assert( !status ); |
---|
110 | |
---|
111 | printf( ctime( &tv.tv_sec ) ); |
---|
112 | |
---|
113 | seconds = time( NULL ); |
---|
114 | printf( ctime( &seconds ) ); |
---|
115 | |
---|
116 | /* just to have the value copied out through the parameter */ |
---|
117 | |
---|
118 | seconds = time( &seconds1 ); |
---|
119 | assert( seconds == seconds1 ); |
---|
120 | |
---|
121 | /* check the time remaining */ |
---|
122 | |
---|
123 | printf( "Init: seconds remaining (%d)\n", (int)remaining ); |
---|
124 | assert( !remaining ); |
---|
125 | |
---|
126 | /* error cases in nanosleep */ |
---|
127 | |
---|
128 | empty_line(); |
---|
129 | puts( "Init: nanosleep - EINVAL (NULL time)" ); |
---|
130 | status = nanosleep ( NULL, &tr ); |
---|
131 | assert( status == -1 ); |
---|
132 | assert( errno == EINVAL ); |
---|
133 | |
---|
134 | tv.tv_sec = 0; |
---|
135 | tv.tv_nsec = TOD_NANOSECONDS_PER_SECOND * 2; |
---|
136 | puts( "Init: nanosleep - EINVAL (too many nanoseconds)" ); |
---|
137 | status = nanosleep ( &tv, &tr ); |
---|
138 | assert( status == -1 ); |
---|
139 | assert( errno == EINVAL ); |
---|
140 | |
---|
141 | /* this is actually a small delay or yield */ |
---|
142 | tv.tv_sec = -1; |
---|
143 | tv.tv_nsec = 0; |
---|
144 | puts( "Init: nanosleep - negative seconds small delay " ); |
---|
145 | status = nanosleep ( &tv, &tr ); |
---|
146 | assert( status == -1 ); |
---|
147 | assert( errno == EINVAL ); |
---|
148 | |
---|
149 | /* use nanosleep to yield */ |
---|
150 | |
---|
151 | tv.tv_sec = 0; |
---|
152 | tv.tv_nsec = 0; |
---|
153 | |
---|
154 | puts( "Init: nanosleep - yield" ); |
---|
155 | status = nanosleep ( &tv, &tr ); |
---|
156 | assert( !status ); |
---|
157 | assert( !tr.tv_sec ); |
---|
158 | assert( !tr.tv_nsec ); |
---|
159 | |
---|
160 | /* use nanosleep to delay */ |
---|
161 | |
---|
162 | tv.tv_sec = 3; |
---|
163 | tv.tv_nsec = 500000; |
---|
164 | |
---|
165 | puts( "Init: nanosleep - 1.05 seconds" ); |
---|
166 | status = nanosleep ( &tv, &tr ); |
---|
167 | assert( !status ); |
---|
168 | |
---|
169 | /* print the current real time again */ |
---|
170 | status = clock_gettime( CLOCK_REALTIME, &tv ); |
---|
171 | assert( !status ); |
---|
172 | printf( ctime( &tv.tv_sec ) ); |
---|
173 | |
---|
174 | /* check the time remaining */ |
---|
175 | |
---|
176 | printf( "Init: sec (%ld), nsec (%ld) remaining\n", tr.tv_sec, tr.tv_nsec ); |
---|
177 | assert( !tr.tv_sec && !tr.tv_nsec ); |
---|
178 | |
---|
179 | puts( "Init: usleep - 1.35 seconds" ); |
---|
180 | useconds = usleep ( 1350000 ); |
---|
181 | assert( useconds < 1350000); |
---|
182 | |
---|
183 | /* print the current real time again */ |
---|
184 | status = clock_gettime( CLOCK_REALTIME, &tv ); |
---|
185 | assert( !status ); |
---|
186 | printf( ctime( &tv.tv_sec ) ); |
---|
187 | |
---|
188 | /* get id of this thread */ |
---|
189 | |
---|
190 | Init_id = pthread_self(); |
---|
191 | printf( "Init: ID is 0x%08x\n", Init_id ); |
---|
192 | |
---|
193 | /* exercise get minimum priority */ |
---|
194 | |
---|
195 | priority = sched_get_priority_min( SCHED_FIFO ); |
---|
196 | printf( "Init: sched_get_priority_min (SCHED_FIFO) -- %d\n", priority ); |
---|
197 | assert( priority != -1 ); |
---|
198 | |
---|
199 | puts( "Init: sched_get_priority_min -- EINVAL (invalid policy)" ); |
---|
200 | priority = sched_get_priority_min( -1 ); |
---|
201 | assert( priority == -1 ); |
---|
202 | assert( errno == EINVAL ); |
---|
203 | |
---|
204 | /* exercise get maximum priority */ |
---|
205 | |
---|
206 | priority = sched_get_priority_max( SCHED_FIFO ); |
---|
207 | printf( "Init: sched_get_priority_max (SCHED_FIFO) -- %d\n", priority ); |
---|
208 | assert( priority != -1 ); |
---|
209 | |
---|
210 | puts( "Init: sched_get_priority_max -- EINVAL (invalid policy)" ); |
---|
211 | priority = sched_get_priority_max( -1 ); |
---|
212 | assert( priority == -1 ); |
---|
213 | assert( errno == EINVAL ); |
---|
214 | |
---|
215 | puts( "Init: sched_rr_get_interval -- ESRCH (invalid PID)" ); |
---|
216 | status = sched_rr_get_interval( 4, &tr ); |
---|
217 | assert( status == -1 ); |
---|
218 | assert( errno == ESRCH ); |
---|
219 | |
---|
220 | puts( "Init: sched_rr_get_interval -- EINVAL (invalid interval pointer)" ); |
---|
221 | status = sched_rr_get_interval( getpid(), NULL ); |
---|
222 | assert( status == -1 ); |
---|
223 | assert( errno == EINVAL ); |
---|
224 | |
---|
225 | /* print the round robin time quantum */ |
---|
226 | |
---|
227 | status = sched_rr_get_interval( getpid(), &tr ); |
---|
228 | printf( |
---|
229 | "Init: Round Robin quantum is %ld seconds, %ld nanoseconds\n", |
---|
230 | tr.tv_sec, |
---|
231 | tr.tv_nsec |
---|
232 | ); |
---|
233 | assert( !status ); |
---|
234 | |
---|
235 | /* create a thread */ |
---|
236 | |
---|
237 | puts( "Init: pthread_create - SUCCESSFUL" ); |
---|
238 | status = pthread_create( &thread_id, NULL, Task_1_through_3, NULL ); |
---|
239 | assert( !status ); |
---|
240 | |
---|
241 | /* too may threads error */ |
---|
242 | |
---|
243 | puts( "Init: pthread_create - EAGAIN (too many threads)" ); |
---|
244 | status = pthread_create( &thread_id, NULL, Task_1_through_3, NULL ); |
---|
245 | assert( status == EAGAIN ); |
---|
246 | |
---|
247 | puts( "Init: sched_yield to Task_1" ); |
---|
248 | status = sched_yield(); |
---|
249 | assert( !status ); |
---|
250 | |
---|
251 | /* switch to Task_1 */ |
---|
252 | |
---|
253 | /* exit this thread */ |
---|
254 | |
---|
255 | puts( "Init: pthread_exit" ); |
---|
256 | pthread_exit( NULL ); |
---|
257 | |
---|
258 | /* switch to Task_1 */ |
---|
259 | |
---|
260 | return NULL; /* just so the compiler thinks we returned something */ |
---|
261 | } |
---|