Review your favorite Linux distribution.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 11-28-2009, 04:25 AM   #1
LQ Newbie
Registered: Nov 2009
Posts: 1

Rep: Reputation: 0
Forking 4 writers for semaphore/shared memory ?Help? please.

I'm currently working on a program where you have to make two separate programs. One is a reader, the other is a writer and 2 semaphores are used, created in the reader. The writer changes some things in shared memory and the reader reads it and prints it out. I have it working correctly with one writer process. The problem is the writer program is supposed to create 4 writers. I'm stuck on how to do this.

There is supposed to be a loop in both the reader and writer and both will end when shared memory reaches 0. I'm assuming only the loop in the writer should be forked.

Here is my forked attempt in the writer area..

for (int p_count = 1; p_count <= 4; p_count++)
{ if (-1 == (pid1 = fork()))
{ perror ("error in fork");
exit (1);

while(val >=0)

////sem wait

if (semop(semid, &semb, 1) == -1)

cout<<"writer "<<semctl(semid, semb.sem_num, GETVAL)
<<" semaphores in C.S.: 0 0"<<endl;

*shm = val;

////sem signal
semb.sem_op = 1;
semb.sem_num = 1;
if (semop(semid, &semb, 1) == -1)


Old 11-29-2009, 07:21 AM   #2
Registered: Jun 2008
Location: Dallas, TX
Distribution: Slackware and FC
Posts: 107

Rep: Reputation: 20
If I have 2 coins that total 30 cents and 1 is not a quarter what are they?


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
Forking and Shared Memory program. the_satsuma_man Programming 5 03-10-2009 06:51 PM
destruction of a shared semaphore gcote Programming 2 02-07-2007 12:13 PM
Hi semaphore and shared memory rammu_sivraj Linux - Newbie 4 06-29-2006 11:49 AM
shared memory/semaphore problem V_Ganesh Programming 1 03-31-2005 10:58 PM
How to change shared memory and semaphore config on RHL9? ryanw Linux - Software 1 01-18-2005 10:14 AM

All times are GMT -5. The time now is 09:02 PM.

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