Link to home
Start Free TrialLog in
Avatar of apaazue
apaazue

asked on

Inter process communication

Lets say i have 5 processes running on a Unix machine. All the processes are of same type. There is a common variable or file which all the processes share, they can read as well as write to it. Each process uses data in this shared file at some point in the program, Each process has to get the updated value when ever it reads this variable or file. I would appreciate if somebody can help me in implementing this scenario.
Avatar of apaazue
apaazue

ASKER

Thanks
/apaazue
Sure, I guess I can help, you need simple redirecton and file monitoring as far as I understood...
If all the programs have the file opened, they will all see the latest value that has been written to the file.  If they're using stdio (vs raw read/write system calls), they may need to do fsync()'s to make sure they're actually updating the file, however.

What's a little trickier is informing all the processes that the file has been changed.  The easiest way to do this is for each process to fstat() the file and see if the timestamp's changed before each time they might need updated information.
Avatar of apaazue

ASKER

I would appreciacte if somebody can send me a piece of code.I am new to network programming.
Thanks,
/apaazue
Something none of the comments have touched on is that, by the question, all of the processes can write as well as read the common data. To avoid "lost writes" and/or corrupt data exclusive write locks need to used. Otherwise there is the real potential that two processes could try to update the common file at the same time.

I think I'd do it with POSIX locks via fnctl as it then won't matter if the file happens to located on an NFS filesystem.
its a crummy answer but I just create a seperate file for each variable and have them open/close just long enough to do the write/read.

Its cludgy but you didnt mention what the program was or what language it was written in so I went for the lowest common denominator as if it were simple shell scripts.

Gandalf Parker
take a look at IPC (inter processes communication), which are kernel resources:
there is shared memory to have common data for several processes
there is message queues
there is semaphores, used to synchronize communications:
so, you can update the data, and then increment a semaphore for each consumer process that are attempting to decrement their;
until  the sem is incremented, they are blocked;
so each process must wait to be able to decrement the sem before attempting to read the data: is sure data is up to date;

really better than reading a file (that need to flush buffer after writind, an re-read the file each time)
IPC are exactly done for that !
apaazue, I think one of the problems here is that you haven't told us what problem you're really trying to solve, but rather asked a very narrow question about a specific implementation.

Are you constrained to this implementation, or are you starting from scratch?

If you're constrained to the file-based implementation, then you want to use file locks as jlevie suggested and be careful about getting raw data if using stdio as I suggested or use raw read/write or use mmap().  See the man pages for (depending on the system flavor) flock, lockf, fcntl, mmap, read, write, and setbuf.

If starting from scratch, then bedot is definitely right that doing it with files is the wrong way to go unless the shared data is very large.  If it's just a bunch of variables with short values, the typical way of handling this is to used shared memory to hold the variables and a semaphore to lock access to the variables while they're being read/written.  See the man pages for ftok, stdipc, semget, semctl, semop, shmget, shmctl, and shmop.
Avatar of apaazue

ASKER

Well! Thanks to all for your suggestions. Hey chris, i understand what your saying.  But as i said i am new to network programming. I have following questions.
1.Where should i declare the shared memory variable.
2.How should i declare the variable.
3.How will different processes access this variable.

Let me give a brief overview of what i am trying to do.I am working on a client/server program. The system has 2 processors,server program runs on one of the processor and clients from other processor send requests for read/write locks on the files with the server. Server decides if requested access can be given based on the information it has about the requested file,like did some other client has already locked the file.

In this system i have to simulate client processor failure, i.e. crashing of the processor. It is proposed to be done as follows.
Each client process has to read a variable called Incarnation number before it sends each message. this variable will be common for all clients.server keeps track of the variable. to simulate failure one of the client increments this variable.which other clients read and send their request with the incremented Incarnation. when server notices an increment in this variable it assumes that client processor has failed and removes locks of all the clients from the processor.




I hope this will give a good understanding of the problem. I would appreciate if somebody can direct me in implementing shared memory between processes.I am stuck at this point in the project.  Otherwise its going good.
Thanks,
/apaazue
Avatar of apaazue

ASKER

I think i will need a little more information as i mentioned in my previous comment.Well thanks chris i think we are heading in right direction.
/apaazue
ASKER CERTIFIED SOLUTION
Avatar of mskrishna
mskrishna

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Read the manual pages I directed you to.  If these don't answer the questions for you, I suggest _Advanced_Programming_in_the_UNIX+Environment_ by Stevens and _Modern_Operating_Systems_ by Tannenbaum