public inbox for cygwin@cygwin.com
 help / color / mirror / Atom feed
* fork()
@ 2001-07-16  6:02 Ronald Landheer
  2001-07-16  6:20 ` fork() Corinna Vinschen
  0 siblings, 1 reply; 10+ messages in thread
From: Ronald Landheer @ 2001-07-16  6:02 UTC (permalink / raw)
  To: CygWin

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/

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: fork()
  2001-07-16  6:02 fork() Ronald Landheer
@ 2001-07-16  6:20 ` Corinna Vinschen
  2001-07-16 10:23   ` fork() Warren Young
  0 siblings, 1 reply; 10+ messages in thread
From: Corinna Vinschen @ 2001-07-16  6:20 UTC (permalink / raw)
  To: CygWin

On Mon, Jul 16, 2001 at 02:59:18PM +0200, Ronald Landheer wrote:
> 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..

Your example is wrong. The output to stdout is line buffered. That means,
the actual output is generated only after the last `printf("\n");

Add a `setbuf(stdout, NULL)' right before the fork() and your
little application will suddenly behave as you've expected...

Corinna

-- 
Corinna Vinschen                  Please, send mails regarding Cygwin to
Cygwin Developer                                mailto:cygwin@cygwin.com
Red Hat, Inc.

--
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/

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: fork()
  2001-07-16  6:20 ` fork() Corinna Vinschen
@ 2001-07-16 10:23   ` Warren Young
  0 siblings, 0 replies; 10+ messages in thread
From: Warren Young @ 2001-07-16 10:23 UTC (permalink / raw)
  To: Corinna Vinschen

Corinna Vinschen wrote:
> 
> Your example is wrong. The output to stdout is line buffered. That means,

Another problem is, the code depends on the OS's multitasking
characteristics.  There's nothing explicit in the code that says the OS
has to take the time slice away from the parent or the child.  If you
threw in a line like this:

	if (rand() % 2) sleep(0);

after each print statement, you might reasonably expect randomness in
the output.
-- 
= Warren -- ICBM Address: 36.8274040 N, 108.0204086 W, alt. 1714m

--
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/

^ permalink raw reply	[flat|nested] 10+ messages in thread

* fork()
  1999-07-30 19:37 fork() unknown user
  1999-07-30 23:41 ` fork() Mumit Khan
@ 1999-07-31 18:34 ` unknown user
  1 sibling, 0 replies; 10+ messages in thread
From: unknown user @ 1999-07-31 18:34 UTC (permalink / raw)
  To: cygwin

I'm trying to compile a Linux program that uses fork() ... it just crashes 
if i compile it with fork() so i have to comment out fork().. are there 
anyway to simulate fork() function under Windows 95/98 so it runs in 
background as it's suppose to like on Linux? Any help would be thankful.


_______________________________________________________________
Get Free Email and Do More On The Web. Visit http://www.msn.com

--
Want to unsubscribe from this list?
Send a message to cygwin-unsubscribe@sourceware.cygnus.com

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: fork()
  1999-07-31 17:05   ` fork() Chris Faylor
@ 1999-07-31 18:34     ` Chris Faylor
  0 siblings, 0 replies; 10+ messages in thread
From: Chris Faylor @ 1999-07-31 18:34 UTC (permalink / raw)
  To: Mumit Khan; +Cc: unknown user, cygwin

On Sat, Jul 31, 1999 at 12:28:20AM -0500, Mumit Khan wrote:
>"unknown user" <jwl1980@hotmail.com> writes:
>> I'm trying to compile a Linux program that uses fork() ... it just crashes 
>> if i compile it with fork() so i have to comment out fork().. are there 
>> anyway to simulate fork() function under Windows 95/98 so it runs in 
>> background as it's suppose to like on Linux? Any help would be thankful.
>
>Why don't show us what you're trying to do? fork() in general does work
>fine under Cygwin b20.1; there're certainly bugs, but you need to come up
>with a testcase shows that problem. 

There are bugs in fork?  Hmm.

cgf

--
Want to unsubscribe from this list?
Send a message to cygwin-unsubscribe@sourceware.cygnus.com

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: fork()
  1999-07-30 23:41 ` fork() Mumit Khan
  1999-07-31 17:05   ` fork() Chris Faylor
@ 1999-07-31 18:34   ` Mumit Khan
  1 sibling, 0 replies; 10+ messages in thread
From: Mumit Khan @ 1999-07-31 18:34 UTC (permalink / raw)
  To: unknown user; +Cc: cygwin

"unknown user" <jwl1980@hotmail.com> writes:
> I'm trying to compile a Linux program that uses fork() ... it just crashes 
> if i compile it with fork() so i have to comment out fork().. are there 
> anyway to simulate fork() function under Windows 95/98 so it runs in 
> background as it's suppose to like on Linux? Any help would be thankful.

Why don't show us what you're trying to do? fork() in general does work
fine under Cygwin b20.1; there're certainly bugs, but you need to come up
with a testcase shows that problem. 

Regards,
Mumit


--
Want to unsubscribe from this list?
Send a message to cygwin-unsubscribe@sourceware.cygnus.com

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: fork()
  1999-07-30 23:41 ` fork() Mumit Khan
@ 1999-07-31 17:05   ` Chris Faylor
  1999-07-31 18:34     ` fork() Chris Faylor
  1999-07-31 18:34   ` fork() Mumit Khan
  1 sibling, 1 reply; 10+ messages in thread
From: Chris Faylor @ 1999-07-31 17:05 UTC (permalink / raw)
  To: Mumit Khan; +Cc: unknown user, cygwin

On Sat, Jul 31, 1999 at 12:28:20AM -0500, Mumit Khan wrote:
>"unknown user" <jwl1980@hotmail.com> writes:
>> I'm trying to compile a Linux program that uses fork() ... it just crashes 
>> if i compile it with fork() so i have to comment out fork().. are there 
>> anyway to simulate fork() function under Windows 95/98 so it runs in 
>> background as it's suppose to like on Linux? Any help would be thankful.
>
>Why don't show us what you're trying to do? fork() in general does work
>fine under Cygwin b20.1; there're certainly bugs, but you need to come up
>with a testcase shows that problem. 

There are bugs in fork?  Hmm.

cgf

--
Want to unsubscribe from this list?
Send a message to cygwin-unsubscribe@sourceware.cygnus.com

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: fork()
  1999-07-30 19:37 fork() unknown user
@ 1999-07-30 23:41 ` Mumit Khan
  1999-07-31 17:05   ` fork() Chris Faylor
  1999-07-31 18:34   ` fork() Mumit Khan
  1999-07-31 18:34 ` fork() unknown user
  1 sibling, 2 replies; 10+ messages in thread
From: Mumit Khan @ 1999-07-30 23:41 UTC (permalink / raw)
  To: unknown user; +Cc: cygwin

"unknown user" <jwl1980@hotmail.com> writes:
> I'm trying to compile a Linux program that uses fork() ... it just crashes 
> if i compile it with fork() so i have to comment out fork().. are there 
> anyway to simulate fork() function under Windows 95/98 so it runs in 
> background as it's suppose to like on Linux? Any help would be thankful.

Why don't show us what you're trying to do? fork() in general does work
fine under Cygwin b20.1; there're certainly bugs, but you need to come up
with a testcase shows that problem. 

Regards,
Mumit


--
Want to unsubscribe from this list?
Send a message to cygwin-unsubscribe@sourceware.cygnus.com

^ permalink raw reply	[flat|nested] 10+ messages in thread

* fork()
@ 1999-07-30 19:37 unknown user
  1999-07-30 23:41 ` fork() Mumit Khan
  1999-07-31 18:34 ` fork() unknown user
  0 siblings, 2 replies; 10+ messages in thread
From: unknown user @ 1999-07-30 19:37 UTC (permalink / raw)
  To: cygwin

I'm trying to compile a Linux program that uses fork() ... it just crashes 
if i compile it with fork() so i have to comment out fork().. are there 
anyway to simulate fork() function under Windows 95/98 so it runs in 
background as it's suppose to like on Linux? Any help would be thankful.


_______________________________________________________________
Get Free Email and Do More On The Web. Visit http://www.msn.com

--
Want to unsubscribe from this list?
Send a message to cygwin-unsubscribe@sourceware.cygnus.com

^ permalink raw reply	[flat|nested] 10+ messages in thread

* fork()
@ 1998-02-21 13:13 A Dark Elf
  0 siblings, 0 replies; 10+ messages in thread
From: A Dark Elf @ 1998-02-21 13:13 UTC (permalink / raw)
  To: gnu-win32

I have a problem with threading. I use fork() so my app listens for
connections from a remote host, accept() it, forks, the parent loops and
closes the sockfd from the child and listens again:

  for(;;)
  {
   if(sockfd!=(int)NULL) close(sockfd);
   sockfd = accept(sockfd2, (struct sockaddr *)&address, &len);
   if(pid=fork()) break;
  }

Now it's telling me this error: child died before initialization..
Am I doing something wrong here ?


 




Drow@DarkElf.net
----------------------------------------------------------------------
DarkElf Network SysAdmin                        http://www.darkelf.net
OKC.OK.US.UnderNet.Org Operator                http://www.undernet.org
    Check the main resource for developers at www.fastethernet.net
----------------------------------------------------------------------

-
For help on using this list (especially unsubscribing), send a message to
"gnu-win32-request@cygnus.com" with one line of text: "help".

^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2001-07-16 10:23 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-07-16  6:02 fork() Ronald Landheer
2001-07-16  6:20 ` fork() Corinna Vinschen
2001-07-16 10:23   ` fork() Warren Young
  -- strict thread matches above, loose matches on Subject: below --
1999-07-30 19:37 fork() unknown user
1999-07-30 23:41 ` fork() Mumit Khan
1999-07-31 17:05   ` fork() Chris Faylor
1999-07-31 18:34     ` fork() Chris Faylor
1999-07-31 18:34   ` fork() Mumit Khan
1999-07-31 18:34 ` fork() unknown user
1998-02-21 13:13 fork() A Dark Elf

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).