fork and stdin


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers fork and stdin
# 1  
Old 11-28-2008
fork and stdin

When a process fork(), the child share the same file descriptors as his father. Thus, they share the same stdin. Quick and dirty exemple below (sorry for the ugly gets() call) :

Code:
#include <stdio.h>
#include <unistd.h>

int main()
{
        char buf[512];

        if (fork()) { /*parent */
                while(1) {
                        gets(buf);
                        printf("[%d] parent: %s\n", getpid(), buf);
                }
        } else { /* child */
                while(1) {
                        gets(buf);
                        printf("[%d] children: %s\n", getpid(), buf);
                }
        }
}

Question 1 :
If I'm right, after the fork(), the parent and the child belong to the same group process which is a foreground group and which share the same console. As there is a concurrent read on stdin, I was thinking that the process that read the lines in input is pick up randomly. However, on my Linux system, the child always read first. Is it normal ?

Question 2:
If I launch the command like that :
Code:
$ ./a.out > /tmp/foobar

I was thinking that after the redirection, the parent and the child would both write on /tmp/foobar. But the /tmp/foobar file remains totaly empty !
Why ?

Thanks for your help,

Arnauld
# 2  
Old 12-05-2008
Q1: Right. On some systems and in some circumstances, both parent and child indeed get the lines randomly. I've seen instances of this when background process and foreground process were both opening the tty for input. Crazy.

Q2: Perform a flush() after each printf. That might change things.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

STDIN-perl

Good morning! How do I make the info that someone inputs from @userArray = <STDIN>, a new array? @userArray = <STDIN>; while () { chomp; last if ! /\d/; push(@userArray,$_); } my($sum,$avg) = &sumIt(@userArray); print "Total:$sum\nAverage:$avg\n"; Im... (2 Replies)
Discussion started by: bigben1220
2 Replies

2. UNIX for Dummies Questions & Answers

stdin redirection

Hello, my C application under unix runs in redirecting stdin to a file. Example:$appli1 <file1. This application waits often on a scanf(). But I would temporarely reassign stdin at the keyboard for waiting a user's answer. So I thought to add system("appli2"); in the code of appli1. In its... (4 Replies)
Discussion started by: cypleen
4 Replies

3. UNIX for Dummies Questions & Answers

redirection stdin

Bonjour, Mon application en C sous linux tourne en redirigeant stdin vers un fichier. Exemple; $appli1 <file1. PB: Je voudrais temporairement redonner la main au user sur le clavier. Alors je pensais ajouter system("appli2"); dans appli1. Dans son main() , appli2() fait seulement un... (1 Reply)
Discussion started by: cypleen
1 Replies

4. Shell Programming and Scripting

Redirecting stdin from fd 3-9?

Hi I'm trying to do something on the bash command line that I will later put into a bash shell script. I'm trying to take a program that reads stdin (using getline) and be able to keep it running in the background and fire "commands" to it. So what I thought I should do was to try taking... (3 Replies)
Discussion started by: niceguyeddie
3 Replies

5. UNIX for Dummies Questions & Answers

How to write to stdin of another program (program A -> [stdin]program B)

Hi, Program A: uses pipe() I am able to read the stdout of PROGAM B (stdout got through system() command) into PROGRAM A using: * child -> dup2(fd, STDOUT_FILENO); -> execl("/path/PROGRAM B", "PROGRAM B", NULL); * parent -> char line; -> read(fd, line, 100); Question: ---------... (3 Replies)
Discussion started by: vvaidyan
3 Replies

6. Programming

C++ How to use pipe() & fork() with stdin and stdout to another program

Hi, Program A: uses pipe() I am able to read the stdout of PROGAM B (stdout got through system() command) into PROGRAM A using: * child -> dup2(fd, STDOUT_FILENO); -> execl("/path/PROGRAM B", "PROGRAM B", NULL); * parent -> char line; -> read(fd, line, 100); Question:... (2 Replies)
Discussion started by: vvaidyan
2 Replies

7. Programming

How to write to stdin of another program (program A -> [stdin]program B)

Hi, Program A: uses pipe() I am able to read the stdout of PROGAM B (stdout got through system() command) into PROGRAM A using: * child -> dup2(fd, STDOUT_FILENO); -> execl("/path/PROGRAM B", "PROGRAM B", NULL); * parent -> char line; -> read(fd, line, 100); Question: ---------... (1 Reply)
Discussion started by: vvaidyan
1 Replies

8. Shell Programming and Scripting

redirect STDIN

can you redirect STDIN with command arguments? I have tried this approach: # ./script -option <argument1> <argument2> 0<$2 # $2: ambiguous redirect Is this possible? (4 Replies)
Discussion started by: prkfriryce
4 Replies

9. HP-UX

stdin device on HP

How can I access the standard-in device in HP-UX? I am trying to automate sftp on an HP-UX system. On solaris I can just do: sftp -b /dev/fd/0 remotehost <<EOF cd pub ascii get filename.txt bye EOF (2 Replies)
Discussion started by: dangral
2 Replies

10. Programming

stdin

hi, how does a program know whether some data are available from stdin? I would like to make a program which could read its data from stdin and _if_there_is_nothing_at_stdin_ from a file which name is given as an argument. If there is nothing in stdin and no filename is given as argument,... (2 Replies)
Discussion started by: marquis
2 Replies
Login or Register to Ask a Question