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


 
Thread Tools Search this Thread
Top Forums Programming display in a child process a command called in the parent one
# 1  
Old 01-17-2007
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  
Old 01-17-2007
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  
Old 01-19-2007
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  
Old 01-19-2007
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  
Old 01-19-2007
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.
# 6  
Old 01-19-2007
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.
# 7  
Old 01-19-2007
My apology for this wasteful post..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

System should not kill the child process when parent id is 1

HI i would like to know how i can simulate a shell scripts for my requirement. example Server name child Process id Parent Process id Vpesh 16013 15637 Server name child Process id Parent Process id Vpesh 16014 15637 Server name child... (1 Reply)
Discussion started by: vpesh
1 Replies

2. Shell Programming and Scripting

forking a child process and kill its parent to show that child process has init() as its parent

Hi everyone i am very new to linux , working on bash shell. I am trying to solve the given problem 1. Create a process and then create children using fork 2. Check the Status of the application for successful running. 3. Kill all the process(threads) except parent and first child... (2 Replies)
Discussion started by: vizz_k
2 Replies

3. Emergency UNIX and Linux Support

signal between parent process and child process

Hello, everyone. Here's a program: pid_t pid = fork(); if (0 == pid) // child process { execvp ...; } I send a signal (such as SIGINT) to the parent process, the child process receive the signal as well as the parent process. However I don't want to child process to receive the... (7 Replies)
Discussion started by: jackliang
7 Replies

4. UNIX for Dummies Questions & Answers

Command to find parent and child process?

Hi, I have a script that calls other scripts/commands which may or may not spawn other process. From my understanding, when I do a ps -ef, the highest numbered process ID is supposed to be the parent ID of all the other related child processes, is this correct? In most or all... (3 Replies)
Discussion started by: newbie_01
3 Replies

5. Programming

getting the return from forked child process to parent in C++

This needs to work on HPUX and Linux. I do a fork and create a child process. During execution of the child process, it is possible child become lost or get killed. That is the reason why I create the child process. However if the child process doesnt get killed, I do want to know the return... (2 Replies)
Discussion started by: usustarr
2 Replies

6. Programming

IPC - pipes between parent and child process

Hi guys, I'm having some problem here, I'm studying pipes, and i want to create a shell in C and at this point a don't want to use semaphores, instead I want to use tricks. Straight to the doubt: I've a parent and a child process, and both of them has some code to execute, and the child process... (5 Replies)
Discussion started by: pharaoh
5 Replies

7. Shell Programming and Scripting

[KSH/Bash] Starting a parent process from a child process?

Hey all, I need to launch a script from within 2 other scripts that can run independently of the two parent scripts... Im having a hard time doing this, if anyone knows how please let me know. More detail. ScriptA (bash), ScriptB (ksh), ScriptC (bash) ScriptA, launches ScriptB ScirptB,... (7 Replies)
Discussion started by: trey85stang
7 Replies

8. Shell Programming and Scripting

How to make the parent process to wait for the child process

Hi All, I have two ksh script. 1st script calls the 2nd script and the second script calls an 'C' program. I want 1st script to wait until the 'C' program completes. I cant able to get the process id for the 'C' program (child process) to make the 1st script to wait for the second... (7 Replies)
Discussion started by: sennidurai
7 Replies

9. UNIX for Advanced & Expert Users

How to find all the child processes of a parent process

Hi I am trying to see if there are some options in ps command or if there is a shell script which basically shows you all the processes spawned by a parent process , then all the processes of its child processes and so on down the hierarchy may be like a tree structure. It might be a generic... (6 Replies)
Discussion started by: clifford
6 Replies

10. Programming

parent and child process question?

Hi everybody, I'm trying to understand how a parent and child processes interact. This function( below) basically measures the fork time from the perspective of the parent only. what i would like to know is how to measure the time from the perspective of parent and child (ie: inserting... (0 Replies)
Discussion started by: tosa
0 Replies
Login or Register to Ask a Question