Question on forks and pipes


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Question on forks and pipes
# 1  
Old 08-28-2006
Question on forks and pipes

I am trying to figure out why when i have the following code

int main( { printf("0\n"); fork(); printf("1\n"); exit(0);}

and type in the shell

a.out | cat

the output of this program is
0
1
0
1

instead of
0
1
1

does anyone know?
# 2  
Old 08-28-2006
What OS are you using? I tried this on Solaris 8, and its working fine.(0 1 1).
# 3  
Old 08-29-2006
Blowtorch, try piping it to 'cat' or echo $(a.out).

I get the same results.
Code:
$ a.out
0
1
1
$ echo $(a.out)
0 1 0 1
$ a.out | cat
0
1
0
1
$

It stumped me for awhile, but I believe I know why this is happening. The fork() call is copying everything in memory to a child process, including the output buffer. Since the output buffer wasn't flushed before the fork, there was a '0' character in the buffer, and that was also copied. Try flushing the buffer before the fork(). It worked for me.

Code:
int main () { printf("0\n"); fflush(stdout);  fork(); printf("1\n"); exit(0);}

# 4  
Old 08-29-2006
Nathan

Thanks that seems to make perfect sense
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shells, forks, subprocesses... oh my

all, i've been reading to try and get an abstract idea of the process effeciency of commands , sed, bash, perl, awk, find, grep, etc which processes will spawn?, fork?, launch subshell?, etc and under what conditions? how do you know which commands have the faster and better stdio... (2 Replies)
Discussion started by: f77hack
2 Replies

2. Programming

Question on pipes in C

If a code forks 2 childs, what can the values be for the process id's of each of the child? I child pid is supposed to be 0, but what if you fork 2 of them? (5 Replies)
Discussion started by: omega666
5 Replies

3. Programming

read from file using forks

i'm tring to make 2 processes each read from the same file but only one of them read the file. FILE * fileptr1; fileptr1 = fopen("file1.txt","rt"); pid2=fork(); while(1) { fscanf(fileptr1,"%s",temp1); if(feof(fileptr1)==0) { printf("%i",getpid()); //id of current process ... (6 Replies)
Discussion started by: ddx08
6 Replies

4. Programming

multiple forks and printf question

Hello *NIX gurus, I have a slight perplexing problem with multiple forks giving different results... Here is the deal. From what I undestand, a fork() call starts executing from the next instruction that follows the fork() call. That means it inherits the PC counter register value of the... (4 Replies)
Discussion started by: navigator
4 Replies

5. Programming

forks, ipc, fifos, update issues...

Hi, so I've got this program("main") that fork executes another ("user"). These programs communicate through fifos. One communication is a spawn call, where user passes an executable, main forks and executes it. So, I'm keeping track of all my processes using a task table. After the fork (for... (6 Replies)
Discussion started by: Funktar
6 Replies

6. Shell Programming and Scripting

cd using pipes

Hi, Can the cd command be invoked using pipes??? My actual question is slightly different. I am trying to run an executable from different folders and the path of these folders are obtained dynamically from the front end. Is there a way in which i can actually run the executable... (2 Replies)
Discussion started by: Sinbad
2 Replies

7. Filesystems, Disks and Memory

PIPEs and Named PIPEs (FIFO) Buffer size

Hello! How I can increase or decrease predefined pipe buffer size? System FreeBSD 4.9 and RedHat Linux 9.0 Thanks! (1 Reply)
Discussion started by: Jus
1 Replies

8. UNIX for Advanced & Expert Users

forks....HELP!!! someone anyone?

Hey guys, I'm given this bit of code, but, I'm having some problems executing it with the functions I've defined so far. I'm suppose to define the funtions "parse" and "execute." Parse splits the command in buf into individual arguments. It strips whitespace, replacing those it finds with NULLS... (3 Replies)
Discussion started by: richardspence2
3 Replies

9. Solaris

Question about pipes in solaris (and others) and buffering....

This is weird, so I'm hoping someone here knows solaris and how it handles pipes... OK... here goes... Theres this log file, right? I want to tail -f it, grep that, gzip that, then pipe that into more commands. Well thats easy, right? tail -f file | grep pattern | gzip | otherstuff... ... (1 Reply)
Discussion started by: sannik
1 Replies
Login or Register to Ask a Question