Solved

Inter process communication

Posted on 2000-03-10
12
214 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
12 Comments
 

Author Comment

by:apaazue
Comment Utility
Thanks
/apaazue
0
 
LVL 5

Expert Comment

by:paulqna
Comment Utility
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
Comment Utility
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
 

Author Comment

by:apaazue
Comment Utility
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
Comment Utility
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
Comment Utility
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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 2

Expert Comment

by:bedot
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Let's say you need to move the data of a file system from one partition to another. This generally involves dismounting the file system, backing it up to tapes, and restoring it to a new partition. You may also copy the file system from one place to…
In tuning file systems on the Solaris Operating System, changing some parameters of a file system usually destroys the data on it. For instance, changing the cache segment block size in the volume of a T3 requires that you delete the existing volu…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
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.

743 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now