The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
Sem_wait() problem
Discuss Sem_wait() problem in the C Programming forum on Dev Shed. Sem_wait() problem C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

March 11th, 2013, 02:41 AM
|
|
Registered User
|
|
Join Date: Mar 2013
Posts: 3
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?)
|

March 11th, 2013, 03:50 AM
|
 |
Contributed User
|
|
|
|
|
But if all three processes always do
sem_wait(S1);
sem_wait(S2);
What is the point of having S2 at all?
|

March 11th, 2013, 04:59 AM
|
|
Registered User
|
|
Join Date: Mar 2013
Posts: 3
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
|

March 11th, 2013, 08:29 AM
|
 |
Contributed User
|
|
|
|
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 
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|