Can We Use Both Ends of Reading and Writing in a Pipe

Inter Procedure Advice - Pipes


Pipe is a communication medium betwixt 2 or more than related or interrelated processes. It tin can be either within one procedure or a communication betwixt the kid and the parent processes. Communication can as well be multi-level such as advice between the parent, the child and the 1000-child, etc. Communication is accomplished past ane process writing into the pipe and other reading from the pipe. To achieve the pipage system call, create 2 files, 1 to write into the file and some other to read from the file.

Pipe mechanism tin can be viewed with a real-time scenario such as filling water with the pipe into some container, say a bucket, and someone retrieving it, say with a mug. The filling procedure is cypher but writing into the pipage and the reading process is naught only retrieving from the pipe. This implies that one output (water) is input for the other (bucket).

Pipe with one

#include<unistd.h>  int pipe(int pipedes[2]);        

This organization call would create a pipage for one-way communication i.e., it creates two descriptors, showtime one is connected to read from the pipe and other one is continued to write into the pipe.

Descriptor pipedes[0] is for reading and pipedes[1] is for writing. Whatever is written into pipedes[ane] can be read from pipedes[0].

This call would return zero on success and -1 in instance of failure. To know the crusade of failure, check with errno variable or perror() function.

#include <sys/types.h> #include <sys/stat.h> #include <fcntl.h>  int open up(const char *pathname, int flags); int open(const char *pathname, int flags, mode_t fashion);        

Fifty-fifty though the bones operations for file are read and write, information technology is essential to open the file before performing the operations and closing the file after completion of the required operations. Usually, past default, 3 descriptors opened for every process, which are used for input (standard input – stdin), output (standard output – stdout) and mistake (standard mistake – stderr) having file descriptors 0, 1 and 2 respectively.

This system call would return a file descriptor used for further file operations of read/write/seek (lseek). Ordinarily file descriptors beginning from 3 and increment by one number as the number of files open.

The arguments passed to open organisation phone call are pathname (relative or absolute path), flags mentioning the purpose of opening file (say, opening for read, O_RDONLY, to write, O_WRONLY, to read and write, O_RDWR, to append to the existing file O_APPEND, to create file, if not exists with O_CREAT and so on) and the required way providing permissions of read/write/execute for user or possessor/group/others. Style can be mentioned with symbols.

Read – 4, Write – 2 and Execute – 1.

For example: Octal value (starts with 0), 0764 implies possessor has read, write and execute permissions, group has read and write permissions, other has read permissions. This can as well be represented as S_IRWXU | S_IRGRP | S_IWGRP | S_IROTH, which implies or operation of 0700|0040|0020|0004 → 0764.

This system call, on success, returns the new file descriptor id and -1 in example of fault. The cause of error tin can be identified with errno variable or perror() function.

#include<unistd.h>  int close(int fd)        

The above organization call endmost already opened file descriptor. This implies the file is no longer in use and resources associated can exist reused by any other process. This system call returns zero on success and -1 in example of error. The crusade of error tin can exist identified with errno variable or perror() function.

#include<unistd.h>  ssize_t read(int fd, void *buf, size_t count)        

The above arrangement telephone call is to read from the specified file with arguments of file descriptor fd, proper buffer with allocated memory (either static or dynamic) and the size of buffer.

The file descriptor id is to identify the respective file, which is returned afterward calling open up() or pipe() system call. The file needs to be opened earlier reading from the file. Information technology automatically opens in case of calling pipe() arrangement telephone call.

This call would return the number of bytes read (or nil in instance of encountering the end of the file) on success and -1 in case of failure. The return bytes can exist smaller than the number of bytes requested, just in case no data is bachelor or file is closed. Proper mistake number is prepare in case of failure.

To know the cause of failure, bank check with errno variable or perror() office.

#include<unistd.h>  ssize_t write(int fd, void *buf, size_t count)        

The to a higher place organisation call is to write to the specified file with arguments of the file descriptor fd, a proper buffer with allocated memory (either static or dynamic) and the size of buffer.

The file descriptor id is to identify the respective file, which is returned afterward calling open() or piping() system call.

The file needs to exist opened before writing to the file. Information technology automatically opens in example of calling pipage() system call.

This call would return the number of bytes written (or goose egg in example nothing is written) on success and -1 in instance of failure. Proper fault number is gear up in case of failure.

To know the cause of failure, cheque with errno variable or perror() function.

Example Programs

Following are some example programs.

Example programme 1 − Programme to write and read two letters using pipe.

Algorithm

Step 1 − Create a pipe.

Step 2 − Send a message to the pipage.

Step 3 − Retrieve the bulletin from the pipe and write information technology to the standard output.

Footstep 4 − Send another message to the pipage.

Step 5 − Retrieve the message from the pipe and write it to the standard output.

Note − Retrieving messages can also be done after sending all messages.

Source Code: simplepipe.c

#include<stdio.h> #include<unistd.h>  int main() {    int pipefds[ii];    int returnstatus;    char writemessages[2][xx]={"Hi", "How-do-you-do"};    char readmessage[20];    returnstatus = pipe(pipefds);        if (returnstatus == -1) {       printf("Unable to create pipe\n");       return ane;    }        printf("Writing to pipe - Bulletin 1 is %s\n", writemessages[0]);    write(pipefds[1], writemessages[0], sizeof(writemessages[0]));    read(pipefds[0], readmessage, sizeof(readmessage));    printf("Reading from pipe – Message 1 is %southward\n", readmessage);    printf("Writing to pipage - Message 2 is %southward\n", writemessages[0]);    write(pipefds[i], writemessages[1], sizeof(writemessages[0]));    read(pipefds[0], readmessage, sizeof(readmessage));    printf("Reading from pipe – Message 2 is %s\n", readmessage);    return 0; }        

Annotation − Ideally, return status needs to be checked for every organization phone call. To simplify the process, checks are non done for all the calls.

Execution Steps

Compilation

gcc -o simplepipe simplepipe.c        

Execution/Output

Writing to pipe - Bulletin i is Hello Reading from piping – Bulletin 1 is Hi Writing to pipe - Message 2 is Hello Reading from pipe – Message ii is Hell        

Example program 2 − Programme to write and read 2 messages through the piping using the parent and the child processes.

Algorithm

Footstep 1 − Create a pipe.

Step 2 − Create a child process.

Stride 3 − Parent process writes to the piping.

Step four − Kid process retrieves the bulletin from the pipe and writes it to the standard output.

Step v − Repeat pace three and pace 4 one time once again.

Source Code: pipewithprocesses.c

#include<stdio.h> #include<unistd.h>  int principal() {    int pipefds[2];    int returnstatus;    int pid;    char writemessages[2][20]={"Hi", "Hello"};    char readmessage[xx];    returnstatus = pipe(pipefds);    if (returnstatus == -1) {       printf("Unable to create pipage\northward");       return 1;    }    pid = fork();        // Child procedure    if (pid == 0) {       read(pipefds[0], readmessage, sizeof(readmessage));       printf("Child Procedure - Reading from pipe – Message one is %southward\n", readmessage);       read(pipefds[0], readmessage, sizeof(readmessage));       printf("Child Process - Reading from pipe – Bulletin 2 is %s\n", readmessage);    } else { //Parent process       printf("Parent Process - Writing to pipe - Message one is %s\due north", writemessages[0]);       write(pipefds[ane], writemessages[0], sizeof(writemessages[0]));       printf("Parent Process - Writing to pipe - Message ii is %s\n", writemessages[1]);       write(pipefds[i], writemessages[1], sizeof(writemessages[1]));    }    render 0; }        

Execution Steps

Compilation

gcc pipewithprocesses.c –o pipewithprocesses        

Execution

Parent Process - Writing to pipe - Bulletin 1 is Hullo Parent Process - Writing to pipage - Message 2 is Hello Child Process - Reading from pipe – Message 1 is How-do-you-do Kid Process - Reading from pipe – Bulletin 2 is Hello        

Ii-fashion Advice Using Pipes

Pipe communication is viewed as only one-style communication i.eastward., either the parent process writes and the kid process reads or vice-versa just not both. However, what if both the parent and the child needs to write and read from the pipes simultaneously, the solution is a two-style communication using pipes. Ii pipes are required to establish two-fashion communication.

Following are the steps to achieve 2-way communication −

Stride 1 − Create two pipes. Offset one is for the parent to write and child to read, say as pipe1. Second ane is for the kid to write and parent to read, say equally pipe2.

Step two − Create a child process.

Step 3 − Close unwanted ends as simply one stop is needed for each communication.

Step 4 − Shut unwanted ends in the parent process, read end of pipe1 and write finish of pipe2.

Stride 5 − Shut the unwanted ends in the child process, write end of pipe1 and read cease of pipe2.

Pace half-dozen − Perform the communication as required.

Pipe with two

Sample Programs

Sample program one − Achieving 2-way advice using pipes.

Algorithm

Pace 1 − Create pipe1 for the parent process to write and the child process to read.

Step 2 − Create pipe2 for the child process to write and the parent process to read.

Footstep 3 − Close the unwanted ends of the pipe from the parent and child side.

Step 4 − Parent process to write a message and kid procedure to read and display on the screen.

Step 5 − Child process to write a message and parent process to read and display on the screen.

Source Code: twowayspipe.c

#include<stdio.h> #include<unistd.h>  int chief() {    int pipefds1[2], pipefds2[2];    int returnstatus1, returnstatus2;    int pid;    char pipe1writemessage[20] = "Hi";    char pipe2writemessage[20] = "Hello";    char readmessage[20];    returnstatus1 = piping(pipefds1);        if (returnstatus1 == -1) {       printf("Unable to create pipage 1 \due north");       return one;    }    returnstatus2 = pipe(pipefds2);        if (returnstatus2 == -1) {       printf("Unable to create pipe 2 \n");       return 1;    }    pid = fork();        if (pid != 0) // Parent process {       close(pipefds1[0]); // Close the unwanted pipe1 read side       close(pipefds2[i]); // Close the unwanted pipe2 write side       printf("In Parent: Writing to pipe ane – Bulletin is %s\due north", pipe1writemessage);       write(pipefds1[one], pipe1writemessage, sizeof(pipe1writemessage));       read(pipefds2[0], readmessage, sizeof(readmessage));       printf("In Parent: Reading from pipe 2 – Message is %southward\north", readmessage);    } else { //kid process       close(pipefds1[ane]); // Shut the unwanted pipe1 write side       close(pipefds2[0]); // Shut the unwanted pipe2 read side       read(pipefds1[0], readmessage, sizeof(readmessage));       printf("In Child: Reading from pipe one – Message is %southward\n", readmessage);       printf("In Child: Writing to pipage 2 – Bulletin is %s\northward", pipe2writemessage);       write(pipefds2[ane], pipe2writemessage, sizeof(pipe2writemessage));    }    return 0; }        

Execution Steps

Compilation

gcc twowayspipe.c –o twowayspipe        

Execution

In Parent: Writing to pipage 1 – Bulletin is Hi In Child: Reading from pipe one – Message is Hi In Child: Writing to pipe ii – Bulletin is Hello In Parent: Reading from pipe 2 – Message is Hello        

Useful Video Courses


Pinterest Marketing Online Training

Video

Learn C++ Pointers with Visual Studio 2017

Video

Analyse and Interpret Hotel Profit &amp; Loss Statements (Hindi)

Video

Analyse And Interpret Hotels Overall Financial Statements

Video

Mastering Python GUI with Tkinter

Video

Internal Combustion Engines - IC Engines

Video

travisscia1940.blogspot.com

Source: https://www.tutorialspoint.com/inter_process_communication/inter_process_communication_pipes.htm

0 Response to "Can We Use Both Ends of Reading and Writing in a Pipe"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel