Process Creation and Termination in the LINUX Environment

Страницы работы

Содержание работы

Process Creation and Termination in the LINUX Environment

CS2403 Lab 3 notes

Operating System Principles

August 1, 2002

Lab Objectives

This lab has been constructed to educate the lab section with process creation and termination.

1.Type the following C program, that will fork() a separate process:
 

#include <stdio.h>

int main(void)

{

  int i;

    /* Process Creation */

    i = fork();

  printf("Hello World\n");

  exit(0);

}

 

Call the above program p1.c, and compile it as follows: gcc -o p1 p1.c <Enter>.

Then Execute p1.

 

Questions: (Due: Monday, August 5 at 4:00pm -- Total Points: 50)

1.1 [1] How many times do you see the “Hello World” message in the output?

1.2 [2] Explain why?

2. fork() system call returns twice, one for each process that is running. At the time the fork() is called, the original process is cloned and begins executing simultaneously. The only distinction between the two processes is the return value from the fork() call.

PARENT          has the return value >0 indicating the child's process ID.

            CHILD              has a return value =0

A return value <0 indicates an error on fork(), which could happen if there was no memory or process table space in the system.

Type the following C program, this is a sample program for getting process ID and forking a separate process:

#include <stdio.h>

#include <sys/types.h>

int main(void)

{

  int i;

  printf("FORK TEST PROGRAM\n");

  printf(" My process id %d and My Parent's process id %d\n",getpid(),getppid());

  i = fork(); /* A child process is created and the codes are the same */

  if (i == -1)

    {

printf("How many times do you SEE this line?\n");

      printf(" Process cannot be created\n");

      exit(0);

    }

  if (i !=0) {

    /* parent process executes */

      printf(" PARENT: I am the parent - My process id %d and My Parent's process id %d\n",getpid(),getppid());

      printf(" PARENT: My child process id is %d\n",i);

  }

  else

    /* Child process continues here. the value that is returned is 0 */

    {

        printf(" CHILD: I am the child - My process id %d and My Parent's process id %d\n",getpid(),getppid());

    }

  exit(0);

}

Call the above program p2.c, and compile it as follows: gcc -o p2 p2.c <Enter>.

Then execute p2 and report the output.

Questions:

2.1 [2] How many times fork() system call returns in this program? Explain your answer?

2.2 [2] What is the return value of fork() system call?

2.3 [1] What is the parent process ID?

2.4 [1] What is the child process ID?

2.5 [1] What is the parents’ parent process ID?

2.6 [2] How many times do you see the "How many times do you see this line?" message? Why?

3. The only way in which a program is executed by UNIX Is for an existing process to issue the exec() system call. The exec() system call replaces the current process with the new program. The process ID does not change. Some UNIX manuals incorrectly refer to new program as the new process, but realize that it is really a new program executing in the context of the calling process. A new process is not created by exec(). There are six versions of exec system call: execlp, execl, execle, execvp, execv, execve.

Here, we are using the execvp(file, argv). This is argv style with automatic searching, containing the pointers to the argument strings. This argv array must contain a NULL pointer to specify its end, since a count is not specified.

Type the following C program to see the function of exec() system call:

#include <stdio.h>

char *myargs[] = {

            "ls",

                "-l",

                NULL

                };              

int main()

{

   int i;

      if (fork()==0)

   {

      execvp("ls",myargs);

      printf("NEVER PRINTED\n");

      exit(0);

   }

   else

   {

        for (i=0;i<5;i++)

        {

            printf("PARENT: %d\n",i);

                sleep(1);

        }

   }

   printf("DONE\n");

   return 0;

}

Call the above program p3.c, and compile it as follows: gcc -o p3 p3.c <Enter>.

Then execute p3 and report the output.

Questions:

3.1 [2] What is the output of child process?

3.2 [2] Why you don’t see the "NEVER PRINTED" message?

3.3 [2] What is the output of parent process?

3.4 [2] Execute p3 several times. Why sometimes the outputs of parent and child are interleaved?

3.5 [4] How could you convert the parent to a long-running process? Convert it and then put it in        the background. Report the status and PID.      

4. A process can wait for one of its child processes to finish by executing the wait() system call. The value returned by wait is the process ID of the child process that terminated. If the process that calls wait() does not have any child process, wait returns the value of –1 immediately. If the process that calls wait has one or more child processes that has not yet terminated, then the calling process is suspended by the kernel until one of its child processes terminates. When a child process terminates and wait returns, if the status argument is not NULL, the value passed to exit() by the terminating child process is stored in the status variable.

Похожие материалы

Информация о работе