delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/2001/07/16/09:02:41

Mailing-List: contact cygwin-help AT sourceware DOT cygnus DOT com; run by ezmlm
List-Subscribe: <mailto:cygwin-subscribe AT sources DOT redhat DOT com>
List-Archive: <http://sources.redhat.com/ml/cygwin/>
List-Post: <mailto:cygwin AT sources DOT redhat DOT com>
List-Help: <mailto:cygwin-help AT sources DOT redhat DOT com>, <http://sources.redhat.com/ml/#faqs>
Sender: cygwin-owner AT sources DOT redhat DOT com
Delivered-To: mailing list cygwin AT sources DOT redhat DOT com
From: "Ronald Landheer" <ronald AT landheer DOT com>
To: "CygWin" <cygwin AT cygwin DOT com>
Subject: fork()
Date: Mon, 16 Jul 2001 14:59:18 +0200
Message-ID: <NEBBJCJLGLCEEOPGCDCEIEJICIAA.ronald@landheer.com>
MIME-Version: 1.0
X-Priority: 3 (Normal)
X-MSMail-Priority: Normal
X-Mailer: Microsoft Outlook IMO, Build 9.0.2416 (9.0.2910.0)
X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4133.2400
Importance: Normal

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/

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019