C Language and Linux Help Docs
C Language and Linux Help Docs
C Language and Linux Help Docs
Topic to be covered
● Monitoring Processes
○ ps
○ pstree
● Process Identification:
○ getpid()
○ getppid()
● Process Creation
○ fork ()
● Process Completion
○ wait (int *)
○ exit (int)
● Orphan Process
● Zombie Process
● Process Binary Replacement
○ exec ()
Objectives
● Students are able to create new process in linux.
● Students are able to load different programs binaries in current
process
● Students are able to handle the termination of the process.
University of Central Punjab
FOIT (Operating Systems)
GL- 4
Monitoring Processes
To monitor the state of your processes under Linux use the p
s command.
ps
This option list all the processes owned by you and associated with
your terminal.
The information displayed by the “ps” command varies according
to which command option(s) you use and the type of UNIX that you
are using.
Exercise:
1. T
o display information about your processes that are currently running.
ps
2. T
o display tree structure of your processes.
pstree
Process Identification:
The pid_t data type represents process IDs which is basically a signed integer type (int). You can get
the process ID of a process by calling getpid(). The function getppid() returns the process ID of the
parent of the current process (this is also known as the parent process ID). Your program should
include the header file ‘unistd.h’ and ‘sys/types.h’ to use these functions.
University of Central Punjab
FOIT (Operating Systems)
GL- 4
pid_t getpid()
Pid_t getppid()
The getppid()function returns the process ID of the parent of the current process.
Process Creation:
The fork function creates a new process.
pid_t fork()
● On Success
○ Return a value 0 i n the child process
○ Return the c
hild's process ID in the parent process.
● On Failure
○ Returns a value - 1 i n the parent process and no child is created.
int main(void)
{
printf("Hello World!\n");
return 0;
}
University of Central Punjab
FOIT (Operating Systems)
GL- 4
Output:
Output:
University of Central Punjab
FOIT (Operating Systems)
GL- 4
Process Completion:
The functions described in this section are used to wait for a child process to terminate or stop, and
determine its status. These functions are declared in the header file "sys/wait.h".
pid_t wait (int * status)
wait() will force a parent process to wait for a child process to stop or terminate. w
ait() return the pid
of the child or -1 for an error. The exit status of the child is returned to status.
void exit (int status)
exit() terminates the process which calls this function and returns the exit status value. Both
UNIX and C (forked) programs can read the status value.
Output:
Orphan processes:
When a parent dies before its child, the child is automatically adopted by the original “init” process
whose PID is 1. To illustrate this insert a sleep statement into the child’s code. This ensured that
the parent process terminated before its child.
Output:
University of Central Punjab
FOIT (Operating Systems)
GL- 4
Zombie processes:
A process that terminates cannot leave the system until its parent accepts its return code. If its
parent process is already dead, it’ll already have been adopted by the “init” process, which
always accepts its children’s return codes. However, if a process’s parent is alive but never
executes a wait ( ), the process’s return code will never be accepted and the process will
remain a zombie.
Output:
University of Central Punjab
FOIT (Operating Systems)
GL- 4
This is where the exec system call comes into play exec will replace the contents of the currently
running process with the information from a program binary. The following code replaces the child
process with the binary created for hello.c
Step 1: Create a file “hello.c” and type following source code
#include <stdio.h>
int main()
{
printf("Hello World\n");
}
Create another file name parent.c parent.c received command line argument (containing relevant
inputs and function name which you have created in math.c) and pass to child process, then child
process load the binary math.out and execute that particular function.
./parent.out 2 3 sum
Math.out loaded
sum=5
./parent.out 3 prime
Math.out loaded
3 is a prime number
University of Central Punjab
FOIT (Operating Systems)
GL- 4