In an attempt to better understand how pipes work in C, I decided to create a simple program. It is supposed to do the following: Firstly, I fork the program. The parent then reads from the standard input and writes everything into a pipe until EOF is reached. The child then reads from that pipe and writes the content back into another pipe, which is then supposed to be read by the parent process and written into the standard output.
Yes, the program isn't very "useful", but I'm just trying to familiarize myself with pipes and how to use them. This is my code:
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>
int main(int argc, char **argv) {
char buf;
int pipe_one[2];
int pipe_two[2];
pid_t child;
if(pipe(pipe_one) == -1) {
perror("pipe");
exit(EXIT_FAILURE);
}
if(pipe(pipe_two) == -1) {
perror("pipe");
exit(EXIT_FAILURE);
}
child = fork();
switch (child) {
case -1:
fprintf(stderr, "Error while forking.\n");
break;
case 0:
// child
// close unnecessary ends
close(pipe_one[1]);
close(pipe_two[0]);
// read input from parent and write it into pipe
while(read(pipe_one[0], &buf, 1) > 0) {
write(pipe_two[1], &buf, 1);
}
write(pipe_two[1], "\n", 1);
close(pipe_one[0]);
close(pipe_two[1]);
break;
default:
// parent
// close unnecessary ends
close(pipe_one[0]);
close(pipe_two[1]);
// read from standard input and write it into pipe
while(read(STDIN_FILENO, &buf, 1) > 0) {
write(pipe_one[1], &buf, 1);
}
write(pipe_one[1], "\n", 1);
close(pipe_one[1]);
// wait for child process to finish
wait(NULL);
// read from pipe that child wrote into
while(read(pipe_two[0], &buf, 1) > 0) {
write(STDOUT_FILENO, &buf, 1);
}
write(STDOUT_FILENO, "\n", 1);
close(pipe_two[0]);
break;
}
exit(EXIT_SUCCESS);
}
Expected behavior: In the beginning, the program reads user input until EOF is reached and then it outputs everything again into the standard output.
Actual behavior: The program reads the whole input, but once EOF is reached it just terminates (succesfully) without writing anything into the standard output. What am I doing wrong? I'd be happy if someone could look over it and help me out.
You close pipes for your parent in your child.
while(read(pipe_one[0], &buf, 1) > 0) {
write(pipe_two[1], &buf, 1);
}
write(pipe_two[1], "\n", 1);
close(pipe_one[0]); // Here you close pipes
close(pipe_two[1]); // for your parent
So the parent can't receive anything. Just remove those two lines and it will work.
Related
I'm following the free book Operating Systems: Three Easy Pieces by Arpaci-Dusseau, and despite being very new to C programming (constructive criticism is welcomed), I tried my luck on coding problem 8 from chapter 5:
Write a program that creates two children, and connects the standard
output of one to the standard input of the other, using the pipe()
system call.
Here is my attempt (some error-checking is removed for brevity):
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>
#include <sys/wait.h>
int main(int argc, char *argv[]) {
int pipefd[2];
char buf;
pipe(pipefd);
int rc1 = fork();
if (rc1 == 0) {
// Child 1, reading from argv, writing to pipe
close(pipefd[0]);
write(pipefd[1], argv[1], strlen(argv[1]));
close(pipefd[1]);
printf("Child 1 done.\n");
} else {
// Parent
wait(NULL);
int rc2 = fork();
if (rc2 == 0) {
// Child 2, reading from pipe, writing to stdout
printf("Entering child 2\n");
close(pipefd[1]);
while (read(pipefd[0], &buf, 1) > 0) {
write(STDOUT_FILENO, &buf, 1);
}
write(STDOUT_FILENO, "\n", 1);
printf("Wrote to stdout!\n");
close(pipefd[0]);
printf("Child 2 done.\n");
} else {
// Still parent
wait(NULL);
printf("Parent finished running.\n");
}
}
}
which generates the following output, and hangs:
$ ./myprogram "Hello world"
Child 1 done.
Entering child 2
Hello world█
where █ is the shell cursor, i.e. the while loop hasn't exited to reach the writing of the newline character thereafter.
I did, however, get this to work by replacing char buf; with char buf[1024]; and the lines
while (read(pipefd[0], &buf, 1) > 0) {
write(STDOUT_FILENO, &buf, 1);
}
with
int n = read(pipefd[0], &buf, 1024);
write(STDOUT_FILENO, &buf, n);
So, now it works. But I don't understand why the previous version did not work. That reading loop was practically identical to the one used in the example at the bottom of the pipe(2) man-pages, which I have verified to be working correctly for me. But why doesn't it work in my own little program?
Possible duplicate questions:
(1) Solution is to close unused ends of pipe. I believe I have done that correctly.
(2) Solution is to break on return codes <= 0, not on < 0. I believe I have done that correctly too.
pipefd[1]) never gets closed in the original parent process. So read(pipefd[0], &buf, 1) will hang in the second child process.
This version
int n = read(pipefd[0], &buf, 1024);
write(STDOUT_FILENO, &buf, n);
doesn't hang because there's no loop. The second child process reads the data written by
write(pipefd[1], argv[1], strlen(argv[1]));
and then continues onward, never checking pipefd[0] again. So it doesn't matter if read(pipefd[0],...) would hang.
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
int main(void){
//Variables, p[2] for each end of the pipe. nbytes to read pipe return value SUCCESS or FAILURE. pid_t to hold pid of fork process.
// buffer to hold response from the child process.
int p[2], nbytes;
pid_t childpid;
char string[] = "Hello, World!\n";
char buffer[80];
//Declaration of pipe
pipe(p);
//Error handling.
if(((childpid = fork()) == -1) || (pipe(p) == -1))
{
perror("fork");
exit(1);
}
//Child process sends message to paprent.
if(childpid == 0)
{
/* Child process closes up input side of pipe */
close(p[0]);
/* Send "string" through the output side of pipe */
write(p[1], string, (strlen(string)+1));
exit(0);
}
else
{
/* Parent process closes up output side of pipe */
close(p[1]);
/* Read in a string from the pipe */
nbytes = read(p[0], buffer, sizeof(buffer));
printf("Received string: %s", buffer);
}
return(0);
}
Output > Received string: #�=zJ
The point of the exercise is to have a child process send a message through a pipe to the parent process and the parent returns the result. This exact code worked the first time I ran it, but then when I tried to run it a second time it started to return seemingly random characters each time. I tried to copy my buffer to another variable but then it was empty. Is the pipe actually not function the way I think it is? What am I doing wrong?
You first create a pipe with pipe(p); and then you create another with ... || (pipe(p) == -1)) Is that deliberate?
2nd Pipe was causing an issue.
You have:
pipe(p);
//Error handling.
if(((childpid = fork()) == -1) || (pipe(p) == -1))
{
perror("fork");
exit(1);
}
This creates two pipes — one in the line pipe(p); and the second in the condition if(((childpid = fork()) == -1) || (pipe(p) == -1)). This is wasteful at best. Moreover, the second pipe is after the fork(), so the parent and child processes don't access the same pipe any more — you overwrote the one created before the fork() which they do share. Test the result of pipe() before calling fork() and remove the extra condition in the if test:
if (pipe(p) != 0)
{
perror("pipe");
exit(1);
}
if ((childpid = fork()) < 0)
{
perror("fork");
exit(1);
}
Get used to testing for errors and writing appropriate code to handle them. It will be a major part of your life as a C programmer.
Later on in the code, you have:
{
/* Parent process closes up output side of pipe */
close(p[1]);
/* Read in a string from the pipe */
nbytes = read(p[0], buffer, sizeof(buffer));
printf("Received string: %s", buffer);
}
You need to heed the value of nbytes. Since it is an int, you could use:
printf("Received %d bytes: [%.*s]\n", nbytes, nbytes, buffer);
This limits the output to what was read, and reports 0 if that's what it gets. I suppose you should also check for -1 in nbytes before using it in the printf() statement:
if (nbytes < 0)
{
fprintf(stderr, "failed to read from pipe descriptor %d\n", p[0]);
// Or perror("read");
// Should you exit here with a non-zero status?
}
else
printf("Received %d bytes: [%.*s]\n", nbytes, nbytes, buffer);
Note: errors are reported on stderr; perror() does that automatically.
The problem is that you create two pipes when you really only need to check the first for errors:
// Declaration of pipe
if(pipe(p) == -1) { // check for error here
perror("pipe");
exit(1);
}
// Error handling.
if((childpid = fork()) == -1) { // and don't create another pipe here
perror("fork");
exit(1);
}
You should also check the return values from write and read. They may not write or read the full string in one go.
Problem
I only get this in the terminal output. I believe the program is getting stuck at the fork() call but I don't know exactly why.
The name of the program is q9:
prompt>$ ./q9 inputString
Parent: writing to pipe 'inputString'
Task
read input from terminal into the parent-to-child pipe.
fork() to create a child process.
read input from parent-to-child pipe.
concatenate some other string to that string read in from the pipe.
write the newly concatenated string to the child-to-parent pipe.
in the parent, read from the child-to-parent pipe and print the output read from the pipe to the terminal.
Attempts
I have tried fixing this by:
attempting to close pipes in different places. I thought I may have missed something or left something open, but I don't think so.
placing a wait() in the parent because perhaps it wasn't letting the child run completely
attempted to print the output of the concatenated string just in case it was that messing up the prints.
Code
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <sys/wait.h>
int main (int argc, char *argv[]) {
// parent RUN
if(argc == 1) {
printf("usage: q9 <string>\n");
return 0;
}
// create two way pipes
int parent_fds[2], child_fds[2];
// create strings to save too
char fromParent[100];
char fromChild[100];
// read:[0] - write:[1]
if (pipe(parent_fds) != 0 && pipe(child_fds) != 0) {
fprintf(stderr, "pipes failed!\n");
}
// close unused pipe end by parent
close(parent_fds[0]);
close(child_fds[1]);
close(child_fds[0]);
// write from terminal to parent pipe FOR child to read
printf("Parent: writing to pipe '%s'\n", argv[1]);
write(parent_fds[1], argv[1], strlen(argv[1]));
close(parent_fds[1]);
// fork() child process
int child = fork();
// NEVER GETS PASSED HERE :(
if (child < 0) {
fprintf(stderr, "fork failed!");
exit(1);
} else if (child == 0) {
printf("I reached the child :)");
// close unwanted pipe ends by child
close(child_fds[0]);
close(parent_fds[1]);
// read from parent pipe
int n = read(parent_fds[0], fromParent, 100);
fromParent[n] = 0;
printf("Child: reading from parent pipe '%s'\n", fromParent);
close(parent_fds[0]);
// Concatinate to what was read in
const char myText[14] = " (added this.)";
strcat(fromParent, myText);
write(child_fds[1], fromParent, strlen(fromParent));
close(child_fds[1]);
printf("Child: writing to pipe - '%s'\n", fromParent);
} else {
// read from child pipe
int n = read(child_fds[0], fromChild, 100);
fromChild[n] = 0;
printf("Parent: reading from pipe - '%s'\n", fromChild);
}
return 0;
}
What is going wrong?
There were several problems, and your diagnostic messages were not guaranteed to appear. Make sure you end your messages with newlines.
You only created one pipe because you used && instead of ||.
You closed the pipes 'for the parent' before you'd created the child (also noted by kaylum in a comment).
There are multiple other cleanups in the code below. The code (still) does not ensure that the write-to-pipe operations succeed (they were failing before). It does ensure that the strings read from the pipes are not longer than the buffers in which they are placed; it doesn't ensure there's enough space to append the extra information in the child. The code shown waits for any child processes to complete before exiting. The child executes the wait() call but it immediately fails (and the child doesn't print anything) and it exits. The parent waits for the child to complete and reports on it doing so before exiting.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/wait.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
if (argc == 1)
{
fprintf(stderr, "Usage: %s <string>\n", argv[0]);
return EXIT_FAILURE;
}
// create two pipes:
// - parent_fds used by parent to write to child
// - child_fds used by child to write to parent
int parent_fds[2], child_fds[2];
// read:[0] - write:[1]
if (pipe(parent_fds) != 0 || pipe(child_fds) != 0) /* || not && */
{
fprintf(stderr, "pipes failed!\n");
return EXIT_FAILURE;
}
// fork() child process
int child = fork();
if (child < 0)
{
fprintf(stderr, "fork failed!");
return EXIT_FAILURE;
}
else if (child == 0)
{
printf("%d: I reached the child :)\n", (int)getpid());
// close unwanted pipe ends by child
close(child_fds[0]);
close(parent_fds[1]);
// read from parent pipe
char fromParent[100];
int n = read(parent_fds[0], fromParent, sizeof(fromParent) - 1);
fromParent[n] = '\0';
printf("%d: Child: read from parent pipe '%s'\n", (int)getpid(), fromParent);
close(parent_fds[0]);
// Append to what was read in
strcat(fromParent, " (added this.)");
write(child_fds[1], fromParent, strlen(fromParent));
close(child_fds[1]);
printf("%d: Child: writing to pipe - '%s'\n", (int)getpid(), fromParent);
}
else
{
// close unwanted pipe ends by parent
close(parent_fds[0]);
close(child_fds[1]);
// write from terminal to parent pipe FOR child to read
printf("%d: Parent: writing to pipe '%s'\n", (int)getpid(), argv[1]);
write(parent_fds[1], argv[1], strlen(argv[1]));
close(parent_fds[1]);
// read from child pipe
char fromChild[100];
int n = read(child_fds[0], fromChild, sizeof(fromChild) - 1);
fromChild[n] = '\0';
close(child_fds[0]);
printf("%d: Parent: read from pipe - '%s'\n", (int)getpid(), fromChild);
}
int corpse;
int status;
while ((corpse = wait(&status)) > 0)
printf("%d: child PID %d exited with status 0x%.4X\n", (int)getpid(), corpse, status);
return EXIT_SUCCESS;
}
Sample output (source pipe43.c, program pipe43):
$ pipe43 'Nobody expects the Spanish Inquisition!'
84543: Parent: writing to pipe 'Nobody expects the Spanish Inquisition!'
84544: I reached the child :)
84544: Child: read from parent pipe 'Nobody expects the Spanish Inquisition!'
84544: Child: writing to pipe - 'Nobody expects the Spanish Inquisition! (added this.)'
84543: Parent: read from pipe - 'Nobody expects the Spanish Inquisition! (added this.)'
84543: child PID 84544 exited with status 0x0000
$
I am trying to send two messages "hello World" and "Goodbye" from parent to a child using a pipe. The child must print the messages when recieves them.
My problem is how to send the second message. I compile and run the program but it only prints the first message. Any sugestions?
Here's my code:
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 50
void main(){
int fd[2], n, status;
char buf[MAX];
pid_t pid;
char str1[]="Hello World!\n";
char str2[]="Goodbye\n";
pipe(fd);
if((pid=fork())<0){
abort();
}
else if(pid>0){// parent code goes here
close (fd[0]); // close read channel of parent
/*Send "hello world" through the pipe*/
write(fd[1],str1,(strlen(str1))); // write to the pipe
wait(&status);
close(fd[0]);
write(fd[1],str2,(strlen(str2)));
}
else{ // child code goes here
close(fd[1]); // close write channel of child
n=read(fd[0], buf, sizeof(buf)); // reads from the pipe
write(STDOUT_FILENO, buf, n);
exit(0);
}
}
In the parent, just write the two messages and then close the write end of the pipe:
close(fd[0]); // close read channel of pipe in parent
write (fd[1], str1, strlen(str1)); // write "hello world"
write (fd[1], str2, strlen(str2)); // write "goodbye"
close(fd[1]); // Tell child that we're done writing
wait(&status); // Wait for child to read everything and exit
In the child, you should read in a loop until you get EOF, indicated by read() returning 0:
close(fd[1]); // close write channel of pipe in child
while ((n = read(fd[0], buf, sizeof(buf)) > 0) { // Read until it returns 0 (EOF) or -1 (error)
write(STDOUT_FILENO, buf, n);
}
if (n < 0) { // -1 = error
perror("read from pipe");
}
I can barely understand the man page for pipe, so I kinda need help understanding how to take a piped input in an external executable.
I have 2 programs: main.o & log.o
I written main.o to fork. Here is what it is doing:
Parent fork will pipe data to the child
Child fork will exec log.o
I need the child fork for main to pipe to STDIN of log.o
log.o simply takes STDIN & logs with time stamp to a file.
My code is composed of some code from various StackOverflow pages I dont remember & the man page for pipe:
printf("\n> ");
while(fgets(input, MAXINPUTLINE, stdin)){
char buf;
int fd[2], num, status;
if(pipe(fd)){
perror("Pipe broke, dood");
return 111;
}
switch(fork()){
case -1:
perror("Fork is sad fais");
return 111;
case 0: // Child
close(fd[1]); // Close unused write end
while (read(fd[0], &buf, 1) > 0) write(STDOUT_FILENO, &buf, 1);
write(STDOUT_FILENO, "\n", 1);
close(fd[0]);
execlp("./log", "log", "log.txt", 0); // This is where I am confused
_exit(EXIT_FAILURE);
default: // Parent
data=stuff_happens_here();
close(fd[0]); // Close unused read end
write(fd[1], data, strlen(data));
close(fd[1]); // Reader will see EOF
wait(NULL); // Wait for child
}
printf("\n> ");
}
I suppose this is what you're going to do:
1. main fork, parent pass message to child via pipe.
2. child receive message from pipe, redirect message to STDIN, execute log.
3. log receive message from STDIN, do something.
the key to do this is dup2 to redirect file descriptor, from pipe to STDIN.
This is the modified simple version:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
int main(int argc, char *argv[]) {
int fd[2];
char buf[] = "HELLO WORLD!";
if(pipe(fd)){
perror("pipe");
return -1;
}
switch(fork()){
case -1:
perror("fork");
return -1;
case 0:
// child
close(fd[1]);
dup2(fd[0], STDIN_FILENO);
close(fd[0]);
execl("./log", NULL);
default:
// parent
close(fd[0]);
write(fd[1], buf, sizeof(buf));
close(fd[1]);
wait(NULL);
}
printf("END~\n");
return 0;
}
I can suggest a simpler approach. There's a function called popen(). It works very similar to the system() function except you can read or write to/from the child stdin/stdout.
Example:
int main(int argc, char* argv[])
{
FILE* fChild = popen("logApp.exe", "wb"); // the logger app is another application
if (NULL == fChild) return -1;
fprintf(fChild, "Hello world!\n");
pclose(fChild);
}
Write "man popen" in your console for a full description.
You could use dup2
See Mapping UNIX pipe descriptors to stdin and stdout in C