This is the mail archive of the cygwin@cygwin.com mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

fork()


Hello all,
Being used to DJGPP - not CygWin - I may have lost my hand on the forking
idea a while back, so I thought I'd try the following piece of code:
-- BEGIN CODE SNIPLET --
#include <stdio.h>
#include <unistd.h>
int main(void) {
 int rVal, i;
 rVal = fork();
 if (!rVal) {
 for (i = 0; i < 100; i++) printf("0");
 } else {
 for (i = 0; i < 100; i++) printf("1");
 } // if
 printf("\n");
 return(0);
} // main()
--- END CODE SNIPLET ---
This compiles without warnings or errors (phew!) but produces an output
somewhat different than I expected:
-- BEGIN OUTPUT SNIPLET --
0000000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000001111111111111111111111111111111111111111111111111111
111111111111111111111111111111111111111111111111
--- END OUTPUT SNIPLET ---
I'd expected the 0's and 1's to be jumbled..
I tried the same with the following Perl script: (please don't mind the
syntax - I'm not a Perl programmer..)
-- BEGIN CODE SNIPLET --
#!/usr/bin/perl
unless (defined($pid = fork)) {
 die("Cannot fork: $!");
}
$i = 0;
unless ($pid) {
 while ($i < 100) {
 printf("0");
 $i = $i + 1;
 }
 exit;
}
while ($i < 100) {
 printf("1");
 $i = $i + 1;
}
--- END CODE SNIPLET ---
expecting the same output as the C thingy, but no:
-- BEGIN OUTPUT SNIPLET --
1111111111111111111111111111111111111111111111111111111111111111111111111111
1111111111111111111111110000000000000000000000000000000000000000000000000000
000000000000000000000000000000000000000000000000
--- END OUTPUT SNIPLET ---
So here's my questions:
 1. does fork() really make a new process?
 2. does the new process (if made at all) run simultaniously with the
 old one?
 3. if so, why this output?
 4. why is there a difference between the Perl and C programs'
 outputs? (please don't laugh too loud if it's because of my Perl
 inabilities..)
Thanx!
Ronald
-----BEGIN GEEK CODE BLOCK-----
Version: 3.1
GCS/LS/L/S/IT d- s-:+ a-- C++
UL+++ P+++ L++ E- W+++ N+++ o--
!K w !O M++ V- PS- PE- Y+ PGP++
t++(+) 5 X- R+++ !tv b++ DI++++
D-- G e+++ h r-- y-
------END GEEK CODE BLOCK------
--
Unsubscribe info: http://cygwin.com/ml/#unsubscribe-simple
Bug reporting: http://cygwin.com/bugs.html
Documentation: http://cygwin.com/docs.html
FAQ: http://cygwin.com/faq/

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

AltStyle によって変換されたページ (->オリジナル) /