root / prex-0.9.0 / usr / test / sem / sem.c @ 03e9c04a
History | View | Annotate | Download (2.85 KB)
1 | 03e9c04a | Brad Neuman | /*-
|
---|---|---|---|
2 | * Copyright (c) 2005, Kohsuke Ohtani
|
||
3 | * All rights reserved.
|
||
4 | *
|
||
5 | * Redistribution and use in source and binary forms, with or without
|
||
6 | * modification, are permitted provided that the following conditions
|
||
7 | * are met:
|
||
8 | * 1. Redistributions of source code must retain the above copyright
|
||
9 | * notice, this list of conditions and the following disclaimer.
|
||
10 | * 2. Redistributions in binary form must reproduce the above copyright
|
||
11 | * notice, this list of conditions and the following disclaimer in the
|
||
12 | * documentation and/or other materials provided with the distribution.
|
||
13 | * 3. Neither the name of the author nor the names of any co-contributors
|
||
14 | * may be used to endorse or promote products derived from this software
|
||
15 | * without specific prior written permission.
|
||
16 | *
|
||
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
||
18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||
19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||
20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
||
21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||
22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||
23 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||
24 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||
25 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||
26 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||
27 | * SUCH DAMAGE.
|
||
28 | */
|
||
29 | |||
30 | /*
|
||
31 | * sem.c - test program for semaphore.
|
||
32 | */
|
||
33 | |||
34 | #include <sys/prex.h> |
||
35 | #include <stdio.h> |
||
36 | |||
37 | int
|
||
38 | main(int argc, char *argv[]) |
||
39 | { |
||
40 | sem_t sem; |
||
41 | int error;
|
||
42 | u_int val; |
||
43 | |||
44 | printf("Semaphore test program\n");
|
||
45 | |||
46 | /*
|
||
47 | * Test initialize
|
||
48 | */
|
||
49 | sem_init(&sem, 3);
|
||
50 | |||
51 | /*
|
||
52 | * Test wait
|
||
53 | */
|
||
54 | error = sem_getvalue(&sem, &val); |
||
55 | printf("Semaphore value=%d\n", val);
|
||
56 | |||
57 | printf("1) Wait semahore\n");
|
||
58 | error = sem_wait(&sem, 0);
|
||
59 | if (error)
|
||
60 | panic("wait error");
|
||
61 | |||
62 | error = sem_getvalue(&sem, &val); |
||
63 | printf("Semaphore value=%d\n", val);
|
||
64 | |||
65 | printf("2) Wait semahore\n");
|
||
66 | error = sem_wait(&sem, 0);
|
||
67 | if (error)
|
||
68 | panic("wait error");
|
||
69 | |||
70 | error = sem_getvalue(&sem, &val); |
||
71 | printf("Semaphore value=%d\n", val);
|
||
72 | |||
73 | printf("3) Wait semahore\n");
|
||
74 | error = sem_wait(&sem, 0);
|
||
75 | if (error)
|
||
76 | panic("wait error");
|
||
77 | |||
78 | error = sem_getvalue(&sem, &val); |
||
79 | printf("Semaphore value=%d\n", val);
|
||
80 | |||
81 | #if 0
|
||
82 | /*
|
||
83 | * Sleep by wait
|
||
84 | */
|
||
85 | printf("4) Wait semahore\n");
|
||
86 | error = sem_wait(&sem, 0);
|
||
87 | if (error)
|
||
88 | panic("wait error");
|
||
89 | #endif
|
||
90 | /*
|
||
91 | * trywait must be error.
|
||
92 | */
|
||
93 | printf("4e) Try to wait semahore\n");
|
||
94 | error = sem_trywait(&sem); |
||
95 | printf("Try wait error=%d\n", error);
|
||
96 | |||
97 | /*
|
||
98 | * Test post
|
||
99 | */
|
||
100 | printf("5) Post semahore\n");
|
||
101 | error = sem_post(&sem); |
||
102 | if (error)
|
||
103 | panic("post error");
|
||
104 | error = sem_getvalue(&sem, &val); |
||
105 | printf("Semaphore value=%d\n", val);
|
||
106 | |||
107 | printf("Test complete\n");
|
||
108 | return 0; |
||
109 | } |