Lecture 2 Foundations and Definitions Processes/Threads.

32
Lecture 2 Foundations and Definitions Processes/Threads

Transcript of Lecture 2 Foundations and Definitions Processes/Threads.

Lecture 2Foundations and Definitions

Processes/Threads

The Role of Abstraction

It is convenient and sometimes necessary to limit our view to one level of detail

In Software Engineering with typically deal with three levels of abstraction

Systems and LibrariesProgramming LanguagesInstruction Sets

The two most important tools for SW abstraction are encapsulation and concurrency

Definitions

A Simple Example

Programs in a Language-Independent Form

Definitions

Scenarios

Multitasking Systems

Multitasking is the process of sharing the resources of one computer with multiple processes.

A process called an interrupt handler deals with I/O interrupts. Once a process in interrupted the OS scheduler may decide which process is to execute next. As shown in the diagram four programs concurrently executing programs are sharing the computer with the OS. When a process in interrupted its register values are saved in memory and the register values for the interrupting process are loaded. This is called a context switch. At the conclusion of the interrupt processing, the symmetric context switch is performed storing the interrupt handler registers and loading the registers for another program (usually but not necessarily the program that was interrupted).

Multiprocessing Systems

The Issue of Contention for Global Memory

Distributed Systems

Topology affects Efficiency

Atomic Statements

Atomic statements are those that, once started, cannot be interrupted before completion. In the abstract world of concurrency examples are:

Assignment statements

Boolean conditional statements

Unfortunately this assumption is not realistic. In a real computer atomic statements are the machine statements generated by the language compiler. On statement in a high-level language may become several machine language statements.

We will continue to use high-level language pseudocode in our example programs and we will assume they are atomic while we understand that this may not be true.

Correctness and Concurrency

Each time we run a sequential program with the same input we expect and get the same result. So program debugging involves running the program, checking for and fixing errors until the problem has been eliminated.

We cannot expect to debug concurrent programs in this manner. Due to arbitrary interleaving of statements we are unlikely to get the same answer twice even with the same input when there is a problem with our program.

In concurrent programs we are interested in the properties of correctness. There are two types of correctness properties:

Safety properties – the property must always be true

Liveness properties – the property must eventually become true.

Often a safety property is of the form “some bad thing is always not true”. In addition, safety properties and liveness properties are duals of one another.

Linear and Branching Temporal Logics

Linear Temporal Logic – used to express properties that must be true at a state in a single (but arbitrary) scenario (some interleaving of statements).

Branching Temporal Logic – used to express properties that if true at a state, must be true in some or all scenarios starting from the state.

Fairness

Machine Code Instructions

There are two common types of machine code computer models:

Register Machines

Stack Machines

We will compare how each would deal with this simple algorithm

Register Machine

Stack Machines

Volatile and Non-Atomic Variables

What is a Process?

A process is the dynamic execution context of an executing program. Several processes may run concurrently, each as a distinct entity with its own state. The process state consists of at least:

• the code for the running program• the static data for running program• a space in the heap for dynamic data• the address of next instruction• execution stack• values of the CPU registers• OS resources (e.g. open filees)• process execution state

Computer MemoryProcess Code

New

Process State

Terminated

Each process transitions from one state to the next as it shares the CPU and additional computer resources with other processes. A process changes states due to actions by the OS and other entities.

Suspend & ResumeSuspending a process indefinitely removes it from contention for time on a processor without being destroyed. Suspending a process is useful for detecting security threats and for software debugging purposes (e.g. deadlock detection and recovery). A suspension may be initiated by the process being suspended or by another process.

Thread DefinitionA thread is a lightweight process (LWP) which accesses a shared address space provided by by the parent process. Each thread keeps its own state parameters and register values so that it may run concurrently with other threads. Threads may be managed by the OS (kernel-level threads) or by a user application (user-level threads).

2004 Deitel & Associates, Inc.

Threads vs. Processes

Processes each have their own address space, text space and resources, while threads spawned within a user application all share the memory and resources of the parent thread or process. A thread defines a single sequential execution stream within a process. Each thread maintains its own PC, SP and CPU register values.

Thread States

2004 Deitel & Associates, Inc.

Thread Operations

Threads have some operations in common with processes

Create Exit (terminate)Suspend ResumeSleep Wake

Other thread operations do not correspond to process operations

Cancel - indicates that a thread should be terminated, but does not guarantee that the thread will be terminated. Threads can mask the cancellation signal.

Join - a primary thread can wait for all other threads to exit by joining them. The joining thread blocks until the thread it joined exits.

2004 Deitel & Associates, Inc.

User-Level Threads

A user-level thread is one that is created and managed by a user application. Usually the OS does not know of the existence of user-level threads so additional resources are not provided by the OS for user-level threads. Each user-level thread must share the resources already allocated to the user application.

2004 Deitel & Associates, Inc.

Kernel-Level Threads

Kernel-level threads are created and managed by the OS. They attempt to address the limitations of user-level threads by mapping each thread to its own execution context. Kernel-level threads offer increased scalability, interactivity, and throughput, but have a higher overhead due to context switching and reduced portability because of OS-specific APIs

2004 Deitel & Associates, Inc.

using System;using System.Threading;namespace first_thread_demo{ public class Version_1 { public static bool done = false;

public static void T1() { System.Random rnd = new Random(); while (!done) { Console.WriteLine("T1 in critical section"); Thread.Sleep(rnd.Next(100)); Console.WriteLine("T1 is leaving critical section"); } }

static void Main(string[] args) { Thread p = new Thread(new ThreadStart(T1)); p.Start(); Thread q = new Thread(new ThreadStart(T2)); q.Start(); Thread.Sleep(5000); done = true; Console.WriteLine("Run Finished"); Console.ReadKey(); } }}

public static void T2() { System.Random rnd = new Random(); while (!done) { Console.WriteLine("T2 in critical section"); Thread.Sleep(rnd.Next(100)); Console.WriteLine("T2 is leaving critical section"); } }

A Simple Multi-Threading Demo

T2 is leaving critical sectionT2 in critical sectionT1 is leaving critical sectionT1 in critical sectionT2 is leaving critical sectionT2 in critical sectionT1 is leaving critical sectionT1 in critical sectionT1 is leaving critical sectionT1 in critical sectionT2 is leaving critical sectionT2 in critical sectionT1 is leaving critical sectionT1 in critical sectionT2 is leaving critical sectionT2 in critical sectionT1 is leaving critical sectionT1 in critical sectionT1 is leaving critical sectionT1 in critical sectionT2 is leaving critical sectionT2 in critical sectionT1 is leaving critical sectionT1 in critical sectionT2 is leaving critical sectionT2 in critical sectionT1 is leaving critical sectionT1 in critical section

BothT1 & T2 can be in Critical Sections at the Same Time

public static int threadnumber = 1; :public static void T1(){ System.Random rnd = new Random(); while (!done) { while (threadnumber == 2); Console.WriteLine("T1 in critical section"); Thread.Sleep(rnd.Next(100)); Console.WriteLine("T1 is leaving critical section"); threadnumber = 2; }}

A "Fix" for the Problem

The inclusion of a globally accessible variable threadnumber that can be set to the number of the preferred thread to execute can prevent two threads from entering their critical sections at the same time, but at what cost?

a very important semicolon