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 * errno return EAGAIN: The semaphore can't be immediately locked by 11 * sem_trywait when its already locked. 12 */ 13 14 15#include <stdio.h> 16#include <errno.h> 17#include <unistd.h> 18#include <semaphore.h> 19#include <sys/stat.h> 20#include <fcntl.h> 21#include "posixtest.h" 22 23 24#define TEST "5-1" 25#define FUNCTION "sem_trywait" 26#define ERROR_PREFIX "unexpected errno: " FUNCTION " " TEST ": " 27 28 29 30int main() { 31 sem_t *mysemp; 32 char semname[50]; 33 34 sprintf(semname, "/" FUNCTION "_" TEST "_%ld", (long)getpid()); 35 36 /* Initial value of Semaphore is 0 Locked */ 37 mysemp = sem_open(semname, O_CREAT, 0777, 0); 38 if( mysemp == SEM_FAILED || mysemp == NULL ) { 39 perror(ERROR_PREFIX "sem_open"); 40 return PTS_UNRESOLVED; 41 } 42 43 /* Lock Semaphore by sem_trywait*/ 44 if (( sem_trywait(mysemp) == -1) && ( errno == EAGAIN) ) { 45 puts("TEST PASSED"); 46 sem_close(mysemp); 47 sem_unlink(semname); 48 return PTS_PASS; 49 } else { 50 puts("TEST FAILED: ERROR IS NOT EAGAIN"); 51 return PTS_FAIL; 52 } 53} 54 55 56