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 locked semaphore and decrement
11 * the semaphore value by one.
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 "12-1"
25#define FUNCTION "sem_trywait"
26#define ERROR_PREFIX "unexpected error: " 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 1 */
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
44	/* Try to Lock Semaphore by sem_trywait*/
45	if( sem_trywait(mysemp) == -1 ) {
46		puts("TEST PASSED");
47		sem_unlink(semname);
48		sem_close(mysemp);
49		return PTS_PASS;
50	} else {
51		puts("TEST FAILED: Semaphore locked when it shouldn't");
52		return PTS_FAIL;
53	}
54}
55
56