Re: semaphore question



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

.



Relevant Pages

  • Re: semaphore question
    ... system-wide global shared memory). ... You protect neither files nor global data. ... You serialize concurrent execution of code. ...
    (comp.os.linux.development.system)
  • Re: semaphore question
    ... You can't protect file-accesses with mutexes. ... You serialize concurrent execution of code. ... Since the two pieces of code use different semaphores, ...
    (comp.os.linux.development.system)
  • Re: Disable usb devices bring out of standby
    ... >>> In memory of Alex Nichol ... >>> Folder customizations ... >>> Protect your PC! ...
    (microsoft.public.windowsxp.customize)
  • Re: [Full-disclosure] Google Talk cleartext credentials in processmemory
    ... operating system already presents a means to protect against one process ... another process's memory, then you've already lost, it's too late. ... process's memory at the moment that it had the password in cleartext. ...
    (Full-Disclosure)
  • Re: Windows shuts down on me without asking me first
    ... Most systems will shutdown or restart to ... you are wasting money on the memory program. ... Windows help - www.rickrogers.org ... I have all the protection I need to protect my computer. ...
    (microsoft.public.windowsxp.general)