Lecture 2 Foundations and Definitions Processes/Threads.

Slides:



Advertisements
Similar presentations
Chapter 6: Process Synchronization
Advertisements

Concurrent Programming James Adkison 02/28/2008. What is concurrency? “happens-before relation – A happens before B if A and B belong to the same process.
1 CSC321 §2 Concurrent Programming Abstraction & Java Threads Section 2 Concurrent Programming Abstraction & Java Threads.
Threads, SMP, and Microkernels Chapter 4. Process Resource ownership - process is allocated a virtual address space to hold the process image Scheduling/execution-
Lecture 4 Thread Concepts. Thread Definition A thread is a lightweight process (LWP) which accesses a shared address space provided by by the parent process.
Computer Systems/Operating Systems - Class 8
CMPT 300: Operating Systems I Dr. Mohamed Hefeeda
Concurrency.
Processes CSCI 444/544 Operating Systems Fall 2008.
3.5 Interprocess Communication Many operating systems provide mechanisms for interprocess communication (IPC) –Processes must communicate with one another.
3.5 Interprocess Communication
 2004 Deitel & Associates, Inc. All rights reserved. Chapter 4 – Thread Concepts Outline 4.1 Introduction 4.2Definition of Thread 4.3Motivation for Threads.
Multithreading in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Process Concept An operating system executes a variety of programs
Threads CS 416: Operating Systems Design, Spring 2001 Department of Computer Science Rutgers University
1 Threads Chapter 4 Reading: 4.1,4.4, Process Characteristics l Unit of resource ownership - process is allocated: n a virtual address space to.
Chapter 51 Threads Chapter 5. 2 Process Characteristics  Concept of Process has two facets.  A Process is: A Unit of resource ownership:  a virtual.
CSE 451: Operating Systems Autumn 2013 Module 6 Review of Processes, Kernel Threads, User-Level Threads Ed Lazowska 570 Allen.
Process Management. Processes Process Concept Process Scheduling Operations on Processes Interprocess Communication Examples of IPC Systems Communication.
CSC 501 Lecture 2: Processes. Process Process is a running program a program in execution an “instantiation” of a program Program is a bunch of instructions.
Computer Architecture and Operating Systems CS 3230: Operating System Section Lecture OS-1 Process Concepts Department of Computer Science and Software.
© 2004, D. J. Foreman 2-1 Concurrency, Processes and Threads.
Lecture 3 Process Concepts. What is a Process? A process is the dynamic execution context of an executing program. Several processes may run concurrently,
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Thread Scheduling.
 2004 Deitel & Associates, Inc. All rights reserved. 1 Chapter 4 – Thread Concepts Outline 4.1 Introduction 4.2Definition of Thread 4.3Motivation for.
Hardware process When the computer is powered up, it begins to execute fetch-execute cycle for the program that is stored in memory at the boot strap entry.
1 Threads Chapter 11 from the book: Inter-process Communications in Linux: The Nooks & Crannies by John Shapley Gray Publisher: Prentice Hall Pub Date:
Threads G.Anuradha (Reference : William Stallings)
CS333 Intro to Operating Systems Jonathan Walpole.
C o n f i d e n t i a l 1 Course: BCA Semester: III Subject Code : BC 0042 Subject Name: Operating Systems Unit number : 1 Unit Title: Overview of Operating.
Processes and Process Control 1. Processes and Process Control 2. Definitions of a Process 3. Systems state vs. Process State 4. A 2 State Process Model.
Chapter 2 Processes and Threads Introduction 2.2 Processes A Process is the execution of a Program More specifically… – A process is a program.
1 Threads, SMP, and Microkernels Chapter Multithreading Operating system supports multiple threads of execution within a single process MS-DOS.
Processes CS 6560: Operating Systems Design. 2 Von Neuman Model Both text (program) and data reside in memory Execution cycle Fetch instruction Decode.
Lecture 5: Threads process as a unit of scheduling and a unit of resource allocation processes vs. threads what to program with threads why use threads.
1 Computer Systems II Introduction to Processes. 2 First Two Major Computer System Evolution Steps Led to the idea of multiprogramming (multiple concurrent.
Department of Computer Science and Software Engineering
Concurrency Properties. Correctness In sequential programs, rerunning a program with the same input will always give the same result, so it makes sense.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Operating Systems Processes and Threads.
1 OS Review Processes and Threads Chi Zhang
Hardware process When the computer is powered up, it begins to execute fetch-execute cycle for the program that is stored in memory at the boot strap entry.
Threads-Process Interaction. CONTENTS  Threads  Process interaction.
Chapter 2 Process Management. 2 Objectives After finish this chapter, you will understand: the concept of a process. the process life cycle. process states.
Cs431-cotter1 Processes and Threads Tanenbaum 2.1, 2.2 Crowley Chapters 3, 5 Stallings Chapter 3, 4 Silberschaz & Galvin 3, 4.
What is a Process ? A program in execution.
Operating Systems Unit 2: – Process Context switch Interrupt Interprocess communication – Thread Thread models Operating Systems.
1 Critical Section Problem CIS 450 Winter 2003 Professor Jinhua Guo.
Introduction to operating systems What is an operating system? An operating system is a program that, from a programmer’s perspective, adds a variety of.
Threads, SMP, and Microkernels Chapter 4. Processes and Threads Operating systems use processes for two purposes - Resource allocation and resource ownership.
Advanced Operating Systems CS6025 Spring 2016 Processes and Threads (Chapter 2)
1 Module 3: Processes Reading: Chapter Next Module: –Inter-process Communication –Process Scheduling –Reading: Chapter 4.5, 6.1 – 6.3.
Processes and Threads Chapter 3 and 4 Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee Community College,
Multiprogramming. Readings r Chapter 2.1 of the textbook.
Introduction to Operating Systems Concepts
Chapter 4 – Thread Concepts
Processes and threads.
Process concept.
CS 6560: Operating Systems Design
Chapter 4 – Thread Concepts
Process Management Presented By Aditya Gupta Assistant Professor
Intro to Processes CSSE 332 Operating Systems
Threads & multithreading
Chapter 15, Exploring the Digital Domain
CSE 451: Operating Systems Spring 2012 Module 6 Review of Processes, Kernel Threads, User-Level Threads Ed Lazowska 570 Allen.
Lecture Topics: 11/1 General Operating System Concepts Processes
Lecture 4- Threads, SMP, and Microkernels
CS510 Operating System Foundations
Foundations and Definitions
CSE 153 Design of Operating Systems Winter 2019
Chapter 3: Process Management
Presentation transcript:

Lecture 2 Foundations 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 Libraries Programming Languages Instruction 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 & 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.

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) 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 CreateExit (terminate) SuspendResume SleepWake 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 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 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 section T2 in critical section T1 is leaving critical section T1 in critical section T2 is leaving critical section T2 in critical section T1 is leaving critical section T1 in critical section T1 is leaving critical section T1 in critical section T2 is leaving critical section T2 in critical section T1 is leaving critical section T1 in critical section T2 is leaving critical section T2 in critical section T1 is leaving critical section T1 in critical section T1 is leaving critical section T1 in critical section T2 is leaving critical section T2 in critical section T1 is leaving critical section T1 in critical section T2 is leaving critical section T2 in critical section T1 is leaving critical section T1 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