EE6253 Operating Systems and Network
Programming
PROCESSES AND THREADS
Processes and Threads
Process Thread
- A program in execution - A thread is the unit of execution
within a process
- A process can have just one
Process thread to many threads
Thread - i.e. within a process, there may
Thread be one or more units of
execution- which are threads
Thread
Thread
Processes and Threads
• CPU- central processing unit – The amount of cores in CPU decides the amount of operations that can happen at the same
time.
• Clock speed of the processor – decide the amount of operations per second
• Every thread is assigned to one of the core in CPU.
• Every core has bunch of different threads and they will execute and switch between them to perform operations.
• Multitasking – is a method that enables multiple processes to share processors and other system resources. With
multitasking, a CPU can switch between executing tasks without the need to wait for any single one to finish..
• Threading - not necessarily doing things in parallel at the same time - changing the order of the operations.
• Then why multiple threads require?
• concurrent programming/ Concurrency - If a thread (t1) is waiting for an operation to occur, the processor core can
switch and execute another thread (t2) while t1 is in a waiting state.
• Multiple threads can run simultaneously, with the CPU core switching between them during the execution stage.
Life Cycle of a Thread (Thread
States)
• New – when a thread is created
• Runnable – When the thread is ready to execute
Active State
• Running – When the thread gets the CPU
• Blocked/Waiting – When the thread is inactive/during any interruption
• Terminated/Stop – When the thread finished its job/ unusual events
(unhandled exception, segmentation fault, etc.)
Creating Threads
There are 2 ways to create Thread object;
• Have a class extending the thread class
• Implementing the Runnable interface
• In both cases run() method need to implement