Annotation of prex/usr/test/sem/sem.c, Revision 1.1
1.1 ! nbrk 1: /*-
! 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 <prex/prex.h>
! 35: #include <stdio.h>
! 36:
! 37: int
! 38: main(int argc, char *argv[])
! 39: {
! 40: sem_t sem;
! 41: int err;
! 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: err = sem_getvalue(&sem, &val);
! 55: printf("Semaphore value=%d\n", val);
! 56:
! 57: printf("1) Wait semahore\n");
! 58: err = sem_wait(&sem, 0);
! 59: if (err)
! 60: panic("wait err");
! 61:
! 62: err = sem_getvalue(&sem, &val);
! 63: printf("Semaphore value=%d\n", val);
! 64:
! 65: printf("2) Wait semahore\n");
! 66: err = sem_wait(&sem, 0);
! 67: if (err)
! 68: panic("wait err");
! 69:
! 70: err = sem_getvalue(&sem, &val);
! 71: printf("Semaphore value=%d\n", val);
! 72:
! 73: printf("3) Wait semahore\n");
! 74: err = sem_wait(&sem, 0);
! 75: if (err)
! 76: panic("wait err");
! 77:
! 78: err = 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: err = sem_wait(&sem, 0);
! 87: if (err)
! 88: panic("wait err");
! 89: #endif
! 90: /*
! 91: * trywait must be error.
! 92: */
! 93: printf("4e) Try to wait semahore\n");
! 94: err = sem_trywait(&sem);
! 95: printf("Try wait err=%d\n", err);
! 96:
! 97: /*
! 98: * Test post
! 99: */
! 100: printf("5) Post semahore\n");
! 101: err = sem_post(&sem);
! 102: if (err)
! 103: panic("post err");
! 104: err = sem_getvalue(&sem, &val);
! 105: printf("Semaphore value=%d\n", val);
! 106:
! 107: printf("Test complete\n");
! 108: return 0;
! 109: }
CVSweb