How to use standard output?
Hi:
I have a very small program (see the code below). The program forks a child process. The child process then execute standard program "oc". The program 'oc' use the standard input of child process. How to let program 'oc' to use standard output of child process?
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int main()
{
int data_processed;
int file_pipes[2];
const char some_data[]="123";
pid_t fork_result;
if (pipe(file_pipes) == 0) {
fork_result = fork();
if (fork_result == (pid_t) -1) {
fprintf(stderr, "Fork failure");
exit(EXIT_FAILURE);
}
if (fork_result==(pid_t)0) {
close(0);
dup(file_pipes[0]);
close(file_pipes[0]);
close(file_pipes[1]);
execlp("od", "od", "-c", (char *)0);
printf("process is executed.\n");
exit(EXIT_FAILURE);
}
else {
close(file_pipes[0]);
data_processed = write(file_pipes[1], some_data,
strlen(some_data));
close(file_pipes[1]);
printf("%d - wrote %d bytes\n", (int)getpid(), data_processed);
}
}
exit(EXIT_SUCCESS);
}
[Edited by - greenpaper on October 7, 2005 1:07:30 PM]
Tried fprintf(stdout, "Your schtuff here") ?
Edit: Oh, you wanted to communicate between the child process and the parent. Named pipes might be a decent method.
[Edited by - Ravuya on October 7, 2005 3:44:57 PM]
Edit: Oh, you wanted to communicate between the child process and the parent. Named pipes might be a decent method.
[Edited by - Ravuya on October 7, 2005 3:44:57 PM]
Here is one tutorial that demonstrates communication with a child via a pipe. You should also get to know the functions you are using a little better. For example you are printing a message after execlp but execlp is supposed to replace the current process with another. If execlp succeeds, it will not return. Also, look into dup2 instead of dup. It is a little simpler to use that to replace stdin or stdout.
There are other good tutorials out there as well. It might be easiest to try to get your program working without execing anything first.
There are other good tutorials out there as well. It might be easiest to try to get your program working without execing anything first.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement