Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 17 Feb 1999 06:01:04 +1000
From:      Greg Black <gjb@comkey.com.au>
To:        "Masahiro Ariga" <mariga@cd.mbn.or.jp>
Cc:        freebsd-questions@FreeBSD.ORG
Subject:   Re: Please untangle my brain. 
Message-ID:  <19990216200104.1976.qmail@alpha.comkey.com.au>
In-Reply-To: <000301be5845$ba8022e0$064ca8c0@gateway>  of Mon, 15 Feb 1999 03:12:27 %2B0900
References:  <000301be5845$ba8022e0$064ca8c0@gateway> 

next in thread | previous in thread | raw e-mail | index | archive | help
> I know this is concerned with UNIX itself rather than FREE BSD,so I am
> afraid this is not the proper site to ask but I am in a predicament so I
> beseech senior UNIX explorer to teach me.

You're right, this is not the right place; and you're wrong, you
don't need senior Unix people to help.  This is one of the most
basic Unix programming questions.

> I assume UNIX runs on time-sharing system,changing processs every time.In
> order to trace process change movement,I made the next  program.

The mechanism has already been explained.  What I am adding is a
minor refinement of the program which will exhibit the behaviour
that you would like to see.  It also illustrates sensible use of
white space to make it legible, as well as more correct C
syntax.  Study the differences and be sure you understand them.

If you run this version several times, you will see interleaved
output from the two processes and you'll probably see different
output from run to run.  If the output doesn't differ on your
system, change the loops to iterate a few more times than the
current five and/or change the multiplier in the usleep() call.

> And if someone knows how to write program to correctly trace process's
> change movement(using IPC,semaphor or whatsoever),please let me know.

This is too big a question to cover here.  Find a decent book on
Unix programming and work your way through it.  One reasonable
book is "Advanced Programming in the Unix Environment" by
W. Richard Stevens.

> Also,if someone knows the proper site for me to ask these UNIX
> programing,would you pleas tell me.

It's too many yers since I frequented any of those places, so my
knowledge is probably completely out of date now.  Personally, I
prefer books.

Here's the revised code:

#include <stdio.h>
#include <unistd.h>

int
main(void)
{
  int i;

  if (fork() == 0) {		/* Child Process */
    for (i = 0; i < 5; i++) {
      printf("This is child! num = %d\n", i);
      usleep(i * 10);
    }
    exit(0);
  }

  /* Parent Process */
  for (i = 0; i < 5; i++) {
    printf("This is parent! num = %d\n", i);
    usleep(i * 10);
  }
  return 0;
}


-- 
Greg Black <gjb@acm.org>



To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-questions" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?19990216200104.1976.qmail>