display in a child process a command called in the parent one

Hi , Could you tell me if I am right

  1. Using fork(), pipe(), execlp() and dup() (see man 2 dup), write a C program executing the command ps -j in a parent process, displaying the result in a child process.
#include <unistd.h>
#include <errno.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <fcntl.h>
#include <termios.h>
int main()
{

	
	int fd[2];
	pid_t childpid;
	
	pipe(fd);
	if(( childpid=fork()) == 0) {
	//child
	
		dup2(fd[1],STDOUT_FILENO);
		close(fd[0]);
		close(fd[1]);
	}
	else{ //parent
		dup2(fd[0],STDIN_FILENO);
		close(fd[0]);
		close(fd[1]);
		
		execl("/bin/ps","ps","-j",NULL);
		perror("the exec of sort failed");
	}
	exit(0);
	
}

Is dup neccessary?

Can't we make this operation just using the pipe itself at 100%?

Thank you very much.

You are using the pipe itself, 100%. The only difference is what file descriptors the pipe is sitting at.

Not all the dups are necessary, the parent can just read directly from the reading end of the pipe. But the child process, /bin/ps, doesn't know or care that you have a pipe open -- it writes to standard output, no ifs, ands, or buts. The only way to tell it to write to your pipe, is to make it's standard output the pipe.

how do we do that?

I would check out the freopen function. I think you can place it just before your exec() call and the new process would inherit the open file descriptors ( someone correct me if I'm wrong ).

NAME
       fopen, fdopen, freopen - stream open functions

SYNOPSIS
       #include <stdio.h>

       FILE *fopen(const char *path, const char *mode);
       FILE *fdopen(int fildes, const char *mode);
       FILE *freopen(const char *path, const char *mode, FILE *stream);

DESCRIPTION
...
       The freopen function opens the file whose name is the string pointed to by path and
       associates the stream pointed to by stream with it.  The  original  stream  (if  it
       exists)  is  closed.  The mode argument is used just as in the fopen function.  The
       primary use of the freopen function is to change the file associated with  a  stan-
       dard text stream (stderr, stdin, or stdout).


Standard input, standard output, and standard error are always sitting at the same file descriptors... stdin is file #0, stdout is file #1, and stderr is file #2. To make them point anywhere else, you duplicate another file descriptor overtop of them; this will force that file descriptor to point somewhere else. That's what the dup2 calls are for.

That's what the dup2 calls are doing already. The stdio method could work, but it's best not to use stdio functions with file descriptors -- stdio has it's own buffers, etc. that can do strange things when forking.

My apology for this wasteful post..

thanks guys :slight_smile: