CPE434 Lab2-Processes Solved

30.00 $

Category:

Description

3.5/5 - (2 votes)

 

A process is an instance of a program. The new process is created by issuing a fork system call. Each process has its own area of memory, protected against modification by other processes. The ​fork ​system call creates a copy of a process that was executing. The process that executed the fork is called the ​parent ​process and the new process is called the ​child ​process. . The only difference in the returns from the fork system call is that the return value in the parent process is the process id of the newly created child process, while the return value in the child process is zero. If the fork is not successful, -1 is returned.

A process terminates by calling the ​exit ​system call. This system call never returns to the caller. When exit is called, an integer exit status is passed by the process to the kernel.

Exercises

  1. Write a program that does the following in the order given below:
    1. declare and initialize a variable named ​val ​(initialize to 0)
    2. call fork system call
    3. in the child process, add 2 to the value of ​val​, and print it on the console screen along with the pid of the child in the same statement.
    4. in the parent process, add 5 to the value of ​val​, and print it on the console screen along with the pid of the parent process in the same statement.
    5. What can you conclude about the values of the variable ​val​?
  2. Write a program, which creates a new process ​child1​. The parent process should print its id and wait for the termination of the ​child1 ​ The ​child1 ​process should call a function that subtracts two numbers passed as arguments, print the result on the console screen along with the pid of the process that is printing. It should then create a new child ​child2 process that adds two numbers. The control should then come back to the first child process ​child1 ​that now should multiply two numbers and then terminate. The waiting parent process should now resume and terminate the program .
  3. Write a program, which creates n child processes and prints their process id. The n must be an even number and passed as argument, otherwise a message indicates that the number is odd and terminates the program. Please make sure that your implementation has one parent process and n-1 child processes.
  4. Write a C program that demonstrates the concept of following:
    1. Orphan Process
    2. Zombie Process
    3. Sleeping Beauty Process
    4. Verify that you actually achieved all the three states mentioned above by using the terminal. Put a screenshot of the process table in your report.

 

Topics for Theory

Define and discuss the following: process, states of processes, orphan process, and zombie process. Additionally, write a paragraph on each of the functions mentioned in the hint (fork(), exit(), and wait()).

Hint

int fork() ​: function either returns 0 for child process or the process id if it is parent.

exit(status)​: when exit is called, an integer exit status is passed by the process to the kernel. (used usually by the child process to terminate and return it’s status to the parent process). int wait(int *status)​: 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 there are more than one child processes, then the wait function returns after any one child is terminated and the value returned is the pid of the child.

  • lab2-1jhgmf.zip