LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
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 11-22-2006, 03:53 AM   #1
beata86
LQ Newbie
 
Registered: Sep 2006
Posts: 1

Rep: Reputation: 0
Unhappy semaphores and shared memory


I have a problem with shared memory synchronization. I have 3 processes, A B and C, and 2 shared memory zones. Process A read from both memory zones, process B write a number in the first memory zone, process C write a number in the second memory zone.
This process is repeated 10-15 times.
I used 2 binary semaphores, for synchronization. Process B and C write the numbers in the shared memory zones 15 times, but process A reads only the last two numbers 15 times. I suppose the problem is that process C and B doesn’t wait for process A to finished.
The part of code which realize this is the following:
// i use an another file, caled "semb.c", where i define a binary semaphore, with its operations
//for the semaphore i include <sys/sem.h>
//semid1-id of the first semaphore
//semid2-id of the second semaphore
//P-lock operation
//V-unlock operation
int rc=0; //reader count
if (fork()>0)
{
//process A

for (i=0;i<15;i++)
{
rc=0;
P(semid2);
rc++;
if (rc==1)
{
P(semid1);
}
V(semid2);
printf("the numbers are: %d %d .\n",ad1->nr,ad2->nr);
P(semid2);
rc=rc-1;
if (rc==0)
{
V(semid1);
}
V(semid2);
}
}
else
{
if (fork()>0)
{
//process B
for (i=0;i<15;i++)
{
P(semid1);
ad1->nr=rand()%30;//ad1 second shared memory zone
printf("B write: %d \n",ad1->nr);
V(semid1);
}
}
else
{
//process C
for (i=0;i<15;i++)
{
P(semid1);
ad2->nr=rand()%60;//ad2 second shared memory zone
printf("C write: %d \n",ad2->nr);
V(semid1);
}
}
}

Last edited by beata86; 11-23-2006 at 04:39 AM.
 
Old 11-22-2006, 05:24 AM   #2
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
Hello beata86,
Please use code tags it keeps indentations, I've reposted your source for you.
Which threading library are you using? Are P and V blocking operations.
Code:
int rc=0; //reader count
if (fork()>0)
{
//process A

	for (i=0;i<15;i++)
	{
		rc=0;
		P(semid2);
		rc++;
		if (rc==1)
		{
			P(semid1);
		}
		V(semid2);
		printf("the numbers are: %d %d .\n",ad1->nr,ad2->nr);
		P(semid2);
		rc=rc-1;
		if (rc==0)
		{
			V(semid1);
		}
		V(semid2);
	}
}
else
{
	if (fork()>0)
	{
		//process B
		for (i=0;i<15;i++)
		{
			P(semid1);
			ad1->nr=rand()%30;//ad1 second shared memory zone
			printf("B write: %d \n",ad1->nr);
			V(semid1);
		}
	}
	else
	{
		//process C
		for (i=0;i<15;i++)
		{
			P(semid1);
			ad2->nr=rand()%60;//ad2 second shared memory zone
			printf("C write: %d \n",ad2->nr);
			V(semid1);
		}
	}
}
 
  


Reply



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
shared memory & semaphores black_man Programming 0 09-22-2006 01:10 AM
is shared memory expandable in memory size? Thinking Programming 4 08-16-2005 09:57 AM
Shared Memory/ Semaphores wildem Programming 3 06-20-2005 01:13 PM
Posix file based process shared semaphores dragonman Programming 9 05-25-2005 04:44 PM
shared memory and semaphores... socket9001 Programming 3 02-16-2004 11:18 AM

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

All times are GMT -5. The time now is 08:04 AM.

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