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-10-2008 12:22 AM
Parent child Relation !! using awk/sed ??? varungupta UNIX for Advanced & Expert Users 0 01-29-2008 02:24 PM
Implementing 2 pipes between a parent and child process bwgoudey High Level Programming 2 09-25-2005 12:14 AM
parent and child process question? tosa High Level Programming 0 02-16-2005 03:04 PM
kill parent and child larry UNIX for Dummies Questions & Answers 4 01-12-2003 12:18 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 01-17-2007
remid1985 remid1985 is offline
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.
  #2 (permalink)  
Old 01-17-2007
Corona688 Corona688 is offline
Registered User
  
 

Join Date: Aug 2005
Location: Saskatchewan
Posts: 1,944
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.
  #3 (permalink)  
Old 01-19-2007
elzalem's Avatar
elzalem elzalem is offline
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?
  #4 (permalink)  
Old 01-19-2007
nathan nathan is offline VIP Member  
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).
  #5 (permalink)  
Old 01-19-2007
Corona688 Corona688 is offline
Registered User
  
 

Join Date: Aug 2005
Location: Saskatchewan
Posts: 1,944
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.
  #6 (permalink)  
Old 01-19-2007
Corona688 Corona688 is offline
Registered User
  
 

Join Date: Aug 2005
Location: Saskatchewan
Posts: 1,944
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.
  #7 (permalink)  
Old 01-19-2007
kermit's Avatar
kermit kermit is offline
Registered User
  
 

Join Date: Jan 2007
Posts: 24
My apology for this wasteful post..
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 07:49 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0