CS5460: Operating Systems

Similar documents
Lecture 4: Process Management

The Big Picture So Far. Today: Process Management

Today: Process Management. The Big Picture So Far. What's in a Process? Example Process State in Memory

Operating Systems CMPSCI 377 Spring Mark Corner University of Massachusetts Amherst

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

W4118 Operating Systems. Junfeng Yang

Processes. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Processes and Threads

Threads. What is a thread? Motivation. Single and Multithreaded Processes. Benefits

Processes and Non-Preemptive Scheduling. Otto J. Anshus

Threads and Too Much Milk! CS439: Principles of Computer Systems January 31, 2018

CSCE 313 Introduction to Computer Systems. Instructor: Dezhen Song

CSCE 313: Intro to Computer Systems

Announcements/Reminders

Process Description and Control

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 20

CS 333 Introduction to Operating Systems. Class 3 Threads & Concurrency. Jonathan Walpole Computer Science Portland State University

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

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

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

CSE 153 Design of Operating Systems Fall 2018

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

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

Introduction to Processes

CSE 410: Systems Programming

CSE 120 Principles of Operating Systems

Threads and Too Much Milk! CS439: Principles of Computer Systems February 6, 2019

CS 333 Introduction to Operating Systems. Class 3 Threads & Concurrency. Jonathan Walpole Computer Science Portland State University

CS 153 Design of Operating Systems Winter 2016

Processes. Johan Montelius KTH

A process. the stack

Arvind Krishnamurthy Spring Threads, synchronization, scheduling Virtual memory File systems Networking

CSE 120 Principles of Operating Systems

PROCESSES. Jo, Heeseung

Processes. Jo, Heeseung

CSE 4/521 Introduction to Operating Systems

Chapter 4: Processes. Process Concept

PROCESS MANAGEMENT. Operating Systems 2015 Spring by Euiseong Seo

The Kernel Abstraction. Chapter 2 OSPP Part I

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

Chapter 4: Processes

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

Notes. CS 537 Lecture 5 Threads and Cooperation. Questions answered in this lecture: What s in a process?

Processes. CS439: Principles of Computer Systems January 24, 2018

Threads Implementation. Jo, Heeseung

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

CSE 120 Principles of Operating Systems

Mon Sep 17, 2007 Lecture 3: Process Management

Lecture 4: Threads; weaving control flow

CS 5523: Operating Systems

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

* What are the different states for a task in an OS?

CSE 380 Computer Operating Systems. Instructor: Insup Lee. University of Pennsylvania Fall 2003

CISC2200 Threads Spring 2015

Processes. Overview. Processes. Process Creation. Process Creation fork() Processes. CPU scheduling. Pål Halvorsen 21/9-2005

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

Computer Systems II. First Two Major Computer System Evolution Steps

www nfsd emacs lpr Process Management CS 537 Lecture 4: Processes Example OS in operation Why Processes? Simplicity + Speed

Processes & Threads. Today. Next Time. ! Process concept! Process model! Implementing processes! Multiprocessing once again. ! More of the same J

CS 326: Operating Systems. Process Execution. Lecture 5

CS 33. Architecture and the OS. CS33 Intro to Computer Systems XIX 1 Copyright 2018 Thomas W. Doeppner. All rights reserved.

Prepared by Prof. Hui Jiang Process. Prof. Hui Jiang Dept of Electrical Engineering and Computer Science, York University

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

CS510 Operating System Foundations. Jonathan Walpole

CS 318 Principles of Operating Systems

Process. Prepared by Prof. Hui Jiang Dept. of EECS, York Univ. 1. Process in Memory (I) PROCESS. Process. How OS manages CPU usage? No.

Operating System Structure

Chapter 3: Process-Concept. Operating System Concepts 8 th Edition,

Why use an Operating System? Operating System Definition

Interrupts, Fork, I/O Basics

Processes. CS3026 Operating Systems Lecture 05

Chapter 3: Processes. Operating System Concepts Essentials 8 th Edition

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

ICS Principles of Operating Systems

THREADS. Jo, Heeseung

CSCB09: Software Tools and Systems Programming. Bianca Schroeder IC 460

csci3411: Operating Systems

Module 4: Processes. Process Concept Process Scheduling Operation on Processes Cooperating Processes Interprocess Communication

Module 4: Processes. Process Concept Process Scheduling Operation on Processes Cooperating Processes Interprocess Communication

CS 318 Principles of Operating Systems

Processes, Threads, SMP, and Microkernels

OS Structure, Processes & Process Management. Don Porter Portions courtesy Emmett Witchel

Yi Shi Fall 2017 Xi an Jiaotong University

What s in a process?

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

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

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

PROCESS CONTROL BLOCK TWO-STATE MODEL (CONT D)

CS 5460/6460 Operating Systems

What is a Process? Processes and Process Management Details for running a program

COP 4610: Introduction to Operating Systems (Spring 2016) Chapter 3: Process. Zhi Wang Florida State University

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

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

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

CSC 539: Operating Systems Structure and Design. Spring 2006

CS 450 Operating System Week 4 Lecture Notes

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

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

CS399 New Beginnings. Jonathan Walpole

Transcription:

CS5460: Operating Systems Lecture 5: Processes and Threads (Chapters 3-4)

Context Switch Results lab2-15 gamow home 3.8 us 1.6 us 1.0 us VirtualBox on lab2-25 VirtualBox on gamow VirtualBox on home 170 us 4.6 us 42 us

Important From Last Time Process state machine Interaction with OS invariants Process control block Presence on OS queues Process creation UNIX vs. Windows style Process termination create process New Ready I/O done schedule deschedule Waiting Terminated exit process Running block on timer, I/O, page fault,

What does this program print? #include <stdio.h> #include <unistd.h> int main (void) { int x = 1000; fork(); printf ( %d\n, x++); printf ( %d\n, x++); return 0;

Termination When process dies, OS reclaims its resources On Unix: A process can terminate itself using exit() system call A process can kill another process using the kill() system call #include <signal.h> #include <unistd.h> #include <stdio.h> int main(void) { int cid = fork(); if (cid == 0) { sleep(10); printf( Child exiting!\n ); exit(0); else { printf( Type to kill child\n ); char answer[10]; gets(answer); if (!kill(cid,sigkill)) { printf( Child dead!\n );

pid_t wait(int *status): Parent process use wait() to request notification when a child dies Returns PID of dead child; sets status to be child s exit code Works regardless of whether child dies before/ after call What does this program do? What happens when a process dies? Do we reclaim all resources? #include <signal.h> #include <unistd.h> #include <stdio.h> int main(void) { int ret, cid; cid = fork(); if (cid == 0) { /* CHILD*/ printf( Child exiting.\n ); exit(100); else { /* PARENT */ wait(&ret); printf( Status: %d\n, ret);

Context switch: Switch from one process/thread running on CPU to another When can it happen? Which process/thread should run next? How is it accomplished? When? Preemptive vs non-preemptive scheduling Can occur whenever the kernel (scheduler) gains control of CPU Which? Scheduler decides based on its policies How? Save state of old process Restore state of new process Question: What constitutes the process s state?

Timeline of a Context Switch P 1 Scheduler (P 1 ) Scheduler (P 1 ) P 1 Interrupt or trap One instr later! int/trap handler Save/restore cpu state Interrupt or trap Save/restore cpu state Scheduler (P 2 ) Return from trap/int (P2) P 2 Question: What parts of timeline execute in kernel mode? P N int/trap handler Scheduler (P N ) TIME

Introducing Threads Processes are heavyweight Memory mappings: may be expensive to swap Cache/TLB state: flushing expensive, especially side effects Lots of kernel state Context switching between processes is expensive Threads are lightweight Multiple threads share process state» Same address space» Same open file/socket tables Goal: make context switching between threads cheap Not all OSes support threads efficiently (e.g., older Unixes)» Had to fake it using things like select() and signal()

What Are Threads Good For? 1. Making apps speed up on a multicore (Bad) alternative to using threads: Use multiple single-threaded processes 2. Creating servers with a lot of internal concurrency 3. Letting programmers express somewhat independent computations within an address space 4. Letting programmers create the hardest debugging programs EVER Race conditions Deadlocks Etc.

Threads separate process into two abstractions: Shared resources: address space, kernel resources, privileges Execution state: PC, SP, PSW, other registers, stack, Address space Thread Add multiprogramming Add Multithreading DOS, small embedded systems Older (unthreaded) UNIX Real-time OSes, Java Multihreaded OSes (Win7, OS X, Linux)

Implementing Threads Address space shared by threads Code Data and heap Thread private state: Registers (inc. pc, sp, psw) Stack Key issue: How do you safely access shared state?» Read-only (e.g., code) à easy» Writable à hard Whole section of course dedicated to this» Synchronization» Concurrency control Context switch between threads Save/restore registers (tricky) SP0 SP1 HP PC1 PC0 Stack: Stack: Data: Code:

Kernel Threads vs User Threads Kernel threads: OS supports threads directly Each thread has separate OS state (ready, blocked, ) Kernel schedules threads rather than processes Kernel context switches between threads (and processes)» Thread switch: save/restore registers (inc. PC and SP)» Process switch: above plus MMU/TLB state» Thread switches cheaper than process (~10us vs ~30us) User threads: OS does not directly support threads User-level thread library implements threads Thread context switch performed entirely within user space Cannot get parallelism on a multicore! Solaris operating system uses both

Kernel Threads vs User Threads Advantages of kernel threads Threads can exploit multiprocessors Blocking I/O does not stall non-blocked threads Advantages of user threads Faster to create, manipulate, and synchronize In theory, can tailor scheduling policy to match application need Alternative to threads à event-based programming See Ousterhout s paper: Why Threads Are a Bad Idea Getting concurrency right is hard à events simplify model» Race conditions, deadlocks, livelocks, Downside: events are also difficult, and prevent running on multiple processors

Cooperating Processes vs.threads Cooperating processes can provide benefits: Improved performance by overlapping activities (parallelism) Simpler program structure by separating activities (web server) Fault isolation (e.g., shell, CGI subsystem of web server)» Example: Apache, Google Chrome Problems arise: New failure modes introduced à concurrency control Errors may be harder to debug Cleanup complicated à cannot just destroy single process Questions to consider: How do cooperating processes communicate? Do the processes need to share the same {language, machine, OS?

IPC via Message Passing Option 1: processes exchange messages Need to be able to name other process (e.g., PID) Some form of msgsnd() and msgrecv() supported by OS main() { if (!fork()) producer(); else consumer(); producer() { while (1) { produce item nextp; msgsnd(nextp, conspid); consumer() { while (1) { msgrcv(nextc {, prodpid); consume item nextc;

IPC via Shared Memory Option 2: processes directly access same memory Need mechanism to establish shared mappings (e.g., threads in same process, mmap(), shmget()). main() { buffer = shmget( ); in = out = 0; if (!fork()) producer(); else consumer(); Shared also (not shown) producer() { while (1) { produce item nextp; while ((in+1)mod N)== out); buffer[in] = nextp; in = (in+1) mod N; consumer() { while (1) { while (in == out); nextc = buffer[out]; out = (out+1) mod N; consume item nextc; Which is more efficient, shared memory or message passing? What are possible problems with this solution?

Important From Today Processes and threads: Encapsulate resources and accounting Threads separate execution context from process abstraction Typical address space layout Context switch Kernel threads versus User threads How do threads communicate (within a process)? How to cooperating processes communicate? Synchronization is hard and important Next up: Scheduling How to decide what to run next