Unix Programming - Shared memory and semaphore synchonization

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > April 2004 > Shared memory and semaphore synchonization





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Shared memory and semaphore synchonization
Doug Ly

2004-04-22, 11:36 am

Hi,
I'm currently working on a school assignment.
We are to simulate checkout lanes at a store.
There are 4 checkout lanes with 4 clerks. When customers arrive, they select
the first available (idle) clerk or if there is no clerk idle, they select
the lane with the least customer waiting and stand in lane waiting for the
clerk.
So, look like I need to have a shared memory area to hold the number of
customers waiting on a lane. To synchronize the shared memory, I use a
semaphore MUTEX. Also, for each lane, I will need a semaphore to control the
clerk (thus, there are 4 semaphores).
For each shared mem. slot corresponding to the lane, I use a struct
{ int num_waiting_customers;
int clerk_status;
}
to store number of waiting customer and clerk status of that lane.
the input format is : x1 x2
x1: number of seconds elapsed since the previous customer arrives
x2: number of seconds the customer needs.
ex: 0 12
5 26
12 75

My code is follow:

while ( scanf("%d %d", &t1, &t2) == 2 )
{
sleep(x1);
if ( fork() == 0 )
{
customer(x2);
_exit(0);
}
}

void customer(int time)
{
P(&MUTEX);
// select the first idle clerk or
// select the shortest lane
// increase the number of waiting cust. in that lane
V(&MUTEX);
P( &LANE_NO); // get clerk
P(&MUTEX);
// decrease number of waiting cust. in that lane.
// set clerk status to BUSY
V(&MUTEX);
sleep(time); // time needed
P(&MUTEX);
// set clerk status to FREE when done
V(&MUTEX);
P(&LANE_NO); // free clerk
}

please advice me if this sync. is correct .
Thanks a lot.

Doug



Jem Berkes

2004-04-22, 12:36 pm

> please advice me if this sync. is correct .
> Thanks a lot.


Many people don't consider this kind of thing appropriate for newsgroups
such as this one. "Here's my attempt at homework -- check it over for me".
Barry Margolin

2004-04-22, 1:37 pm

In article <Xns94D372AF7C880jbuserspc9org@130.179.16.24>,
Jem Berkes <jb@users.pc9.org> wrote:

>
> Many people don't consider this kind of thing appropriate for newsgroups
> such as this one. "Here's my attempt at homework -- check it over for me".


Actually, what we don't like is when someone doesn't ask specific
questions, or asks us to just write the code for them. Even worse is
when they don't admit that it's an assignment, as if we can't recognize
them on sight.

But if he's written code and just wants some advice, most of us are
comfortable with it. It shows that he's made an effort.

--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
Doug Ly

2004-04-23, 9:34 am

Well, I checked it yesterday and found a deadlock. The test file is:

0 4
2 4
4 15
3 5
2 4
1 6

"Doug Ly" <dmly@sbcglobal.net> wrote in message
news:cwRhc.19877$bv1.8695@newssvr22.news.prodigy.com...
> Hi,
> I'm currently working on a school assignment.
> We are to simulate checkout lanes at a store.
> There are 4 checkout lanes with 4 clerks. When customers arrive, they

select
> the first available (idle) clerk or if there is no clerk idle, they select
> the lane with the least customer waiting and stand in lane waiting for the
> clerk.
> So, look like I need to have a shared memory area to hold the number of
> customers waiting on a lane. To synchronize the shared memory, I use a
> semaphore MUTEX. Also, for each lane, I will need a semaphore to control

the
> clerk (thus, there are 4 semaphores).
> For each shared mem. slot corresponding to the lane, I use a struct
> { int num_waiting_customers;
> int clerk_status;
> }
> to store number of waiting customer and clerk status of that lane.
> the input format is : x1 x2
> x1: number of seconds elapsed since the previous customer arrives
> x2: number of seconds the customer needs.
> ex: 0 12
> 5 26
> 12 75
>
> My code is follow:
>
> while ( scanf("%d %d", &t1, &t2) == 2 )
> {
> sleep(x1);
> if ( fork() == 0 )
> {
> customer(x2);
> _exit(0);
> }
> }
>
> void customer(int time)
> {
> P(&MUTEX);
> // select the first idle clerk or
> // select the shortest lane
> // increase the number of waiting cust. in that lane
> V(&MUTEX);
> P( &LANE_NO); // get clerk
> P(&MUTEX);
> // decrease number of waiting cust. in that lane.
> // set clerk status to BUSY
> V(&MUTEX);
> sleep(time); // time needed
> P(&MUTEX);
> // set clerk status to FREE when done
> V(&MUTEX);
> P(&LANE_NO); // free clerk
> }
>
> please advice me if this sync. is correct .
> Thanks a lot.
>
> Doug
>
>
>



Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com