Pipe bc calculator output back to parent fork() - c

I am trying to get the program to take input from the initial console. Take the arguments and send them to a child fork, run the bc calculator on the data, then return the finished value back to the parent.
I want a user to enter echo "11*13" | ./mycalc
And get the response of: 143
mycalc.c
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<unistd.h>
#include <sys/wait.h>
int main(int argc, char **argv) {
int p[2];
int r[2];
pipe(p);
pipe(r);
pid_t childId = fork();
if(childId == -1)
{
perror("Failed to fork");
return -1;
}
if ( childId == 0)
{
printf("Child Process Has Run\n");
close(p[1]);
close(r[0]);
dup2(p[0], STDIN_FILENO);
dup2(r[1], STDOUT_FILENO);
execlp("bc", "bc", NULL);
} else {
printf("Parent process has run\n");
close(p[0]);
close(r[1]);
write(p[1], argv[1], strlen(argv[1]));
char data[128];
int len = read(r[0], data, 13);
if (len < 0) {
perror("Error reading from child");
}
printf("The data is %s", data);
}
return 1;
}
When I run it I get
Parent process has run
Child Process Has Run
and the cursor just sits there like it is waiting for input, but no matter what I type it does nothing.

The issue that I had was the pipes not being properly closed. After I make sure the pipes closed I was able to continue.

Related

How to fix data write or read using pipe in c program is giving wrong output?

I am trying to get an integer input in the child process and send it to the parent process using pipe()
but I receive garbage values every time in the parent process.
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include<sys/wait.h>
int main(int argc, char *argv[])
{
pid_t pid;
int fd[2];
char *args[] = {"", NULL};
int cnum,pnum;
pid = fork();
if (pid < 0)
{
perror("fork");
exit(1);
}
if(pipe(fd) == -1)//fd[0] for read fd[1] for write
{
perror("pipe");
exit(1);
}
if(pid == 0)
{
close(fd[0]);
printf("\n**In the child process**\n");
printf("Enter Number : ");
scanf("%d",&cnum);
write(fd[1],&cnum,sizeof(int));
close(fd[1]);
}
else
{
wait(NULL);
close(fd[1]);
printf("\n**In the parent precess**\n");
read(fd[0],&pnum,sizeof(int));
close(fd[0]);
printf("Number recieved = %d\n",pnum);
printf("PID = %d\n", getpid());
execv("./sayHello", args);
printf("Error");
}
}
Output of the above code
**In the child process**
Enter Number : 212
**In the parent precess**
Number recieved = 1036468968
PID = 22528
Hillo Amol
PID = 22528
I give input of 212 but in parent 1036468968 received.
You call fork before you create the pipe FDs. After you call fork, the parent and the child both create their own pair of pipe FDs, and there's no shared pipe between them.
Create the pipe before you fork and it could work.
As drorfromthenegev suggest problem is arising due to I am calling pipe() after fork().
So I call pipe() first and the i call fork() and it works..
Workable solution
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include<sys/wait.h>
int main(int argc, char *argv[])
{
pid_t pid;
int fd[2];
char *args[] = {"", NULL};
int cnum,pnum;
if(pipe(fd) == -1)//fd[0] for read fd[1] for write
{
perror("pipe");
exit(1);
}
pid = fork();
if (pid < 0)
{
perror("fork");
exit(1);
}
if(pid == 0)
{
close(fd[0]);
printf("\n**In the child process**\n");
printf("Enter Number : ");
scanf("%d",&cnum);
write(fd[1],&cnum,sizeof(int));
close(fd[1]);
}
else
{
wait(NULL);
close(fd[1]);
printf("\n**In the parent precess**\n");
read(fd[0],&pnum,sizeof(int));
close(fd[0]);
printf("Number recieved = %d\n",pnum);
printf("PID = %d\n", getpid());
execv("./sayHello", args);
printf("Error");
}
}

close pipe from parent and child process

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/wait.h>
int main() {
int p[2];
pipe(p);
if (fork() == 0) {
// child
/*#0*/ close(p[1]);
int received = -1;
while (read(p[0], &received, 4) != 0) {
printf("receive integer: %d\n", received);
received = -1;
}
printf("child exit\n");
exit(0);
} else {
// parent
/*#1*/ close(p[0]);
int sent = 42;
write(p[1], &sent, 4);
/*#2*/ close(p[1]);
printf("wait for child\n");
wait(0);
}
printf("finished\n");
}
I'm trying to understand fork and pipe in C. This program fork a child process, which receive an integer from parent process then exit when pipe closed. When executing, it prints
wait for child
receive integer: 42
child exit
finished
Yet the while loop got stuck after close(p[1]); at position #0 removed: that read would infinitely wait for an incoming variable from the pipe and never detect the pipe closed.
Can someone explain to me why p[1] has to be closed by both parent (position #2) and child (position #0) process?
Here is the code (from Linux manual page) with comments at the bottom of the code.
https://man7.org/linux/man-pages/man2/pipe.2.html
At /#2/ close(pipefd[1]), the comment states that "Reader will see EOF". It means there is nothing to read into child process anymore and then the statement "read(p[0], &received, 4)" will return 0. In the Linux manaul page https://man7.org/linux/man-pages/man2/read.2.html
states that "On success, the number of bytes read is returned (zero indicates end of file)"
#include <sys/types.h>
#include <sys/wait.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
int
main(int argc, char *argv[])
{
int pipefd[2];
pid_t cpid;
char buf;
if (argc != 2) {
fprintf(stderr, "Usage: %s <string>\n", argv[0]);
exit(EXIT_FAILURE);
}
if (pipe(pipefd) == -1) {
perror("pipe");
exit(EXIT_FAILURE);
}
cpid = fork();
if (cpid == -1) {
perror("fork");
exit(EXIT_FAILURE);
}
if (cpid == 0) { /* Child reads from pipe */
close(pipefd[1]); /* Close unused write end */
while (read(pipefd[0], &buf, 1) > 0)
write(STDOUT_FILENO, &buf, 1);
write(STDOUT_FILENO, "\n", 1);
close(pipefd[0]);
_exit(EXIT_SUCCESS);
} else {/* Parent writes argv[1] to pipe */
close(pipefd[0]); /* Close unused read end */
write(pipefd[1], argv[1], strlen(argv[1]));
/*#2*/ close(pipefd[1]); /* Reader will see EOF */
wait(NULL); /* Wait for child */
exit(EXIT_SUCCESS);
}
}

Communicating between a child process and a parent process

I'm trying to communicate between two processes, a parent process that sends instructions and a child process that returns a response.
void create_player(Game *g, Player *p, int player, char* command) {
int send[2];
int receive[2];
int file;
if(pipe(send)!=0) {
_exit(99);
}
if(pipe(receive)!=0) {
_exit(99);
}
g->player[player].id = fork();
if(g->player[player].id < 0) {
fprintf(stderr, "Unable to start subprocess\n");
fflush(stderr);
exit(5);
}
//Parent process
else if(g->player[player].id>0) {
g->player[player].send = fdopen(send[1], "w");
g->player[player].receive = fdopen(receive[0], "r");
close(send[0]);
close(receive[1]);
//Child process
} else {
close(send[1]);
close(receive[0]);
dup2(send[0], 0);
dup2(receive[1], 1);
close(send[0]);
close(receive[1]);
file = open("/dev/null", O_WRONLY);
for(i =0; i < player; i++) {
fclose(g->player[i].send);
fclose(g->player[i].receive);
}
char width[1024];
char playerId[26];
char numOfPlayers[26];
char seed[1024];
char numOfCarriages[1024];
sprintf(seed, "%d", g->seed);
sprintf(width, "%d", g->numOfCarriages);
sprintf(playerId, "%d", player);
sprintf(numOfPlayers, "%d", g->numOfPlayers);
char* args[] = {command, numOfPlayers, playerId, width, seed, NULL};
execlp(command, command, numOfPlayers, playerId, width, seed, (char*) 0);
_exit(99);
}
When I run the code block to make the child processes, none of the messages are sent through (Parent stdout -> child stdin). I've added some examples to show how I'm handling the messaging. Any help would be greatly appreciated.
In the parent process: Parent Process sends message to child process
//Send message to start the game
void send_startGame(Game *g) {
fprintf(g->player[g->currentPlayer].send, "startGame\n");
fflush(g->player[g->currentPlayer].send);
}
In the child process: child message receives message from parent
void read_message(Game *g) {
char message[2048];
if(fgets(message, 2048, stdin) == NULL) {
fprintf(stderr, "Error in message\n");
}
//Receive start game message
if(strncmp("startGame\n", message, 9)==0){
start_game(g);
}
}
Try to reduce your code, there is wrong close in the child code :
close(send[1]);
dup2(send[0], 0);
close(send[0]);
After the dup2, the filedescriptor send[0] is no more related to the send pipe input, and it could close unexpectly an other filedescriptor in the child process.
Your code doesnot make communication from stdout of parent to stdin of child.
Hereafter a small sample that redirect stdout of parent to input of pipe and output of pipe to stdin of child.
#include <stdio.h>
#include <unistd.h>
int main(int argc, char **argv) {
int send[2];
if(pipe(send)!=0) {
perror("pipe");
} else {
int pid = fork();
if(pid < 0) {
perror("fork");
}
else if(pid>0) {
//Parent process
close(send[0]); // close pipe input
dup2(send[1], 1); // replace stdout with pipe output
// send message to child
fprintf(stdout, "send to child\n");
fflush(stdout);
} else {
//Child process
close(send[1]); // close pipe output
dup2(send[0], 0); // replace stdin with pipe input
char message[2048];
if(fgets(message, 2048, stdin) != NULL) {
fprintf(stderr, "message from parent:%s", message);
}
}
}
}
Ideone link

How can I redirect stdout back to the terminal in a multi process c program?

I'm trying to write a c program that is the equivalent of the linux command ps -aux | sort -r -n -k 5 but I'm not getting any output
Here's my code
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
int main(int argc, char ** argv){
int pipes[2];
int r;
r = pipe(pipes);
if (r < 0) {
fprintf(stderr, "pipe failed\n\n"); // stderr is a FILE* variable for the standard error file (terminal)
exit(2);
}
int saved_stdout = dup(1);
int pid = fork();
if(pid > 0){
// Parent
pid = fork();
if(pid > 0){
// Parent
wait(NULL);
}else if (pid == 0){
// Child 1
printf("Child 1\n");
dup2(pipes[1], 1);
close(pipes[0]);
close(pipes[1]);
execlp("/bin/ps", "ps", "-aux", (char*) NULL);
exit(0);
}else{
fprintf(stderr, "FORK FAILED\n\n");
return 1;
}
}else if (pid == 0){
// Child 2
printf("Child 2\n");
dup2(pipes[0], 0);
close(pipes[0]);
close(pipes[1]);
dup2(saved_stdout, 1);
close(saved_stdout);
execlp("/bin/sort", "sort", "-r", "-n", "-k", "5", (char*)NULL);
exit(0);
}else{
fprintf(stderr, "FORK FAILED\n\n");
return 1;
}
wait(NULL);
printf("Exiting parent\n");
}
The output I get is this
Child 1
Child 2
Exiting parent
I doesn't actually print the execlp command, I've tried saving stdout to variable saved_stdout which is the solution I found in another answer, but that doesn't seem to work.
How can I redirect stdout back to the terminal?
Strange my output with your code is:
Child 1
Child 2
and the program don't stop. Or you sure that your output is valid ?
Whatever, your problem is that you don't close your pipe in your parents. Just add:
close(pipes[0]);
close(pipes[1]);
In your both parents (before your two call to wait()).
Plus saved_stdout is useless in your case, because you only change stdout in your child1. saved_stdout and 1 describe the same file in your child2.

pipe call and synchronization

I'm experimenting some problems with this code:
#include <stdio.h>
#include <stdlib.h>
#define SIZE 30
#define Error_(x) { perror(x); exit(1); }
int main(int argc, char *argv[]) {
char message[SIZE];
int pid, status, ret, fd[2];
ret = pipe(fd);
if(ret == -1) Error_("Pipe creation");
if((pid = fork()) == -1) Error_("Fork error");
if(pid == 0){ //child process: reader (child wants to receive data from the parent)
close(fd[1]); //reader closes unused ch.
while( read(fd[0], message, SIZE) > 0 )
printf("Message: %s", message);
close(fd[0]);
}
else{//parent: writer (reads from STDIN, sends data to the child)
close(fd[0]);
puts("Tipe some text ('quit to exit')");
do{
fgets(message, SIZE, stdin);
write(fd[1], message, SIZE);
}while(strcmp(message, "quit\n") != 0);
close(fd[1]);
wait(&status);
}
}
Code works fine but I can't explain why! There is no explicit sync between parent and child processes. If the child-process executes before parent, read must return 0 and the process ends, but for some reason it waits for the parent execution. How do you explain this? Maybe I'm missing something.
(Edited)
Since you didn't use O_NONBLOCK in pipe2, read is blocking by default. Therefore it waits until data are written into the pipe.

Resources