Sistemi in Tempo Reale

Similar documents
Sistemi in Tempo Reale

Concurrent programming: Introduction II. Anna Lina Ruscelli Scuola Superiore Sant Anna

Operating Systems Introduction

Processes and Non-Preemptive Scheduling. Otto J. Anshus

Process Description and Control

Process Scheduling Queues

CSCE Introduction to Computer Systems Spring 2019

Processes. CS3026 Operating Systems Lecture 05

CSCE 313 Introduction to Computer Systems. Instructor: Dezhen Song

Motivation. Threads. Multithreaded Server Architecture. Thread of execution. Chapter 4

CSCE 313: Intro to Computer Systems

Mon Sep 17, 2007 Lecture 3: Process Management

Processes and Threads

Operating System. Chapter 4. Threads. Lynn Choi School of Electrical Engineering

Processes. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Real-Time Operating Systems Design and Implementation. LS 12, TU Dortmund

Lecture 5: Process Description and Control Multithreading Basics in Interprocess communication Introduction to multiprocessors

Processes. Process Management Chapter 3. When does a process gets created? When does a process gets terminated?

Processes and Threads. Processes and Threads. Processes (2) Processes (1)

Concurrent programming: Introduction I

Introduction. CS3026 Operating Systems Lecture 01

Wednesday, September 14, Chapter 3: Processes

Computer Systems II. First Two Major Computer System Evolution Steps

3. Process Management in xv6

Module 1. Introduction:

Unit 2 : Computer and Operating System Structure

Last class: Today: Course administration OS definition, some history. Background on Computer Architecture

CPSC 341 OS & Networks. Processes. Dr. Yingwu Zhu

W4118 Operating Systems. Junfeng Yang

CS2506 Quick Revision

Roadmap. Tevfik Ko!ar. CSC Operating Systems Fall Lecture - III Processes. Louisiana State University. Processes. September 1 st, 2009

Protection and System Calls. Otto J. Anshus

THE PROCESS ABSTRACTION. CS124 Operating Systems Winter , Lecture 7

Process Description and Control. Chapter 3

Chapter 3 Process Description and Control

Threads Chapter 5 1 Chapter 5

CS 326: Operating Systems. Process Execution. Lecture 5

Scuola Superiore Sant Anna. I/O subsystem. Giuseppe Lipari

UC Santa Barbara. Operating Systems. Christopher Kruegel Department of Computer Science UC Santa Barbara

PROCESSES AND THREADS THREADING MODELS. CS124 Operating Systems Winter , Lecture 8

For use by students enrolled in #71251 CSE430 Fall 2012 at Arizona State University. Do not use if not enrolled.

Processes. OS Structure. OS Structure. Modes of Execution. Typical Functions of an OS Kernel. Non-Kernel OS. COMP755 Advanced Operating Systems

Announcement. Exercise #2 will be out today. Due date is next Monday

1 PROCESSES PROCESS CONCEPT The Process Process State Process Control Block 5

Course: Operating Systems Instructor: M Umair. M Umair

What s the difference between threads/processes. Operating Systems

CS370 Operating Systems

Operating System Architecture. CS3026 Operating Systems Lecture 03

Last time: introduction. Networks and Operating Systems ( ) Chapter 2: Processes. This time. General OS structure. The kernel is a program!

Kernels & Processes The Structure of the Operating System

PROCESS CONTROL BLOCK TWO-STATE MODEL (CONT D)

Process Description and Control

Today s Topics. u Thread implementation. l Non-preemptive versus preemptive threads. l Kernel vs. user threads

3.1 Introduction. Computers perform operations concurrently

Chap 4, 5: Process. Dongkun Shin, SKKU

REVIEW OF COMMONLY USED DATA STRUCTURES IN OS

Introduction to Process in Computing Systems SEEM

ECE 7650 Scalable and Secure Internet Services and Architecture ---- A Systems Perspective. Part I: Operating system overview: Processes and threads

Chapter 4: Multithreaded Programming

! The Process Control Block (PCB) " is included in the context,

IT 540 Operating Systems ECE519 Advanced Operating Systems

Lecture Topics. Announcements. Today: Threads (Stallings, chapter , 4.6) Next: Concurrency (Stallings, chapter , 5.

A process. the stack

EECS 3221 Operating System Fundamentals

EECS 3221 Operating System Fundamentals

Processes and Threads

CSC 539: Operating Systems Structure and Design. Spring 2006

Four Components of a Computer System

Operating Systems CMPSCI 377 Spring Mark Corner University of Massachusetts Amherst

Ricardo Rocha. Department of Computer Science Faculty of Sciences University of Porto

Hardware OS & OS- Application interface

Operating Systems: Internals and Design Principles. Chapter 4 Threads Seventh Edition By William Stallings

Threads. Raju Pandey Department of Computer Sciences University of California, Davis Spring 2011

Questions answered in this lecture: CS 537 Lecture 19 Threads and Cooperation. What s in a process? Organizing a Process

Learning Outcomes. Processes and Threads. Major Requirements of an Operating System. Processes and Threads

Lecture 4: Process Management

I/O Handling. ECE 650 Systems Programming & Engineering Duke University, Spring Based on Operating Systems Concepts, Silberschatz Chapter 13

Operating Systems. Computer Science & Information Technology (CS) Rank under AIR 100

CSC369 Lecture 2. Larry Zhang

Last 2 Classes: Introduction to Operating Systems & C++ tutorial. Today: OS and Computer Architecture

Processes. Johan Montelius KTH

Lecture 4: Threads; weaving control flow

Processes COMPSCI 386

Process Concepts. CSC400 - Operating Systems. 3. Process Concepts. J. Sumey

Part V. Process Management. Sadeghi, Cubaleska RUB Course Operating System Security Memory Management and Protection

Operating Systems. Operating System Structure. Lecture 2 Michael O Boyle

Operating System. Chapter 3. Process. Lynn Choi School of Electrical Engineering

Processes & Threads. Recap of the Last Class. Microkernel System Architecture. Layered Structure

ENGR 3950U / CSCI 3020U Midterm Exam SOLUTIONS, Fall 2012 SOLUTIONS

Process! Process Creation / Termination! Process Transitions in" the Two-State Process Model! A Two-State Process Model!

ECE 550D Fundamentals of Computer Systems and Engineering. Fall 2017

Lecture 1 Introduction (Chapter 1 of Textbook)

OS Main Goals. 10/24/2013 Operating Systems, Beykent University 1

Distributed Systems Operation System Support

Processes. Dr. Yingwu Zhu

ELEC 377 Operating Systems. Week 1 Class 2

operating system Spring 2017 Prof.Dr. Hasan Balik Student Name : Walid.W. Ramadan mansour Student ID :

CPS221 Lecture: Threads

Process- Concept &Process Scheduling OPERATING SYSTEMS

CSC369 Lecture 2. Larry Zhang, September 21, 2015

Transcription:

Laurea Specialistica in Ingegneria dell'automazione Sistemi in Tempo Reale Giuseppe Lipari Introduzione alla concorrenza

Fundamentals Algorithm: It is the logical procedure to solve a certain problem It is informally specified a a sequence of elementary steps that an execution machine must follow to solve the problem It is not necessarily expressed in a formal programming language! Program: It is the implementation of an algorithm in a programming language Can be executed several times with dufferent inputs Process: An instance of a program that given a sequence of inputs produces a set of outputs

Operating System An operating system is a program that Provides an abstraction of the physical machine Provides a simple interface to the machine Each part of the interface is a service An OS is also a resource manager With the term resource we denote all physical entities of a computing machine The OS provides access to the physical resources The OS provides abstract resources (for example, a file, a virtual page in memory, etc.)

Levels of abstraction User Level Kim Lisa Bill Programmer Level Web Browser Shell Videogame Printer Daemon System Level HW Level Operating System Device Driver Interface (System API) Virtual Memory Scheduler Virtual File Sys. Device Driver Device Driver Device Driver Device Driver Device Driver Main Board CPU Keyboard Video Card Network Card Printer Printer Hard disk

Abstraction mechanisms Why abstraction? Programming the HW directly has several drawbacks It is difficult and error-prone It is not portable Suppose you want to write a program that reads a text file from disk and outputs it on the screen Without a proper interface it is virtually impossible!

Abstraction Mechanisms Application programming interface (API) Provides a convenient and uniform way to access to one service so that HW details are hidden to the high level programmer One application does not depend on the HW The programmer can concentrate on higher level tasks Example For reading a file, linux and many other unix OS provide the open(), read() system calls that, given a file name allow to load the data from an external support

Classification of Operating Systems The OS provides an abstraction of a physical machine To allow portability To make programmer s life easier The level of abstraction depends on the application context It means that the kind of services an OS provides depend on which kind of services the application requires General purpouses OS should provide a wide range of services to satisfy as many users as possible Specialised OS provide only a group of specialised services OS can be classified depending on the application context General purpouse (windows, linux, etc), servers, micro-kernel, embedded OS, real-time OS

Virtual processor Services An OS provides concurrency between processes Many processes are executed at the same time in the same system Each process executes for a fraction of the processor bandwidth (as it were on a dedicated slower processor) Provided by the scheduling sub-system Provided by almost all OS, from nano-kernels to general-purpouse systems CPU CPU CPU CPU

Virtual memory Services Physical memory is limited; In old systems, the number of concurrent processes was limited by the amount of physical memory IDEA: extend the physical memory by using a fast mass storage system (disk) Some of the processes stay in memory, some are temporarily saved on the disk When a process must be executed, if it is on the disk it is first loaded in memory and then executed This technique is called swapping

Virtual memory and physical memory Process A D A DB E B C E CA CPU Process B Process C Process E Process D Process E Virtual memory Process AB Process C Physical memory BA D Disk Virtual memory is very large (virtually infinite!) The program functionality does not depend on the size of the memory The program performance could be reduced by the swapping mechanism

Advantages Virtual infinite memory Virtual Memory The program is not limited by the size of the physical memory Disadvantages If we have too many programs, we spend most of the time swapping back and forth Performance degradation! Not suitable for real-time systems It is not possible to guarantee a short response time because it depends on the program location

Virtual File System Basic concepts File: sequence of data bytes It can be on a mass storage (hard disk, cd-rom, etc.) It can be on special virtual devices (i.e. RAM disks) It can be on a remote system! Directory: list of files Usually organised in a tree Represents how files are organised on the mass storage system Virtualisation In most OS, external serial devices (like the console or the video terminal) can be seen as files (i.e. stdin, stout, stderr)

Virtual file system A good virtual file system provides additional features: Buffering & caching For optimising I/O from block devices Transactions For example the Reiser FS Fault tolerance capabilities For example, the RAID system Virtual file system is not provided by all OS categories Micro and nano kernels do not even provide a file system!

Processes

Process The fundamental concept in any operating system is the process A process is an executing program An OS can execute many processes at the same time (concurrency) Processes have separate memory addresses They cannot directly share variable Processes are mostly used in general purpouse OS

Process Control Block It contains all the data concerning one process All PCBs are stored in the Process Table Process Table PID PPID UID Page table File Table Handles State Statistics...

The role of PCB Virtually every routine in the OS will access the PCBs The scheduler The Virtual memory The Virtual File System Interrupt handlers (I/O devices)... It can only be accessed by the OS! The user can access some of the information in the PCB by using appropriate system calls The PCB is a critical point of any OS!

Memory layout of a Process Other data Heap Stack BSS Initialized Data Text In some system, the PCB is in the other data section For example, Linux

Memory protection Every process has its own memory space Part of it is private to the process Part of it can be shared with other processes For examples: two processes that are instances of the same program will probably share the TEXT part If two processes want to communicate by shared memory, they can share a portion of the data segment Other data Heap Other data Heap Stack Stack BSS Initialized Data Initialized Data Text

Memory Protection By default, two processes cannot share their memory If one process tries to access a memory location outside its space, a processor exception is raised (trap) and the process is terminated The famous Segmentation Fault error!! We will come back to this when we study the Virtual Memory

Processes and Threads

Processes We can distinguish two aspects in a process Resource Ownership A process includes a virtual address space, a process image (code + data) It is allocated a set of resources, like file descriptors, I/O channels, etc Scheduling/Execution The execution of a process follows an ececution path, and generates a trace (sequence of internal states) It has a state (ready, Running, etc.) And scheduling parameters (priority, time left in the round, etc.)

Multi-threading Many OS separate these aspects, by providing the concept of thread The process is the resource owner The thread is the scheduling entity One process can consists of one or more threads Threads are sometime called (improperly) lightweight processes Therefore, on process can have many different (and concurrent) traces of execution!

Single threaded Process Model In the single-threaded process model one process has only one thread One address space One stack One PCB only

Multi-threaded process model In the multi-threaded process model each process can have many threads One address space One PCB Many stacks Many TCB (Thread Control blocks) The threads are scheduled directly by the global scheduler

Threads Generally, processes do not share memory To communicate between process, it is necessary to user OS primitives Process switch is more complex because we have to change address space Two threads in the same process share the same address space They can access the same variables in memory Communication between threads is simpler Thread switch has less overhead

Processes vs. Threads Processes are mainly used to compete for some resource For example, two different users run two separate applications that need to print a file The printer is a shared resource, the two processes compete for the printer Threads are mainly used to collaborate to some goal For example, one complex calculation can be split in two parallel phases, each thread does one phase In a multi-processor machine the two threads go in parallel and the calculation becomes faster

Example - I Consider a Word Processor application Main cycle 1. Wait for input from the keyboard 2. Update the document 3. Format the document 4. Check for syntax errors 5. Check for other events (i.e. temporary save) 6. Return to 1 One single process would be a waste of time! 1. wait 2. update 3. format 4. syntax 5. Other events 1. wait

Example - II Problems Most of the time, the program waits for input Idea, while waiting we could perform some other task Activities 3 and 4 (formatting and syntax checking) are very time consuming Idea: let s do them while waiting for input Solution with multiple processes One process waits for input Another process periodically formats the document A third process periodically performs a syntax checking A fourth process visualize the document Input Process Format Process Syntax Process Graphic Process

Example - III Problem with multiple processes All processes needs to access the same data structure, the document Which process holds the data structure? Solution 1: message passing A dedicated process holds the data, all the others communicate with it to read/update the data Very inefficient! Input Process Format Process Syntax Process Graphic Process Data Server

Another solution... Example - IV Solution 2: shared memory One process holds the data and makes that part of its memory shareable with the others Still not very efficient: We have a lot of process switches Memory handling becomes very complex

Why using threads Speed of creation Creating a thread takes far less time than creating a process Speed of switching Thread switch is faster than process switch Shared memory Threads of the same process run in the same memory space They can naturally access the same data! Input Thread Format Thread Syntax Thread Graphic Thread Process Document

Threads support in OS Different OS implement threads in different ways Some OS supports directly only processes Threads are implemented as special processes Some OS supports only threads Processes are threads groups Some OS natively supports both concepts For example Windows NT We will come back to this part later, after we have studied the problem of synchronization By now we need to abstract away the different implementations

Summary Important concepts Process: provides the abstraction of memory space Threads: provide the abstraction of execution trace The scheduler manages threads! Processes do not normally share memory Two threads of the same process share memory We need to explore all the different ways in which two threads can communicate Shared memory Message passing In the next section we will only refer to threads

The thread control block In a OS that supports threads Each thread is assigned a TCB (Thread Control Block) The PCB holds mainly information about memory The TCB holds information about the state of the thread Thread Table TID PID CR IP SP Other Reg. State Priority Time left...

Thread states The OS can execute many threads at the same time Each thread, during its lifetime can be in one of the following states Starting (the thread is being created) Ready (the thread is ready to be executed) Executing (the thread is executing) Blocked (the thread is waiting on a condition) Terminating (the thread is about to terminate)

Thread states a) Creation The thread is created b) Dispatch The thread is selected to execute c) Preemption The thread leaves the processor d) W ait on condition The thread is blocked on a condition e) Condition true The thread is unblocked f) Exit The thread terminates a Start Ready Running b c e d f Blocked Terminate

Single processor Thread queues Admit Ready queue Dispatch CPU Preemption Blocked queue Event occurs Wait condition

Thread queues Multi-processor with migration CPU Admit Ready queue Dispatch CPU CPU Preemption Blocked queue Event occurs Wait condition

Multiple blocking queues Admit Ready queue Dispatch CPU Preemption Event occurs Wait condition 1 Event occurs Wait condition 2 Event occurs Wait condition 3

Modes of operation (revised) Every modern processor supports at least 2 modes of operation User Supervisor The Control Register (CR) contains one bit that tells us in which mode the processor is running Operating system routines run in supervisor mode They need to operate freely on every part of the hardware with no restriction User code runs into user mode Mode switch Every time we go from user to supervisor mode or viceversa

Mode switch It can happen in one of the following cases Interrupts or traps In this case, before calling the interrupt handler, the processor goes in supervisor mode and disables interrupts Traps are interrupts that are raised when a critical error occurs (for example, division by zero, or page fault) Returning from the interrupt restores the previous mode Invoking a special instruction In the Intel family, it is the INT instruction This instruction is similar to an interrupt It takes a number that identifies a service All OS calls are invoked by calling INT Returning from the handler restores the previous mode

Example of system call int fd,n; char buff[100]; fd = open( Dummy.txt, O_RDONLY); n = read(fd, buff, 100); The open system call can potentially block the thread! In that case we have a context switch Saves parameters on the stack Executes INT 20h 1. Change to supervisor mode 2. Save context 3. Execute the function open 4. Restores the context 5. IRET Back to user mode Delete parameters from the stack

Context switch It happens when The thread has been preempted by another higher priority thread The thread blocks on some condition In time-sharing systems, the thread has completed its round and it is the turn of some other thread We must be able to restore the thread later Therefore we must save its state before switching to another thread

The exec pointer Every OS has one pointer ( exec ) to the TCB of the running thread The status of the exec thread is RUNNING When a context switch occurs, The status of the exec thread is changed to BLOCKING or READY The scheduler is called The scheduler selects another exec from the ready queue

System call with context switch Saves parameters on stack INT 20h Change to supervisor mode Save context in the TCB of exec (including SP) Execute the code The thread change status and goes into BLOCKING mode Calls the scheduler Moves exec into the blocking queue Selects another thread to go into RUNNING mode Now exec points to the new process Restores the context of exec (including SP) This changes the stack IRET Returns to where the new thread was interrupted

Stacks exec Blocking Blocking exec Stack TCB TID PID Stack TCB TID PID PPID PID PPID PID CR CR IP IP SP SP Other Reg. Other Reg. IP CR Param. State Priority Time left... IP CR Param. State Priority Time left...

This is only an example Context switch Every OS has little variations on the same theme For example, in most cases, registers are saved on the stack, not on the TCB You can try to look into some real OS Linux Free BSD Shark (http://shark.sssup.it) Every OS is different!

Time sharing systems In time sharing systems, Every thread can execute for maximum one round For example, 10msec At the end of the round, the processor is given to another thread Ready queue CPU Context Switch Timer interrupt

Interrupt with context switch It is very similar to the INT with context switch An interrupt arrives Change to supervisor mode Save CR and IP Save processor context Execute the interrupt handler Call the scheduler IRET This may change the exec pointer

Causes for a context switch A context switch can be Voluntary: the thread calls a blocking primitive, i.e. it executes an INT For example, by calling a read() on a blocking device Non-voluntary: an interrupt arrives that causes the context switch It can be the timer interrupt, in time-sharing systems It can be an I/O device which unblocks a blocked process with a higher priority