SlideShare a Scribd company logo
INTRODUCTION TO THREADS
PREVIOUS SESSION
• Program vs Process
• Process states
• Process control block
• Process creation through fork()
• Exec(), wait and exit() system calls
• Zombie and orphan process
AGENDA
• Interprocess communication
• What is a thread
• Thread vs process
• Multithreading and multiprocessing
• Multithreading models
• Challenges with multithreading
INTERVIEW QUESTIONS
• What are similarities and differences between a process and a
thread?
• What is IPC? Give some examples of IPC
• Why process coordination is necessary in OS?
• What are advantages and disadvantages of using shared memory and
IPC?
• What is multi-tasking, multi-programming, multithreading and
multiprocessing?
• What are the challenges faced in multithreading programs?
• Difference between chrome and firefox in terms of multiprocessing
and multithreading?
• What is the difference between named pipe and unnamed pipe?
• What are the benefits of multithreading?
INTERPROCESS COMMUNICATION
• Independent vs cooperating process
• Does a process shares data with other process?
• Debugging becomes tough in cooperating processes.
• Why process cooperation is necessary?
Need for process cooperation
• Information sharing
• Computational speedup
• Modularity
• Convenience
• Detailed explanation: https://youtu.be/7jN8tgLqbFc
Models/Types of interprocess communication
• Shared memory
• A region of shared memory is first established
• Processes then share information by reading and writing to this shared space.
• Threads
• Message passing
• A link is established between two or more processes
• Messages are then shared on this link
• Detailed explanation: https://youtu.be/FCPUTFmAlIg
Threads in Operating System | Multithreading | Interprocess Communication
Shared memory vs Message Passing
Examples
• Shared memory is achieved using multithreading.
• Message passing examples
• Sockets
• Pipes
• Remote procedure calls
• Pipes
• Ordinary pipes
• Named pipes
Threads in Operating System | Multithreading | Interprocess Communication
PIPES
• Unnamed pipe
• ls | grep “hello”
• cat filename | grep “hello”
• Named pipe
• mkfifo <file name>
• Unnamed pipe can only be used to communicate between parent and child
processes.
• A named pipe exists on the file system.
• Named pipe can be used for communication over a network.
• Named pipe are bidirectional.
WHAT IS A THREAD
• A thread is a basic unit of CPU utlilization.
• A thread consists of:
• Thread ID
• Program counter
• Register sets
• Stack segment memory
• What does CPU needs to execute instructions?
• Registers
• Program counter
• Process is also a unit of CPU utilization, but not a basic unit.
• Detailed explanation - https://youtu.be/pYbBgA76wDg
What a thread shares with other threads?
• Code section memory
• Data segment memory
• Heap segment memory
• Open files
• Signals
• Linux has a unique implementation of threads. To the Linux kernel,
there is no concept of a thread. Linux implements all threads as
standard processes.
• Each thread has a unique process control block and appears to the
kernel as a normal process—threads just happen to share resources,
such as an address space, with other processes.
• Windows – explicit support for threads (lightweight process)
Threads implementation in Linux
• Normal fork()
• clone(SIGCHLD, 0);
• Thread implementation
• clone(CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND, 0);
Threads in Operating System | Multithreading | Interprocess Communication
THREAD VS PROCESS
PROCESS THREAD
Differences continued
Detailed explanation -
https://youtu.be/MwDccB9tvp0
MULTITHREADING
• Multiple threads within a process
• Should be supported by the hardware
• Concurrency vs parallelism
• Most applications are multithreaded today
• Browser – rendering + fetching
• Web server having multiple clients
• Word processor – displaying graphics + waiting for keystroke + spell
check
Benefits of multithreading
• Responsiveness – browser, word-processor
• Resource sharing – threads share the memory and resources by
default
• Economy – creating multiple processes is very costly.
• Scalability – increases the parallelism, web servers
Challenges
• Concurrency issues
• Multiple threads trying to update the resource at the same time.
• Leads to race condition
• How i++ is actually executed?
Initially i = 7, final value = 9 and two threads
incrementing i
Initial value = 7, final value = 8
MULTIPROCESSING
• Multiple processes within a process
• Using fork() to create multiple processes
• Process creation is time-consuming than creating threads.
• Process address space is not shared among processes.
CPUs, cores and hyperthreading
• A core is the basic computational unit of CPU. It has registers and all
other resources to execute a program.
• Processes -> threads
• CPU are the processor chips.
• Initially, a CPU had only one core.
• Today, dual-core or quad-core processors
• Hyper threading – allows a single core to run multiple threads. Some
hardware are duplicated.
Threads in Operating System | Multithreading | Interprocess Communication
When to use thread vs process
• Threads are faster
• Crashing one thread crashes other threads too.
• No security between threads – lead to race condition
• A tradeoff between speed vs reliability
• Chrome vs firefox – multiprocessing vs multithreading
• Web servers
• Word processor
• Computer games
Multiprogramming, multithreading,
multiprocessing
• Multiprogramming – ability of the system to run multiple programs
concurrently. Time sharing OS. Can happen even on a single core.
• Multitasking is similar to multiprogramming as we are able to do
multiple tasks at the same time.
• Multiprocessing – Using multiple cores for executing multiple
programs in parallel.
• Multithreading – Similar to multiprocessing but here the memory is
shared between threads. Usually, very fast.
Multithreading models
• How user threads are mapped to kernel threads?
• Not asked in any interviews
• 1:1 mapping
• N:N mapping
• M:N mapping
• Videos:
• https://youtu.be/lYNk1cNWCfc
• https://youtu.be/1Jij_qY2kp0
NEXT SESSION: PROCESS SCHEDULING
ALGORITHMS

More Related Content

PPTX
Process management in operating system | process states | PCB | FORK() | Zomb...
PPTX
Process Scheduling Algorithms | Interviews | Operating system
PPTX
Process Synchronization in operating system | mutex | semaphore | race condition
PPTX
Introduction to operating system, system calls and interrupts
PPTX
Operating System Operations ppt.pptx
PPT
OS Process and Thread Concepts
PPT
Introduction to System Calls
PPTX
Memory management in operating system | Paging | Virtual memory
Process management in operating system | process states | PCB | FORK() | Zomb...
Process Scheduling Algorithms | Interviews | Operating system
Process Synchronization in operating system | mutex | semaphore | race condition
Introduction to operating system, system calls and interrupts
Operating System Operations ppt.pptx
OS Process and Thread Concepts
Introduction to System Calls
Memory management in operating system | Paging | Virtual memory

What's hot (20)

PPT
12 process control blocks
PPT
Os Threads
PPTX
INTER PROCESS COMMUNICATION (IPC).pptx
PPT
OPERATING SYSTEM SERVICES, OPERATING SYSTEM STRUCTURES
PPTX
Process synchronization
PPTX
cpu scheduling
PDF
Process Scheduling in OS
PPTX
Operating system 11 system calls
PDF
CS6401 OPERATING SYSTEMS Unit 2
PPT
Chapter 6 - Process Synchronization
PDF
Inter Process Communication
PPTX
Deadlock ppt
PPTX
Distributed Operating Systems
PPT
Unix.system.calls
PPTX
Lecture 2 process
PDF
Monitors
PPT
Part 1 - PROCESS CONCEPTS
PPTX
Multithreading
12 process control blocks
Os Threads
INTER PROCESS COMMUNICATION (IPC).pptx
OPERATING SYSTEM SERVICES, OPERATING SYSTEM STRUCTURES
Process synchronization
cpu scheduling
Process Scheduling in OS
Operating system 11 system calls
CS6401 OPERATING SYSTEMS Unit 2
Chapter 6 - Process Synchronization
Inter Process Communication
Deadlock ppt
Distributed Operating Systems
Unix.system.calls
Lecture 2 process
Monitors
Part 1 - PROCESS CONCEPTS
Multithreading
Ad

Similar to Threads in Operating System | Multithreading | Interprocess Communication (20)

PDF
Threads operating system slides easy understand
PPTX
Chapter -2 operating system presentation
PPTX
Lecture 3 threads
PPTX
PPTX
Os lectures
PDF
Parallel and Distributed Computing chapter 3
PPTX
Lecture 3- Threads (1).pptx
PPTX
Gpgpu intro
PDF
Threads lecture slides for operating systems
PPTX
Operating Systems & Applications
PDF
threads (1).pdfmjlkjfwjgliwiufuaiusyroayr
PPTX
Processes, Threads.pptx
PPTX
Processes, Threads.pptx
PPTX
2014 01-21-mpi-community-feedback
PDF
Ch4 threads
PPTX
PPTX
Thread
PDF
EuroMPI 2013 presentation: McMPI
PPTX
Taming the resource tiger
PPTX
Taming the resource tiger
Threads operating system slides easy understand
Chapter -2 operating system presentation
Lecture 3 threads
Os lectures
Parallel and Distributed Computing chapter 3
Lecture 3- Threads (1).pptx
Gpgpu intro
Threads lecture slides for operating systems
Operating Systems & Applications
threads (1).pdfmjlkjfwjgliwiufuaiusyroayr
Processes, Threads.pptx
Processes, Threads.pptx
2014 01-21-mpi-community-feedback
Ch4 threads
Thread
EuroMPI 2013 presentation: McMPI
Taming the resource tiger
Taming the resource tiger
Ad

More from Shivam Mitra (16)

PPTX
Preparing for SRE Interviews
PPTX
PART 4 - Python Tutorial | If Else In Python With Examples
PPTX
PART 3 - Python Tutorial | For Loop In Python With Examples
PPTX
PART 9 - Python Tutorial | While Loop In Python With Examples
PPTX
PART 8 - Python Tutorial | User Input In Python With Examples
PPTX
PART 6 - Python Tutorial | Tuples In Python With Examples
PPTX
PART 10 - Python Tutorial | Functions In Python With Examples
PPTX
PART 7 - Python Tutorial | Dictionaries In Python With Examples
PPTX
PART 0 - Python Tutorial | Why should you learn python
PPTX
PART 1 - Python Tutorial | Variables and Data Types in Python
PPTX
Part 2 - Python Tutorial | Introduction to Lists
PPTX
What is Internet and How it Works
PPTX
OSI Model Layers and Internet Protocol Stack
PPTX
Basics of Stock Market
PPTX
Assets vs liability
PPTX
Pycricbuzz - a python library to fetch live cricket scores
Preparing for SRE Interviews
PART 4 - Python Tutorial | If Else In Python With Examples
PART 3 - Python Tutorial | For Loop In Python With Examples
PART 9 - Python Tutorial | While Loop In Python With Examples
PART 8 - Python Tutorial | User Input In Python With Examples
PART 6 - Python Tutorial | Tuples In Python With Examples
PART 10 - Python Tutorial | Functions In Python With Examples
PART 7 - Python Tutorial | Dictionaries In Python With Examples
PART 0 - Python Tutorial | Why should you learn python
PART 1 - Python Tutorial | Variables and Data Types in Python
Part 2 - Python Tutorial | Introduction to Lists
What is Internet and How it Works
OSI Model Layers and Internet Protocol Stack
Basics of Stock Market
Assets vs liability
Pycricbuzz - a python library to fetch live cricket scores

Recently uploaded (20)

PDF
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
PPTX
Internet of Things (IOT) - A guide to understanding
PDF
composite construction of structures.pdf
PPTX
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
PPTX
Welding lecture in detail for understanding
DOCX
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
PPTX
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
PPTX
CH1 Production IntroductoryConcepts.pptx
PDF
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
PDF
Digital Logic Computer Design lecture notes
PPTX
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
PPTX
Foundation to blockchain - A guide to Blockchain Tech
PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
PPTX
bas. eng. economics group 4 presentation 1.pptx
PPTX
UNIT-1 - COAL BASED THERMAL POWER PLANTS
PDF
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
PPTX
Lecture Notes Electrical Wiring System Components
DOCX
573137875-Attendance-Management-System-original
PDF
Model Code of Practice - Construction Work - 21102022 .pdf
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
Internet of Things (IOT) - A guide to understanding
composite construction of structures.pdf
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
Welding lecture in detail for understanding
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
CH1 Production IntroductoryConcepts.pptx
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
Digital Logic Computer Design lecture notes
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
Foundation to blockchain - A guide to Blockchain Tech
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
bas. eng. economics group 4 presentation 1.pptx
UNIT-1 - COAL BASED THERMAL POWER PLANTS
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
Lecture Notes Electrical Wiring System Components
573137875-Attendance-Management-System-original
Model Code of Practice - Construction Work - 21102022 .pdf

Threads in Operating System | Multithreading | Interprocess Communication

  • 2. PREVIOUS SESSION • Program vs Process • Process states • Process control block • Process creation through fork() • Exec(), wait and exit() system calls • Zombie and orphan process
  • 3. AGENDA • Interprocess communication • What is a thread • Thread vs process • Multithreading and multiprocessing • Multithreading models • Challenges with multithreading
  • 4. INTERVIEW QUESTIONS • What are similarities and differences between a process and a thread? • What is IPC? Give some examples of IPC • Why process coordination is necessary in OS? • What are advantages and disadvantages of using shared memory and IPC? • What is multi-tasking, multi-programming, multithreading and multiprocessing? • What are the challenges faced in multithreading programs?
  • 5. • Difference between chrome and firefox in terms of multiprocessing and multithreading? • What is the difference between named pipe and unnamed pipe? • What are the benefits of multithreading?
  • 6. INTERPROCESS COMMUNICATION • Independent vs cooperating process • Does a process shares data with other process? • Debugging becomes tough in cooperating processes. • Why process cooperation is necessary?
  • 7. Need for process cooperation • Information sharing • Computational speedup • Modularity • Convenience • Detailed explanation: https://youtu.be/7jN8tgLqbFc
  • 8. Models/Types of interprocess communication • Shared memory • A region of shared memory is first established • Processes then share information by reading and writing to this shared space. • Threads • Message passing • A link is established between two or more processes • Messages are then shared on this link • Detailed explanation: https://youtu.be/FCPUTFmAlIg
  • 10. Shared memory vs Message Passing
  • 11. Examples • Shared memory is achieved using multithreading. • Message passing examples • Sockets • Pipes • Remote procedure calls • Pipes • Ordinary pipes • Named pipes
  • 13. PIPES • Unnamed pipe • ls | grep “hello” • cat filename | grep “hello” • Named pipe • mkfifo <file name> • Unnamed pipe can only be used to communicate between parent and child processes. • A named pipe exists on the file system. • Named pipe can be used for communication over a network. • Named pipe are bidirectional.
  • 14. WHAT IS A THREAD • A thread is a basic unit of CPU utlilization. • A thread consists of: • Thread ID • Program counter • Register sets • Stack segment memory • What does CPU needs to execute instructions? • Registers • Program counter • Process is also a unit of CPU utilization, but not a basic unit. • Detailed explanation - https://youtu.be/pYbBgA76wDg
  • 15. What a thread shares with other threads? • Code section memory • Data segment memory • Heap segment memory • Open files • Signals
  • 16. • Linux has a unique implementation of threads. To the Linux kernel, there is no concept of a thread. Linux implements all threads as standard processes. • Each thread has a unique process control block and appears to the kernel as a normal process—threads just happen to share resources, such as an address space, with other processes. • Windows – explicit support for threads (lightweight process)
  • 17. Threads implementation in Linux • Normal fork() • clone(SIGCHLD, 0); • Thread implementation • clone(CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND, 0);
  • 20. Differences continued Detailed explanation - https://youtu.be/MwDccB9tvp0
  • 21. MULTITHREADING • Multiple threads within a process • Should be supported by the hardware • Concurrency vs parallelism • Most applications are multithreaded today • Browser – rendering + fetching • Web server having multiple clients • Word processor – displaying graphics + waiting for keystroke + spell check
  • 22. Benefits of multithreading • Responsiveness – browser, word-processor • Resource sharing – threads share the memory and resources by default • Economy – creating multiple processes is very costly. • Scalability – increases the parallelism, web servers
  • 23. Challenges • Concurrency issues • Multiple threads trying to update the resource at the same time. • Leads to race condition • How i++ is actually executed?
  • 24. Initially i = 7, final value = 9 and two threads incrementing i
  • 25. Initial value = 7, final value = 8
  • 26. MULTIPROCESSING • Multiple processes within a process • Using fork() to create multiple processes • Process creation is time-consuming than creating threads. • Process address space is not shared among processes.
  • 27. CPUs, cores and hyperthreading • A core is the basic computational unit of CPU. It has registers and all other resources to execute a program. • Processes -> threads • CPU are the processor chips. • Initially, a CPU had only one core. • Today, dual-core or quad-core processors • Hyper threading – allows a single core to run multiple threads. Some hardware are duplicated.
  • 29. When to use thread vs process • Threads are faster • Crashing one thread crashes other threads too. • No security between threads – lead to race condition • A tradeoff between speed vs reliability • Chrome vs firefox – multiprocessing vs multithreading • Web servers • Word processor • Computer games
  • 30. Multiprogramming, multithreading, multiprocessing • Multiprogramming – ability of the system to run multiple programs concurrently. Time sharing OS. Can happen even on a single core. • Multitasking is similar to multiprogramming as we are able to do multiple tasks at the same time. • Multiprocessing – Using multiple cores for executing multiple programs in parallel. • Multithreading – Similar to multiprocessing but here the memory is shared between threads. Usually, very fast.
  • 31. Multithreading models • How user threads are mapped to kernel threads? • Not asked in any interviews • 1:1 mapping • N:N mapping • M:N mapping • Videos: • https://youtu.be/lYNk1cNWCfc • https://youtu.be/1Jij_qY2kp0
  • 32. NEXT SESSION: PROCESS SCHEDULING ALGORITHMS