Interoperation of tasks

Similar documents
CS 450 Operating System Week 4 Lecture Notes

Chapter 4: Threads. Operating System Concepts with Java 8 th Edition

PROCESS MANAGEMENT. Operating Systems 2015 Spring by Euiseong Seo

Collaboration of Tasks

Chapter 4: Threads. Operating System Concepts 8 th Edition,

Outline. Threads. Single and Multithreaded Processes. Benefits of Threads. Eike Ritter 1. Modified: October 16, 2012

CS 3305 Intro to Threads. Lecture 6

Last Class: CPU Scheduling. Pre-emptive versus non-preemptive schedulers Goals for Scheduling: CS377: Operating Systems.

Processes and Threads

Operating Systems 2 nd semester 2016/2017. Chapter 4: Threads

CSE 153 Design of Operating Systems Fall 2018

Agenda Process Concept Process Scheduling Operations on Processes Interprocess Communication 3.2

Chapter 4: Threads. Overview Multithreading Models Threading Issues Pthreads Windows XP Threads Linux Threads Java Threads. Operating System Concepts

Chapter 4: Threads. Operating System Concepts. Silberschatz, Galvin and Gagne

CS 475. Process = Address space + one thread of control Concurrent program = multiple threads of control

Distributed Systems Operation System Support

Chapter 4: Threads. Chapter 4: Threads

Chapter 4: Threads. Overview Multithreading Models Thread Libraries Threading Issues Operating System Examples Windows XP Threads Linux Threads

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

Chapter 4: Multithreaded Programming

Computer Science 322 Operating Systems Mount Holyoke College Spring Topic Notes: Processes and Threads

4. Concurrency via Threads

1 System & Activities

Chapter 4: Multi-Threaded Programming

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

CS420: Operating Systems

Chapter 4 Multithreaded Programming

PROCESSES. Jo, Heeseung

Processes. Jo, Heeseung

W4118 Operating Systems. Junfeng Yang

Yi Shi Fall 2017 Xi an Jiaotong University

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

CS307: Operating Systems

Chapter 5: Threads. Outline

Chapter 5: Threads. Overview Multithreading Models Threading Issues Pthreads Windows XP Threads Linux Threads Java Threads

I.-C. Lin, Assistant Professor. Textbook: Operating System Concepts 8ed CHAPTER 4: MULTITHREADED PROGRAMMING

OPERATING SYSTEM. Chapter 4: Threads

SMD149 - Operating Systems

Chapter 3: Processes. Operating System Concepts 9 th Edit9on

Process Description and Control

Chapter 5: Processes & Process Concept. Objectives. Process Concept Process Scheduling Operations on Processes. Communication in Client-Server Systems

CHAPTER 3 - PROCESS CONCEPT

CSE 4/521 Introduction to Operating Systems. Lecture 29 Windows 7 (History, Design Principles, System Components, Programmer Interface) Summer 2018

CS 326: Operating Systems. Process Execution. Lecture 5

Process. Operating Systems (Fall/Winter 2018) Yajin Zhou ( Zhejiang University

Threads. CS-3013 Operating Systems Hugh C. Lauer. CS-3013, C-Term 2012 Threads 1

CHAPTER 2: PROCESS MANAGEMENT

Lecture 17: Threads and Scheduling. Thursday, 05 Nov 2009

Chapter 4: Multithreaded

Processes & Threads. Recap of the Last Class. Layered Structure. Virtual Machines. CS 256/456 Dept. of Computer Science, University of Rochester

Page 1. Analogy: Problems: Operating Systems Lecture 7. Operating Systems Lecture 7

Chapter 4: Threads. Chapter 4: Threads

THE PROCESS ABSTRACTION. CS124 Operating Systems Winter , Lecture 7

Multithreaded Programming

Threads Implementation. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

CSE325 Principles of Operating Systems. Processes. David P. Duggan February 1, 2011

Chapter 3: Process Concept

Chapter 3: Process Concept

Chapter 3: Processes. Operating System Concepts 8th Edition

Chapter 4: Multithreaded Programming Dr. Varin Chouvatut. Operating System Concepts 8 th Edition,

Process. Program Vs. process. During execution, the process may be in one of the following states

Process and Its Image An operating system executes a variety of programs: A program that browses the Web A program that serves Web requests

Disciplina Sistemas de Computação

Chapter 3: Processes. Operating System Concepts 9 th Edition

Chapter 3: Process Concept

CS 471 Operating Systems. Yue Cheng. George Mason University Fall 2017

Chapter 4: Multithreaded Programming. Operating System Concepts 8 th Edition,

Processes. CS 475, Spring 2018 Concurrent & Distributed Systems

Operating Systems CMPSCI 377 Spring Mark Corner University of Massachusetts Amherst

Processes and Threads

Chapter 4: Threads. Operating System Concepts 9 th Edit9on

Operating Systems & Concurrency: Process Concepts

What s in a process?

Chapter 4: Threads. Operating System Concepts 9 th Edition

PROCESS CONCEPTS. Process Concept Relationship to a Program What is a Process? Process Lifecycle Process Management Inter-Process Communication 2.

Chapter 4: Threads. Operating System Concepts 9 th Edition

Processes and Threads

Computer Science 322 Operating Systems Mount Holyoke College Spring Topic Notes: Processes and Threads

From Processes to Threads

Lecture 4: Process Management

CISC 7310X. C03: Process. Hui Chen Department of Computer & Information Science CUNY Brooklyn College. 2/15/2018 CUNY Brooklyn College

Chapter 3: Processes

2/14/2012. Using a layered approach, the operating system is divided into N levels or layers. Also view as a stack of services

Threads. CSE Computer Systems November 16, 2001

OS lpr. www. nfsd gcc emacs ls 1/27/09. Process Management. CS 537 Lecture 3: Processes. Example OS in operation. Why Processes? Simplicity + Speed

Threads. Computer Systems. 5/12/2009 cse threads Perkins, DW Johnson and University of Washington 1

Lecture 2 Process Management

Roadmap. Tevfik Ko!ar. CSC Operating Systems Spring Lecture - III Processes. Louisiana State University. Virtual Machines Processes

Threads. Still Chapter 2 (Based on Silberchatz s text and Nachos Roadmap.) 3/9/2003 B.Ramamurthy 1

Lecture 4: Threads; weaving control flow

Chapter 3 Processes. Process Concept. Process Concept. Process Concept (Cont.) Process Concept (Cont.) Process Concept (Cont.)

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

CSE 120 Principles of Operating Systems

Chapter 4: Multithreaded Programming

Chapter 4: Multithreaded Programming

CSCE 313 Introduction to Computer Systems. Instructor: Dezhen Song

CSE 4/521 Introduction to Operating Systems

Concurrent Programming. Alexandre David

Processes. CS3026 Operating Systems Lecture 05

THREADS. Jo, Heeseung

Transcription:

Operating systems (vimia219) Interoperation of tasks Tamás Kovácsházy, PhD 4 th topic, Implementation of tasks, processes and threads Budapest University of Technology and Economics Department of Measurement and Information Systems BME-MIT 2014, All Rights Reserved

Implementation of the concept of task... From the point of view of implementation the concept of process is quite close to the concept of task The process is a program under execution o From the same program multiple processes can be created o It has its own code, data, heap, stack, and free memory o It is protected from other processes Separation, virtual machine, sandbox Stack Free memory Heap Data Code BME-MIT 2014, All Rights Reserved 2. lap

Separation of processes They run on their own virtual machine: o The virtual machine is created by the OS o They cannot have access to other processes and to the operating system (to the CPU states and memory areas) o There is a context switch if another process gets to run They have their own virtual memory (details will be given later). o Processes cannot have access to the virtual memory of other processes and to the physical memory directly o The MMU of the CPU provides this functionality It is a possibility of sharing memory areas with READ privileges (e.g. shared library code memory). Modern MMUs provide more detailed sharing capabilities (e.g. Write, No Execute, etc.)... BME-MIT 2014, All Rights Reserved 3. lap

Creation of Processes OS specific system call (e.g. CreateProcess() in Windows, fork() in UNIX) Parent/child relationship between creator/created processes o Process tree o The child may have access to the resources of the parent in a configurable way (everything nothing). o The parent may wait for the termination of the child (luckily life is different ) o The parent can pass parameters to the child (command line). UNIX fork() is going to be introduced later in detail Requires lot of administration and resources BME-MIT 2014, All Rights Reserved 4. lap

Communication of processes Processes must interoperate (the actual solutions are going to be detailed later) o For that, they need to communicate Arbitrary two processes cannot communication through memory o The main task of the MMU and the virtual memory is to separate processes from this aspect o They can communicate only through system calls, which is resource hungry The process is efficient from the point of view of protection/separation The process is en inefficient way of solving parallel, strongly interrelated problems o E.g. GUI and some CPU intensive computation in the background (WORD typesetting after some edit) BME-MIT 2014, All Rights Reserved 5. lap

Termination of processes OS specific system call (e.g. TerminateProcess() on Windows, exit() on UNIX) The opened, previously used resources must be closed o E.g. opened files or TCP/IP sockets, etc. The parent may get a return value (most cases an integer), it informs it about the status of termination What if the parent terminates before the child? o OS specific implementation, typical solutions are: The child is assigned a default parent (e.g. UNIX: init process). Automatic termination of all childs (cascading termination). Requires lot of administration and resources BME-MIT 2014, All Rights Reserved 6. lap

Evaluation of the concept of process From the point of view of protection and separation it is good solution, but it requires lot of resources o Creation and termination of processes o Communication and resource sharing between processes Solution: Introduction of the thread o The thread the default unit of CPU utilization, it is a sequential code o It has its own virtual CPU and stack o The code, data, heap and other resources are shared with other threads running in the context of a process o The process is a memory container, the thread is a CPU container Process = heavyweight process Thread = lightweight process BME-MIT 2014, All Rights Reserved 7. lap

Processes and threads on a figure Code Heap Data CPU Stack Process Code CPU Data Heap CPU Resources CPU Resources Stack Stack Stack Thread Thread Thread Thread... Process Single thread OS supporting only processes, e.g. traditional UNIX Thread based operating system, e.g. Windows NT and later, modern UNIX BME-MIT 2014, All Rights Reserved 8. lap

Support of threads Modern operating systems support threads in a native way Windows: o Program or service = process and under the process multiple threads o The scheduler schedules threads Modern UNIX, Linux: o Program or daemon = process and under processes multiple threads o The scheduler schedules tasks, and a task can be a process (legacy programs from traditional UNIX) or a thread (new programs) BME-MIT 2014, All Rights Reserved 9. lap

User space threads Under UNIX (even Linux in earlier times). o green threads The OS knows only processes Threads are needed, support is coming, programmers want to use it o User space thread libraries... The OS can only schedule processes, so if the process runs, its user space thread library can run its own thread level scheduler o Multiple threads form a scheduling unit! Only one of those threads or the user space thread scheduler can run Cannot utilize multiple execution units BME-MIT 2014, All Rights Reserved 10. lap

Thread support (Creation) E.g. Win32 API, Pthreads, JAVA thread Win32: CreateThread() with complex parameters Pthreads: POSIX threads e.g. Linux and other UNIX variants, it supports kernel and user space threads also JAVA (VM is the process, inside the VM you can have threads): o If the class is inherited from the Thread class o If the Runnable interface is implemented o JAVA implements threads in a platform specific way Nativ OS specific threads (one-to-one, today it is the typical solution). JAVA specific threads mapping all JAVA threads to one native OS thread (many-to-one, if the OS does not support threads) many-to-many mapping (may require less resources the one-to-one, but allows parallel execution not supported by many-to-one). BME-MIT 2014, All Rights Reserved 11. lap

Advantages of using threads Low amount of resources are needed to create and terminate them o Some estimates it is 1/10 of the processes. Multiple running thread in an application o The GUI responsive of the application does some computation in the background Fast communication in-between threads running in the context of a process o They run in the same virtual memory o Stack is thread specific, but also shared as memory Lot of problems may be caused by this Scalability o Multiple execution unit can be utilizes in one application BME-MIT 2014, All Rights Reserved 12. lap

Consequences of using threads Communication using shared memory is dangerous o The consistency of data structures used for communication may be violated by multiple threads accessing them o We are going to deal with this problem later in at least two lectures o Threads running in the context of different processes must use system calls for communication It is needed less frequently, because closely interrelated functionalities can be implemented using thread in a process BME-MIT 2014, All Rights Reserved 13. lap

Thread 1 Process 1 Thread 2 Process 2 Thread M HW support Process N Thread1 Thread2 ThreadN Virtual memory with MMU Physical memory only (some embedded operating system) Multiple Address Space OS Single Address Space OS CPU MMU CPU BME-MIT 2014, All Rights Reserved 14. lap

Thread 1 Process 1 Thread 2 Process 2 Thread M HW support Process N Thread1 Thread2 ThreadN Virtual memory with MMU Physical memory only (some embedded operating system) Multiple Address Space OS no MMU or MMU is not programmed Single Address Space OS CPU MMU CPU BME-MIT 2014, All Rights Reserved 15. lap

Coroutine or fiber Cooperative multitasking o Inside a process or a thread OS support or programming language level implementation On the OS level the process or thread is scheduled The scheduling of coroutines or fibers are in the hand of the programmer (cooperative scheduling). o Coroutine: programming language level construct Haskell, JavaScript, Modula-2, Perl, Python, Ruby, etc. o Fiber: system (OS) level solution Win32 API (ConvertThreadToFiber and CreateFiber). Symbian BME-MIT 2014, All Rights Reserved 16. lap

Coroutine Generalization of the Subroutine o Subroutine: LIFO (Last In/called, First Out/returns). Single entry point, multiple exit points (return/exit) The stack is used to pass parameters and return value o Coroutine: First entry point is the same as in case of the subroutine After that its entry point is after the last exit point! Transfer is with the yield to Coroutine_id call. Cannot use stack, it never returns! BME-MIT 2014, All Rights Reserved 17. lap

Coroutine example, 1 st call var q := new queue coroutine produce loop while q is not full create some new items add the items to q yield to consume coroutine consume loop while q is not empty remove some items from q use the items yield to produce BME-MIT 2014, All Rights Reserved 18. lap

Coroutine example, all other calls var q := new queue coroutine produce loop while q is not full create some new items add the items to q yield to consume coroutine consume loop while q is not empty remove some items from q use the items yield to produce BME-MIT 2014, All Rights Reserved 19. lap

Evaluation of coroutine and fiber For problems solvable with cooperative multitasking For stack based environments (e.g. C/C++) implementation is hard if not done on the system level (fiber) No resource sharing is required o No specific OS calls for resource sharing o Less overhead The OS schedules them in a thread, they cannot utilize multiple execution units BME-MIT 2014, All Rights Reserved 20. lap

Some other approaches Android is based on Linux o Based on the process and thread support it builds an interesting framework for supporting mobile applications Let s see it in details BME-MIT 2014, All Rights Reserved 21. lap

Android BME-MIT 2014, All Rights Reserved 22. lap

Android and Linux Application Security Sandbox Android applications run in an application specific instance of Dalvik Virtual Machine (VM) o The Dalvik VM runs in a UNIX process Thread and virtual memory support of Linux are used o Dalvik is developed for mobile/embedded use Low memory usage Register based, not stack based VM Every running Android application gets its own Linux User ID (UID), to deny access to other applications o By default it can access only files created by the application o Principle of least privilege Properties of the application is described in the Manifest File An application can be terminated by the OS any time in case of low resource availability o It is done by the OS automatically o There are Task managers on the Android Market to do this o No Exit/Quit button in most of the Android Apps (not needed) BME-MIT 2014, All Rights Reserved 23. lap

Android application components 1. Relevant from the point of view of task implementation Activity o A screen with user interface o Multiple one in an application o In an application activities are independent entities, but they interoperate while the user accesses the application o It can be 3 states: Resumed (active screen), the user can tap on it Paused (inactive, visible, part of covered by the active screen), Stopped (inactive and not visible screen) Only activities visible on the screen are executed, all the others are stored with their state (resource optimization) Lifecycle callbacks to inform the application on activity state changes o Other applications may start an activity in the application If that is allowed by the application BME-MIT 2014, All Rights Reserved 24. lap

Android application components 2. Service o It runs in the background without any user interface For background tasks running continuously, such as the MP3 playing component of an MP3 player It does not create a thread for itself, if it is CPU intensive, a thread must be created for it for better user experience o Started service It runs as long as it finishes its task, the application has minimal control over it It can run longer than the application starting it Example: Downloading a big file o Bound service Its lifecycle bound to the application It provides a well-defined interface to the application Example: Background MP3 player service controlled from an application (play, stop, forward, backward, volume, speed, etc.) o Lifecycle callbacks are present here also BME-MIT 2014, All Rights Reserved 25. lap