From owner-freebsd-sparc64@FreeBSD.ORG Wed Jul 30 09:37:46 2003 Return-Path: Delivered-To: freebsd-sparc64@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 6BA0437B401 for ; Wed, 30 Jul 2003 09:37:46 -0700 (PDT) Received: from tts.orel.ru (tts.orel.ru [213.59.64.67]) by mx1.FreeBSD.org (Postfix) with ESMTP id 0365543FA3 for ; Wed, 30 Jul 2003 09:37:45 -0700 (PDT) (envelope-from bel@orel.ru) Received: from orel.ru (lg.orel.ru [195.90.189.89]) by tts.orel.ru (8.12.6/8.12.6/bel) with ESMTP id h6UGbg0A024179 for ; Wed, 30 Jul 2003 20:37:42 +0400 Message-ID: <3F27F457.8030401@orel.ru> Date: Wed, 30 Jul 2003 20:37:43 +0400 From: Andrew Belashov Organization: ORIS User-Agent: Mozilla/5.0 (X11; U; FreeBSD i386; en-US; rv:1.3) Gecko/20030411 X-Accept-Language: ru, en-us, en MIME-Version: 1.0 To: freebsd-sparc64@freebsd.org Content-Type: multipart/mixed; boundary="------------070807040700090203040900" Subject: Simple C program to demonstrate bug in fork(2) X-BeenThere: freebsd-sparc64@freebsd.org X-Mailman-Version: 2.1.1 Precedence: list List-Id: Porting FreeBSD to the Sparc List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 30 Jul 2003 16:37:46 -0000 This is a multi-part message in MIME format. --------------070807040700090203040900 Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit I have written the small program for demonstrate bug in fork(2). Normal output: bel@bel$ ./forkbug Main: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa Child working... Parent changed memory Child: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa Parent waiting a child... Child exiting... Parent exiting... Bad output: bel@trash$ ./forkbug Main: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa Parent changed memory Parent waiting a child... Child working... Child: bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa Child exiting... Parent exiting... --------------070807040700090203040900 Content-Type: text/plain; name="forkbug.c" Content-Transfer-Encoding: 7bit Content-Disposition: inline; filename="forkbug.c" #include #include #include #include #include #include #define BUFFLEN (128 * 1024 * 1024) /* 128 Mb */ #define CHECKLEN 60 char * buff; void docheck(const char * prefix); int main() { int i; pid_t pid; if ((buff = (char *) malloc(BUFFLEN)) == NULL) { perror("Out of memory"); exit(1); } memset(buff, 'a', BUFFLEN); buff[BUFFLEN-1] = '\0'; docheck(" Main"); if ((pid = fork()) == -1) { perror("fork() failed"); exit(1); } if (pid) { memset(buff + BUFFLEN - 1 - CHECKLEN, 'b', CHECKLEN/2); printf("Parent changed memory\n"); printf("Parent waiting a child...\n"); wait(&i); printf("Parent exiting...\n"); } else { printf("Child working...\n"); docheck("Child"); printf("Child exiting...\n"); } return 0; } void docheck(const char * prefix) { printf ("%s: %s\n", prefix, buff + BUFFLEN - 1 - CHECKLEN); } --------------070807040700090203040900--