Re: semaphore question
- From: Josef Moellers <josef.moellers@xxxxxxxxxxxxxxxxxxx>
- Date: Tue, 15 Aug 2006 09:17:23 +0200
42Bastian Schick wrote:
On 14 Aug 2006 12:06:45 -0700, "Jack" <junw2000@xxxxxxxxx> wrote:
Below is a simple pseudocode using semaphore:
while(1){
wait(mutex1); //LINE0
open(file_f); //LINE1
write(file_f); //LINE2
signal(mutex1);
}
In short: You can't protect file-accesses with mutexes.
Why not?
Mutexes are there to protect global data (either process global or
system-wide global shared memory).
You protect neither files nor global data.
You serialize concurrent execution of code.
You can even protect serial (or other) communications using mutexs!
In simple terms:
When you signal a mutex, you do so only when the subsystem you try to protect, is consistent.
But this works only if all processes/threads obey the mutex.
If you have one code piece protecting data by using a mutex and another code piece happily modifying it, you're not protecting it.
So, two instances concurrently trying to execute the code above will not damage the file (provided the close() that's missing is actually there, otherwise both processes may have incorrect notions of the end-of-file).
--
Josef Möllers (Pinguinpfleger bei FSC)
If failure had no penalty success would not be a prize
-- T. Pratchett
.
- Follow-Ups:
- Re: semaphore question
- From: 42Bastian Schick
- Re: semaphore question
- From: David Schwartz
- Re: semaphore question
- From: Jack
- Re: semaphore question
- References:
- semaphore question
- From: Jack
- Re: semaphore question
- From: 42Bastian Schick
- semaphore question
- Prev by Date: Re: HowTo check whether PAE is enabled on IA32?
- Next by Date: Inject code where in device driver for checking on receive packets?
- Previous by thread: Re: semaphore question
- Next by thread: Re: semaphore question
- Index(es):
Relevant Pages
|