MESSAGE
DATE | 2015-03-03 |
FROM | Ruben Safir
|
SUBJECT | Subject: [LIU Comp Sci] Re: fork problem
|
From owner-learn-outgoing-at-mrbrklyn.com Tue Mar 3 10:29:25 2015 Return-Path: X-Original-To: archive-at-mrbrklyn.com Delivered-To: archive-at-mrbrklyn.com Received: by mrbrklyn.com (Postfix) id 6878D161168; Tue, 3 Mar 2015 10:29:25 -0500 (EST) Delivered-To: learn-outgoing-at-mrbrklyn.com Received: by mrbrklyn.com (Postfix, from userid 28) id 5B97F1612E2; Tue, 3 Mar 2015 10:29:25 -0500 (EST) Delivered-To: learn-at-nylxs.com Received: from mailbackend.panix.com (mailbackend.panix.com [166.84.1.89]) by mrbrklyn.com (Postfix) with ESMTP id AC32E161168 for ; Tue, 3 Mar 2015 10:29:01 -0500 (EST) Received: from [10.0.0.19] (unknown [96.57.23.82]) by mailbackend.panix.com (Postfix) with ESMTPSA id 0D6AA11BEC; Tue, 3 Mar 2015 10:29:01 -0500 (EST) Message-ID: <54F5D33C.9030002-at-panix.com> Date: Tue, 03 Mar 2015 10:29:00 -0500 From: Ruben Safir User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.3.0 MIME-Version: 1.0 To: Mohammed Ghriga Subject: [LIU Comp Sci] Re: fork problem References: <20150302233129.GA30034-at-www.mrbrklyn.com> In-Reply-To: Content-Type: text/plain; charset=windows-1252 Content-Transfer-Encoding: 7bit Sender: owner-learn-at-mrbrklyn.com Precedence: bulk Reply-To: learn-at-mrbrklyn.com
On 03/03/2015 07:40 AM, Mohammed Ghriga wrote: > Ruben, > > Everything is fine on my system- see the attached. The volume of information printed is what makes it hard to follow your output. If you think there is a problem test the issue with a very simple program which includes a code segment such as: > > > int pid = fork(); > printf("Testing fork\n"); > if ( pid == 0 ) > printf( "This is being printed from the child process\n" ); > ETC.... > > mg > > -----Original Message----- > From: Ruben Safir [mailto:ruben-at-mrbrklyn.com] > Sent: Monday, March 02, 2015 6:32 PM > To: Mohammed Ghriga > Subject: fork problem > > I must be doing something wrong > > -- > So many immigrant groups have swept through our town that Brooklyn, like Atlantis, reaches mythological proportions in the mind of the world - RI Safir 1998 http://www.mrbrklyn.com > > DRM is THEFT - We are the STAKEHOLDERS - RI Safir 2002 http://www.nylxs.com - Leadership Development in Free Software http://www2.mrbrklyn.com/resources - Unpublished Archive http://www.coinhangout.com - coins! > http://www.brooklyn-living.com > > Being so tracked is for FARM ANIMALS and and extermination camps, but incompatible with living as a free human being. -RI Safir 2013 > It completely normalized with fflush(). My friend from Google suggested this on a chat lasst night and I didn't believe him because it seemed to be giving me a recursion, but then on usenet, I had the same suggestion and it completely fixed everything. You need to flush the output buffer before forking.
#include #include #include #include #include
int main( int argc, char * argv[]){ pid_t pid; pid_t pid_ret; int i = 0; printf("\n___Before the While Loop where Loop is ==> %d__", i);
while( i++ < 100){ puts("\nBefore the Fork in the Loop:"); fflush(stdout); <<============================== printf( "Loop ==> %d \n", i); pid = fork(); printf("\nFork: This should print twice for every loop: %d", i);
if(pid < 0){ puts( "fork failed\n" ); return 1; } if(pid == 0) { puts("\n\nChildren of the world unite"); printf("Am Yisrael Chai! pid =>%d \n", pid); printf( "Loop in Child ==> %d \n", i); puts("BYE!!"); exit( EXIT_SUCCESS ); } if (pid > 0 ){ printf("\nIn the Parent => I'm waiting %d\n", pid ); printf( "Loop in Parent ==> %d \n", i); pid_ret = wait(NULL); puts("Done Waiting for Child"); printf("\nProcess that returned was ==> %d", pid_ret ); printf("\n*******************************\n" ); } } return 0; } ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~`
usenet - comp.lang.c
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
On Monday March 2 2015 18:41, in comp.lang.c, "Ruben Safir" wrote:
> Anyone know why this might not fuction as I thought it might. It seems > to start at the beginning of main when a process is forked > > #include > #include > #include > #include > #include > > > int main( int argc, char * argv[]){ > pid_t pid; > pid_t pid_ret; > int i = 0; > printf("\n___Before the While Loop where Loop is ==> %d__", i); [snip] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ > > ___Before the While Loop where Loop is ==> 0__ > Before the Fork in the Loop: > Loop ==> 1 > > Fork: This should print twice for every loop: 1 > > Children of the world unite > Am Yisrael Chai! pid =>0 > Loop in Child ==> 1 > BYE!! > > ___Before the While Loop where Loop is ==> 0__ [snip]
As Keith said, this is off-topic for comp.lang.c, and you'd get a better explanation from comp.unix.programmer or one of the other Unix groups.
Having said that, take a look at your program. Specifically, the line that reads printf("\n___Before the While Loop where Loop is ==> %d__", i);
Note that, at this point in your code, if you are outputing to a line-buffered device (such as a console), you /have not/ written the string out. The next newline (or a call to fflush() ) will flush the buffer, and cause the string to write out. Such is the way of buffered I/O.
So, prior to the fork(), you have a buffer that contains a string, not written to stdout.
Consider what happens after the fork(). You now write a newline to stdout, along with more text. In both the parent process, and the child process.
So, in the parent process, the next newline causes the parent process' buffer to be flushed, and you see the text ___Before the While Loop where Loop is ==> 0__
And, in the child process, which has inherited a copy of the buffer and it's flags, the next newline causes the child process' buffer to be flushed, and again, you see ___Before the While Loop where Loop is ==> 0__
Such is the way of programming in a Unix multiprogramming environment. -- Lew Pitcher "In Skills, We Trust" PGP public key available upon request
|
|