*BSD News Article 50325


Return to BSD News archive

Path: euryale.cc.adfa.oz.au!newshost.anu.edu.au!harbinger.cc.monash.edu.au!simtel!news.sprintlink.net!dispatch.news.demon.net!demon!jraynard.demon.co.uk!jraynard.demon.co.uk!not-for-mail
From: james@jraynard.demon.co.uk (James Raynard)
Newsgroups: comp.unix.bsd.freebsd.misc
Subject: vfork question
Date: 2 Sep 1995 22:40:53 -0000
Organization: A FreeBSD box
Lines: 51
Message-ID: <42amhl$vb@jraynard.demon.co.uk>
NNTP-Posting-Host: localhost.jraynard.demon.co.uk
X-NNTP-Posting-Host: jraynard.demon.co.uk

I've been experimenting with some of the code in Stevens's Advanced
Programming in the Unix Environment. According to the book, the code
shown at the end (Program 8.2) should produce the following output:-

$ a.out
before vfork
pid=977, glob=7, var=89

because the increments done by the child also change the values in the
parent. However, I get

james@jraynard:Chapter8$ ./prog2
before vfork
pid=977, glob=6, var=88

implying that the child has not changed the parent's values. Is this
something which changed between Net/2 (which Stevens describes) and 4.4?

Also, if the _exit call is replaced by exit, the output from the parent
supposedly disappears, because the exit in the child closes stdout in the
parent. However, I get the same output in both cases!

TIA,
James

#include <sys/types.h>
#include "ourhdr.h"

int glob=6;     /* external variable in intialised data */

int main(void) {
  int var,temp; /* automatic variable on the stack */
  pid_t pid;
  var=88;
  printf("before vfork\n");     /* We don't flush stdio */
  if ((pid=vfork()) < 0)
      err_sys("vfork error");
  else if (pid == 0) {  /* child */
    glob++;     /* modify parent's variables */
    var++;
    _exit(0);   /* child terminates */
  }
  /* parent */
  temp=printf("pid=%d, glob=%d, var=%d\n",getpid(),glob,var);
  exit(0);
}

-- 
"If the King's English was good enough for Jesus, it's good enough for me!"
	-- "Ma" Ferguson, Governor of Texas (circa 1920)