/**/ Program for IPC using popen() - Dextutor Programs

Program for IPC using popen()

IPC (Inter-process communication) can be achieved using popen/pclose functions. popen() function opens a process by creating a pipe, forking and invoking the shell. This pipe is a unidirectional pipe. Hence, it can be used either for reading or writing purpose only. We understand this concept with the help of a Program for IPC using popen()

Syntax

#include<stdio.h>
FILE *popen(const char *command, const char *type)

The first argument specifies the name of the process with which the communication is to take place and the second argument tells whether your current process is going to send data (writing into pipe) or receive data (reading from pipe).

We will demonstrate the use of popen using two different programs. One to send data to another process and second to receive data from the other process.

//Q. Program to write into a pipe i.e to send data from one process to another process.
//ipc1.c

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<string.h>
int main()
{
FILE *rd;
char buffer[50];
sprintf(buffer,"name first");
rd=popen("wc -c","w"); // wc -c -> is the process which counts the number of characters passed. 2nd parameter is "w" which means pipe is opened in writing mode
fwrite(buffer,sizeof(char),strlen(buffer),rd); // to write the data into the pipe
pclose(rd);
}
How it Works?

There are two programs “ipc1.c” (which will send the data) and “wc” command(which will receive the data). As the data will be sent to another process, so the mode of opening the pipe is writing mode “w”. popen() establishes the pipe between ipc1.c and wc. fwrite() function writes data into this pipe.
ipc1.c stores some data in the buffer, then it connects with “wc” using popen. Finally, the fwrite() function writes data into the pipe.
The data is received by “wc” which then counts the number of characters in the input and prints it

Output:

The output will be
10
because ipc1.c passed “name first” which contains 10 characters. So, when wc -c reads this it prints the count of number of characters which is 10

Program 2:

//Q. Program to read from a pipe i.e. to receive data from another process
// ipc2.c

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<string.h>
int main()
{
FILE *rd;
char buffer[50];
rd=popen("ls","r"); //pipe opened in reading mode
fread(buffer, 1, 40, rd); //will read only 50 characters
printf("%s\n", buffer);
pclose(rd);
}

How it works?

In this case, we establish the pipe between “ipc2.c” and “ls”. Since, the data will be read, hence, the pipe is opened in reading mode “r”. ls sends the data through the pipe. This data will be read by ipc2.c. So, this time our program is the one receiving the data. ls will send the list of files in current working directory. ipc2.c will read that, save it in buffer and then finally print it.

Remember: The ls command generates the list of all files in the current working directory but our process will read only first 40 character as specified in the fread() function. So, the output will consist of 40 characters only.

Output

Program for IPC using popen()

Viva Questions on Program for IPC using popen()

Q1. What does the popen() return on success?
Ans: On success, the popen() function returns a pointer to an open stream. This stream can be used to read or write to the pipe.
Q2. What is the role of fwrite() function in Program 1?
Q3. Can the pipe created by popen() be used for writing and reading both at the same time?

Practice Programs on IPC using popen()

Q. You have an existing program “input.c” which prints a welcome message for the current logged in user and prints the date and time. The compiled file of this program is “input”. Write another program which calls the compiled file “input” prints the data entered by the user

Video Link

Relevant Programs for Inter-Process Communication

Inter-Process Communication using pipe() function
IPC using named pipes (mkfifo)
Program on IPC using shared memory concept

Leave a Comment