Advertisement

How best share data between processes and process runs (Unix pthreads)?

Started by January 19, 2004 07:39 AM
2 comments, last by Lord Bart 20 years, 9 months ago
Hello All, I ran into a problem. How best to shared data between processes and also store between process runs? I running on Sun Solaris uses pthread library (pthreads because hopeful can move it onto Linux). I have two processes that must shared data between themselves and if the processes go down keep the data until a process come up again. Its simple data a few ints and a buffer. So I using a share memory file and map the memory into each process. I am uses a pthread mutex so only one process/thread can access the data. I am storing the mutex struct, and a reference count to how many process are current mapping the share memory file in the shared memory file since it has to be shared across processes. Now I am trying to make it as safe as possible. Using a class that when mapping this file. If it the first time creating it it creates the mutex and inc the ref count. Or if the ref count is 0 recreates the mutex and inc the ref count. At class destruction it locks check the ref count if == 1 and is so when dec the ref count and destroy the mutex, otherwise ec ref count. But what happens if the process dies(seg fault/bus error) while holding the lock on mutex. I set up the mutex attrubutes and protocall to return EOWNERDEAD if the owning thread died while holding the lock. Which let the process that alive get the mutex and can call the function that makes it consistent again and go on from there. My problem is this the best I can do? Is there a better way to shared data between processes and have it presistantly stored? I look through web, books, and man pages, and this is the best I can come up with, any ides, or places I can imporve or not though of? Any Help greatly welcome Lord Bart
damn.

i think thats is a great method.

I would have just let a segfault deadlock all the other threads. after all, if one crashes, they''re all buggy...

nice

does it work?

or, ooh. I don''t know how the mutex locking mechanism works, but might there be a race condition with multiple threads recieving EOWNERDEAD and all of them trying to fix the memory at once?

I''m not sure how the othre threads are getting this error (i''ve only used pthreads vaguely), but if multiple might get it at once, then you might need a "fixing the memory after a fault" mutex in order for the correction function to run.

Of course, then a thread might segfault while running that (say corrupted data)... maybe just deadlock at this point? or better. All Threads die, so the error is immediately evident.
Advertisement
Hello C-Junkie,

Yes it works well.
But I haven''t truly gaven it a real work out.

Since its a mutux only when a thread calls lock or was waiting to lock will it get the error EOWNERDEAD form the man pages it now becomes the owener must make it constentant and then unlock the thread.

So it man is right then there is not a possible of deadlock here.
Since only one thread will get the lock.

I just check to error return and if EOWNERDEAD make mutex consentant and continue on.

Lord Bart
quote: Original post by Lord Bart
get the error EOWNERDEAD form the man pages it now becomes the owener


Cool. *notes to self*

This topic is closed to new replies.

Advertisement