Presentation is loading. Please wait.

Presentation is loading. Please wait.

Foundations and Definitions

Similar presentations


Presentation on theme: "Foundations and Definitions"— Presentation transcript:

1 Foundations and Definitions
Lecture 2 Foundations and Definitions Processes/Threads

2 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 Libraries Programming Languages Instruction Sets The two most important tools for SW abstraction are encapsulation and concurrency

3 Definitions

4 A Simple Example

5 Programs in a Language-Independent Form

6 Definitions

7 Scenarios

8 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 is interrupted the OS scheduler may decide which process is to execute next. As shown in the diagram four concurrently executing programs are sharing the computer with the OS. When a process is 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).

9 Multiprocessing Systems

10 The Issue of Contention for Global Memory

11 Distributed Systems

12 Topology affects Efficiency

13 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.

14 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.

15 Volatile and Non-Atomic Variables

16 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 Memory Process Code

17 Process State 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. Terminated New

18 Suspend & Resume Suspending 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.

19 Thread Definition A 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.

20 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.

21 Thread States 2004 Deitel & Associates, Inc.

22 Thread Operations Threads have some operations in common with processes Create Exit (terminate) Suspend Resume Sleep 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.

23 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.

24 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.

25 A Simple Multi-Threading Demo
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"); }

26 BothT1 & T2 can be in Critical Sections at the Same Time
T2 is leaving critical section T2 in critical section T1 is leaving critical section T1 in critical section

27 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? 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 very important semicolon


Download ppt "Foundations and Definitions"

Similar presentations


Ads by Google