Docsity

Lecture Slides for Thread Concepts | 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
pf3
pf4
pf5
pf8
pf9
pfa
pfd

Partial preview of the text

Download Lecture Slides for Thread Concepts | 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

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

Java threads:

Any class that implements Runnable can be run in a

separate thread in Java.

To create a thread in Java, do one of the following:

] create an object that extends Thread

(which implements Runnable, but has all of the

infrastructure for running as a separate thread)

] create an object that implements Runnable

(and then create a Thread to run the object)

You must explicitly call start to run a thread.

Extending a Thread object:

public class SimpleOutputThread extends Thread { String msg; int repetition;

public SimpleOutputThread(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); } }

Running Thread objects

public class SimpleOutputThreadMain { public static void main(String[] args) { SimpleOutputThread t1 = new SimpleOutputThread("Thread 1", 100); t1.start(); SimpleOutputThread t2 = new SimpleOutputThread("Thread 2", 100); t2.start(); } }

When are the thread objects created? When do they run? When does the program finish? Are there any sharing conflicts between the threads?

Thread and process life-times :

] A process starts with a single non-daemon thread that calls a main method. ] The process exits when either: \ Runtime.exit is called (as long as the security manager doesn't object), or \ All non-daemon threads have died (run complete or exception thrown).

] A thread object exists when it is constructed, but it doesn't start running until the start method is called. ] A thread completes (or dies) when its run method finishes or when it throws an exception. The object representing this thread can still be accessed. You must explicitly call start to run a thread.

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.

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.