Concurrency. Stefan D. Bruda. Winter 2018

Similar documents
PROCESS CONTROL BLOCK TWO-STATE MODEL (CONT D)

Concurrent Processing in Client-Server Software

Reading Assignment 4. n Chapter 4 Threads, due 2/7. 1/31/13 CSE325 - Processes 1

CSC 1600 Unix Processes. Goals of This Lecture

CSci 4061 Introduction to Operating Systems. Processes in C/Unix

518 Lecture Notes Week 3

Operating Systems & Concurrency: Process Concepts

CSCE 313 Introduction to Computer Systems. Instructor: Dezhen Song

CSCE 313: Intro to Computer Systems

Processes. Process Concept

Operating Systems Lab

Chapter 4: Processes. Process Concept

Process Management! Goals of this Lecture!

Chapter 4: Processes

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

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

Fall 2015 COMP Operating Systems. Lab #3

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

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

Process Management 1

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

Operating System Structure

Processes. Operating System Concepts with Java. 4.1 Sana a University, Dr aimen

Introduction to OS Processes in Unix, Linux, and Windows MOS 2.1 Mahmoud El-Gayyar

Process a program in execution; process execution must progress in sequential fashion. Operating Systems

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

Processes. Today. Next Time. ! Process concept! Process model! Implementing processes! Multiprocessing once again. ! Scheduling processes

What Is A Process? Process States. Process Concept. Process Control Block (PCB) Process State Transition Diagram 9/6/2013. Process Fundamentals

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

CS510 Operating System Foundations. Jonathan Walpole

4.8 Summary. Practice Exercises

Processes. Operating System CS 217. Supports virtual machines. Provides services: User Process. User Process. OS Kernel. Hardware

Process Management 1

CSE 451: Operating Systems Winter Module 4 Processes. Mark Zbikowski Allen Center 476

COE518 Lecture Notes Week 2 (Sept. 12, 2011)

Princeton University Computer Science 217: Introduction to Programming Systems. Process Management

Processes, Threads, SMP, and Microkernels

Operating Systems. Engr. Abdul-Rahman Mahmood MS, PMP, MCP, QMR(ISO9001:2000) alphapeeler.sf.net/pubkeys/pkey.htm

CS240: Programming in C

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

Princeton University Computer Science 217: Introduction to Programming Systems. Process Management

Multiservice servers. Stefan D. Bruda. Winter 2018

Getting to know you. Anatomy of a Process. Processes. Of Programs and Processes

Exceptions and Processes

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

Princeton University. Computer Science 217: Introduction to Programming Systems. Process Management

CS 350 : COMPUTER SYSTEM CONCEPTS SAMPLE TEST 2 (OPERATING SYSTEMS PART) Student s Name: MAXIMUM MARK: 100 Time allowed: 70 minutes

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

Processes. q Process concept q Process model and implementation q Multiprocessing once again q Next Time: Scheduling

Process management. What s in a process? What is a process? The OS s process namespace. A process s address space (idealized)

Notice: This set of slides is based on the notes by Professor Perrone of Bucknell and the textbook authors Silberschatz, Galvin, and Gagne

Part Two - Process Management. Chapter 3: Processes

CSC 252: Computer Organization Spring 2018: Lecture 19

CHAPTER 2: PROCESS MANAGEMENT

File Descriptors and Piping

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

Operating systems and concurrency - B03

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

Processes and Threads

Processes. Electrical and Computer Engineering Stephen Kim ECE/IUPUI RTOS & Apps 1

fork System-Level Function

Chapter 3: Processes. Operating System Concepts 8th Edition

SE350: Operating Systems

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

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

OS Interaction and Processes

CS 261 Fall Mike Lam, Professor. Processes

Process Creation in UNIX

UNIX Input/Output Buffering

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

Windows architecture. user. mode. Env. subsystems. Executive. Device drivers Kernel. kernel. mode HAL. Hardware. Process B. Process C.

CSE 410: Computer Systems Spring Processes. John Zahorjan Allen Center 534

Chapter 3: Processes

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

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

Part II Processes and Threads Process Basics

There are 10 total numbered pages, 5 Questions. You have 60 minutes. Budget your time carefully!

Layers in a UNIX System. Create a new process. Processes in UNIX. fildescriptors streams pipe(2) labinstructions

Processes. CS3026 Operating Systems Lecture 05

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

Parents and Children

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

Introduction to Processes

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 20

LSN 13 Linux Concurrency Mechanisms

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

Processes. CS 416: Operating Systems Design, Spring 2011 Department of Computer Science Rutgers University

Section 2: Processes

Assignment 1. Teaching Assistant: Michalis Pachilakis (

Unix-Linux 2. Unix is supposed to leave room in the process table for a superuser process that could be used to kill errant processes.

CS 3305 Intro to Threads. Lecture 6

OS Lab Tutorial 1. Spawning processes Shared memory

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

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

Multithreaded Programming

Operating Systems. Engr. Abdul-Rahman Mahmood MS, PMP, MCP, QMR(ISO9001:2000) alphapeeler.sf.net/pubkeys/pkey.htm

Chap 4, 5: Process. Dongkun Shin, SKKU

CS240: Programming in C

CS 261 Fall Mike Lam, Professor. Exceptional Control Flow and Processes

Week 2 Intro to the Shell with Fork, Exec, Wait. Sarah Diesburg Operating Systems CS 3430

Transcription:

Concurrency Stefan D. Bruda Winter 2018

DOING MORE THINGS SIMULTANEOUSLY Concurrency can be achieved by multiprocessing and time-sharing Best definition for concurrency: apparently simultaneous execution Concurrency is fundamental to distributed computing Multiprocessing: many machines run simultaneously many programs Time-sharing: a single machine runs multiple programs in an interleaved fashion (context switching) Whether things run in a time-share fashion or on a multiprocessor computer is immaterial; the observable behaviour is the same Concurrency (S. D. Bruda) Winter 2018 1 / 8

DOING MORE THINGS SIMULTANEOUSLY Concurrency can be achieved by multiprocessing and time-sharing Best definition for concurrency: apparently simultaneous execution Concurrency is fundamental to distributed computing Multiprocessing: many machines run simultaneously many programs Time-sharing: a single machine runs multiple programs in an interleaved fashion (context switching) Whether things run in a time-share fashion or on a multiprocessor computer is immaterial; the observable behaviour is the same The fundamental unit of computation: a process A process consists of an address space and one (or more) threads of execution (i.e., instruction pointers) Each process receives a separate copy of all the variables (address space) Each thread in a process have a copy of local variables (stack) but they all share the rest of the address space (global variables, heap) Concurrency (S. D. Bruda) Winter 2018 1 / 8

PROCESS CREATION We now create singly threaded processes: #include <iostream.h> #include <sys/types.h> #include <unistd.h> to To other side. get the Why did the multithreaded chicken cross the road? other to side. To the get int main (int argc, char** argv) { int i; int sum = 0; fork(); for (int i=1; i<10000; i++) { sum = sum + i; cout << "\ni computed " << sum << "\n"; Concurrency (S. D. Bruda) Winter 2018 2 / 8

PARENTS AND CHILDREN The call to fork() duplicates the current process; both processes continue execution from the instruction following the call to fork() The initial process is the parent, and the newly created copy is called a child Processes are identified in a Unix system by a unique process identifier (or PID, unsigned integer) fork() returns two different integers in the child and parent processes In the child process fork() returns zero In the parent process fork() returns the PID of the newly created child So we can provide different code for the parent and the child, by surrounding them in appropriate conditional statements Concurrency (S. D. Bruda) Winter 2018 3 / 8

DIVERGING PROCESSES #include <iostream.h> #include <sys/types.h> #include <unistd.h> int main (int argc, char** argv) { int i; int sum = 0; int pid; pid = fork(); for (int i=1; i<10000; i++) { if (pid == 0) cout << "+"; // child process else cout << "-"; cout.flush(); sum = sum + i; // parent process // both processes if (pid == 0) cout << "\n[child] I computed " << sum << "\n"; else cout << "\n[parent] I computed " << sum << "\n"; Concurrency (S. D. Bruda) Winter 2018 4 / 8

CHILDREN DOING COMPLETELY DIFFERENT STUFF The call execve replaces completely the current process with another executable The arguments are the name of the command to execute, then two null-terminated arrays of strings containing the command line arguments and the environment, similar to the ones received by the function main Suppose now that we want to run an external command (so we use execve), but also we want to continue the execution of the original program Concurrency (S. D. Bruda) Winter 2018 5 / 8

CHILDREN DOING COMPLETELY DIFFERENT STUFF The call execve replaces completely the current process with another executable The arguments are the name of the command to execute, then two null-terminated arrays of strings containing the command line arguments and the environment, similar to the ones received by the function main Suppose now that we want to run an external command (so we use execve), but also we want to continue the execution of the original program We use a combination of fork and execve: int childp = fork(); if (childp == 0) { // child execve(command, argv, envp); else { // parent // code that continues our program Concurrency (S. D. Bruda) Winter 2018 5 / 8

KNOW WHAT YOUR CHILDREN DO Again, suppose that we want to execute an external command (execve again) We still want to continue the execution of the main program But only after the run of the external command is complete (synchronous as opposed to asynchronous execution) Concurrency (S. D. Bruda) Winter 2018 6 / 8

KNOW WHAT YOUR CHILDREN DO Again, suppose that we want to execute an external command (execve again) We still want to continue the execution of the main program But only after the run of the external command is complete (synchronous as opposed to asynchronous execution) int run_it (char* command, char* argv [], char* envp[]) { int childp = fork(); int status; if (childp == 0) { // child execve(command, argv, envp); else { // parent waitpid(childp, &status,0); return status; Concurrency (S. D. Bruda) Winter 2018 6 / 8

WHY CREATE PROCESSES? We will build eventually servers (programs that serve requests from clients) Instead of serving requests from one client at a time, our servers will handle many clients quasi-simultaneously loop listen for clients if a client requests connection then fork if child process then handle client terminate forever Concurrency (S. D. Bruda) Winter 2018 7 / 8

WHY CREATE PROCESSES? We will build eventually servers (programs that serve requests from clients) Instead of serving requests from one client at a time, our servers will handle many clients quasi-simultaneously loop listen for clients if a client requests connection then fork if child process then handle client terminate forever... or even many types of clients loop listen for clients if a client of type x requests connection fork then if child process then launch server x terminate forever execve Concurrency (S. D. Bruda) Winter 2018 7 / 8

WHY CREATE PROCESSES? (CONT D) connect do stuff done x Client 1 connect do stuff done x Client 2 fork fork Parent loop do stuff x Child 1 do stuff x Child 2 Server Time Three processes running concurrently on the server s machine Concurrency (S. D. Bruda) Winter 2018 8 / 8