C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old March 11th, 2013, 02:41 AM
Gyong Gyong is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2013
Posts: 3 Gyong User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 37 m 2 sec
Reputation Power: 0
Sem_wait() problem

I am using semaphores for 3 processes, P1, P2, P3
that is I can have multiple processes runnning at the same time just that
P1 and P3 cannot occur at the same time with P2.

Basically I use 2 sema, S1 for P1 and P2; S2 for P3 and P2

but in P2 how do i check for P1 and P3??

is

sem_wait(S1);
sem_wait(S2);

valid?

(I know it's not, cause if P3 is on, my P2 will block the entrance of P1 while waiting for P3 at S2 which I don't wan, is there any other ways?)

Reply With Quote
  #2  
Old March 11th, 2013, 03:50 AM
salem's Avatar
salem salem is offline
Contributed User
Click here for more information
 
Join Date: Jun 2005
Posts: 3,836 salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)  Folding Points: 153 Folding Title: Novice Folder
Time spent in forums: 2 Months 3 Weeks 2 Days 16 h 15 m 5 sec
Reputation Power: 1774
But if all three processes always do
sem_wait(S1);
sem_wait(S2);

What is the point of having S2 at all?
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper

Reply With Quote
  #3  
Old March 11th, 2013, 04:59 AM
Gyong Gyong is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2013
Posts: 3 Gyong User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 37 m 2 sec
Reputation Power: 0
Quote:
Originally Posted by salem
But if all three processes always do
sem_wait(S1);
sem_wait(S2);

What is the point of having S2 at all?


I am sorry, perhaps my question is not clear enough.
I want to have 3 processes running P1, P2 and P3 but
P2 cannot run concurrently with P1 and P3.

So my solution is using 2 sema, S1 and S2:

In P1:
sem_wait(S1);
//code;

In P3:
sem_wait(S2);
//code;

In P2:
sem_wait(S1);
sem_wait(S2);
//code

I was asking for an alternative solution to this problem, cause
when I don't want my P2 to block the entrance of P1 when P2 is waiting for S2 as P1 and P3 can occur concurrently.

As in P2 can only occur when both P1 and P3 are not occuring

Reply With Quote
  #4  
Old March 11th, 2013, 08:29 AM
salem's Avatar
salem salem is offline
Contributed User
Click here for more information
 
Join Date: Jun 2005
Posts: 3,836 salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)  Folding Points: 153 Folding Title: Novice Folder
Time spent in forums: 2 Months 3 Weeks 2 Days 16 h 15 m 5 sec
Reputation Power: 1774
http://linux.die.net/man/3/sem_init
The first thing to do is initialise the semaphore with the value 2.
This will permit P1 and P3 to lock and unlock at will.

P2 on the other hand must tread a bit more carefully, since it needs to lock twice.
So it could either do
Code:
sem_wait(&sem);
sem_wait(&sem);

or perhaps
Code:
do {
    int s;
    sem_wait(&sem);
    if ( (s=sem_trywait(&sem)) == -1 && errno == EAGAIN ) {  // see also sem_timedwait
        sem_post(&sem);  // didn't get it a 2nd time, back off
        continue;
    } else if ( s == 0 ) {
        // success - break out
        break;
    } else {
        // something wrong - deal with it
    }
} while ( 1 );

If P2 is low priority compared to P1/P3, then backing off if it can't get the semaphore twice in quick succession seems like a good idea.

Don't forget to post() twice, when you're done

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Sem_wait() problem

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap