Home | History | Annotate | Download | only in sem_post
      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_post() can be entered into again if interrupted by a signal
     11  * --> This is tough to do with 100% guarantees.  This test does a
     12  * best attempt.
     13  *
     14  * 1) Set up a signal catching function for SIGALRM.
     15  * 2) Set up a semaphore
     16  * 3) Call sleep(1) and then alarm(1)
     17  * 4) Call sempost() immediately after (thinking that alarm(1) will interrupt
     18  * 5) If sempost() succeeds (verified if val is incremented by 1),
     19  *    test passes.  Otherwise, failure.
     20  */
     21 
     22 #include <stdio.h>
     23 #include <unistd.h>
     24 #include <semaphore.h>
     25 #include <fcntl.h>
     26 #include <signal.h>
     27 #include <stdlib.h>
     28 #include "posixtest.h"
     29 
     30 #define TEST "5-1"
     31 #define FUNCTION "sem_post"
     32 #define ERROR_PREFIX "unexpected error: " FUNCTION " " TEST ": "
     33 
     34 #define SEMINITVAL 0		//initial value of semaphore = 0
     35 
     36 sem_t *gsemp;
     37 
     38 void sighdl(int sig)
     39 {
     40 	return;
     41 }
     42 
     43 int main(void)
     44 {
     45 	char semname[28];
     46 	int val;
     47 
     48 	sprintf(semname, "/" FUNCTION "_" TEST "_%d", getpid());
     49 
     50 	gsemp = sem_open(semname, O_CREAT, 0777, SEMINITVAL);
     51 	if (gsemp == SEM_FAILED || gsemp == NULL) {
     52 		perror(ERROR_PREFIX "sem_open");
     53 		return PTS_UNRESOLVED;
     54 	}
     55 	signal(SIGALRM, sighdl);
     56 	sleep(1);
     57 	alarm(1);
     58 
     59 	if (sem_post(gsemp) == -1) {
     60 		perror(ERROR_PREFIX "sem_post");
     61 		exit(PTS_UNRESOLVED);
     62 	}
     63 
     64 	/* Checking if the value of the Semaphore incremented by one */
     65 	if (sem_getvalue(gsemp, &val) == -1) {
     66 		perror(ERROR_PREFIX "sem_getvalue");
     67 		return PTS_UNRESOLVED;
     68 	}
     69 	if (val != SEMINITVAL + 1) {
     70 #ifdef DEBUG
     71 		printf("semaphore value was not incremented\n");
     72 #endif
     73 		printf("TEST FAILED\n");
     74 		return PTS_FAIL;
     75 	}
     76 #ifdef DEBUG
     77 	printf("semaphore value was %d\n", val);
     78 #endif
     79 
     80 	printf("TEST PASSED\n");
     81 	sem_close(gsemp);
     82 	sem_unlink(semname);
     83 	return PTS_PASS;
     84 }
     85