1 | /* |
---|
2 | * COPYRIGHT (c) 1989, 1990, 1991, 1992, 1993, 1994. |
---|
3 | * On-Line Applications Research Corporation (OAR). |
---|
4 | * All rights assigned to U.S. Government, 1994. |
---|
5 | * |
---|
6 | * This material may be reproduced by or for the U.S. Government pursuant |
---|
7 | * to the copyright license under the clause at DFARS 252.227-7013. This |
---|
8 | * notice must appear in all copies of this file and its derivatives. |
---|
9 | * |
---|
10 | * $Id$ |
---|
11 | */ |
---|
12 | |
---|
13 | #define CONFIGURE_INIT |
---|
14 | #include "system.h" |
---|
15 | #include <sched.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 | |
---|
31 | puts( "\n\n*** POSIX TEST 1 ***" ); |
---|
32 | |
---|
33 | build_time( &tm, TM_FRIDAY, TM_MAY, 24, 96, 11, 5, 0 ); |
---|
34 | |
---|
35 | /* error cases in clock_gettime and clock_settime */ |
---|
36 | |
---|
37 | puts( "Init: clock_gettime - EINVAL (invalid clockid)" ); |
---|
38 | status = clock_settime( -1, &tv ); |
---|
39 | assert( status == -1 ); |
---|
40 | assert( errno == EINVAL ); |
---|
41 | |
---|
42 | puts( "Init: clock_settime - EINVAL (invalid clockid)" ); |
---|
43 | status = clock_settime( -1, &tv ); |
---|
44 | assert( status == -1 ); |
---|
45 | assert( errno == EINVAL ); |
---|
46 | |
---|
47 | /* exercise clock_getres */ |
---|
48 | |
---|
49 | puts( "Init: clock_getres - EINVAL (invalid clockid)" ); |
---|
50 | status = clock_getres( -1, &tv ); |
---|
51 | assert( status == -1 ); |
---|
52 | assert( errno == EINVAL ); |
---|
53 | |
---|
54 | puts( "Init: clock_getres - EINVAL (NULL resolution)" ); |
---|
55 | status = clock_getres( CLOCK_REALTIME, NULL ); |
---|
56 | assert( status == -1 ); |
---|
57 | assert( errno == EINVAL ); |
---|
58 | |
---|
59 | puts( "Init: clock_getres - SUCCESSFUL" ); |
---|
60 | status = clock_getres( CLOCK_REALTIME, &tv ); |
---|
61 | printf( "Init: resolution = sec (%ld), nsec (%ld)\n", tv.tv_sec, tv.tv_nsec ); |
---|
62 | assert( !status ); |
---|
63 | |
---|
64 | /* set the time of day, and print our buffer in multiple ways */ |
---|
65 | |
---|
66 | tv.tv_sec = mktime( &tm ); |
---|
67 | assert( tv.tv_sec != -1 ); |
---|
68 | |
---|
69 | tv.tv_nsec = 0; |
---|
70 | |
---|
71 | /* now set the time of day */ |
---|
72 | |
---|
73 | empty_line(); |
---|
74 | |
---|
75 | printf( asctime( &tm ) ); |
---|
76 | puts( "Init: clock_settime - SUCCESSFUL" ); |
---|
77 | status = clock_settime( CLOCK_REALTIME, &tv ); |
---|
78 | assert( !status ); |
---|
79 | |
---|
80 | printf( asctime( &tm ) ); |
---|
81 | printf( ctime( &tv.tv_sec ) ); |
---|
82 | |
---|
83 | /* use sleep to delay */ |
---|
84 | |
---|
85 | remaining = sleep( 3 ); |
---|
86 | assert( !remaining ); |
---|
87 | |
---|
88 | /* print new times to make sure it has changed and we can get the realtime */ |
---|
89 | |
---|
90 | status = clock_gettime( CLOCK_REALTIME, &tv ); |
---|
91 | assert( !status ); |
---|
92 | |
---|
93 | printf( ctime( &tv.tv_sec ) ); |
---|
94 | |
---|
95 | seconds = time( NULL ); |
---|
96 | printf( ctime( &seconds ) ); |
---|
97 | |
---|
98 | /* just to have the value copied out through the parameter */ |
---|
99 | |
---|
100 | seconds = time( &seconds1 ); |
---|
101 | assert( seconds == seconds1 ); |
---|
102 | |
---|
103 | /* check the time remaining */ |
---|
104 | |
---|
105 | printf( "Init: seconds remaining (%d)\n", (int)remaining ); |
---|
106 | assert( !remaining ); |
---|
107 | |
---|
108 | /* error cases in nanosleep */ |
---|
109 | |
---|
110 | empty_line(); |
---|
111 | puts( "Init: nanosleep - EINVAL (NULL time)" ); |
---|
112 | status = nanosleep ( NULL, &tr ); |
---|
113 | assert( status == -1 ); |
---|
114 | assert( errno == EINVAL ); |
---|
115 | |
---|
116 | tv.tv_sec = 0; |
---|
117 | tv.tv_nsec = TOD_NANOSECONDS_PER_SECOND * 2; |
---|
118 | puts( "Init: nanosleep - EINVAL (too many nanoseconds)" ); |
---|
119 | status = nanosleep ( &tv, &tr ); |
---|
120 | assert( status == -1 ); |
---|
121 | assert( errno == EINVAL ); |
---|
122 | |
---|
123 | /* this is actually a small delay or yield */ |
---|
124 | tv.tv_sec = -1; |
---|
125 | tv.tv_nsec = 0; |
---|
126 | puts( "Init: nanosleep - negative seconds small delay " ); |
---|
127 | status = nanosleep ( &tv, &tr ); |
---|
128 | assert( !status ); |
---|
129 | |
---|
130 | /* use nanosleep to yield */ |
---|
131 | |
---|
132 | tv.tv_sec = 0; |
---|
133 | tv.tv_nsec = 0; |
---|
134 | |
---|
135 | puts( "Init: nanosleep - yield" ); |
---|
136 | status = nanosleep ( &tv, &tr ); |
---|
137 | assert( !status ); |
---|
138 | assert( !tr.tv_sec ); |
---|
139 | assert( !tr.tv_nsec ); |
---|
140 | |
---|
141 | /* use nanosleep to delay */ |
---|
142 | |
---|
143 | tv.tv_sec = 3; |
---|
144 | tv.tv_nsec = 500000; |
---|
145 | |
---|
146 | puts( "Init: nanosleep - 3.05 seconds" ); |
---|
147 | status = nanosleep ( &tv, &tr ); |
---|
148 | assert( !status ); |
---|
149 | |
---|
150 | /* print the current real time again */ |
---|
151 | |
---|
152 | status = clock_gettime( CLOCK_REALTIME, &tv ); |
---|
153 | assert( !status ); |
---|
154 | |
---|
155 | printf( ctime( &tv.tv_sec ) ); |
---|
156 | |
---|
157 | /* check the time remaining */ |
---|
158 | |
---|
159 | printf( "Init: sec (%ld), nsec (%ld) remaining\n", tr.tv_sec, tr.tv_nsec ); |
---|
160 | assert( !tr.tv_sec && !tr.tv_nsec ); |
---|
161 | |
---|
162 | /* get id of this thread */ |
---|
163 | |
---|
164 | Init_id = pthread_self(); |
---|
165 | printf( "Init: ID is 0x%08x\n", Init_id ); |
---|
166 | |
---|
167 | /* exercise get minimum priority */ |
---|
168 | |
---|
169 | priority = sched_get_priority_min( SCHED_FIFO ); |
---|
170 | printf( "Init: sched_get_priority_min (SCHED_FIFO) -- %d\n", priority ); |
---|
171 | assert( priority != -1 ); |
---|
172 | |
---|
173 | puts( "Init: sched_get_priority_min -- EINVAL (invalid policy)" ); |
---|
174 | priority = sched_get_priority_min( -1 ); |
---|
175 | assert( priority == -1 ); |
---|
176 | assert( errno == EINVAL ); |
---|
177 | |
---|
178 | /* exercise get maximum priority */ |
---|
179 | |
---|
180 | priority = sched_get_priority_max( SCHED_FIFO ); |
---|
181 | printf( "Init: sched_get_priority_max (SCHED_FIFO) -- %d\n", priority ); |
---|
182 | assert( priority != -1 ); |
---|
183 | |
---|
184 | puts( "Init: sched_get_priority_min -- EINVAL (invalid policy)" ); |
---|
185 | priority = sched_get_priority_min( -1 ); |
---|
186 | assert( priority == -1 ); |
---|
187 | assert( errno == EINVAL ); |
---|
188 | |
---|
189 | /* print the round robin time quantum */ |
---|
190 | |
---|
191 | status = sched_rr_get_interval( getpid(), &tr ); |
---|
192 | printf( |
---|
193 | "Init: Round Robin quantum is %ld seconds, %ld nanoseconds\n", |
---|
194 | tr.tv_sec, |
---|
195 | tr.tv_nsec |
---|
196 | ); |
---|
197 | assert( !status ); |
---|
198 | |
---|
199 | /* create a thread */ |
---|
200 | |
---|
201 | puts( "Init: pthread_create - SUCCESSFUL" ); |
---|
202 | status = pthread_create( &thread_id, NULL, Task_1_through_3, NULL ); |
---|
203 | assert( !status ); |
---|
204 | |
---|
205 | /* too may threads error */ |
---|
206 | |
---|
207 | puts( "Init: pthread_create - EAGAIN (too many threads)" ); |
---|
208 | status = pthread_create( &thread_id, NULL, Task_1_through_3, NULL ); |
---|
209 | assert( status == EAGAIN ); |
---|
210 | |
---|
211 | puts( "Init: sched_yield to Task_1" ); |
---|
212 | status = sched_yield(); |
---|
213 | assert( !status ); |
---|
214 | |
---|
215 | /* switch to Task_1 */ |
---|
216 | |
---|
217 | /* exit this thread */ |
---|
218 | |
---|
219 | puts( "Init: pthread_exit" ); |
---|
220 | pthread_exit( NULL ); |
---|
221 | |
---|
222 | /* switch to Task_1 */ |
---|
223 | |
---|
224 | return NULL; /* just so the compiler thinks we returned something */ |
---|
225 | } |
---|