2

I need to write a simple function which does the following in linux:

  • Create two processes.
  • Have thread1 in Process1 do some small operation, and send a message to Process2 via thread2 once operation is completed. *Process2 shall acknowledge the received message.

I have no idea where to begin

  1. I have written two simple functions which simply count from 0 to 1000 in a loop (the loop is run in a function called by a thread) and I have compiled them to get the binaries.
  2. I am executing these one after the other (both running in the background) from a shell script
  3. Once process1 reaches 1000 in its loop, I want the first process to send a "Complete" message to the other.

I am not sure if my approach is correct on the process front and I have absolutely no idea how to communicate between these two. Any help will be appreciated.

LostinSpace

1
  • Looks like homework to me. I'll still give you a tip : SIGUSR1.
    – Aralicia
    Commented May 22, 2013 at 10:46

3 Answers 3

3

You'd probably want to use pipes for this. Depending on how the processes are started, you either want named or anonymous pipes:

  • Use named pipes (aka fifo, man mkfifo) if the processes are started independently of each other.
  • Use anonymous pipes (man 2 pipe) if the processes are started by a parent process through forking. The parent process would create the pipes, the child processes would inherit them. This is probably the "most beautiful" solution.

In both cases, the end points of the pipes are used just like any other file descriptor (but more like sockets than files).

If you aren't familiar with pipes yet, I recommend getting a copy of Marc Rochkind's book "Advanced UNIX programming" where these techniques are explained in great detail and easy to understand example code. That book also presents other inter-process communication methods (the only really other useful inter-process communication method on POSIX systems is shared memory, but just for fun/completeness he presents some hacks).

2

Since you create the processes (I assume you are using fork()), you may want to look at eventfd().

eventfd()'s provide a lightweight mechanism to send events from one process or thread to another. More information on eventfd()s and a small example can be found here http://man7.org/linux/man-pages/man2/eventfd.2.html.

2

Signals or named pipes (since you're starting the two processes separately) are probably the way to go here if you're just looking for a simple solution. For signals, your client process (the one sending "Done") will need to know the process id of the server, and for named pipes they will both need to know the location of a pipe file to communicate through.

However, I want to point out a neat IPC/networking tool that can make your job a lot easier if you're designing a larger, more robust system: 0MQ can make this kind of client/server interaction dead simple, and allows you to start up the programs in whatever order you like (if you structure your code correctly). I highly recommend it.

Not the answer you're looking for? Browse other questions tagged or ask your own question.