OS 2 Threads System Calls

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 9

OPERATING SYSTEMS

THREADS & SYSTEM CALLS


2
1. THREADS IN OS

A thread may be defined as a flow of execution through some process code.


● A thread is also known as a lightweight process.
● Threads improves application’s performance by providing parallelism.
● Threads improve the performance of operating systems by reducing the process overhead.
● Thread is equivalent to a classical process, and it is a software approach for software
improvement.
● Every thread belongs to only one process and not a single thread can exist outside a process.
● It is a basic unit of CPU utilization.
● throughput: number of threads that are executed by CPU per unit time.

Attributes of thread:
1. thread ID
2. program counter
3. stack pointer
4. registers
5. code counter
6. data counter
7. file counter
● the context of threads is less than the context of the process.
● context of threads stored in thread control block.
● TCB is unique for each thread.
● |TCB|< |PCB| (Size)
1.1. Difference between Process and Thread

S.N. Process Thread

1 Process is resource intensive or heavy Thread is a light-weight process


weight. which requires less resources than a
process.

2 Process switching needs interaction Thread switching doesn’t require any


with the OS. interaction with the operating system.

3 In multiprocessing environments, All threads can share the same open


each process has its own memory files, child processes, etc..
and file resources but executes the
same code.

4 If one process gets blocked, then Whereas if one thread gets blocked
another process cannot run until the and waiting, a second thread of the
first process is unblocked. same task can execute.

5 Multiple processes without threads Multi-threaded processes use less


needs more resources. resources.

6 In multiple processes each process Thread can read, write or alternate


operates independently of the others. another thread's data.

Table-1: Process and Thread

1.2. Advantages of Threads


● Responsiveness: If any process is divided into multiple threads and one thread
completes its execution, then its output will be returned immediately.
● Faster context switch: Context switching is faster in threads as compared to
context switching in processes because process context-switching requires CPU
overhead
● Effective utilization of multiprocessor system: Multiple threads in a
multiprocessor system results in faster and more efficient utilization of resources.
● Resource sharing: All threads of a process can share resources like code, data, etc.
Note-1: Stack space, registers, and Program counter cannot be shared among the
threads. Each thread maintains its own program counter and has its own stack and
registers.
Note-2: A thread shares few information like data segment, code segment, and open
files with its peer threads.
Fig-1: threads

● Communication: As the threads share common address space, the communication


between multiple threads is easier. Whereas in process we have to follow some
communication techniques to establish communication between processes.
● Enhanced throughput of the system: If a single process is divided into multiple
threads, and every thread functioning is considered as one complete job, then the
total number of jobs completed per unit of time will be increased, thus, this will
result in increasing system throughput.
1.3. classification of threads based on number of threads per process:
1. Single level threaded process

code data file

Stack Pointer Program Counter Registers

T1

2. Multi-level threaded process

code data file shared

Stack Pointer Stack Pointer Stack Pointer private


Program Counter Program Program Counter
Registers Counter Registers
Registers

T1 T2 T3

● all the current OS using multithreading concept


Some advantages of multithreading
● responsiveness (parallelly working more than one threads.)
● context switches time is less
● in single threaded, the thread itself is processed. so we can say context switches
time between processes is more,
● effective utilization of multiprocessor architecture, so if we have three CPU in a
computer then we can make all 3 CPUs using a multithreaded process, but in single
tp, one CPU can be busy.
● enhanced system: single threaded process system throughput w.r.t number of
processes is 1, but for multi-threaded process system, throughput w.r.t number of
process is more than 1.
● In multithreading, resources can be shared.
1.4. Types of Thread
Threads are of two −
● User Level Threads − Threads managed by users.
● Kernel Level Threads − Threads managed by operating system.

1.4.1 User Level Threads


In user level threads, the thread management kernel is unaware of the existence of
user level threads. The thread library consists of code for creating and destroying
threads, for passing messages & data between various threads, for scheduling thread
execution and for saving and restoring thread contexts.
1.4.2 Kernel Level Threads
Kernel is responsible for thread-management. Application area of the program doesn’t
have any code for thread-management. Kernel threads are directly supported by the
operating system. Any system application can be programmed to be multithreaded.
1.4.3 Difference between User Level thread and Kernel Level thread

USER LEVEL THREAD KERNEL LEVEL THREAD

User threads are implemented by users. kernel threads are implemented by OS.

OS doesn’t recognize user level threads. Kernel threads are recognized by OS.

Implementation of User threads is easy. Implementation of Kernel thread is complicated.

Context switch time is less. Context switch time is more.

Context switch requires no hardware support. Hardware support is needed.


If one user level thread perform blocking If one kernel thread performs blocking operation,
operation, then entire process will be blocked. then another thread can continue execution.
User level threads are designed as dependent Kernel level threads are designed as independent
threads. threads.
Table-2: Types of threads
Relation in multi-threading
1. one to one relation
2. many to one relation
3. many to many relation
one to one relation
● in this for each ULT implementation, it is compulsory to implement one KLT
corresponds.
● reliable: if any ULT involves one block() system call then it affects itself
not another ult.
● concurrency: [parallel execution]
● n parallel process has to create n KLT, one KLT is difficult to implement then n KLT
is more complicated.
many to one relation
● more user level thread, and one kernel level thread.
● easy to implement than one to one relationship.
● not reliable
● no concurrency
many to many
● many users level threads and many kernel level threads.
● there is no condition related to the number of user level threads and kernel level
threads.
● can have more user level threads and less kernel level threads.
● achieve reliability.
● easy to implement that one to one.
● supports concurrency.
some important points
● For user level threads a system call can block the entire process.
● blocking one kernel level thread can block all related threads because
it's dependent.
● On a per thread basis, the OS maintains CPU register state, stack pointer and
program counter.
● the OS does not maintain a separate stack for each process, shared by threads.
● on per thread, the OS does not maintain virtual memory state.
● On a per thread basis, the OS maintains scheduling info.
● threads of process share both heap and global variables.
System calls: -
A system call is the method through which a computer program asks a service from the
kernel of the operating system on which it is running. A system call is a method of
interacting with the operating system via programs. When a computer software makes
a request to the kernel of the operating system, it is called a system call. The operating
system's services are provided to user programs via the Application Program Interface
(API) (API). It acts as a link between a process and the operating system, allowing
user- level programs to request operating system services. The kernel system can only
be accessed using system calls. All programs needing resources must use system calls.
How are system calls made?
A system call is made when a computer program needs to access the kernel of the
operating system. The system call exposes the operating system's services to user
programs via an API. It's the only way to get into the kernel system. System calls are
used as an interface between the operating system and user programs by any programs
or processes that require resources for execution.
Below are some examples of how a system call varies from a user function.
1. A system call function may create and use kernel processes to execute the
asynchronous processing.
2. A system call has greater authority than a standard subroutine. A system call with
kernel-mode privilege executes in the kernel protection domain.
3. System calls are not permitted to use shared libraries or any symbols that are not
present in the kernel protection domain.
4. The code and data for system calls are stored in global kernel memory.
Why do you need system calls in Operating System?
There are a variety of situations in which system calls in the operating system are
required. The following are some of the situations:
1. It must require when a file system wants to create or delete a file.
2. Network connections require the system calls to sending and receiving data packets.
3. If you want to read or write a file, you need to system calls.
4. If you want to access hardware devices, including a printer, scanner, you need a
system call.
5. System calls are used to create and manage new processes.
Services Provided by System Calls:
1. Process creation and management
2. Main memory management
3. File Access, Directory and File system management
4. Device handling(I/O)
5. Protection
6. Networking, etc.
Types of System Calls:
There are 5 different categories of system calls --
1. Process control: end, abort, create, terminate, allocate and free memory.
2. File management: create, open, close, delete, read file etc.
3. Device management
4. Information maintenance
5. Communication
Examples of Windows and Unix system calls
There are various examples of Windows and Unix system calls. These are as listed
below in the table:

Process Windows Unix

Process Control CreateProcess() Fork()


ExitProcess() Exit()
WaitForSingleObject() Wait()

File Manipulation CreateFile() Open()


ReadFile() Read()
WriteFile() Write()
CloseHandle() Close()

Device Management SetConsoleMode() Ioctl()


ReadConsole() Read()
WriteConsole() Write()

Information Maintenance GetCurrentProcessID() Getpid()


SetTimer() Alarm()
Sleep() Sleep()

Communication CreatePipe() Pipe()


CreateFileMapping() Shmget()
MapViewOfFile() Mmap()

Protection SetFileSecurity() Chmod()


InitializeSecurityDescriptor() Umask()
SetSecurityDescriptorgroup() Chown()
open ()
The open () system call allows you to access a file on a file system. It allocates resources
to the file and provides a handle that the process may refer to. Many processes can open a
file at once or by a single process only. It's all based on the file system and structure.
read ()
It is used to obtain data from a file on the file system. It accepts three arguments in general:
o A file descriptor.
o A buffer to store read data.
o The number of bytes to read from the file.
The file descriptor of the file to be read could be used to identify it and open it
using open() before reading.
wait ()
In some systems, a process may have to wait for another process to complete its execution
before proceeding. When a parent process makes a child process, the parent process
execution is suspended until the child process is finished. The wait () system call is used to
suspend the parent process. Once the child process has completed its execution, control is
returned to the parent process.
write ()
It is used to write data from a user buffer to a device like a file. This system call is one way
for a program to generate data. It takes three arguments in general:
o A file descriptor.
o A pointer to the buffer in which data is saved.
o The number of bytes to be written from the
buffer. fork ()
The fork () system call is used by processes to create clones of themselves. It's one of the
most prevalent techniques for operating systems to create processes. When a parent process
spawns a child process, the parent process's execution is halted until the child process is
finished. Control is returned to the parent process once the child process has completed its
execution.
close ()
It is used to end file system access. When this system call is invoked, it signifies that the
program no longer requires the file, and the buffers are flushed, the file information is
altered, and the file resources are de-allocated as a result.
exec ()
When an executable file replaces an earlier executable file in an already executing
process, this system function is invoked. As a new process is not built, the old process
identification stays, but the new process replaces data, stack, data, head, etc.
exit ()
The exit () is a system call that is used to end program execution. This call indicates that the
thread execution is complete, which is especially useful in multi-threaded environments. The
operating system reclaims resources spent by the process following the use of the exit
() system function.

You might also like