Home » Software » Basic Guide of Interprocess Communication and Pipes

Basic Guide of Interprocess Communication and Pipes

(追記) (追記ここまで)

Interprocess Communication (IPC) is a method of communication by exchanging data / information among multiple threads or multiple process. The usefulness of creating a child process from parent process is limited if they don’t have ability to communicate between the process. Interprocess communication (IPC) enables the exchange of information / data that are running on same platform or same CPU. The oldest form and most commonly used of IPC is called a pipe.

IPC Basic Guide of Interprocess Communication and Pipes

Mainly, communication using pipe consists of two file descriptors (one for reading and one for writing). There are two kinds of pipes :

  • Half Duplex, a pipe which can send data/information in one direction.
  • Full Duplex, a pipe which can send and receiving data/information at the same time.

You can create pipes by using Pipe function, which returns two files (a read file and a write file) descriptor in an integer array format. In C and C++ Programming Language, Interprocess Communications (IPC) Functions can be obtained from unistd.h

(追記) (追記ここまで)
List-of-IPC-functions-unistd.h Basic Guide of Interprocess Communication and Pipes

List of Some Interprocess Communication Functions from unistd.h

Interprocess Communication Example in C

In this example we will show you a complete program that creates a pipe to communicating between parent and child process.

Parent Process Example

Parent process will create a new process (child) and communicate using a pipe

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <stdio.h>
#include <unistd.h>
#include <wait.h>
#include <string.h>
#define contentMessage "Parent successfully communicate using pipe"
int main (void)
{
int communicate[2]; /* Variable of array for the pipe*/
pid_t apid; /*forked process id*/
int status; /*for identifying status of communication*/
/*Now lets create Interprocess Communications (IPC) pipe*/
if (pipe(communicate) < 0)
printf ("Error initiating communication using pipe\n");
apid = fork(); /*create a new process*/
if (apid < 0)
{
printf ("Error Create New Process\n");
if (close (communicate[0]) != 0)
printf("Error closing file descriptor \n);
if (close (communicate[1]) != 0)
printf("Error closeing file secriptor \n);
}
else if (apid == 0)
{
printf ("Process Created With ID %d\n", getpid());
if (execl("child.exe", "child.exe", NULL) < 0)
printf ("Error replace image process\n");
}
else
{
printf ("Process Created With ID %d\n", getpid());
if (close (communicate[0]) != 0)
printf ("Error Closing Pipe\n");
sleep(1);
printf ("Parent Process Writing '%s'\n", contentMessage);
if (write (communicate[1], contentMessage, sizeof(contentMessage)) < 0)
printf ("Error Writing message\n");
if (write (communicate[1], "\n", 1) < 0);
printf ("Error Writing message\n");
printf ("Waiting....\n");
if (wait(&status) == apid)
printf ("Status = WEXITSTATUS(status));
else
printf ("Error Waiting Process\n");
}
printf ("Parent Process Stopping\n");
return (0);
}

Child Process Example

Child Process will be a program that launched from parent

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <stdio.h>
#include <string.h>
#include <unistd.h>
int main (void)
{
char text[64]; /* Message buffer */
/* Display the child process ID */
printf (" Child Process ID %d\n", getpid());
/* Read a newline delimited string from the standard input */
if (fgets (text, sizeof (text) - 1, stdin) == NULL)
printf ("Error Reading Standard Input\n");
/* Display the message read from standard input */
printf (" Child Process Reading '%s'\n", text);
printf (" Child Process Stopping\n");
/* Return 1 to parent to indicate successful completion */
return (1);
}

Now it’s done. Try to compile them. If it can’t be compiled maybe you can check for common errors.

February 19, 2014 Technology Tips Software 1 Comment

You May Want to See :

One thought on “Basic Guide of Interprocess Communication and Pipes

  1. Sabina Carlisle says:

    magnificent submit, very informative. I’m wondering why the other specialists of this sector do not understand this.
    You should continue your writing. I’m sure, you’ve a huge readers’ base already!

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Name *
Email *
Website

This site uses Akismet to reduce spam. Learn how your comment data is processed.

AltStyle によって変換されたページ (->オリジナル) /