0

I have a parent program that sends a integer to a child, and the child program multiplies the number by two and gives back to the parent.

In a main program I create a pipe and fork() and execl() the child, after a switch I pass the value through pip to child in child i can get the value, but How can I get a result back from the child to the parent after a execl()?.

child.c
#include <stdio.h> 
#include <stdlib.h>
#include <unistd.h>

int main(){
int fd,nread,result;
char data[20];
fd=atoi(argv[1]);
nread=read(fd,data,sizeof(data));
switch(nread)
{
    case -1:
        break;
    default:
         result=atoi(data)*2;
         sprintf(result,"%d",(result));
         //how can here return data to the parent?
         break;

}
}
Z xie
  • 9
  • 2
  • 1
    Either write another integer back on the child's stdout (so you need another pipe), or just return the value from main (so the parent can read it from wait or waiting) – Useless Oct 19 '19 at 21:20
  • see https://stackoverflow.com/questions/12864265/using-pipe-to-pass-integer-values-between-parent-and-child . – muaz Oct 19 '19 at 21:32
  • if I want to get back by another pipe, I should create on the parent program? – Z xie Oct 19 '19 at 21:41
  • and how could i read in the parent program the data? – Z xie Oct 19 '19 at 21:42
  • OT: for ease of readability and understanding: 1) consistently indent the code. Indent after EVERY opening brace '{'. Unindent before every closing brace '}'. Suggest each indent level be 4 spaces. – user3629249 Oct 20 '19 at 00:52
  • BTW: the function: `read()` returns a `ssize_t`, not an `int` – user3629249 Oct 20 '19 at 00:59

2 Answers2

0

at this point in the code:

//how can here return data to the parent?

insert the statement:

return result;

and eliminate the call to sprintf()

Then in the parent process:

int status;
waitpid( pid, &status );
printf( "%d\n", status );
user3629249
  • 16,402
  • 1
  • 16
  • 17
0

Use two pipes.

#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>

void xclose(int fd);
void xdup2(int a, int b);
int
main(int argc, char **argv)
{
        int p1[2];
        int p2[2];
        int val = argc > 1 ? strtol(argv[1], NULL, 10) : 1;
        char buf[128];
        int len;
        ssize_t rc;
        if(pipe(p1) || pipe(p2)) {
                perror("pipe");
                return EXIT_FAILURE;
        }
        switch(fork()) {
        case -1:
                perror("fork");
                return EXIT_FAILURE;
        case 0:
                xdup2(p1[0],STDIN_FILENO);
                xclose(p1[0]);
                xclose(p1[1]);
                xdup2(p2[1],STDOUT_FILENO);
                xclose(p2[0]);
                xclose(p2[1]);
                execlp("awk", "awk", "{print $1 * 2}", NULL);
                perror("exec");
                return EXIT_FAILURE;
       default:
                len = sprintf(buf, "%d", val);
                write(p1[1], buf, len);
                xclose(p1[1]);
                xclose(p1[0]);
                xclose(p2[1]);
                if(( rc = read(p2[0], buf, sizeof buf)) == -1) {
                        perror("read");
                }
                xclose(p2[0]);
                buf[rc] = '\0';
                printf("%s", buf);
        }
        return EXIT_SUCCESS;
}

void
xclose(int fd) {
        if(close(fd)) {
                perror("close");
                exit(EXIT_FAILURE);
        }
}
void
xdup2(int a, int b) {
        if(dup2(a,b) == -1) {
                perror("dup2");
                exit(EXIT_FAILURE);
        }
}
William Pursell
  • 204,365
  • 48
  • 270
  • 300