creating multi process application in linux is a complex task. It requires good knowledge of linux system call. multi process application needs to communicate each other. one of measure problem every developer faces, is “race condition”. multi process application design must avoid race condition or deadlock condition. Let us discuss some available system calls which are used to create multiprocess application.

The fork() System Call

  • If fork() returns a negative value, when it fails to create child process.
  • fork() returns a zero to a newly created child process.
  • fork() returns a positive value (process ID of the child process) , to the parent process

Socketpair() system call C/Unix/Linux

calling socketpair system call creates a pair of connected sockets. Through Socket pairs we can achieve a two-way communication chanel.  Messages can be sent in both directions. we can allocate one end to be the parent and one to be the child end. It doesn’t matter which end, just make a choice and remember the allocation.

sample example of creating multi process application in linux

How it works

  • Before creating a new process through fork, we made socket pair.
  • After fork we detect parent process and child proces.
  • In the parent process we have closed one end of socket pairs and calls parent_proc(sockets[1]).
  • In the child process we have closed other end of socket pairs and calls child_msg(sockets[0]).

Ref:

http://man7.org/linux/man-pages/man2/socketpair.2.html