#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <semaphore.h>
#include <fcntl.h>
#include <wait.h>
int main(){
sem_t *sem = NULL;
pid_t pid;
pid = fork();
setbuf(stdout,NULL);
char msg[] = "This is the race condition test\n";
char* ptr;
int c;
if (pid == 0){
sem = sem_open("sem1", O_CREAT|O_RDWR, 0666, 1);
for(int i=0; i < 10;i++)
{
sem_wait(sem);
for(ptr = msg; (c = *ptr)!= 0; ptr++)
putc(c,stdout);
sem_post(sem);
}
sem_close(sem);
}
else {
sem = sem_open("sem1", O_CREAT|O_RDWR, 0666, 1);
for(int i=0; i < 10;i++)
{
sem_wait(sem);
for(ptr = msg; (c = *ptr)!= 0; ptr++)
putc(c,stdout);
sem_post(sem);
}
sem_close(sem);
wait(NULL);
}
sem_unlink("sem1");
return 0;
}