Docsity

Thread Concepts - Operating Systems - Lecture Notes | CS 5523, Exams of Operating Systems

Toggle sidebar
bg1
1
CS 5523 Lecture 16: Thread concepts
]Return midterm examination
]Questions on laboratory 2
]Thread state
]Strategies for threading servers
]Kernel-level versus user-level threads
]Introduction to threads in Java
What is a thread?
]
Thread – an abstract data type representing flow of
control within a process
]
Multiple threads can be created within a single
execution environment (a process) to achieve parallelism
Thread state:
]Thread ID
]Machine state (registers, program counter, stack pointer)
]Priority
]Signal mask
]Error designation (e.g., errno)
Why threads?
Upside:
]Cheaper to create than processes
]Cheaper to switch between than processes
]Easier to share data
Downside:
]Less robust against programming errors
]Have to share resources allocated to the execution environment
Instructor’s Guide for Coulouris, Dollimoreand Kindb erg Distributed Systems: Concepts and Design Edn. 3
© Addison-Wesley Publishers 2000
Figure 6.5
Client and server with threads
Server
N threads
Input-output
Client
Thread 2 makes
T1
Thread 1
requests to server
generates
results
Requests
Receipt &
queuing
Instructor’s Guide for Coulouris, Dollimoreand Kindb erg Distributed Systems: Concepts and Design Edn. 3
© Addison-Wesley Publishers 2000
Figure 6.6
Alternative server threading architectures (see also Figure 6.5)
a. Thread-per-request b. Thread-per-connection c. Thread-per-object
remote
workers
I/O remote
remote I/O
per-connection threads per-object threads
objects objects objects
pf3
pf4
pf5

Partial preview of the text

Download Thread Concepts - Operating Systems - Lecture Notes | CS 5523 and more Exams Operating Systems in PDF only on Docsity!

CS 5523 Lecture 16: Thread concepts

] Return midterm examination

] Questions on laboratory 2

] Thread state

] Strategies for threading servers

] Kernel-level versus user-level threads

] Introduction to threads in Java

What is a thread?

] Thread – an abstract data type representing flow of

control within a process

] Multiple threads can be created within a single

execution environment (a process) to achieve parallelism

Thread state:

] Thread ID

] Machine state (registers, program counter, stack pointer)

] Priority

] Signal mask

] Error designation (e.g., errno)

Why threads?

Upside:

] Cheaper to create than processes

] Cheaper to switch between than processes

] Easier to share data

Downside:

] Less robust against programming errors

] Have to share resources allocated to the execution environment

Instructor’s Guide for Coulouris, Dollimore and Kindberg© Addison-Wesley Publishers 2000 Distributed Systems: Concepts and Design Edn. 3

Figure 6. Client and server with threads

Server

N threads

Input-output

Client

Thread 2 makes

T

Thread 1

requests to server

generates results Requests

Receipt & queuing

Instructor’s Guide for Coulouris, Dollimore and Kindberg© Addison-Wesley Publishers 2000 Distributed Systems: Concepts and Design Edn. 3

Figure 6. Alternative server threading architectures (see also Figure 6.5)

a. Thread-per-request b. Thread-per-connection c. Thread-per-object

remote

workers I/O (^) remote I/O remote

per-connection threads per-object threads

objects objects^ objects

Thread operations:

] Thread management:

\ creation \ destruction \ changing priority or other attributes

] Synchronization

\ join \ condition variables \ locks

Kernel-level threads:

] Each thread is a schedulable entity

] Threads compete for process resources on system-wide basis

] Can take advantage of multiple processor

] Operating system must provide support

] Context switch involves the kernel and can be expensive

User-level threads:

] Run on top of existing operating systems

] Encapsulated with processes and invisible to kernel

] Schedule by a runtime system that is part of process code

] Each library function and system call is enclosed by a jacket to

let the run-time library i

Hybrid approaches:

] Application contains user-level scheduler to manage threads

within a process

] Kernel allocates virtual processors for a process

] Kernel makes upcalls to notify application’s thread scheduler of

events:

\ Virtual processor allocated \ Scheduler activation blocked \ Scheduler activation unblocked \ Scheduler application preempted

Instructor’s Guide for Coulouris, Dollimore and Kindberg© Addison-Wesley Publishers 2000 Distributed Systems: Concepts and Design Edn. 3

Figure 6. Scheduler activations

Process A

Process B

Virtual processors Kernel

Process Kernel

P idle P needed

P added

SA blocked

SA unblocked

SA preempted

A. Assignment of virtual processors to processes

B. Events between user-level scheduler & kernel Key: P = processor; SA = scheduler activation

Examples of different approaches:

] User-level threads – pthreads

] Kernel-level threads – NT, Solaris Lightweight Processes

] Hybrid approaches – Solaris threads + LWP

FastThreads

Java threads are user-level, however, their mapping to underlying

thread support is implementation dependent. (Threads are

scheduled differently on different underlying OS’s.)

Implementing the Runnable interface:

public class SimpleOutputRunnable implements Runnable { String msg; int repetition;

public SimpleOutputRunnable(String msg, int repetition) { this.msg = msg; this.repetition = repetition; }

public void run() { for (int i = 0; i < repetition; i++) System.out.println("[" + i + "]" + msg); } }

Starting Runnable objects in separate threads

public class SimpleOutputRunnableMain {

public static void main(String[] args) { SimpleOutputRunnable r1 = new SimpleOutputRunnable("Thread 1", 100); new Thread(r1).start(); SimpleOutputRunnable r2 = new SimpleOutputRunnable("Thread 2", 100); new Thread(r2).start(); } }

Starting the same Runnable in separate threads

public static void main(String[] args) { SimpleOutputRunnable r1 = new SimpleOutputRunnable("Thread 1", 100); new Thread(r1).start(); new Thread(r1).start(); }

Making a Daemon Thread

public static void main(String[] args) { SimpleOutputRunnable r1 = new SimpleOutputRunnable("Thread 1", 100); Thread aThread = new Thread(r1); aThread.setDaemon(true); aThread.start(); } }

Compare the behavior with and without making aThread a

Daemon thread.

Extending Thread versus implementing Runnable

] Extending Thread allows you to use join and other

methods of Thread without creating a separate object.

] Implementing Runnable is useful if your class needs

to extend an other object or if you are using thread pools.

Thread versus simple method call:

void *doit(void *) a.doit(x );

void *doit(void *) pthread_create( , , &doit, x);

For next time:

] Read Chapter 6 of CDK

] Read the Mars Pathfinder article

] Work on CDK questions 6.4, 6.8, 6.9, 6.10, 6.14, 6.23, 6.24, 6.