how to store output from "bc" into a variable? - c

What this program is supposed to do is ask user a simple arithmetic question, e.g. 5 + 7 and then check the answer with "bc" (whether it's correct).
I have the following code, but I don't understand how to edit it to to store the result from "5 + 7" into a variable (currently the result goes into STDOUT).
Any help is welcome.
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/wait.h>
int main( int argc, char *argv[], char *env[] )
{
char *expr = "5 + 7\n";
int answer;
printf("%s = ", expr);
scanf("%d", &answer);
int pfds[2];
pipe(pfds);
if (!fork()) {
close(1); /* close normal stdout */
dup(pfds[1]); /* make stdout same as pfds[1] */
close(pfds[0]); /* we don't need this */
printf("%s\n", expr);
/***********************/
/* How to store printf()'s output into a variable? */
exit(0);
} else {
close(0); /* close normal stdin */
dup(pfds[0]); /* make stdin same as pfds[0] */
close(pfds[1]); /* we don't need this */
execlp("bc", "bc", NULL);
}
return 0;
}

You need to create a second pipe and redirect stdout to it in the child process.

You can simply read STOUD or read on your pipe's Output. Then a call to Read() and to Atoi might do the work. Atoi man page here
I can't code it for you, but here the logic
`int fds[2];
pipe(fds);
dup2(fds[1], stdout);
read(fds[1], buf, buf_sz);
int ResultOfbc = Atoi(buf)`

Ooof oof, this stuff I remember doing last year.
I was basically a program which used my own telnet clone to communicate with the internet.
Problem is telnet used stdin and stdout to work.
It looks like you have the same situation!
What I did to solve this was to fork a process, grab stdin and stdout and put them in pipes and then overwrite the forked process image with an invocation of telnet (which now instead of stdin and stdout, uses those pipes).
You need ONE pipe to send text to bc and ANOTHER to receive from bc.
If you use a single pipe for everything, chances are you'll end up reading what you sent to bc and mix data.
WARNING: MASSIVE AMOUNT OF CODE INCOMING.
I'm sure you won't need to understand everything, since I use threads to write and read simultaneously and select() to see if there's anything to read in the pipe.
VERY IMPORTANT!!! When communications break, your process will receive SIGPIPE which terminates not cleanly (if you're using dynamic memory or stuff like that).
And you MUST fflush(outpipe) or else bc won't receive it. (This is because system only flushes when it finds '\n' or something like that, if I recall correctly).
I put all the code just in case you want to read what X does. But what you need is only the small fork right after the comment "LOCAL FUNCTIONS END HERE"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <errno.h>
#include <signal.h>
#include <stdbool.h>
#include "irc.h"
#include "coloring.h"
#include "rtsp.h"
#define BUFFERSIZE 255
int main (int argc, char *argv[]) {
/* XXX: When kid dies, program doesn't exit */
char *serverName, *port, *nick, *channel;
int ptelnetin[2];
int ptelnetout[2];
FILE *fpipes[2];
bool running = true;
pid_t kid;
pthread_t pthread_input, pthread_output;
/************************************************
LOCAL FUNCTIONS START HERE
***********************************************/
void *inputprocess(void *pipes) {
bool bracket;
int i;
fd_set rfds;
struct timeval tv;
tv.tv_sec = 0.2;
tv.tv_usec = 0;
char buffer[BUFFERSIZE];
FILE *out = ((FILE **) pipes)[1];
while (running){
FD_ZERO(&rfds);
FD_SET(fileno(stdin), &rfds);
switch (select(FD_SETSIZE, &rfds, NULL, NULL, &tv)) {
case -1:
fprintf(stderr, "Error reading data at select() Line %d, File %s\n", __LINE__, __FILE__);
running = false;
break;
case 0:
/* There's no data avaiable just yet.
Do nothing and keep checking */
break;
default:
/* This check needs to be done;
select isn't completely reliable */
if(!fgets(buffer, BUFFERSIZE, stdin)) {
running = false;
break;
}
/* Check message not to contain brackets*/
for (i = 0, bracket = false; running && !bracket && buffer[i] && i < BUFFERSIZE; i++) {
if (buffer[i] == '[' || buffer[i] == ']') {
PRINT_YELLOW;
printf("Use of brackets not allowed\n");
RESET_COLOR;
fflush(stdout);
bracket = true;
break;
}
}
if (running && !bracket) ircInputWrite(out, buffer);
fflush(out);
}
}
}
void *outputprocess(void *pipes){
fd_set rfds;
struct timeval tv;
tv.tv_sec = 0.2;
tv.tv_usec = 0;
char buffer[BUFFERSIZE];
char from[100];
FILE *in = ((FILE **) pipes)[0];
FILE *out = ((FILE **) pipes)[1];
while (running){
FD_ZERO(&rfds);
FD_SET(fileno(in), &rfds);
switch (select(FD_SETSIZE, &rfds, NULL, NULL, &tv)) {
case -1:
fprintf(stderr, "Error reading data at select() Line %d, File %s\n", __LINE__, __FILE__);
running = false;
break;
case 0:
/* There's no data avaiable just yet. */
/* Select sometimes returns 0 when there IS
data to read so we'll read anyway */
default:
/* This check needs to be done;
select isn't completely reliable */
if(!fgets(buffer, BUFFERSIZE, in)) {
running = false;
break;
}
switch(ircWhatsthis(buffer)) {
case iPING:
PRINT_BLUE;
printf("PingPong!\n");
RESET_COLOR;
ircPingPong(out, buffer); fflush(out);
fflush(stdout);
break;
case iROOMMSG:
if (ircUnpackPRIVMSG(from, buffer, buffer)) {
PRINT_BRIGHT_RED;
fprintf(stdout, "Malformed private message received\n");
RESET_COLOR;
}
else {
PRINT_CYAN;
printf("<%s>: ", from);
puts(buffer);
RESET_COLOR;
}
fflush(stdout);
break;
case iPRIVMSG:
fflush(stdout);
if (ircUnpackPRIVMSG(from, buffer, buffer)) {
PRINT_BRIGHT_RED;
fprintf(stdout, "Malformed private message received\n");
RESET_COLOR;
fflush(stdout);
}
else {
if (rtspExecBrackets(out, from, buffer)) {
PRINT_BRIGHT_MAGENTA;
printf("[%s]: ", from);
puts(buffer);
RESET_COLOR;
fflush(stdout);
}
}
break;
case iERROR:
PRINT_BRIGHT_RED;
fwrite(buffer, sizeof(char), strlen(buffer)+1, stdout);
RESET_COLOR;
break;
case iOK:
PRINT_BRIGHT_CYAN;
fwrite(buffer, sizeof(char), strlen(buffer)+1, stdout);
RESET_COLOR;
break;
default:
PRINT_BRIGHT_BLACK;
fwrite(buffer, sizeof(char), strlen(buffer)+1, stdout);
RESET_COLOR;
}
fflush(stdout);
}
}
}
void terminate(int signum) {
/* XXX irc.c calls rtsp.c which uses threads.
These threads never clean up if exiting via ^C
*/
RESET_COLOR;
running = false;
/*Close IO*/
fclose(fpipes[0]);
fclose(fpipes[1]);
/* Call child */
kill(kid, SIGINT);
wait(NULL);
exit(0);
}
/************************************************
LOCAL FUNCTIONS END HERE
***********************************************/
signal(SIGPIPE, terminate);
signal(SIGINT, terminate);
/* Get parameters */
if (argc != 5) {
fprintf(stderr, "Usage:\n %s <server> <port> <nick> <channel>\n", argv[0]);
return -1;
}
serverName = argv[1];
port = argv[2];
nick = argv[3];
channel = argv[4];
/* Startup pipes */
pipe(ptelnetin);
pipe(ptelnetout);
/* Launch telnete */
switch (kid = fork()) {
case -1:
perror("OMG ABORTION at main");
exit(-2);
case 0: /* CHILD */
/*Overwrite stdin with pipein and discard pipe*/
dup2(ptelnetin[0], 0);
close(ptelnetin[0]);
close(ptelnetin[1]);
/*Overwrite stdout with pipeout and discard pipe*/
dup2(ptelnetout[1], 1);
close(ptelnetout[0]);
close(ptelnetout[1]);
/*Overwrite process image with telnete*/
execlp("./telnete", "./telnete", argv[1], argv[2], (char *) NULL);
perror("Call to exec failed at main");
exit(-3);
default: /* PARENT */
/* Close reading end of pipein */
close(ptelnetin[0]);
/* Close writing end on pipeout */
close(ptelnetout[1]);
}
/* Turn (fileno) into (FILE *) */
fpipes[1] = fdopen(ptelnetin[1],"w");
if(!fpipes[1]) {
perror("Error at fdopen(in) at main");
kill(kid, SIGINT);
abort();
}
fpipes[0] = fdopen(ptelnetout[0],"r");
if(!fpipes[0]) {
perror("Error at fdopen(out) at main");
kill(kid, SIGINT);
abort();
}
/* Sleep for a few seconds so server doesn't ignore it */
PRINT_YELLOW;
printf("Logging in IRC...\n");
RESET_COLOR;
fflush(stdout);
if (ircRegister(argv[3], fpipes[1], fpipes[0])) {
fprintf(stderr, "Error registering in IRC.\n");
terminate(-1);
}
PRINT_YELLOW;
printf("Joining room %s\n", argv[4]);
RESET_COLOR;
ircJOIN(fpipes[1], argv[4]);
fflush(fpipes[1]);
/* Launch threads */
if (pthread_create(&pthread_input, NULL, inputprocess, fpipes)){
fprintf(stderr,"Couldn't launch input thread");
kill(kid, SIGINT);
abort();
}
if (pthread_create(&pthread_output, NULL, outputprocess, fpipes)){
fprintf(stderr,"Couldn't launch output thread");
kill(kid, SIGINT);
abort();
}
/* Wait for threads */
if (pthread_join(pthread_input,NULL)){
fprintf(stderr, "Error joining thread.\n");
}
if (pthread_join(pthread_output,NULL)){
fprintf(stderr,"Error joining thread.\n");
}
terminate(0);
}
I'll put the key fragment here so it's clearer:
/* Startup pipes */
pipe(ptelnetin);
pipe(ptelnetout);
/* Launch telnete */
switch (kid = fork()) {
case -1:
perror("OMG ABORTION at main");
exit(-2);
case 0: /* CHILD */
/*Overwrite stdin with pipein and discard pipe*/
dup2(ptelnetin[0], 0);
close(ptelnetin[0]);
close(ptelnetin[1]);
/*Overwrite stdout with pipeout and discard pipe*/
dup2(ptelnetout[1], 1);
close(ptelnetout[0]);
close(ptelnetout[1]);
/*Overwrite process image with telnete*/
execlp("./telnete", "./telnete", argv[1], argv[2], (char *) NULL);
perror("Call to exec failed at main");
exit(-3);
default: /* PARENT */
/* Close reading end of pipein */
close(ptelnetin[0]);
/* Close writing end on pipeout */
close(ptelnetout[1]);
}
/* Turn (fileno) into (FILE *) */
fpipes[1] = fdopen(ptelnetin[1],"w");
if(!fpipes[1]) {
perror("Error at fdopen(in) at main");
kill(kid, SIGINT);
abort();
}
fpipes[0] = fdopen(ptelnetout[0],"r");
if(!fpipes[0]) {
perror("Error at fdopen(out) at main");
kill(kid, SIGINT);
abort();
}
After doing this you can read bc's result from the (FILE *) fpipes[0] and write to it in fpipes[1].
Remember to fflush(fpipes[1]) after each write.
Treat those two as you would with any file.

Related

C program using pipes hangs when trying to terminate

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <string.h>
#include <sys/wait.h>
#include <fcntl.h>
#define MSGSIZE 64
char msgbuf[MSGSIZE];
int main() {
int p1[2];
int p2[2];
int nread;
int choice = 0;
pid_t child_a, child_b;
if (pipe(p1) == -1) {
printf("error in creating pipe\n");
exit(-1);
}
if (pipe(p2) == -1) {
printf("error in creating pipe\n");
exit(-1);
}
child_a = fork();
if (child_a == 0) {
while (1) {
dup2(p1[0], STDIN_FILENO);
read(STDIN_FILENO, msgbuf, MSGSIZE);
printf("%d receives message: %s\n", getpid(), msgbuf);
close(p1[0]);
close(p1[1]);
}
} else {
child_b = fork();
if (child_b == 0) {
while (1) {
dup2(p2[0], STDIN_FILENO);
read(STDIN_FILENO, msgbuf, MSGSIZE);
printf("%d receives message: %s\n", getpid(), msgbuf);
close(p2[0]);
close(p2[1]);
}
} else {
while (1) {
printf("<child_to_receive_msg> <message>\n");
scanf("%d %s", &choice, msgbuf);
switch (choice) {
case 1:
usleep(250);
write(p1[1], msgbuf, MSGSIZE);
break;
case 2:
usleep(250);
write(p2[1], msgbuf, MSGSIZE);
break;
default:
printf("Process does not exist");
break;
case -1:
close(p1[0]);
close(p2[0]);
printf("parent waiting");
wait(NULL);
exit(0);
}
}
}
}
return 0;
}
In the above program I have a parent making two child processes belonging to that same parent. The user writes to the parent process which pipes the message to be read by either child 1 or child 2. It keeps doing this continuously unless the user inputs -1.
The problem is that case in my switch statement doesn't get executed and instead the program hangs. I think I have my pipes closed at the correct places.
You need to send some signal to your child process to inform then to terminate before waiting for them to exit. You should define some pre-defined message which means its time for child to terminate. Check below code. Here pre-defined message is "-1". You should choose your own which doesn't conflict with your application's real data.
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#define MSGSIZE 64
char msgbuf[MSGSIZE];
int main() {
int p1[2];
int p2[2];
int nread;
int choice = 0;
pid_t child_a, child_b;
if (pipe(p1) == -1) {
printf("error in creating pipe\n");
exit(-1);
}
if (pipe(p2) == -1) {
printf("error in creating pipe\n");
exit(-1);
}
child_a = fork();
if (child_a == 0) {
while (1) {
dup2(p1[0], STDIN_FILENO);
read(STDIN_FILENO, msgbuf, MSGSIZE);
printf("%d receives message: %s\n", getpid(), msgbuf);
close(p1[0]);
close(p1[1]);
if (strcmp(msgbuf, "-1") == 0) { // check if time to end
break;
}
}
} else {
child_b = fork();
if (child_b == 0) {
while (1) {
dup2(p2[0], STDIN_FILENO);
read(STDIN_FILENO, msgbuf, MSGSIZE);
printf("%d receives message: %s\n", getpid(), msgbuf);
close(p2[0]);
close(p2[1]);
if (strcmp(msgbuf, "-1") == 0) { // check if time to end
break;
}
}
} else {
while (1) {
printf("<child_to_receive_msg> <message>\n");
scanf("%d %s", &choice, msgbuf);
switch (choice) {
case 1:
usleep(250);
write(p1[1], msgbuf, MSGSIZE);
break;
case 2:
usleep(250);
write(p2[1], msgbuf, MSGSIZE);
break;
default:
printf("Process does not exist\n");
break;
case -1:
strcpy(msgbuf, "-1");
write(p1[1], msgbuf, MSGSIZE); // send message to end
close(p1[0]);
close(p2[0]);
printf("parent waiting\n");
wait(NULL);
exit(0);
}
}
}
}
return 0;
}
First, you need to start performing error checking. Check the man page of the calls you make. Add checks in your code to detect errors. When they return an error, use perror and exit(EXIT_FAILURE);.
Second, you need to start paying attention to the values returned by read and write since they could be less than expected. These need to be called in a loop.
For example, for read, you'd use the following:
#include <errno.h>
#include <limits.h>
// Returns the number of bytes read.
// EOF was reached if the number of bytes read is less than requested.
// On error, returns -1 and sets errno.
ssize_t read_fixed_amount(int fd, char *buf, size_t size) {
if (size > SSIZE_MAX) {
errno = EINVAL;
return -1;
}
ssize_t bytes_read = 0;
while (size > 0) {
ssize_t rv = read(fd, buf, size);
if (rv < 0)
return -1;
if (rv == 0)
return bytes_read;
size -= rv;
bytes_read += rv;
buf += rv;
}
return bytes_read;
}
It would be used something like this:
ssize_t bytes_read = read_fixed_amount(fd, buf, size);
if (bytes_read < 0) {
perror("read");
exit(EXIT_FAILURE);
}
if (bytes_read == 0) {
printf("EOF reached\n");
exit(EXIT_SUCCESS);
}
if (bytes_read != size) {
fprintf(stderr, "read: Premature EOF.\n");
exit(EXIT_FAILURE);
}
Third, reading from the pipe will only return EOF once all file descriptors of the write end of the pipes have been closed.
Right after the fork, the parent should do
close(p1[0]);
close(p2[0]);
Right after the fork, child 1 should do
close(p1[1]);
close(p2[0]);
close(p2[1]);
Right after the fork, child 2 should do
close(p1[0]);
close(p1[1]);
close(p2[1]);
Fourth, there's this monstrosity:
while (1) {
dup2(p1[0], STDIN_FILENO);
read(STDIN_FILENO, msgbuf, MSGSIZE);
...
close(p1[0]);
close(p1[1]);
}
Really? Infinite loop. Attempt to repeatedly make STDIN a dup of p1[0]. Duping of a closed descriptor.
This should appear before the loop:
dup2(p1[0], STDIN_FILENO);
close(p1[0]);
Or you could skip those two call and simply read from p1[0] instead of STDIN_FILENO.
As for the infinite loop, it goes back to the second point. Check the value returned by read.
Fifth, you only wait for one child to finish, but there are two children to wait for. You need to call wait twice.

Close pipe end error

I have a program that forks a child and want it to communicate with its parent. However, I seem to get an error when closing the write end in the child.
The program stops inside the child and in the if (close(pfd1[1]) == -1)
Apparently it fails when the child wants to close the write end. Why?
/* Note: working under the assumption that the messages are of equal length */
int main(int argc, char * argv[])
{
int pfd1[2];
char buf[BUF_SIZE];
//checks pipefd1
if (pipe(pfd1) == -1)
{
printf("Error opening pipe 1!\n");
exit(1);
}
printf("Pipe opened with success. Forking ...\n");
// child 1
switch (fork())
{
case -1:
printf("Error forking child 1!\n");
exit(1);
case 0:
printf("\nChild 1 executing...\n");
/* close writing end of first pipe */
if (close(pfd1[1]) == -1)
{
printf("Error closing writing end of pipe 1.\n");
_exit(1);
}
/* read from pipe 1 */
if (read(pfd1[0], buf, 2000))
{
printf("Error reading to pipe 1.\n");
_exit(1);
}
/* close reading end of first pipe */
if (close(pfd1[1]) == -1)
{
printf("Error closing writing end of pipe 1.\n");
_exit(1);
}
printf("Message received child ONE: %s", buf);
printf("Exiting child 1...\n");
_exit(0);
default: //parent breaks just out
break;
}
printf("inside parent\n");
int child = 1;
char *message = "Hey child1, this is your parent speaking";
if(child == 1)
{
//close read end of pipe
if(close(pfd1[0]) == -1)
{
printf("Error closing reading end of the pipe.\n");
exit(EXIT_FAILURE);
}
printf("Parent closed read end of pipe1\n");
//read end is closed, now write to child
if(write(pfd1[1], message, strlen(message)))
{
printf("Error writing to the pipe.");
_exit(EXIT_FAILURE);
}
printf("Writing to child1 succeeded\n");
}
if (wait(NULL) == -1)
{
printf("Error waiting.\n");
exit(EXIT_FAILURE);
}
if (wait(NULL) == -1)
{
printf("Error waiting.\n");
exit(EXIT_FAILURE);
}
printf("Parent finishing.\n");
exit(EXIT_SUCCESS);
}
First of all, in the child's case you attempt to close the writing end of the pipe twice. I guess the second call to close(2) was meant to close the reading end, as mentioned in the comment above it:
/* close reading end of first pipe */
if (close(pfd1[0]) == -1)
{
printf("Error closing writing end of pipe 1.\n");
_exit(1);
}
Besides that, note that both read(2) and write(2) return the number of bytes that were actually read or written; in the case of error the return value is -1, so your error-checking conditions there should be fixed too, to something like:
/* read from pipe 1 */
if (read(pfd1[0], buf, 2000) < 0) {
printf("Error reading to pipe 1.\n");
_exit(1);
}
and
//read end is closed, now write to child
if(write(pfd1[1], message, strlen(message)) < 0) {
printf("Error writing to the pipe.");
_exit(EXIT_FAILURE);
}
On the principle of teaching to fish, a good technique to diagnose problems like this is to check what the error was and print a more informative message. Here is a technique I frequently put into a header file and use:
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* This declaration and macro would really go into a header file: */
void fatal_error_helper( const char* msg, const char* sourcefile, int lineno, const char* syserr );
#define fatal_system_error(m) \
fatal_error_helper( (m), __FILE__, __LINE__, strerror(errno) )
/* This function definition would really go into a .c file: */
void fatal_error_helper( const char* const msg,
const char* const sourcefile,
const int lineno,
const char * const syserr )
{
fflush(stdout); /* Don't cross the streams! */
fprintf( stderr,
"%s at %s:%d: %s. Program terminated.\n",
msg, sourcefile, lineno, syserr
);
exit(EXIT_FAILURE);
}
/* Test driver: */
FILE* fails_to_open_file( const char* filename )
/* Returns a FILE* to an open file. If the operation fails, prints an
* error message and terminates the program.
*/
{
/* Do this in general before calling the function whose error value
* you check. Otherwise, you might report the wrong error message
* from an earlier call and really confuse someone.
*/
errno = 0;
FILE* result = NULL;
result = fopen(filename, ""); /* Fails. */
if (!result)
fatal_system_error("Failed to open file");
return result;
}
int main(void)
{
fails_to_open_file("nonexistent.file");
return EXIT_SUCCESS;
}
This gives an error message such as: Failed to open file at prog.c:26: Invalid argument. Program terminated.

write operation on pipe is always failing

I'm a bit new to pipes and concurrency, and have been frustrated with this problem for hours. I am struggling to understand why this write operation is constantly failing on my pipe. I am trying to have the child process write data through a pipe that will be received by the parent process. My current code is this:
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 4096
int main() {
pid_t status;
int fd[2]; //The array of file descriptors
if (pipe(fd) == -1) {
printf("Error piping");
}
status = fork(); //Begin the fork process
switch (status) {
case -1:
perror("Error forking");
break;
case 0:
//Child process
close(fd[0]); //Only send data
char some_string[15] = "hi there";
if (write(fd[1], some_string, MAXSIZE) == -1) {
printf("Error writing to the pipe");
}
close(fd[1]); //Close write end
exit(1);
default:
close(fd[1]); //Only receive data
char readed[500] = "";
while(read(fd[0], readed, MAXSIZE) != 0) {
printf("read this %s\n", readed);
}
printf("Done reading");
close(fd[0]);
break;
}
return 1;
}
However, I constantly get the message "Error writing to pipe", meaning that the write operation has failed in the child process. Another interesting thing is that if I change some_string to a string literal instead, this code works fine with the exception that it never terminates and instead, the read operation in the parent process reads from STDIN! I don't understand why this could be happening, is it possible that we have a zombie child when parent executes so the pipe is "dead"? Or perhaps that the parent process terminates and we have an orphaned child? How can I avoid this and how does this explain the weird behaviour from the string literal instead? Any insights?
You told write() to read the data from out-of-range of the array and allowed read() to write the data read to out-of-range of the array. That is very bad.
Write only valid data and limit the length to read not to cause out-of-range access.
Try this:
#include <unistd.h>
#include <sys/types.h> /* add this to use pid_t */
#include <sys/wait.h> /* add this to use wait() */
#include <stdio.h>
#include <stdlib.h>
/* remove unused MAXSIZE */
int main() {
pid_t status;
int fd[2]; //The array of file descriptors
int st; /* variable for receiving the status */
if (pipe(fd) == -1) {
printf("Error piping");
return 1; /* return 1 when the execution failed */
}
status = fork(); //Begin the fork process
switch (status) {
case -1:
perror("Error forking");
return 1; /* return 1 when the execution failed */
break;
case 0:
//Child process
close(fd[0]); //Only send data
char some_string[15] = "hi there";
if (write(fd[1], some_string, sizeof(some_string)) == -1) {
printf("Error writing to the pipe");
}
close(fd[1]); //Close write end
exit(0); /* return 0 if the execution finished successfully */
default:
close(fd[1]); //Only receive data
char readed[500] = "";
while(read(fd[0], readed, sizeof(readed) - 1) != 0) { /* -1 for reserving space for terminating null-character */
printf("read this %s\n", readed);
}
printf("Done reading");
close(fd[0]);
wait(&st); /* wait for the child process to exit and release the data of the process */
break;
}
return 0; /* return 0 if the execution finished successfully */
}

How to loop through stdin & pipe output to a child execl command in C?

I have been trying to figure out how to loop through stdin from a file, then send it to a child process who sorts int using execl(). The code below works in that it takes the file & sorts the lines, but I am not seeing the "end of sentence" debug string I have added. Somehow this part of the code is being bypassed. I could use some help understanding the flow of data as it comes in from the file, then gets printed out to the screen.
int main(int argc, char *argv[])
{
pid_t p;
int status;
int fds[2];
FILE *writeToChild;
char word[50];
if(pipe(fds) == -1) {
perror("Error creating pipes");
exit(EXIT_FAILURE);
}
switch(p = fork()) {
case 0: //this is the child process
close(fds[1]); //close the write end of the pipe
execl("/usr/bin/sort", "sort", (char *) 0);
break;
case -1: //failure to fork case
perror("Could not create child");
exit(EXIT_FAILURE);
default: //this is the parent process
close(fds[0]); //close the read end of the pipe
writeToChild = fdopen(fds[1], "w");
wait(&status);
break;
}
while (fscanf(stdin, "%s", word) != EOF) {
//the below isn't being printed. Why?
fprintf(writeToChild, "%s end of sentence\n", word);
}
return 0;
}
Your primary problem is that you have the wait() in the wrong place. You wait for the child to die before you've written anything to it. You also have a secondary problem that don't redirect the read end of the pipe to the sort process's standard input.
You're not closing fds[0] in the child; cleanliness suggests that you should. You do need to fclose(writeToChild) before waiting; the sort won't stop until the parent has closed the pipe to the child.
These changes (and a few other ones) lead to:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(void)
{
pid_t p;
int status;
int fds[2];
FILE *writeToChild;
char word[50];
if (pipe(fds) == -1)
{
perror("Error creating pipes");
exit(EXIT_FAILURE);
}
switch (p = fork())
{
case 0: //this is the child process
close(fds[1]); //close the write end of the pipe
dup2(fds[0], 0);
close(fds[0]);
execl("/usr/bin/sort", "sort", (char *) 0);
fprintf(stderr, "Failed to exec sort\n");
exit(EXIT_FAILURE);
case -1: //failure to fork case
perror("Could not create child");
exit(EXIT_FAILURE);
default: //this is the parent process
close(fds[0]); //close the read end of the pipe
writeToChild = fdopen(fds[1], "w");
break;
}
if (writeToChild != 0)
{
while (fscanf(stdin, "%49s", word) != EOF)
{
//the below isn't being printed. Why?
fprintf(writeToChild, "%s end of sentence\n", word);
}
fclose(writeToChild);
}
wait(&status);
return 0;
}

C: Got stuck with dup2() :-(

I have prepared a program which emulates shell (cmd) interface using pipes. There are two versions of the program:
1. Using one pipe (using a pipe from parent to child communication)
2. Using double pipe (using two pipes from parent to child and from child to parent to communicate).
So, the first program provides desired interface and works how I want, but I cannot reach the same result (interface) in the second program (using dup2() and similar).
So, I relay on your help and put the both codes below.
B.S.: You may compile and try both programs with the same way using these commands:
$ gcc prog1.c -o prog1
Next let's run:
$ ./prog1
Next let's run new terminal and try to write some data to input.txt:
$ echo pwd > input.txt
And then watch the result in the first terminal.
(This working fine for the first program but I need to get this working wit the same interface in the second program)
CODE OF THE FIRST PROGRAM (WORKING FINE):
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
void do_child(int data_pipe[]) {
int c;
int rc;
close(data_pipe[1]);
dup2(data_pipe[0], 0); /* This string provides the desired interface of the program */
char* cmd[] = { "bash", (char *)0 };
execvp("bash", cmd);
while ((rc = read(data_pipe[0], &c, 1)) > 0)
{
putchar(c);
}
exit(0);
}
void do_parent(int data_pipe[])
{
int c;
int rc;
FILE *in;
close(data_pipe[0]);
while (1)
{
in = fopen("input.txt", "r");
while ((c = fgetc(in)) > 0)
{
rc = write(data_pipe[1], &c, 1);
if (rc == -1)
{
perror("Parent: write");
close(data_pipe[1]);
exit(1);
}
}
fclose(in);
}
close(data_pipe[1]);
exit(0);
}
int main(int argc, char* argv[])
{
int data_pipe[2];
int pid;
int rc;
umask(0);
mknod("input.txt", S_IFIFO|0666, 0);
rc = pipe(data_pipe);
if (rc == -1)
{
perror("pipe");
exit(1);
}
pid = fork();
switch (pid)
{
case -1:
perror("fork");
exit(1);
case 0:
do_child(data_pipe);
default:
do_parent(data_pipe);
}
return 0;
}
CODE OF THE SECOND PROGRAM (NEED TO BE CORRECTED A LITTLE BIT):
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
/* Original version got from http://www.iakovlev.org */
int parent_to_child[2];
int child_to_parent[2];
void do_parent()
{
int c;
char ch;
int rc;
FILE *in;
close(child_to_parent[1]); /* we don't need to write to this pipe. */
close(parent_to_child[0]); /* we don't need to read from this pipe. */
while (1)
{
in = fopen("input.txt", "r");
while ((c = fgetc(in)) > 0) {
ch = (char)c;
/* write to child */
rc = write(parent_to_child[1], &ch, 1);
if (rc == -1) {
perror("child: write");
close(child_to_parent[0]);
close(parent_to_child[1]);
exit(1);
}
/* read back from child */
rc = read(child_to_parent[0], &ch, 1);
c = (int)ch;
if (rc <= 0) {
perror("parent: read");
close(child_to_parent[0]);
close(parent_to_child[1]);
exit(1);
}
putchar(c);
}
fclose(in);
}
close(child_to_parent[0]);
close(parent_to_child[1]);
exit(0);
}
void do_child()
{
int c;
char ch;
int rc;
close(parent_to_child[1]); /* we don't need to write to this pipe. */
close(child_to_parent[0]); /* we don't need to read from this pipe. */
//dup2(parent_to_child[0], STDIN_FILENO);
//dup2(child_to_parent[1], STDOUT_FILENO);
/* Some dup2() routines must be added here
to get this working as the first program above */
char* cmd[] = { "bash", (char *)0 };
execvp("bash", cmd);
while (read(parent_to_child[0], &ch, 1) > 0) {
c = (int)ch;
ch = (char)c;
putchar(ch);
rc = write(child_to_parent[1], &ch, 1);
if (rc == -1) {
perror("child: write");
close(parent_to_child[0]);
close(child_to_parent[1]);
exit(1);
}
}
close(parent_to_child[0]);
close(child_to_parent[1]);
exit(0);
}
int main(int argc, char* argv[])
{
int pid;
int rc;
umask(0);
mknod("input.txt", S_IFIFO|0666, 0);
rc = pipe(parent_to_child);
if (rc == -1) {
perror("main: pipe parent_to_child");
exit(1);
}
rc = pipe(child_to_parent);
if (rc == -1) {
perror("main: pipe child_to_parent");
exit(1);
}
pid = fork();
switch (pid) {
case -1:
perror("main: fork");
exit(1);
case 0:
do_child();
default:
do_parent();
}
return 0;
}
The major difference is here:
while ((c = fgetc(in)) > 0) {
ch = (char)c;
/* write to child */
rc = write(parent_to_child[1], &ch, 1);
/* .... */
/* read back from child */
rc = read(child_to_parent[0], &ch, 1);
/* .... */
putchar(c);
}
As I'm lazy to compile/test for you, I would simply speculate that the parent is blocked in the read(). Because other side (bash in child process) isn't guaranteed to echo every written character back. Or it might even decide to print more than one character what your code is incapable of handling.
In the case you have to poll() to see whether there is something to read or not. Or set the O_NONBLOCK flag on the child_to_parent[0] with fcntl(F_SETFL) and when errno==EAGAIN, simply skip the read() branch. And loop while there are still characters to read.
Edit1. BTW I totally missed the part: you in do_parent() loop have to use poll() on the both child_to_parent[0] and in, since other side might write something (read() wouldn't block) even when you do not write() any character to it.
Thanks to you it seems I got it's working.
So, here is updated code of do_parent:
void do_parent()
{
int c;
char ch;
int rc;
FILE *in;
struct pollfd fds[2];
int pol_ret;
fds[0].fd = child_to_parent[0];
close(child_to_parent[1]); /* we don't need to write to this pipe. */
close(parent_to_child[0]); /* we don't need to read from this pipe. */
while (1)
{
in = fopen("input.txt", "r");
fds[1].fd = fileno(in);
pol_ret = poll(fds, 2, 500);
while ((c = fgetc(in)) > 0) {
ch = (char)c;
/* write to child */
rc = write(parent_to_child[1], &ch, 1);
if (rc == -1) {
perror("child: write");
close(child_to_parent[0]);
close(parent_to_child[1]);
exit(1);
}
/* read back from child */
if (fds[0].revents & POLLIN)
{
rc = read(child_to_parent[0], &ch, 1);
c = (int)ch;
if (rc <= 0) {
perror("parent: read");
close(child_to_parent[0]);
close(parent_to_child[1]);
exit(1);
}
putchar(c);
}
}
fclose(in);
}
close(child_to_parent[0]);
close(parent_to_child[1]);
exit(0);
}
Also I have added this into do_child():
dup2(parent_to_child[0], STDIN_FILENO);

Resources