Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 227
  • Last Modified:

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.
0
apaazue
Asked:
apaazue
1 Solution
 
apaazueAuthor Commented:
Thanks
/apaazue
0
 
paulqnaCommented:
Sure, I guess I can help, you need simple redirecton and file monitoring as far as I understood...
0
 
chris_calabreseCommented:
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.
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
apaazueAuthor Commented:
I would appreciacte if somebody can send me a piece of code.I am new to network programming.
Thanks,
/apaazue
0
 
jlevieCommented:
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.
0
 
GP1628Commented:
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
0
 
bedotCommented:
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 !
0
 
chris_calabreseCommented:
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.
0
 
apaazueAuthor Commented:
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
0
 
apaazueAuthor Commented:
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
0
 
mskrishnaCommented:
apaazue,
       i think my answer could give you a little bit releif.i dont know whether you know the concept of semaphores in
any OS.each process can access the shared variable only when it is in the
critical region.other time it cannot.
so suppose you have started some 5 process then all the five process come to the critical region periodically where they modify variables etc...
when one process is in the critical section the other process cannot enter it.so, i think you have got the logic.
0
 
chris_calabreseCommented:
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
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now