Unix Programming - Question about semaphore

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > August 2006 > Question about semaphore





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 Question about semaphore
Jack

2006-08-14, 7:21 pm

Below is a simple pseudocode using semaphore:

while(1){
wait(mutex1); //LINE0

open(file_f); //LINE1
write(file_f); //LINE2

signal(mutex1);
}

Here, LINE1 and LINE2 are critical section. If one process is executing
LINE1 or LINE2, no other processes can enter the critical section. Can
other processes or users access the file file_f, such as delete of
change it?
My understanding is that the critical section is only LINE1 and LINE2,
not the file_f, so the semaphore can only control the access of the
LINE1 and LINE2, not the file file_f.

Another situation is that if another user A of the same machine write
another piece of code (different code from the above code) to access
file_f as below:

while(1){
wait(mutex2);

open(file_f); //LINE3
write(file_f); //LINE4

signal(mutex2);
}

Can mutex1 at LINE0 prevents user A from executing LINE3 and LINE4? Or
can mutex2 control the execution of LINE1 and LINE2?

Thanks a lot.

Jack

Barry Margolin

2006-08-15, 1:25 am

In article <1155582195.563843.309840@p79g2000cwp.googlegroups.com>,
"Jack" <junw2000@gmail.com> wrote:

> Below is a simple pseudocode using semaphore:
>
> while(1){
> wait(mutex1); //LINE0
>
> open(file_f); //LINE1
> write(file_f); //LINE2
>
> signal(mutex1);
> }
>
> Here, LINE1 and LINE2 are critical section. If one process is executing
> LINE1 or LINE2, no other processes can enter the critical section. Can
> other processes or users access the file file_f, such as delete of
> change it?
> My understanding is that the critical section is only LINE1 and LINE2,
> not the file_f, so the semaphore can only control the access of the
> LINE1 and LINE2, not the file file_f.


Code like this assumes that all the processes that need to be guarded
against make use of the same mutex. It's called "advisory locking".
You aren't protected against processes that don't participate in the
mutex protocol.

>
> Another situation is that if another user A of the same machine write
> another piece of code (different code from the above code) to access
> file_f as below:
>
> while(1){
> wait(mutex2);
>
> open(file_f); //LINE3
> write(file_f); //LINE4
>
> signal(mutex2);
> }
>
> Can mutex1 at LINE0 prevents user A from executing LINE3 and LINE4? Or
> can mutex2 control the execution of LINE1 and LINE2?


No. He must use mutex1 if you want the two applications to cooperate.

--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com