1

so in this code snippet a process forked a child process. the child process calculated a random number r, and called linux command 'head -r "file' with an exec function which destroys the process itself, but to send the result back to the parent process the child process first duplicated the writing end of a pipe p, shared with the parent process and then closed both ends of the pipe p and closed the stdout file descriptor too...after execlp the parent process could read the result of the command 'head -r "fil2"' from the pipe p. How is this possible?

 if (pid == 0) { /* code of child */ srand(time(NULL)); nr=atoi(argv[(i*2)+2]); r=mia_random(nr); //calc random value close(1); //closing standard output??? dup(p[1]); //duplicating write end of inherited pipe from parent close(p[0]);//closing read end of inherited pipe close(p[1]);//closing write end of inherited pipe //creating a variable to hold an argument for `head` sprintf(option, "-%d", r); //calling head on a file given as argument in main execlp("head", "head", option, argv[(i*2)+1], (char *)0); /* must not be here anymore*/ /* using perror to check for errors since stdout is closed? or connected to the pipe?*/ perror("Problem esecuting head by child process"); exit(-1); } 

Why wasn't the result of head written to stderr instead? How come it was written to the dup(p[1])???

3
  • Keep in mind srand(time(NULL)) is not really random at all, it's entirely predictable what those values will be. If actual randomness is important, avoid using that family of functions and use one that's actually random, or read data from /dev/random. Commented Dec 1, 2017 at 16:13
  • Why would it go to stderr? Head writes its data to its file descriptor 1. You arranged it so that was the write side of the pipe. So the data went into the pipe. Commented Dec 1, 2017 at 16:20
  • You closed fd 1. Then, when you called dup, it successfully duplicated the write end of the pipe on file descriptor 1. Check the return value of dup. Also, try running the code without closing stdout before you call dup. Also, try the code with dup2. Commented Dec 1, 2017 at 16:21

1 Answer 1

1

The system is guaranteed to open each new file at the lowest possible file descriptor.

Effectively, that means that if fd 0 and 1 are open and p[1] != 1, then

close(1); dup(p[1]); 

in a single-threaded process is equivalent to

dup2(p[1],1); 

or in other words, if the dup call in this context succeeds, it will return (filedescriptor) 1.

Sign up to request clarification or add additional context in comments.

2 Comments

the best answer, thats explains it!
@Micky It's also the worst answer, since there's only one answer :D. But thanks. Please upvote/accept if you're satisfied with it.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.