Solved

Inter process communication

Posted on 2000-03-10
12
220 Views
Last Modified: 2010-04-21
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
Comment
Question by:apaazue
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
12 Comments
 

Author Comment

by:apaazue
ID: 2606371
Thanks
/apaazue
0
 
LVL 5

Expert Comment

by:paulqna
ID: 2606393
Sure, I guess I can help, you need simple redirecton and file monitoring as far as I understood...
0
 
LVL 14

Expert Comment

by:chris_calabrese
ID: 2606428
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:apaazue
ID: 2606979
I would appreciacte if somebody can send me a piece of code.I am new to network programming.
Thanks,
/apaazue
0
 
LVL 40

Expert Comment

by:jlevie
ID: 2608937
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
 
LVL 2

Expert Comment

by:GP1628
ID: 2609839
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
 
LVL 2

Expert Comment

by:bedot
ID: 2610063
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
 
LVL 14

Expert Comment

by:chris_calabrese
ID: 2612409
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
 

Author Comment

by:apaazue
ID: 2612753
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
 

Author Comment

by:apaazue
ID: 2612867
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
 

Accepted Solution

by:
mskrishna earned 100 total points
ID: 2619868
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
 
LVL 14

Expert Comment

by:chris_calabrese
ID: 2619890
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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Installing FreeBSD… FreeBSD is a darling of an operating system. The stability and usability make it a clear choice for servers and desktops (for the cunning). Savvy?  The Ports collection makes available every popular FOSS application and packag…
FreeBSD on EC2 FreeBSD (https://www.freebsd.org) is a robust Unix-like operating system that has been around for many years. FreeBSD is available on Amazon EC2 through Amazon Machine Images (AMIs) provided by FreeBSD developer and security office…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question