LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   semaphores and shared memory (https://www.linuxquestions.org/questions/programming-9/semaphores-and-shared-memory-503842/)

beata86 11-22-2006 03:53 AM

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);
}
}
}

dmail 11-22-2006 05:24 AM

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);
                }
        }
}



All times are GMT -5. The time now is 06:15 PM.