You are on page 1of 2

OS lab

The working plateform in lab is LINUX (text based access only)


using putty application from window PC to connect to linux server. IP address of linux server : 172.16.10.244 (port No22)
enter login name and password(initial password is abcdefgh) change your password using passwd command
(select 8 character password using following rule -
first two chars- alphabet, next char is special char, next three char-digits, last two chars-alphabet)

Sheet #3

objective- to learn process creation


[ read manual of fork ]

format of fork pid_t fork(void)


fork() creates a new process by duplicating the calling process. The new process, referred to as the
child, is an exact duplicate of the calling process, referred to as the parent. On success, the PID of the
child process is returned in the parent, and 0 is returned in the child. On failure, -1 is returned in the
parent, no child process is created.

If-else or switch syntax (based on return value of fork() ) is used to execute separate code for parent and
child.
If(!fork() { child body} else {parent body} rest of programme common to both
switch(fork){case 0:child codes;break; default: parent code;}rest of programme common to both

vfork() - similar to fork() except parent blocks until child terminates. It shares data space where is in
fork() both processes run concurrently but does not share memory.

Parent waits for specific child to terminate.


function used to wait for specific child process to terminate.
wait(int *status) function returns the pid of child terminated.
status is pointer to location where exit status of child (returned by main or exit() of the child.
include sys/wait.h need to be included
WIFEXITED(stat_val) and WEXITSTATUS(stat_val)- nonzero and status code
WIFSIGNALED(stat_val) and WTERMSIG(stat_val)- nonzero on uncaught signal and signal number
WIFSTOPPED(stat_val) and WSTOPSIG(stat_val)- nonzero on child stopped and signal number

another function used to wait for specific child process to terminate


waitpid(pid_t pid, int *status, int options) // if pid is -1 wait for nay child
(if this function returns
-1 ,it means ERROR ; returns 0 if child is not terminated ; returns pid of the child that is terminated or
stopped)
pid- pid of specific child to wait for
*status - pointer of location of where exit status of child
options-to change behaviour of waitpid()
waitpid() system call suspends execution of the calling process until a child specified by pid argument
has changed state. By default, waitpid() waits only for terminated children, but this behavior is
modifiable via the options argument.

WEXITED Wait for children that have terminated.


WSTOPPED Wait for children that have been stopped by delivery of a signal.
WCONTINUED Wait for (previously stopped) children that have been resumed by delivery of
SIGCONT.
The following flags may additionally be ORed in options:
WNOHANG return immediately if no child has exited.
WNOWAIT Leave the child in a waitable state; a later wait call can be used to again retrieve the
child status information.

The exec() family of functions replaces the current process image with a new process image.

Problem
Q1
(a) write basic fork programme where each child and parent execute following code separately-
{ for(n=0;n<5;n++){x++;
printf("\nchild id %d, father %d x=%d",getpid(),getppid(),x);
sleep(1);
}
{ for(n=0;n<5;n++){x++;
printf("\nparent id %d, father %d x=%d",getpid(),getppid(),x);
sleep(1);
}
where x and n are integers.
(b) repeat above with child n<5 and parent n<3;
(c) repeat above with child n<3 and parent n<6;

If parent process survives child then still child process remains in system as parent may call child's exit
code. So child is called zombie process or defunct that is no longer running but still holding system
resources. If parent terminated abnormally (before removing child) child become orphan and its ppid
becomes 1. init() will clean up orphan processes until child hold resources and affects the system
performance.
RUN the following programme in back ground and then run ps command (fast) many time (at leat 5
times) to see defunct message.
(d) write programme that gives orphaned or defunct process.
(use sleep(10); in parent

(e) repeat part (a) with vfork and verify parent blocks and data modified in child reflects in parent code..
Q2
(a) include following code in parent after loop
childpid=wait(&status);
printf("\n*****\n child finished id %d",childpid);
if(WIFEXITED(status)) printf("\n child exit code %d",WEXITSTATUS(status));

and
exit(m); // common to both (outside if-else)
where m is exit code assigned in child part of the code
and childpid and m are integers.

Q3

You might also like