C How to Know When to Stop Reading a Pipe
Inter Process Advice - Pipes
Piping is a communication medium between two or more related or interrelated processes. It can be either within one procedure or a communication between the child and the parent processes. Communication can also be multi-level such as advice between the parent, the child and the m-child, etc. Communication is achieved by one process writing into the pipage and other reading from the piping. To achieve the pipe system call, create two files, one to write into the file and another to read from the file.
Pipe mechanism can be viewed with a real-fourth dimension scenario such equally filling water with the pipe into some container, say a bucket, and someone retrieving it, say with a mug. The filling process is nothing just writing into the pipage and the reading procedure is nothing but retrieving from the pipe. This implies that one output (h2o) is input for the other (bucket).
#include<unistd.h> int pipe(int pipedes[2]);
This system call would create a pipe for one-style communication i.e., information technology creates ii descriptors, kickoff one is connected to read from the pipe and other one is connected to write into the pipe.
Descriptor pipedes[0] is for reading and pipedes[1] is for writing. Any is written into pipedes[ane] can be read from pipedes[0].
This call would render cypher on success and -1 in case of failure. To know the cause of failure, check with errno variable or perror() function.
#include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> int open(const char *pathname, int flags); int open(const char *pathname, int flags, mode_t fashion);
Even though the basic operations for file are read and write, it is essential to open the file earlier performing the operations and closing the file after completion of the required operations. Usually, past default, 3 descriptors opened for every procedure, which are used for input (standard input – stdin), output (standard output – stdout) and mistake (standard error – stderr) having file descriptors 0, 1 and 2 respectively.
This organisation call would return a file descriptor used for further file operations of read/write/seek (lseek). Usually file descriptors offset from iii and increase past 1 number as the number of files open.
The arguments passed to open system 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 suspend to the existing file O_APPEND, to create file, if not exists with O_CREAT and so on) and the required mode providing permissions of read/write/execute for user or owner/group/others. Mode can exist mentioned with symbols.
Read – four, Write – two and Execute – one.
For example: Octal value (starts with 0), 0764 implies owner has read, write and execute permissions, group has read and write permissions, other has read permissions. This tin 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 phone call, on success, returns the new file descriptor id and -1 in case of error. The cause of mistake can be identified with errno variable or perror() office.
#include<unistd.h> int shut(int fd)
The higher up organization phone call endmost already opened file descriptor. This implies the file is no longer in use and resources associated can be reused by whatsoever other process. This arrangement telephone call returns goose egg on success and -1 in case of error. The crusade of error tin can be identified with errno variable or perror() function.
#include<unistd.h> ssize_t read(int fd, void *buf, size_t count)
The above system 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 after calling open() or pipe() organization telephone call. The file needs to be opened earlier reading from the file. It automatically opens in case of calling pipe() organisation call.
This call would return the number of bytes read (or zero in instance of encountering the terminate of the file) on success and -ane in instance of failure. The return bytes can be smaller than the number of bytes requested, only in instance no information is bachelor or file is closed. Proper error number is set in example of failure.
To know the crusade of failure, check with errno variable or perror() office.
#include<unistd.h> ssize_t write(int fd, void *buf, size_t count)
The above system 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 place the respective file, which is returned after calling open() or piping() system call.
The file needs to exist opened before writing to the file. It automatically opens in case of calling pipe() system call.
This telephone call would return the number of bytes written (or zero in case zilch is written) on success and -i in case of failure. Proper error number is set in case of failure.
To know the crusade of failure, check with errno variable or perror() function.
Case Programs
Post-obit are some instance programs.
Example program i − Program to write and read two messages using pipage.
Algorithm
Pace 1 − Create a piping.
Step ii − Transport a message to the piping.
Pace iii − Retrieve the message from the pipe and write it to the standard output.
Step iv − Send some other bulletin to the pipe.
Step 5 − Retrieve the message from the pipe and write it to the standard output.
Note − Retrieving messages can also be done subsequently sending all messages.
Source Code: simplepipe.c
#include<stdio.h> #include<unistd.h> int main() { int pipefds[2]; int returnstatus; char writemessages[2][20]={"Hi", "Hello"}; char readmessage[20]; returnstatus = pipe(pipefds); if (returnstatus == -1) { printf("Unable to create pipe\n"); return 1; } printf("Writing to pipe - Message ane is %s\n", writemessages[0]); write(pipefds[1], writemessages[0], sizeof(writemessages[0])); read(pipefds[0], readmessage, sizeof(readmessage)); printf("Reading from pipe – Message ane is %south\n", readmessage); printf("Writing to pipe - Message 2 is %s\north", writemessages[0]); write(pipefds[1], writemessages[1], sizeof(writemessages[0])); read(pipefds[0], readmessage, sizeof(readmessage)); printf("Reading from pipe – Message ii is %due south\n", readmessage); return 0; } Note − Ideally, return condition needs to be checked for every organization call. To simplify the process, checks are non washed for all the calls.
Execution Steps
Compilation
gcc -o simplepipe simplepipe.c
Execution/Output
Writing to pipe - Message ane is Hi Reading from pipe – Bulletin i is Hi Writing to pipe - Message 2 is Hi Reading from pipe – Message ii is Hell
Example program 2 − Programme to write and read two messages through the pipe using the parent and the child processes.
Algorithm
Step 1 − Create a pipe.
Step 2 − Create a kid process.
Pace three − Parent process writes to the pipe.
Step 4 − Child procedure retrieves the message from the pipage and writes it to the standard output.
Step 5 − Repeat step three and step 4 once again.
Source Code: pipewithprocesses.c
#include<stdio.h> #include<unistd.h> int main() { int pipefds[2]; int returnstatus; int pid; char writemessages[2][xx]={"Hi", "Hello"}; char readmessage[20]; returnstatus = pipe(pipefds); if (returnstatus == -1) { printf("Unable to create pipe\n"); return 1; } pid = fork(); // Child process if (pid == 0) { read(pipefds[0], readmessage, sizeof(readmessage)); printf("Kid Process - Reading from pipe – Bulletin 1 is %southward\n", readmessage); read(pipefds[0], readmessage, sizeof(readmessage)); printf("Kid Process - Reading from pipage – Message 2 is %s\n", readmessage); } else { //Parent process printf("Parent Process - Writing to pipe - Message ane is %south\n", writemessages[0]); write(pipefds[1], writemessages[0], sizeof(writemessages[0])); printf("Parent Process - Writing to pipage - Message 2 is %southward\n", writemessages[one]); write(pipefds[i], writemessages[1], sizeof(writemessages[1])); } return 0; } Execution Steps
Compilation
gcc pipewithprocesses.c –o pipewithprocesses
Execution
Parent Process - Writing to pipage - Message 1 is Hi Parent Procedure - Writing to pipage - Message 2 is Hullo Child Process - Reading from piping – Message 1 is Howdy Child Process - Reading from pipe – Message 2 is Howdy
Ii-way Communication Using Pipes
Pipe advice is viewed as but 1-way communication i.e., either the parent process writes and the child procedure reads or vice-versa but not both. However, what if both the parent and the child needs to write and read from the pipes simultaneously, the solution is a 2-fashion communication using pipes. 2 pipes are required to establish 2-way communication.
Following are the steps to achieve two-way communication −
Pace 1 − Create ii pipes. Beginning one is for the parent to write and child to read, say equally pipe1. 2d ane is for the child to write and parent to read, say as pipe2.
Footstep 2 − Create a kid process.
Footstep 3 − Close unwanted ends as only 1 terminate is needed for each communication.
Step 4 − Close unwanted ends in the parent process, read end of pipe1 and write end of pipe2.
Step five − Shut the unwanted ends in the child procedure, write end of pipe1 and read end of pipe2.
Step vi − Perform the communication as required.
Sample Programs
Sample programme ane − Achieving two-style communication using pipes.
Algorithm
Step 1 − Create pipe1 for the parent process to write and the kid process to read.
Step 2 − Create pipe2 for the child process to write and the parent procedure to read.
Step iii − Close the unwanted ends of the pipe from the parent and child side.
Step four − Parent process to write a message and child procedure to read and display on the screen.
Pace 5 − Child process to write a message and parent procedure to read and display on the screen.
Source Code: twowayspipe.c
#include<stdio.h> #include<unistd.h> int chief() { int pipefds1[2], pipefds2[two]; int returnstatus1, returnstatus2; int pid; char pipe1writemessage[twenty] = "Hello"; char pipe2writemessage[xx] = "Hullo"; char readmessage[20]; returnstatus1 = piping(pipefds1); if (returnstatus1 == -one) { printf("Unable to create pipe 1 \n"); render 1; } returnstatus2 = pipe(pipefds2); if (returnstatus2 == -1) { printf("Unable to create pipe 2 \north"); return one; } 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 1 – Message is %s\n", pipe1writemessage); write(pipefds1[1], pipe1writemessage, sizeof(pipe1writemessage)); read(pipefds2[0], readmessage, sizeof(readmessage)); printf("In Parent: Reading from pipe 2 – Message is %due south\n", readmessage); } else { //kid procedure close(pipefds1[1]); // Close the unwanted pipe1 write side close(pipefds2[0]); // Close the unwanted pipe2 read side read(pipefds1[0], readmessage, sizeof(readmessage)); printf("In Child: Reading from pipe i – Bulletin is %s\n", readmessage); printf("In Child: Writing to pipe 2 – Message is %southward\northward", pipe2writemessage); write(pipefds2[1], pipe2writemessage, sizeof(pipe2writemessage)); } return 0; } Execution Steps
Compilation
gcc twowayspipe.c –o twowayspipe
Execution
In Parent: Writing to pipe 1 – Message is Hi In Kid: Reading from pipe ane – Message is Hello In Child: Writing to pipe 2 – Message is Hello In Parent: Reading from pipe 2 – Message is Hello
Useful Video Courses
Video
Video
Video
Video
Video
Video
Source: https://www.tutorialspoint.com/inter_process_communication/inter_process_communication_pipes.htm
Belum ada Komentar untuk "C How to Know When to Stop Reading a Pipe"
Posting Komentar