Sie sind auf Seite 1von 13

Introduction to Process

Process
 A operation that runs on a Unix system is a
process.
 Process is a program in execution.

 Unix creates a process when executing the


commands.

 Process is removed from the system when the


command finish its execution.

 Multiple process can be executed simultaneously


by quickly switching from one process to the
other.
Types of Process

1. User Defined Process


Created by user using APIs like Fork, clone,
etc..

2. Kernel Thread Process


Created by kernel using kernel threads
Bash/System Commands to know running
Process

ps-command

ps- list the process running in the system


ps -a -> all the process list
ps #no -> display a particular process
ps -H -> print hierarchially

Useful to find the process identifier (pid).


Bash/System Commands to know running
Process(Contd.)

top – command

* Similar to ps command, also gives information


about CPU usage, zombies, etc..

* List all the process including the root initiated


processes.
Bash/System Commands to know running
Process(Contd.)

kill – command

* To send signal to process

* kill -l -> list all options for communication with


process

* kill #processid -> kill a particular process


Process IDs - PID

* Each process has a unique ID called process ID


– PID.
* Can be read by using getpid() API

Other IDs:
* Parent process ID - getppid()
* Group ID - getgid()
* User ID - getuid()

* Process ID are dynamic and thus changes


every time when the process invoked.
User Defined Process

* init – Parent of all process

* User can generate their own process

* Each process(Parent process) can in-turn


generate subprocesses(Child process)
Forking

* Parent process can generate child process and control them.

* Child will inherit the file accessed by the parent. Inherits:


1. File descriptor
2. Memory Image
3. CPU States(Registers)

* Advantage - Uses the same memory page of the parent and


copy is made only during wirte attempt by the child process
– Copy On Write
Forking(Contd.)
* Each process has own set of variables.

* When child exits then system resources allotted to the child


are restored back.

* After forking the operation shifting between both the process


happens by linux scheduler.

* Forking performed by using Fork() API.

#include <unistd.h>
pid_t fork(void);
return value :
> 0 -> parent context
= 0 -> Child context
< 0 -> Child not created
Wait() and Zombies
* If wait not called by the parent then child exists after parent
exits => Child works for none and occupies unwanted
memory space and runtime - > ZOMBIE processes.

* In case parent dies before child, then child inherited by the


mother process i.e 'init' process.

* Wait function suspends the parent until the child exits thus
avoiding Zombies.

* prototype : pid_t wait(int *status)

returns pid of the child process that was over.

* Waits for all child created by that process to exit.


waitpid()

* Waits for particular child to exit.

Prototype: pid_t waitpid(pid_t pid, int *status, int option)

returns the pid of the exited child process.

>0 - suspend till certain process exits


=0 - suspends till group process exits
-1 - suspends all child exits
Thank You

Das könnte Ihnen auch gefallen