Home | History | Annotate | Download | only in sem_wait
      1 /*
      2  * Copyright (c) 2003, Intel Corporation. All rights reserved.
      3  * Created by:  majid.awad REMOVE-THIS AT intel DOT com
      4  * This file is licensed under the GPL license.  For the full content
      5  * of this license, see the COPYING file at the top level of this
      6  * source tree.
      7  */
      8 
      9 /*
     10  * sem_trywait shall try to lock the unlocked semaphore and decrement
     11  * the semaphore value by one.
     12  */
     13 
     14 #include <stdio.h>
     15 #include <errno.h>
     16 #include <unistd.h>
     17 #include <semaphore.h>
     18 #include <sys/stat.h>
     19 #include <fcntl.h>
     20 #include "posixtest.h"
     21 
     22 #define TEST "11-1"
     23 #define FUNCTION "sem_trywait"
     24 #define ERROR_PREFIX "unexpected error: " FUNCTION " " TEST ": "
     25 
     26 int main(void)
     27 {
     28 	sem_t *mysemp;
     29 	char semname[28];
     30 	int val;
     31 
     32 	sprintf(semname, "/" FUNCTION "_" TEST "_%d", getpid());
     33 
     34 	/* Initial value of Semaphore is 1 */
     35 	mysemp = sem_open(semname, O_CREAT, 0777, 1);
     36 	if (mysemp == SEM_FAILED || mysemp == NULL) {
     37 		perror(ERROR_PREFIX "sem_open");
     38 		return PTS_UNRESOLVED;
     39 	}
     40 
     41 	/* Lock Semaphore by sem_trywait */
     42 	if (sem_trywait(mysemp) == -1) {
     43 		perror(ERROR_PREFIX "sem_wait");
     44 		return PTS_UNRESOLVED;
     45 	}
     46 
     47 	/* Value of Semaphore */
     48 	if (sem_getvalue(mysemp, &val) == -1) {
     49 		perror(ERROR_PREFIX "sem_getvalue");
     50 		return PTS_UNRESOLVED;
     51 
     52 		/* Checking if the value of the Semaphore decremented by one */
     53 	} else if (val == 0) {
     54 		puts("TEST PASSED");
     55 		sem_unlink(semname);
     56 		sem_close(mysemp);
     57 		return PTS_PASS;
     58 	} else {
     59 		puts("TEST FAILED");
     60 		return PTS_FAIL;
     61 	}
     62 }
     63