Semaphore issues...example program included
Hi all,
I've got a simple example C program that uses semaphores to make 2 processes (via fork) just toggle a counter...
So the "parent" will obtain a semaphore, display its value, the release, and the child will do the same.
The issue I am having is that sometimes the parent or child will do something 2 times and not wait!
Any thoughts would be greatly appriciated!
~Kam
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~`
// This file is a semaphore example
#include <sys/wait.h>
#include <sys/types.h> // pid_t
#include <sys/sem.h>
#include <unistd.h>
#include <stdio.h> // printf
#include <errno.h>
#include <stdlib.h>
#define MY_SEM_ID 111
#define SEM_COUNT 1
int start()
{
pid_t ret;
int x;
int status;
int semID;
int csemID;
struct sembuf SB;
struct sembuf cSB;
ret=fork();
// lets see if we're the parent
if(ret>0) //PARENT
{
printf("Parent: My PID is %d\n", getpid());
// lets get the semaphore
semID=semget(MY_SEM_ID, 1, 0);
if(semID>=0)
{
for(x=0; x<10;x++) // lets do this 10 times!
{
// lets update the semaphore settings
SB.sem_num=0;
SB.sem_op=-1;
SB.sem_flg=0;
// lets now aquire the semaphore
if(semop(semID, &SB, 1)!=-1)
{
printf("Parent: count [%d]\n", x);
usleep(rand()%2000);
// lets release it now
// lets update the semaphore settings
SB.sem_num=0;
SB.sem_op=1;
SB.sem_flg=0;
if(semop(semID, &SB, 1)==-1)
{
printf("Parent: Error releasing semaphore!\n");
}
}
else
{
printf("Parent: Could not aquire semaphore!\n");
}
}
}
// lets wait for the child to die!
printf("Parent: Waiting for child to die...\n");
// if we don't issue the wait, the parent will exit, but the child will still continue!
ret=wait(&status);
printf("Parent: All done, exiting!\n");
}
else if(ret==0) // CHILD
{
printf("Child: My PID is %d\n", getpid());
// lets get the semaphore
csemID=semget(MY_SEM_ID, 1, 0);
if(csemID>=0)
{
for(x=0; x<10;x++) // lets do this 10 times!
{
// lets update the semaphore settings
cSB.sem_num=0;
cSB.sem_op=-1;
cSB.sem_flg=0;
// lets now aquire the semaphore
if(semop(csemID, &cSB, 1)!=-1)
{
printf("Child: count [%d]\n", x);
usleep(rand()%1000);
// lets release it now
// lets update the semaphore settings
cSB.sem_num=0;
cSB.sem_op=1;
cSB.sem_flg=0;
if(semop(csemID, &cSB, 1)==-1)
{
printf("child: Error releasing semaphore!\n");
}
}
else
{
printf("Child: Could not aquire semaphore!\n");
}
}
}
printf("Child: All done, exiting!\n");
}
else
{
printf("Parent: ERROR, could not fork! Error code is %d\n", errno);
}
return 0;
}
int main()
{
int semID;
int ret;
printf("\nSemaphore Example-1\n\n");
// if we already have this semaphore, lets remove it
semID=semget(MY_SEM_ID, 1, 0);
if(semID>=0)
{
printf("Semaphore found, removing it...\n");
ret=semctl(MY_SEM_ID, 0, IPC_RMID);
if(ret!=-1)
{
printf("Semaphore successfully removed!\n");
}
}
// lets create the semaphore
semID=semget(MY_SEM_ID, SEM_COUNT, 0666 | IPC_CREAT);
if(semID!=-1)
{
printf("Created a semaphore with ID [%d]\n", semID);
}
else
{
printf("Error creating semaphore...\n");
}
printf("\n\n");
start();
printf("\n\nAll done with test, exiting...\n\n\n");
return 0;
}
|