next up previous
Next: Submit Your Program(s) Electronically

Programming Project One
Computer Science 4334 -- Fall 2000
Operating Systems

CSCI4334 Assigned: Friday September 1, 2000
  Due: Monday September 18, 2000

You are to complete the ``Laboratory Exercise: A Shell Program'' on page 47 of the textbook.

Before you do that, you need to get familiar with the Linux programming environment. This description here gives you an exercise around the Linux programming environment and will get you ready for the lab exercise in the book.

You don't need to turn in this part of the exercise. Just turn in the completed the ``Laboratory Exercise: A Shell Program'' on the due date specified above.

This warm-up exercise lets you work with process and its related system calls. A process in Unix is created using the fork() system call. The following example from the text book illustrates the idea. For details of how fork() works, consult the man pages on Linux by typing man fork at the command prompt.

The program is divided into two parts, the parent and the child. The parent process executes a fork() system call that activates the child process. Both parent and child processes print some information on the screen. You may copy these programs from textbook on page 37 (Figure 2.8), or from my Linux account directory ~meng/testdir/fork.

First the parent code:

/* this is a simple example of how fork works */
/* classroom demonstration                    */
/* X.Meng aug-28-2000                         */
/* copied from Nutt's textbook                */
#include <sys/wait.h>
#define NULL 0

int main(void)
{
  if (fork () == 0) /* child process */
    {
      /* "child" is the name of the program called */
      execve("child", NULL, NULL);
      exit(0);      /* should never reach here */
    }

  /* parent code starts here */
  printf("Process[%d]: Parent in execution ...\n", getpid());
  
  sleep(2);    /* wait a couple of seconds */
  if (wait(NULL) > 0)      /* child is terminating */
    printf("Process[%d]: Parent detects terminating child\n", getpid());
  printf("Process[%d]: Parent terminating ...\n",getpid());
  return 0;
}
Now the child code:
/* child process */
/* this is a simple example of how fork works */
/* classroom demonstration                    */
/* X.Meng aug-28-2000                         */
/* copied from Nutt's textbook                */

int main(void)
{
  /* The child process' new program. This program replaces the parent's */
  /* program when 'fork()' is called */
  printf("Process[%d]: child in execution ... \n",getpid());
  sleep(1);
  printf("Process[%d]: child terminating ... \n", getpid());
}
You can compile these programs separately, for example,
[meng@red3 fork]$ gcc -o parent parent.c
[meng@red3 fork]$ gcc -o child child.c
or you can use the make utility to compile the programs automatically.
[meng@red3 fork]$ make
gcc     -c parent.c
gcc     -o parent parent.o
gcc     -c child.c
gcc     -o child child.o
[meng@red3 fork]$
To use the make utility, you need to have a makefile created in the current directory. The following is an example of the makefile for the above program.
G =         # debug option here
CC = gcc    # compiler
EXEC = parent child

# target
all: $(EXEC)
parent : parent.o
	$(CC) -o parent parent.o
parent.o : parent.c
	$(CC) -c parent.c
child : child.o
	$(CC) -o child child.o
child.o : child.c
	$(CC) -c child.c
clean:
	/bin/rm -f *.o core $(EXEC)

You may also choose to use the K-developer tool in Linux to develop program.




next up previous
Next: Submit Your Program(s) Electronically
Meng Xiannong 2006-07-14