0

The objective is to use dup, dup2 and pipe to communicate between parent and child processes. Just to get a feel of how to use dup and pipes. The function multby, called in by the child process, takes a number as an argument (3) and multiply it with a user input (in this case 5 as printed in parent) to get a product. (which should be 15)

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

void main(int argv, char *argc) { 
    int testpipe[2]; 
    pipe(testpipe); 
    int PID = fork(); 
    if (PID == 0) { 
        dup2(testpipe[0], 0); 
        close(testpipe[1]); 
        execl("./multby", "multby", "3", NULL); 
        close(testpipe[0]); 
        close(0); 
    } else { 
        dup2(testpipe[1], 1); 
        close(testpipe[0]); 
        printf("5"); 
        close(1); 
        close(testpipe[1]); 
        wait(NULL); 
    }  
}

Here is the code for multby. I also added some fprintf statement to troubleshoot. In particular, it prints out the 2 numbers to be multiplied (which should be 3 and 5 as mentioned above).

#include <stdio.h>

int main(int argv, char *argc[]) {
  fprintf(stderr,"exec successful\n");
  if (argv < 2) {
    fprintf(stderr, "Usage: %s <factor>\n", argc[0]);
    return 1;
  }      

  size_t a, b;
  sscanf(argc[1], "%zu", &a);
  fprintf(stderr, "a: %zu\n", a);

  if (scanf("%zu", &b) != 1) {
    fprintf(stderr, "No input %zu\n", a);
    return 1; 
  } 
  fprintf(stderr, "b: %zu\n", b);
  printf("%zu", a*b);
  fprintf(stderr, "multby successful\n");
  return 0;
}

However, this is my result: exec successful a: 3 No input 3

5 was not registered from the read end of the pipe.

Could someone please advise me on what I am doing wrong?

1 Answers1

3

You use printf(“5”); to relay the data to the child, but standard output is at least line-buffered so nothing is written until a newline is sent (or fflush(stdout)). Then you close(1); to close standard output, which does not flush the standard I/O buffers. So nothing is sent to the child. Use fclose(stdout); to get the 5 sent to the child.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • Thank you! I get that fflush and fclose take in file struct, while dup returns an integer. In the case I am not using stdout or stdin, is there a way for me to get the file stuct from the int? I tried doing fclose(fdopen(1, "w")); but it does not work. – Marine Biologist Kujo Feb 17 '20 at 01:34
  • No, there isn't. I've seen your question [How do I get the file pointer that is being referenced by a file descriptor?](https://stackoverflow.com/q/60254987/15168), and I agree with the comments there which also say "No". You're going at it backwards. On POSIX-like systems, file descriptors are the fundamental type and file streams (`FILE *`) are derived types. You can find the file descriptor used by a file stream. You cannot find the file stream using a file descriptor because there may not be one, and the kernel has no knowledge of file streams and cannot help, and there's no API either. – Jonathan Leffler Feb 17 '20 at 02:56