Skip to content

Commit

Permalink
posix/conformance/interfaces/sem_timedwait/2-1: Fix test
Browse files Browse the repository at this point in the history
Since the parent and child processes are not operating on the
same semaphore, this code wasn't doing its job correctly before,
so we mapped the semaphore to a piece of shared memory and
changed some implementation details in the original code to make it work.

Signed-off-by: Chen Haonan <[email protected]>
Reviewed-by: Cyril Hrubis <[email protected]>
  • Loading branch information
Chen Haonan authored and metan-ucw committed Aug 28, 2024
1 parent 6943423 commit 66300d0
Showing 1 changed file with 17 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <fcntl.h>
#include <signal.h>
#include <time.h>
#include <sys/mman.h>
#include "posixtest.h"

#define TEST "2-1"
Expand All @@ -28,12 +29,17 @@

int main(void)
{
sem_t mysemp;
sem_t *mysemp;
struct timespec ts;
int pid;
mysemp = mmap(NULL, sizeof(*mysemp), PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0);
if (mysemp == MAP_FAILED) {
perror(ERROR_PREFIX "mmap");
return PTS_UNRESOLVED;
}

/* Semaphore started out locked */
if (sem_init(&mysemp, 0, 0) == -1) {
if (sem_init(mysemp, 1, 0) == -1) {
perror(ERROR_PREFIX "sem_init");
return PTS_UNRESOLVED;
}
Expand All @@ -44,19 +50,19 @@ int main(void)
ts.tv_sec = time(NULL) + 2;
ts.tv_nsec = 0;

if (sem_timedwait(&mysemp, &ts) == -1) {
if (sem_timedwait(mysemp, &ts) == -1) {
puts("TEST FAILED");
return PTS_FAIL;
} else {
puts("TEST PASSED");
sem_destroy(&mysemp);
sem_destroy(mysemp);
return PTS_PASS;
}
} else if (pid > 0) // parent to unlock semaphore
{
int i;
sleep(1);
if (sem_post(&mysemp) == -1) {
if (sem_post(mysemp) == -1) {
perror(ERROR_PREFIX "sem_post");
return PTS_FAIL;
}
Expand All @@ -65,11 +71,15 @@ int main(void)
return PTS_UNRESOLVED;
}

if (!WEXITSTATUS(i)) {
if (!WIFEXITED(i) || WEXITSTATUS(i)) {
return PTS_FAIL;
}
puts("TEST PASSED");
sem_destroy(&mysemp);
sem_destroy(mysemp);
if (munmap(mysemp, sizeof(*mysemp)) == -1) {
perror(ERROR_PREFIX "munmap");
return PTS_UNRESOLVED;
}
return PTS_PASS;
}
return PTS_UNRESOLVED;
Expand Down

0 comments on commit 66300d0

Please sign in to comment.