LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-16-2007, 05:01 PM   #1
kamputty
LQ Newbie
 
Registered: Mar 2007
Posts: 6

Rep: Reputation: 0
Smile 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;
}
 
Old 04-16-2007, 05:09 PM   #2
phil.d.g
Senior Member
 
Registered: Oct 2004
Posts: 1,272

Rep: Reputation: 154Reputation: 154
When you call fork() the memory allocated for the parent process is copied for the child. The parent and child aren't blocking on the same semaphore
 
Old 04-16-2007, 05:14 PM   #3
kamputty
LQ Newbie
 
Registered: Mar 2007
Posts: 6

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by phil.d.g
When you call fork() the memory allocated for the parent process is copied for the child. The parent and child aren't blocking on the same semaphore
Thanks for the reply...still a question (still not sure)

Within each process (parent/child), I then obtain the semaphore "stuff", not before the fork. Is this wrong?

~Kam (^8*
 
Old 04-16-2007, 06:18 PM   #4
kamputty
LQ Newbie
 
Registered: Mar 2007
Posts: 6

Original Poster
Rep: Reputation: 0
Still questions!

Okay, I looked into the fork() api, and I'm still lost if this is a fork() issue.

I changed my "start()" routine (that creates the fork) to call 2 sub-routines, the "parent()" or "child()". These routines are totally self contained and share no variables between themselves. So it still does not sync. I can make the child be faster then the parent by delaying the parent more and the child just chugs away.

Can anyone shed more light? I'm missing something!

Thanks!

~Kam (^8*
 
Old 04-16-2007, 07:06 PM   #5
kamputty
LQ Newbie
 
Registered: Mar 2007
Posts: 6

Original Poster
Rep: Reputation: 0
Smile Got it!

Hi all,

I found the issue. when I created the semaphore, I did not set the initial value to "1" to make it a binary semaphore...

Works great!

~Kam (^8*
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Suse 10.1 - Different Program Compiling issues crazibri SUSE / openSUSE 1 06-16-2006 11:59 PM
does Red Hat Enterprise have lilo support included in the install program darkhatter Red Hat 1 01-14-2006 04:03 PM
(GTK) included files in a program...Where? ooagentbender Programming 2 02-13-2005 02:00 PM
What is this system monitor program (link to screenshot included) enigma Z Linux - Software 3 09-06-2004 06:08 PM
How to compile a C program in Glade which is included the <pcap.h> header file. swaviswa Programming 0 03-21-2004 07:47 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:22 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration