0% found this document useful (0 votes)
125 views20 pages

Hawassa University Chapter 2 Linux Operating System Project

A process in Linux is a running program. Each process is identified by its Process ID (PID) and has a Parent Process ID (PPID) except for the init process. There are different types of processes like parent, child, and zombie processes. The parent process creates child processes using the fork() system call. Process termination can occur voluntarily through exit() or involuntarily through signals. Resources are allocated to processes using techniques like resource partitioning and pool-based allocation. Context switching occurs when the CPU switches from the current running process to run an interrupt routine.

Uploaded by

kangmo Abel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
125 views20 pages

Hawassa University Chapter 2 Linux Operating System Project

A process in Linux is a running program. Each process is identified by its Process ID (PID) and has a Parent Process ID (PPID) except for the init process. There are different types of processes like parent, child, and zombie processes. The parent process creates child processes using the fork() system call. Process termination can occur voluntarily through exit() or involuntarily through signals. Resources are allocated to processes using techniques like resource partitioning and pool-based allocation. Context switching occurs when the CPU switches from the current running process to run an interrupt routine.

Uploaded by

kangmo Abel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 20

Chapter 2:

Process and thread


management
Part 1: process
2.1 Introduction to Linux
process
 A process in Linux is nothing but a program in execution. It’s a running
instance of a program. Any command that you execute starts a process.

The shell is a process: shell is a process when we enter a command from


the terminal it will execute that command and it passes control so our
screen, keyboard all pass over to that program until it’s finished. Once that
program finishes control for our keyboard and screen, passes back to the
shell. Each Linux process is identified by a unique process number called
PID and every process has a parent process id (PPID) Except init.
2.2 process types in Linux

A process can be:


 Zombie
 Parent
 Child
During the execution of a program, the kernel creates a process
that helps store the program’s execution details in the system’s
memory. When a program executes, it becomes a process for
the system. So, we can say a process is a program until it
executes.
 The process created by the kernel is known as the “Parent
Process,” and all the processes derived from the parent
process are termed as “Child Processes.” A single process
may consist of several child processes having a
unique PID but with the same PPID.
 A zombie process or defunct process is a process that has
completed execution (via the exit system call) but still has an entry
in the process table. This occurs for the child processes, where the
entry is still needed to allow the parent process to read its child's
exit status. 

How to Find Parent Process ID (PPID) in Linux:


We have a couple of approaches to find the PPID of a running
process in Linux systems:
1. Using the “pstree “Command
2. Using the “ps” Command
How to Find PPID using pstree Command in Linux:
The “pstree” command is a good approach to identify the
parent process ID (PPID) as it shows the parent-child
relationship in a tree hierarchy.
Type the simply “pstree” command with the “-p” option in the
terminal to check how it displays all running parent processes
along with their child processes and respective PIDs.

2.3 process creation in Linux


Fork (): the fork () system call is used to create a
separate, duplicate process.
A new process can be created by the fork() system call. The new process
consists of a copy of the address space of the original process. fork()
creates new process from existing process. Existing process is called the
parent process and the process is created newly is called child process.

The following system calls are used for basic


process management.
Fork
A parent process uses fork to create a new child process. The child
process is a copy of the parent. After fork, both parent and child
executes the same program but in separate processes.
Exec
Replaces the program executed by a process. The child may use
exec after a fork to replace the process’ memory space with a new
program executable making the child execute a different program
than the parent.
Exit
Terminates the process with an exit status.
Wait
The parent may use wait to suspend execution until a child
terminates. Using wait the parent can obtain the exit status of a
terminated child.

For Example:
lets create a single process in c language .
# include <stdio.h>

# include <sys/types.h>

# include <unistd.h>

Int main ()

Printf(“ hello Linux ! \n PID =%d\n”, getpid());

Then after that in the terminal by typing gcc(it is the compiler used to
compile c language in linux operating system) and let say the above c
program is saved in linux.c folder in the desktop so when we apply
the gcc command and pressing enter the c file would be compiled
and an executable file would generated (it is the output file) so
inorder to run this program we should type this command ./a.out
and press enter then we get this:

// output

Hello linux pid = 1123

Now let’s add the fork system call in the main function:

# include <stdio.h>

# include <sys/types.h>

# include <unistd.h>

Int main ()

Fork ();
Printf(“ hello Linux ! \n PID =%d\n”, getpid());

Fork will produce another duplicate process with different pid!

// output

Hello linux pid = 1123

Hello linux pid = 1124

Finally let’s call fork system call three times in main function.

# include <stdio.h>

# include <sys/types.h>

# include <unistd.h>

Int main ()

Fork ();

Fork ();

Fork ();

Printf(“ hello Linux ! \n PID =%d\n”, getpid());

// output

Hello linux pid = 1123

Hello linux pid = 1124

Hello linux pid = 1125


Hello linux pid = 1126

Hello linux pid = 1127

Hello linux pid = 1128

Hello linux pid = 1129

Hello linux pid = 1130

Nb : 3 fork system calls produces 8 dupliacte process with different


pid .

Number of process created is given by 2^n:


where n = number of fork ().
Number of fork () used is given by log2n:
where n = number of process.

Exec (): when an exec () system call is activated. The program


specified in the parameter to exec () will replace the entire process
including all threads. The children usually executes exec () system call
they have the smae id but the content is different.

2.4 process termination in Linux


There are generally two ways that a process can terminate in Linux:

 Voluntarily: calling the exit () system call. This means the


process has finished its tasks, so it chooses to terminate.
 At that point, the process may return a status value (typically an
intger) to its parent process (by wait() system call).
 All the resources of the process – including physical and virtual
memory, open files, and i/0 buffers- are deallocated by the
operating system.
 Involuntarily: when receiving a signal. This signal can be sent
by another user, another process, or Linux itself, which cause the
termination of another process.

Termination can occur by other factors:

A process can cause the termination of another process by an


appropriate system call. Usually such a system call can be invoked
only by the parent of the process that is to be terminated. Only
parents can kill child process.

A parent may terminate the execution of one of its children for


a variety of reasons for example:

 The child has exceeded its usage of some of the resources


that has been allocated. (To determine whether this has
occurred, the parent process must have a mechanism to
inspect the state of its children.)
 The task assigned to the children is no longer required.
 The parent is exiting, and the operating system doesn’t allow a
child to continue if the parent is terminating.

Some systems do not allow a child to exist if its parent has


terminated. If process terminates then all its children must also
be terminated. This is known as cascading termination.
On many occasions, some other user or process may choose to kill
a process by typing command into the terminal.

Killing commands:
 Killall
 Kill-9
 Xkill
 Pkill

2.5 process’s PCB and process


management in Linux.
In Linux each process is represented by a PCB (process control
block) sometimes it is called task control block (TCB).
How to view a process?
 Two commands you can use to view the process
from the command line: ps and top
 To view all the processes with ps, use ps -ef

ps –ef command
This command is used to find the PID (Process ID, Unique
number of the process) of the process. Each process will have
the unique number which is called as PID of the process.
Top command
This command shows all running process with their
cpu usage and other resources that they used.
2.6 process Resource allocation and
context switching in linux.
The Operating System allocates resources when a program needs
them. When the program terminates, the resources are de-allocated,
and allocated to other programs that need them. Now the question
is, what strategy does the operating system use to allocate these
resources to user programs?

There are two Resource allocation techniques:


1. Resource partitioning approach –
In this approach, the operating system decides beforehand, that
what resources should be allocated to which user program. It
divides the resources in the system to many resource partitions,
where each partition may include various resources – for
example, 1 MB memory, disk blocks, and a printer.
Then, it allocates one resource partition to each user program
before the program’s initiation. A resource table records the
resource partition and its current allocation status (Allocated or
Free).
Advantages:
 Easy to Implement
 Less Overhead
Disadvantages:
 Lacks flexibility – if a resource partition contains more
resources than what a particular process requires, the
additional resources are wasted.
 If a program needs more resources than a single resource
partition, it cannot execute (Though free resources are present
in other partitions).
An example resource table may look like:
Pool based approach –
In this approach, there is a common pool of resources. The
operating System checks the allocation status in the resource
table whenever a program makes a request for a resource. If the
resource is free, it allocates the resource to the program.

Advantages:
 Allocated resources are not wasted.
 Any resource requirement can be fulfilled if the resource is free
(unlike Partitioning approach)
Disadvantages:
 Overhead of allocating and de-allocating the resources on
every request and release.

Context switching
 Interrupts cause the operating system to change a CPU from its
current task and to run a kernel routine. Such operations happen
frequently on general-purpose systems. When an interrupt occurs,
the system needs to save the current context of the process
currently running on the cpu so that it can restore that context
when its processing is done, basically suspending the process and
then resuming it.
 The context is represented in the PCB of the process.
 Switching the cpu to another process requires performing a state
save of the current process and a state restore of a different
process.

Part 2: threads
2.7 Introduction to Linux threads
 A thread is a basic unit of CPU utilization which comprises a thread
id, a program counter, a register set and a stack. It shares with
other threads belonging to same process its code section, data
section, and other operating system resources, such as open files
and signals. A traditional (heavyweight) process has a single thread
of control. But if a process has multiple threads of control, it can
perform more than one task at a time.

2.8 thread management on Linux


Let’s see an example and identify the process and its thread in
Linux using the ps -eLf command. We’re interested in
PID, LWP, and NLWP attributes:
 PID: Unique process identifier
 LWP: Unique thread identifier inside a process
 NLWP: Number of threads for a given process
We can easily identify single-threaded and multi-threaded
processes by their NLWP values. PIDs 690 and 709 have an NLWP
of 2 and 4, respectively. Hence, they are multi-threaded, with 2 and 4
threads. All other processes have an NLWP of 1 and are single-
threaded.
On careful observation, we can see that single-threaded processes
have the same PID and LWP values as if they are the same thing.
However, in a multi-threaded process, only one LWP matches its PID,
and the others have different values of LWP. Also, note that the value,
once assigned to an LWP, is never given to another process.

3.1. Single-Threaded Process


Any thread created within the process shares the same memory
and resources of the process. In a single-threaded process, the
process and thread are the same, as there’s only one thing
happening. We can also validate ps -eLf output from our previous
discussion that PID and LWP are the same for the single-threaded
process.
3.2. Multi-Threaded Process
In a multi-threaded process, the process has more than one thread.
Such a process accomplishes multiple tasks simultaneously or almost
at the same time.
As we know, the thread shares the same address space of the
process. Therefore, spawning a new thread within a process
becomes cheap (in terms of the system resources) compared to
starting a new process. Threads also can switch faster (since they
have shared address space with the process) compared to the
processes in the CPU. Internally, the thread has only a stack in the
memory, and they share the heap (process memory) with the
parent process.
Due to this nature of thread, we also call it a Light-Weight Process
(LWP).
There are both benefits and drawbacks of sharing the same memory
with other threads.
The most important benefit is that we can create threads faster than
processes since we don’t have to allocate memory and resources. The
other benefit is the low cost of inter-thread communication.
Similar to process context switch, there is a concept of a thread
context switch. A thread context switch is faster as the thread records
only its stack values before the switch happens.
There is one major disadvantage: Since the threads share the same
memory, they can become slow if the process does many concurrent
tasks.

2.9 thread creation in Linux


In Linux operating system, the C/C++ languages provide the POSIX
thread(pthread) standard API(Application program Interface) for all
thread related functions. It allows us to create multiple threads for
concurrent process flow. It is most effective on multiprocessor or multi-
core systems where threads can be implemented on a kernel-level for
achieving the speed of execution. Gains can also be found in uni-
processor systems by exploiting the latency in IO or other system
functions that may halt a process.
We must include the pthread.h header file at the beginning of the script
to use all the functions of the pthreads library. To execute the c file, we
have to use the -pthread or -lpthread in the command line while
compiling the file.
cc -pthread file.c or
cc -lpthread file.c

2.91 thread termination in Linux


A thread automatically terminates when it returns from its entry-point
routine.

A thread can also explicitly terminate itself or terminate any other thread
in the process, using a mechanism called cancelation. Because all
threads share the same data space, a thread must perform cleanup
operations at termination time; the threads library provides cleanup
handlers for this purpose.

Exiting a thread
A process can exit at any time when a thread calls the exit subroutine.
Similarly, a thread can exit at any time by calling
the pthread_exit subroutine.

Calling the exit subroutine terminates the entire process, including all its


threads. In a multithreaded program, the exit subroutine should only be
used when the entire process needs to be terminated; for example, in the
case of an unrecoverable error. The pthread_exit subroutine should be
preferred, even for exiting the initial thread.

Calling the pthread_exit subroutine terminates the calling thread.


The status parameter is saved by the library and can be further used
when joining the terminated thread. Calling the pthread_exit subroutine is
similar, but not identical, to returning from the thread's initial routine. The
result of returning from the thread's initial routine depends on the thread:

 Returning from the initial thread implicitly calls the exit subroutine, thus


terminating all the threads in the process.
 Returning from another thread implicitly calls
the pthread_exit subroutine. The return value has the same role as
the status parameter of the pthread_exit subroutine.

To avoid implicitly calling the exit subroutine, to use


the pthread_exit subroutine to exit a thread.

Exiting the initial thread (for example, by calling


the pthread_exit subroutine from the main routine) does not terminate the
process. It terminates only the initial thread. If the initial thread is
terminated, the process will be terminated when the last thread in it
terminates. In this case, the process return code is 0.

The following program displays exactly 10 messages in each language.


This is accomplished by calling the pthread_exit subroutine in
the main routine after creating the two threads, and creating a loop in
the Thread routine.

2.92 thread implementation


and thread context switching
in Linux.

Implementation of threads
User - Level Threads
The user-level threads are implemented by users and the kernel is not
aware of the existence of these threads. It handles them as if they were
single-threaded processes. User-level threads are small and much faster
than kernel level threads. They are represented by a program
counter(PC), stack, registers and a small process control block. Also,
there is no kernel involvement in synchronization for user-level threads.
Advantages of User-Level Threads
Some of the advantages of user-level threads are as follows −
 User-level threads are easier and faster to create than kernel-level
threads. They can also be more easily managed.
 User-level threads can be run on any operating system.
 There are no kernel mode privileges required for thread switching in
user-level threads.
Disadvantages of User-Level Threads
Some of the disadvantages of user-level threads are as follows −
 Multithreaded applications in user-level threads cannot use
multiprocessing to their advantage.
 The entire process is blocked if one user-level thread performs
blocking operation.

Kernel-Level Threads
Kernel-level threads are handled by the operating system directly and
the thread management is done by the kernel. The context information
for the process as well as the process threads is all managed by the
kernel. Because of this, kernel-level threads are slower than user-level
threads.
Advantages of Kernel-Level Threads
Some of the advantages of kernel-level threads are as follows −
 Multiple threads of the same process can be scheduled on different
processors in kernel-level threads.
 The kernel routines can also be multithreaded.
 If a kernel-level thread is blocked, another thread of the same
process can be scheduled by the kernel.
Disadvantages of Kernel-Level Threads
Some of the disadvantages of kernel-level threads are as follows −
 A mode switch to kernel mode is required to transfer control from
one thread to another in a process.
 Kernel-level threads are slower to create as well as manage as
compared to user-level threads.

You might also like