0% found this document useful (0 votes)
4 views38 pages

Lecture 9

Uploaded by

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

Lecture 9

Uploaded by

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

1

Lahore Garrison University


CSC351-Operating System
Week-5 Lecture-9
Semester 5 Fall 2018
Prepared by:

Sabreena Nawaz
2
Instructor Contact Details

 Name: Sabreena Nawaz


 Course Instructor: CSC351- Operating System
 Credit Hours: 5
 Office Location: CS-Female Staff Room
 Email: [email protected]
 Visiting Hours: Wednesday (9-11 am)
 Lab Work: Video Tutorials & Supervision by Instructor

Lahore Garrison University


3
Course Objectives

 To learn the fundamentals of Operating Systems.


 To learn the mechanisms of OS to handle processes and
threads and their communication
 To learn the mechanisms involved in memory
management in contemporary OS.
 To gain knowledge on distributed operating
system concepts that includes architecture,
Mutual exclusion algorithms, deadlock detection
algorithms and agreement protocolsUnderstand
how to read C++ doc library documentation and
reuse library code.
 To learn programmatically to implement simple
OS mechanisms.

Lahore Garrison University


4
Course Contents

 Following contents will be covered throughout the


semester in week wise lectures:
 OS basics, OS types, system calls, process management,
inter process communication, communication in client
server systems, Thread concepts, thread types, thread
control block, thread designs, CPU scheduling, process
synchronization, bakery algorithm, hardware solutions to
synchronization, Problems of synchronization, deadlock
occurrence, prevention, avoidance recovery, Memory
management, virtual memory, page faults, replacement
algorithms, operating system security.

Lahore Garrison University


5
Text Book

 Operating system concepts by


Abraham Silberschatz, Galvin,
Gagne, 9th edition

Lahore Garrison University


6

► Userand kernel
Preamble thread
(Past lesson ► Concurrency

brief) ► parallelism

Lahore Garrison University


7
Multithreading Models

 Many-to-One

 One-to-One

 Many-to-Many

Lahore Garrison University


8
Many-to-One

 Many user-level threads mapped to


single kernel thread
 One thread blocking causes all to
block
 Multiple threads may not run in
parallel on muticore system
because only one may be in kernel
at a time
 Few systems currently use this
model
 Examples:
 Solaris Green Threads

Lahore GarrisonGNU Portable
University Threads
9
One-to-One

 Each user-level thread maps to kernel thread


 Creating a user-level thread creates a kernel
thread
 More concurrency than many-to-one
 Number of threads per process sometimes
restricted due to overhead
 Examples
 Windows
 Linux
 Solaris 9 and later

Lahore Garrison University


1
0
Many-to-Many Model

 Allows many user level threads to


be mapped to many kernel
threads
 Allows the operating system to
create a sufficient number of
kernel threads
 Solaris prior to version 9
 Windows with the ThreadFiber
package

Lahore Garrison University


1
Two-level Model 1

 Similar to M:M, except that it


allows a user thread to be bound
to kernel thread
 Examples
 IRIX
 HP-UX
 Tru64 UNIX
 Solaris 8 and earlier

Lahore Garrison University


1
Thread Libraries 2

 Thread library provides programmer with API for


creating and managing threads
 Two primary ways of implementing
 Library entirely in user space
 Kernel-level library supported by the OS

Lahore Garrison University


1
Pthreads 3

 May be provided either as user-level or kernel-level


 A POSIX standard (IEEE 1003.1c) API for thread creation
and synchronization
 Specification, not implementation
 API specifies behavior of the thread library,
implementation is up to development of the library
 Common in UNIX operating systems (Solaris, Linux, Mac
OS X)

Lahore Garrison University


1
Pthreads Example 4

Lahore Garrison University


1
Pthreads Example (Cont.) 5

Pthreads Example (Cont.)

Lahore Garrison University


1
Pthreads Codefor
Pthreads Code for Joining
Joining 10 Threads
10 Threads 6

Lahore Garrison University


Windows Multithreaded C 1
7
Program

Lahore Garrison University


1
8
Windows Multithreaded C Program (Cont.)

Lahore Garrison University


1
Java Threads 9

 Java threads are managed by the JVM


 Typically implemented using the threads model provided
by underlying OS
 Java threads may be created by:

 Extending Thread class


 Implementing the Runnable interface

Lahore Garrison University


2
Java Multithreaded Program 0

Lahore Garrison University


2
Java Multithreaded Program 1
(Cont.)

Lahore Garrison University


2
Implicit Threading 2

 Growing in popularity as numbers of threads increase,


program correctness more difficult with explicit threads
 Creation and management of threads done by
compilers and run-time libraries rather than
programmers
 Three methods explored
 Thread Pools
 OpenMP
 Grand Central Dispatch
 Other methods include Microsoft Threading Building
Blocks (TBB), java.util.concurrent package
Lahore Garrison University
2
Thread Pools 3

 Create a number of threads in a pool where they await


work
 Advantages:
 Usually slightly faster to service a request with an existing
thread than create a new thread
 Allows the number of threads in the application(s) to be bound
to the size of the pool
 Separating task to be performed from mechanics of creating
task allows different strategies for running task
 i.e.Tasks could be scheduled to run periodically
 Windows API supports thread pools:

Lahore Garrison University


2
4
Threading Issues

 Semantics of fork() and exec() system calls


 Signal handling
 Synchronous and asynchronous
 Thread cancellation of target thread
 Asynchronous or deferred
 Thread-local storage
 Scheduler Activations

Lahore Garrison University


2
Semantics of fork() and exec() 5

 Does fork()duplicate only the calling thread or all


threads?
 Some UNIXes have two versions of fork
 exec() usually works as normal – replace the
running process including all threads

Lahore Garrison University


2
6
Signal Handling

 Signals are used in UNIX systems to notify a process that


a particular event has occurred.
 A signal handler is used to process signals
1. Signal is generated by particular event
2. Signal is delivered to a process
3. Signal is handled by one of two signal handlers:
1. default
2. user-defined
 Every signal has default handler that kernel runs when
handling signal
 User-defined signal handler can override default
 For single-threaded, signal delivered to process
Lahore Garrison University
2
Signal Handling (Cont.) 7

 Where should a signal be delivered for multi-


threaded?
 Deliver the signal to the thread to which the signal
applies
 Deliver the signal to every thread in the process
 Deliver the signal to certain threads in the process
 Assign a specific thread to receive all signals for the
process

Lahore Garrison University


2
8
Thread Cancellation

 Terminating a thread before it


has finished
 Thread to be canceled is
target thread
 Two general approaches:
 Asynchronous cancellation
terminates the target thread
immediately
 Deferred cancellation allows
the target thread to periodically
check if it should be cancelled
 Pthread code to create and
cancel a thread:
Lahore Garrison University
Thread Cancellation (Cont.)
2
9

 Invoking thread cancellation requests cancellation, but actual


cancellation depends on thread state

 If thread has cancellation disabled, cancellation remains pending


until thread enables it
 Default type is deferred
 Cancellation only occurs when thread reaches cancellation point
 I.e. pthread_testcancel()
 Then cleanup handler is invoked
 On Linux systems, thread cancellation is handled through signals
Lahore Garrison University
3
Thread-Local Storage 0

 Thread-local storage (TLS) allows each thread to


have its own copy of data
 Useful when you do not have control over the thread
creation process (i.e., when using a thread pool)
 Different from local variables
 Local variables visible only during single function
invocation
 TLS visible across function invocations
 Similar to static data
 TLS is unique to each thread

Lahore Garrison University


3
Scheduler Activations 1

 Both M:M and Two-level models require


communication to maintain the appropriate number
of kernel threads allocated to the application
 Typically use an intermediate data structure
between user and kernel threads – lightweight
process (LWP)
 Appears to be a virtual processor on which process can
schedule user thread to run
 Each LWP attached to kernel thread
 How many LWPs to create?
 Scheduler activations provide upcalls - a
communication mechanism from the kernel to the
upcall handler in the thread library
 This communication allows an application to
maintain the correct number kernel threads

Lahore Garrison University


3
Windows Threads 2

 Windows implements the Windows API – primary API for


Win 98, Win NT, Win 2000, Win XP, and Win 7
 Implements the one-to-one mapping, kernel-level
 Each thread contains
 A thread id
 Register set representing state of processor
 Separate user and kernel stacks for when thread runs in user
mode or kernel mode
 Private data storage area used by run-time libraries and
dynamic link libraries (DLLs)
 The register set, stacks, and private storage area are
known as the context of the thread
Lahore Garrison University
3
Windows Threads (Cont.) 3

 The primary data structures of a thread include:


 ETHREAD (executive thread block) – includes pointer to
process to which thread belongs and to KTHREAD, in
kernel space
 KTHREAD (kernel thread block) – scheduling and
synchronization info, kernel-mode stack, pointer to TEB,
in kernel space
 TEB (thread environment block) – thread id, user-mode
stack, thread-local storage, in user space

Lahore Garrison University


3
Windows Threads Data 4
Structures

Lahore Garrison University


3
Linux Threads 5

 Linux refers to them as tasks rather than threads


 Thread creation is done through clone() system call
 clone() allows a child task to share the address space of the parent
task (process)
 Flags control behavior

 struct task_struct points to process data structures (shared or


unique)
Lahore Garrison University
3
6

Q&A

Lahore Garrison University


3
7
Reference

 To cover this topics , different reference material has


been used for consultation.
 Operating systems concept by Abraham siberchatz
edition 9
 Tutorialspoint.com
 Google.com

Lahore Garrison University


3
8

Thank you 

Lahore Garrison University

You might also like