How to Read a String From a Pipe C
Inter Process Communication - Pipes
Pipe is a communication medium between two or more than related or interrelated processes. It can be either within one process or a communication between the child and the parent processes. Communication can too be multi-level such every bit communication between the parent, the child and the grand-kid, etc. Communication is achieved past ane process writing into the pipe and other reading from the pipe. To achieve the pipe organisation call, create two files, ane to write into the file and another to read from the file.
Pipe mechanism tin can be viewed with a real-fourth dimension scenario such as filling water with the pipe into some container, say a bucket, and someone retrieving it, say with a mug. The filling process is nothing but writing into the piping and the reading process is zippo simply retrieving from the piping. This implies that i output (water) is input for the other (bucket).
#include<unistd.h> int piping(int pipedes[2]);
This arrangement call would create a pipe for one-way communication i.e., it creates two descriptors, first 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[one] is for writing. Any is written into pipedes[ane] can exist read from pipedes[0].
This call would return zilch on success and -1 in case of failure. To know the cause of failure, cheque 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 mode);
Even though the basic operations for file are read and write, information technology is essential to open up the file before performing the operations and closing the file subsequently 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 error (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 start from 3 and increase by 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 append to the existing file O_APPEND, to create file, if non exists with O_CREAT and and then on) and the required mode providing permissions of read/write/execute for user or possessor/group/others. Mode tin can be mentioned with symbols.
Read – 4, Write – 2 and Execute – 1.
For example: Octal value (starts with 0), 0764 implies owner has read, write and execute permissions, grouping has read and write permissions, other has read permissions. This tin also be represented equally S_IRWXU | S_IRGRP | S_IWGRP | S_IROTH, which implies or operation of 0700|0040|0020|0004 → 0764.
This organization call, on success, returns the new file descriptor id and -1 in case of mistake. The cause of mistake tin be identified with errno variable or perror() function.
#include<unistd.h> int close(int fd)
The above organisation call closing already opened file descriptor. This implies the file is no longer in use and resources associated tin exist reused by any other procedure. This system call returns goose egg on success and -1 in case of error. The cause 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 place the respective file, which is returned afterward calling open() or pipage() system call. The file needs to be opened before reading from the file. Information technology automatically opens in instance of calling pipage() system call.
This call would return the number of bytes read (or zero in case of encountering the end of the file) on success and -1 in example of failure. The return bytes can exist smaller than the number of bytes requested, just in case no information is available or file is closed. Proper fault number is fix in case of failure.
To know the cause of failure, check with errno variable or perror() function.
#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 identify the respective file, which is returned subsequently calling open() or pipe() arrangement call.
The file needs to be opened before writing to the file. Information technology automatically opens in case of calling pipe() system telephone call.
This call would return the number of bytes written (or zero in case zip is written) on success and -ane in example of failure. Proper error number is gear up in instance of failure.
To know the cause of failure, check with errno variable or perror() function.
Example Programs
Post-obit are some case programs.
Example program 1 − Programme to write and read two messages using pipe.
Algorithm
Step i − Create a pipage.
Step 2 − Transport a message to the pipe.
Step three − Retrieve the bulletin from the pipe and write it to the standard output.
Stride iv − Send another message to the pipe.
Step 5 − Retrieve the message from the piping and write it to the standard output.
Notation − Retrieving messages tin besides be done subsequently sending all messages.
Source Code: simplepipe.c
#include<stdio.h> #include<unistd.h> int main() { int pipefds[ii]; int returnstatus; char writemessages[2][twenty]={"Hi", "Hello"}; char readmessage[20]; returnstatus = pipe(pipefds); if (returnstatus == -one) { printf("Unable to create pipe\northward"); return 1; } printf("Writing to pipe - Bulletin ane is %s\n", writemessages[0]); write(pipefds[1], writemessages[0], sizeof(writemessages[0])); read(pipefds[0], readmessage, sizeof(readmessage)); printf("Reading from pipe – Bulletin 1 is %s\n", readmessage); printf("Writing to piping - Message ii is %south\n", writemessages[0]); write(pipefds[1], writemessages[one], sizeof(writemessages[0])); read(pipefds[0], readmessage, sizeof(readmessage)); printf("Reading from pipage – Message 2 is %s\northward", readmessage); return 0; }
Note − Ideally, render status needs to be checked for every system call. To simplify the process, checks are not washed for all the calls.
Execution Steps
Compilation
gcc -o simplepipe simplepipe.c
Execution/Output
Writing to pipe - Bulletin 1 is How-do-you-do Reading from pipage – Message 1 is Hi Writing to pipe - Bulletin 2 is Hullo Reading from pipe – Bulletin 2 is Hell
Case program 2 − Plan to write and read ii letters through the pipe using the parent and the kid processes.
Algorithm
Step 1 − Create a piping.
Pace ii − Create a child procedure.
Pace three − Parent process writes to the piping.
Step iv − Child process retrieves the message from the pipe and writes it to the standard output.
Step 5 − Echo footstep 3 and pace 4 once again.
Source Lawmaking: pipewithprocesses.c
#include<stdio.h> #include<unistd.h> int main() { int pipefds[2]; int returnstatus; int pid; char writemessages[2][20]={"Hi", "Howdy"}; char readmessage[20]; returnstatus = pipe(pipefds); if (returnstatus == -ane) { printf("Unable to create pipe\northward"); return ane; } pid = fork(); // Child process if (pid == 0) { read(pipefds[0], readmessage, sizeof(readmessage)); printf("Child Process - Reading from pipe – Message 1 is %due south\n", readmessage); read(pipefds[0], readmessage, sizeof(readmessage)); printf("Kid Process - Reading from pipe – Message ii is %s\n", readmessage); } else { //Parent process printf("Parent Process - Writing to pipe - Message 1 is %s\n", writemessages[0]); write(pipefds[one], writemessages[0], sizeof(writemessages[0])); printf("Parent Process - Writing to pipe - Bulletin 2 is %s\n", writemessages[i]); write(pipefds[i], writemessages[1], sizeof(writemessages[1])); } return 0; }
Execution Steps
Compilation
gcc pipewithprocesses.c –o pipewithprocesses
Execution
Parent Process - Writing to pipe - Message i is Howdy Parent Process - Writing to piping - Message 2 is Hullo Kid Procedure - Reading from pipe – Message i is Hi Child Process - Reading from pipe – Message 2 is Howdy
Two-mode Advice Using Pipes
Pipe advice is viewed equally just one-style communication i.eastward., either the parent procedure writes and the child procedure reads or vice-versa just non both. Nevertheless, what if both the parent and the kid needs to write and read from the pipes simultaneously, the solution is a two-way communication using pipes. Two pipes are required to establish two-way communication.
Following are the steps to achieve two-mode communication −
Footstep 1 − Create two pipes. First i is for the parent to write and child to read, say as pipe1. Second one is for the kid to write and parent to read, say as pipe2.
Footstep 2 − Create a kid process.
Stride 3 − Shut unwanted ends as but one end is needed for each communication.
Footstep 4 − Close unwanted ends in the parent process, read terminate of pipe1 and write terminate of pipe2.
Step 5 − Shut the unwanted ends in the kid procedure, write terminate of pipe1 and read end of pipe2.
Step half-dozen − Perform the communication as required.
Sample Programs
Sample program one − Achieving two-manner communication using pipes.
Algorithm
Step 1 − Create pipe1 for the parent process to write and the child procedure to read.
Step 2 − Create pipe2 for the child procedure to write and the parent process to read.
Pace 3 − Shut the unwanted ends of the pipage from the parent and child side.
Footstep 4 − Parent process to write a message and kid procedure to read and display on the screen.
Footstep 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 main() { int pipefds1[2], pipefds2[2]; int returnstatus1, returnstatus2; int pid; char pipe1writemessage[xx] = "Hi"; char pipe2writemessage[twenty] = "How-do-you-do"; char readmessage[xx]; returnstatus1 = piping(pipefds1); if (returnstatus1 == -one) { printf("Unable to create pipe 1 \n"); render 1; } returnstatus2 = piping(pipefds2); if (returnstatus2 == -ane) { printf("Unable to create pipe 2 \n"); return 1; } pid = fork(); if (pid != 0) // Parent procedure { close(pipefds1[0]); // Shut the unwanted pipe1 read side close(pipefds2[1]); // Shut the unwanted pipe2 write side printf("In Parent: Writing to pipe ane – Message is %s\due north", pipe1writemessage); write(pipefds1[1], pipe1writemessage, sizeof(pipe1writemessage)); read(pipefds2[0], readmessage, sizeof(readmessage)); printf("In Parent: Reading from pipe 2 – Message is %s\n", readmessage); } else { //child process close(pipefds1[i]); // 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 pipage 1 – Message is %s\n", readmessage); printf("In Child: Writing to pipe ii – Bulletin is %s\n", pipe2writemessage); write(pipefds2[i], pipe2writemessage, sizeof(pipe2writemessage)); } render 0; }
Execution Steps
Compilation
gcc twowayspipe.c –o twowayspipe
Execution
In Parent: Writing to pipage ane – Message is How-do-you-do In Kid: Reading from pipage 1 – Bulletin is Hi In Child: Writing to pipage 2 – Message is Hi 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
Postar um comentário for "How to Read a String From a Pipe C"