]>
git.saurik.com Git - apple/xnu.git/blob - tools/tests/xnu_quick_test/sema_tests.c
5 * Created by Jerry Cottingham on 6/2/2005.
6 * Copyright 2005 Apple Computer Inc. All rights reserved.
12 #include <semaphore.h>
14 /* **************************************************************************************************************
15 * Test semctl, semget, semop system calls.
16 * **************************************************************************************************************
18 int sema_tests( void * the_argp
)
22 union semun my_sem_union
;
23 struct sembuf my_sembuf
;
25 srand( (unsigned int)getpid() );
26 my_sem_id
= semget( (key_t
)1234, 1, (0666 | IPC_CREAT
) );
27 if ( my_sem_id
== -1 ) {
28 printf( "semget failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
29 goto test_failed_exit
;
33 my_err
= semctl( my_sem_id
, 0, SETVAL
, my_sem_union
);
34 if ( my_sem_id
== -1 ) {
35 printf( "semget failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
36 goto test_failed_exit
;
39 /* verify semop failure for bad nsop values */
40 my_err
= semop( my_sem_id
, &my_sembuf
, 10000);
41 if (my_err
!= -1 || errno
!= E2BIG
) {
42 printf( "semop did not fail with E2BIG - instead %d - \"%s\" \n", errno
, strerror( errno
) );
43 goto test_failed_exit
;
47 for ( i
= 0; i
< 10000; i
++ ) {
48 my_sembuf
.sem_num
= 0;
49 my_sembuf
.sem_op
= -1;
50 my_sembuf
.sem_flg
= SEM_UNDO
;
52 my_err
= semop( my_sem_id
, &my_sembuf
, 1 );
54 printf( "semop failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
55 goto test_failed_exit
;
58 my_err
= semctl( my_sem_id
, 0, GETVAL
, 0 );
60 printf( "semctl failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
61 goto test_failed_exit
;
64 printf( "semctl(getval) returned %d. it should be 0 (locked) here \n", my_err
);
65 goto test_failed_exit
;
68 my_sembuf
.sem_num
= 0;
70 my_sembuf
.sem_flg
= SEM_UNDO
;
72 my_err
= semop( my_sem_id
, &my_sembuf
, 1 );
74 printf( "semop failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
75 goto test_failed_exit
;
79 my_err
= semctl( my_sem_id
, 0, IPC_RMID
, my_sem_union
);
81 printf( "semctl (IPC_RMID) failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
82 goto test_failed_exit
;
86 goto test_passed_exit
;
92 if ( my_sem_id
!= -1 ) {
93 semctl( my_sem_id
, 0, IPC_RMID
, my_sem_union
);
99 /* **************************************************************************************************************
100 * Test sem_close, sem_open, sem_post, sem_trywait, sem_unlink, sem_wait system calls.
101 * **************************************************************************************************************
103 int sema2_tests( void * the_argp
)
106 sem_t
* my_sem_t
= (sem_t
*)SEM_FAILED
;
107 char my_sema_name
[ 64 ];
109 /* get a semaphore (initialized as locked) */
110 sprintf( &my_sema_name
[0], "sema_testing_%d", getpid( ) );
111 my_sem_t
= sem_open( &my_sema_name
[0], (O_CREAT
| O_EXCL
), (S_IRUSR
| S_IWUSR
), 0 );
112 if ( my_sem_t
== (sem_t
*)SEM_FAILED
) {
113 printf( "sem_open failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
114 goto test_failed_exit
;
117 /* get the lock - should return EAGAIN (EWOULDBLOCK) */
118 my_err
= sem_trywait( my_sem_t
);
119 if ( my_err
== -1 ) {
121 if ( my_err
!= EAGAIN
) {
122 printf( "sem_trywait failed with error %d - \"%s\" \n", my_err
, strerror( my_err
) );
123 goto test_failed_exit
;
127 /* unlock our semaphore */
128 my_err
= sem_post( my_sem_t
);
129 if ( my_err
== -1 ) {
130 printf( "sem_post failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
131 goto test_failed_exit
;
134 /* get the lock again */
135 my_err
= sem_wait( my_sem_t
);
136 if ( my_err
== -1 ) {
137 printf( "sem_wait failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
138 goto test_failed_exit
;
141 my_err
= sem_unlink( &my_sema_name
[0] );
142 if ( my_err
== -1 ) {
143 printf( "sem_unlink failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
144 goto test_failed_exit
;
147 my_err
= sem_close( my_sem_t
);
148 if ( my_err
== -1 ) {
149 printf( "sem_close failed with error %d - \"%s\" \n", errno
, strerror( errno
) );
150 goto test_failed_exit
;
152 my_sem_t
= (sem_t
*)SEM_FAILED
;
155 goto test_passed_exit
;
161 if ( my_sem_t
!= (sem_t
*)SEM_FAILED
) {
162 sem_close( my_sem_t
);