The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
Google UNIX.COM


High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Can a child process return a specific value to a parent process ? Ametis1970 High Level Programming 8 04-09-2008 08:22 PM
Parent child Relation !! using awk/sed ??? varungupta UNIX for Advanced & Expert Users 0 01-29-2008 11:24 AM
Implementing 2 pipes between a parent and child process bwgoudey High Level Programming 2 09-24-2005 08:14 PM
parent and child process question? tosa High Level Programming 0 02-16-2005 12:04 PM
kill parent and child larry UNIX for Dummies Questions & Answers 4 01-11-2003 09:18 PM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1  
Old 01-17-2007
Registered User
 

Join Date: Sep 2006
Posts: 9
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.


PHP Code:
#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.
Reply With Quote
Forum Sponsor
  #2  
Old 01-17-2007
Registered User
 

Join Date: Aug 2005
Location: Saskatchewan
Posts: 979
Quote:
Originally Posted by remid1985
Is dup neccessary?

Can't we make this operation just using the pipe itself at 100%?
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.
Reply With Quote
  #3  
Old 01-18-2007
elzalem's Avatar
Registered User
 

Join Date: Nov 2006
Location: Lebanon
Posts: 33
Quote:
Originally Posted by Corona688
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?
Reply With Quote
  #4  
Old 01-19-2007
Supporter
 

Join Date: Jul 2006
Posts: 156
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 ).

Code:
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).
Reply With Quote
  #5  
Old 01-19-2007
Registered User
 

Join Date: Aug 2005
Location: Saskatchewan
Posts: 979
Quote:
Originally Posted by elzalem
how do we do that?
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.
Reply With Quote
  #6  
Old 01-19-2007
Registered User
 

Join Date: Aug 2005
Location: Saskatchewan
Posts: 979
Quote:
Originally Posted by nathan
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 ).
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.
Reply With Quote
  #7  
Old 01-19-2007
kermit's Avatar
Registered User
 

Join Date: Jan 2007
Posts: 17
My apology for this wasteful post..
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 03:25 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0