Systemnahe Programmierung KU



Similar documents
THREADS, LIKE PROCESSES, ARE A MECHANISM TO ALLOW A PROGRAM to do more than

Shared Address Space Computing: Programming

Intel TSX (Transactional Synchronization Extensions) Mike Dai Wang and Mihai Burcea

CS 33. Multithreaded Programming V. CS33 Intro to Computer Systems XXXVII 1 Copyright 2015 Thomas W. Doeppner. All rights reserved.

Overview Motivating Examples Interleaving Model Semantics of Correctness Testing, Debugging, and Verification

Monitors, Java, Threads and Processes

CPE453 Laboratory Assignment #2 The CPE453 Monitor

SYSTEM ecos Embedded Configurable Operating System

Last Class: Semaphores

Chapter 2: OS Overview

Chapter 6, The Operating System Machine Level

Win32 API Emulation on UNIX for Software DSM

Mutual Exclusion using Monitors

Linux Driver Devices. Why, When, Which, How?

Distributed Systems [Fall 2012]

umps software development

3 - Lift with Monitors

I/O. Input/Output. Types of devices. Interface. Computer hardware

Model Checking of Concurrent Algorithms: From Java to C

Understanding Hardware Transactional Memory

CS4410 Final Exam : Solution set. while(ticket[k] && (ticket[k],k) < (ticket[i],i))

MASSACHUSETTS INSTITUTE OF TECHNOLOGY Computer Systems Engineering: Spring Quiz I Solutions

Programming with Threads in Unix

A Survey of Parallel Processing in Linux

CS414 SP 2007 Assignment 1

Paper 3F6: Software Engineering and Systems Distributed Systems Design Solutions to Examples Paper 3

Lecture 25 Symbian OS

Facing the Challenges for Real-Time Software Development on Multi-Cores

Topics. Producing Production Quality Software. Concurrent Environments. Why Use Concurrency? Models of concurrency Concurrency in Java

Shared Memory Segments and POSIX Semaphores 1

Replication on Virtual Machines

Linux/UNIX System Programming. POSIX Shared Memory. Michael Kerrisk, man7.org c February 2015

Outline. Review. Inter process communication Signals Fork Pipes FIFO. Spotlights

Operating Systems and Networks

How To Write A Multi Threaded Software On A Single Core (Or Multi Threaded) System

High Availability Solutions for the MariaDB and MySQL Database

6 Synchronization with Semaphores

Monitor Object. An Object Behavioral Pattern for Concurrent Programming. Douglas C. Schmidt

2.1 What are distributed systems? What are systems? Different kind of systems How to distribute systems? 2.2 Communication concepts

Operating System Overview. Otto J. Anshus

An Easier Way for Cross-Platform Data Acquisition Application Development

Processes and Non-Preemptive Scheduling. Otto J. Anshus

Lecture 18: Reliable Storage

SSC - Concurrency and Multi-threading Java multithreading programming - Synchronisation (I)

Testing Database Performance with HelperCore on Multi-Core Processors

Lecture 6: Semaphores and Monitors

POSIX. RTOSes Part I. POSIX Versions. POSIX Versions (2)

An Implementation Of Multiprocessor Linux

Kernel Optimizations for KVM. Rik van Riel Senior Software Engineer, Red Hat June

Migration of Process Credentials

CS11 Java. Fall Lecture 7

Design Patterns in C++

Lecture 22: C Programming 4 Embedded Systems

Operating System Manual. Realtime Communication System for netx. Kernel API Function Reference.

A Comparison Of Shared Memory Parallel Programming Models. Jace A Mogill David Haglin

Code Aware Resource Scheduling

Pitfalls in Embedded Software

Microsoft DFS Replication vs. Peer Software s PeerSync & PeerLock

CS11 Advanced C++ Spring Lecture 9 (!!!)

Debugging with TotalView

IPC. Semaphores were chosen for synchronisation (out of several options).

Port of the Java Virtual Machine Kaffe to DROPS by using L4Env

First-class User Level Threads

Shared Memory Introduction

Parallel Programming

COS 318: Operating Systems. Virtual Memory and Address Translation

The first program: Little Crab

Why Threads Are A Bad Idea (for most purposes)

Operating Systems. Design and Implementation. Andrew S. Tanenbaum Melanie Rieback Arno Bakker. Vrije Universiteit Amsterdam

Outline. Operating Systems Design and Implementation. Chap 1 - Overview. What is an OS? 28/10/2014. Introduction

ReturnStar HDD Lock V3.0 User Manual

Implementation of Operator Authentication Processes on an Enterprise Level. Mark Heard Eastman Chemical Company

Database Replication with Oracle 11g and MS SQL Server 2008

LiveBackup. Jagane Sundar

OPERATING SYSTEMS PROCESS SYNCHRONIZATION

MTP: Continuous User Authentication on Android Using Face Recognition

CSC 2405: Computer Systems II

Operating Systems 4 th Class

2 The first program: Little Crab

MatrixSSL Porting Guide

Deadlock Victim. dimanche 6 mai 12

A Deduplication File System & Course Review

Overview. CMSC 330: Organization of Programming Languages. Synchronization Example (Java 1.5) Lock Interface (Java 1.5) ReentrantLock Class (Java 1.

3.5. cmsg Developer s Guide. Data Acquisition Group JEFFERSON LAB. Version

Code Injection From the Hypervisor: Removing the need for in-guest agents. Matt Conover & Tzi-cker Chiueh Core Research Group, Symantec Research Labs

JOURNAL OF OBJECT TECHNOLOGY

EMC RepliStor for Microsoft Windows ERROR MESSAGE AND CODE GUIDE P/N REV A02

Java Concurrency Framework. Sidartha Gracias

Concurrent Programming

Exceptions in MIPS. know the exception mechanism in MIPS be able to write a simple exception handler for a MIPS machine

Linux scheduler history. We will be talking about the O(1) scheduler

Semaphores and Monitors: High-level Synchronization Constructs

Concepts of Concurrent Computation

Performance Comparison of RTOS

Using the Intel Inspector XE

Transcription:

S C I E N C E P A S S I O N T E C H N O L O G Y Systemnahe Programmierung KU A6,A7 www.iaik.tugraz.at

1. Virtual Memory 2. A7 - Fast-Food Restaurant 2

Course Overview A8, A9 System Programming A7 Thread Interaction and Synchronization A4, A5, A6 Multithreading, Processes, Virtual Memory A1, A2, A3 Git, Compiler, C, C++ 3

What is a pointer? 4

What is a pointer? Address in memory 4

What is a pointer? Address in memory? Index in RAM 4

What is a pointer? Address in memory? Index in RAM? 4

Segmentation fault 5

Segmentation fault Common scenario: User program accesses invalid memory location 5

Segmentation fault Common scenario: User program accesses invalid memory location The OS invokes a fault handler This fault handler can abort the user program or fix the situation by making the address valid Efficient: Don t assign memory until necessary 5

Segmentation fault Common scenario: User program accesses invalid memory location The OS invokes a fault handler This fault handler can abort the user program or fix the situation by making the address valid Efficient: Don t assign memory until necessary Are pointers addresses in physical memory? How can addresses in memory be invalid? 5

Virtual Memory 6

Virtual Memory Pointers are not addresses in physical memory 6

Virtual Memory Pointers are not addresses in physical memory Pointers are virtual addresses Addresses are translated from virtual addresses to real physical addresses transparently 6

Virtual Memory Pointers are not addresses in physical memory Pointers are virtual addresses Addresses are translated from virtual addresses to real physical addresses transparently Therefore, memory is split into parts called pages Operating system can map pages Operating system can maintain this mapping per process 6

Virtual Memory Pointers are not addresses in physical memory Pointers are virtual addresses Addresses are translated from virtual addresses to real physical addresses transparently Therefore, memory is split into parts called pages Operating system can map pages Operating system can maintain this mapping per process different processes can use the same addresses, but see different things there 6

Virtual Memory - It s a map! 7

Virtual Memory - It s a map! Pseudo code: // in software: int a = *(virt_address); // in hardware: std::map<virt*,phys*> addr_map; auto fetch_memory(virt* virt_address) { phys* phys_address = addr_map[virt_address]; return *phys_addr; } 7

A6 - Memory Layout and Demand Paging www.iaik.tugraz.at Experiment with different kinds of variables, which addresses do they get? Observe memory usage in practice, when does it really increase? Write many small test programs (else you won t find the right answers). Answer questions from the test system questionnaire! 8

Task Overview Resources: FoodTrays, FoodCounters, Tables Actors: Employees and Customers Customers acquire FoodTray, get food from the FoodCounter, and then eat at the table Resources may only be used/cleaned/renewed by one actor at a time 9

First Steps Pull upstream Try make and execute the program It will not work ;) Fix it! 10

A7 - Fast-Food Restaurant main() Restaurant Thread FoodCounter Customer Table Employee FoodTray 11

Classes to touch main() Restaurant Thread FoodCounter Customer Table Employee FoodTray 12

Classes? Yes, but still it s C ;) You will be able to run it on SWEB! 13

What do we need? Locks: Semaphores Mutexes Condition Variables 14

Semaphores sem_t sem ; // int sem_init ( sem_t * sem, int pshared, // unsigned int value ); sem_init (& sem, 0, 1); //... sem_wait (& sem ); // critical region sem_post (& sem ); //... sem_destroy (& sem ); 15

Mutexes pthread_mutex_t mutex ; // int pthread_mutex_init ( pthread_mutex_t * // mutex, pthread_mutexattr_t * attr ); pthread_mutex_init (& mutex, 0); //... pthread_mutex_lock (& mutex ); // critical region pthread_mutex_unlock (& mutex ); //... pthread_mutex_destroy (& mutex ); 16

Condition Variables pthread_cond_t cond ; // int pthread_cond_init ( pthread_cond_t * // cond, pthread_condattr_t * attr ); pthread_cond_init (& cond, 0); //... pthread_mutex_lock (& mutex ); // critical region pthread_cond_wait (& cond, & mutex ); // release mutex until condition is true // still critical region pthread_mutex_unlock (& mutex ); //... pthread_cond_destroy (& cond ); 17

Typical Errors if ( parking_lot. empty ()) { parking_lot. drivein ( my_car ); // go shopping parking_lot. leave ( my_car ); } 18

Typical Errors Something is not locked. Random behaviour, race conditions, strange crashes. 19

Typical Errors LOCK ( parking_lot ); if ( parking_lot. empty ()) { UNLOCK ( parking_lot ); parking_lot. drivein ( my_car ); // go shopping parking_lot. leave ( my_car ); } else UNLOCK ( parking_lot ); 20

Typical Errors Correct locking at first glance, but...... actually the critical part is not locked. 21

Typical Errors - Thread 1 LOCK ( bike ); bike. dismount (); // go shopping bike. mount (); UNLOCK ( bike ); 22

Typical Errors - Thread 2 bike. mount (); // do something 23

Typical Errors Correct locking, but...... some other thread accessed the resource without using the lock. 24

Typical Errors - Thread 1 acquir ealllocksi nthesystem (); // do something releas ealllocksi nthesystem (); 25

Typical Errors - Thread 2 somelock. acquire (); // wait a while //... 26

Typical Errors Everything is locked. Several times. Just to be sure. Will result in a fatal performance. Maybe nothing can happen simultaneously because of the way it is locked. 27

Typical Errors LOCK ( parking_lot ); if ( parking_lot. empty ()) { parking_lot. drivein ( my_car ); UNLOCK ( parking_lot ); // go shopping parking_lot. leave ( my_car ); } // no unlock... 28

Typical Errors Not unlocking the critical region in some situations. Other threads will wait forever. 29

Typical Errors - Thread 1 LOCK ( harddisk ); LOCK ( floppy ); copysomething ( floppy, harddisk ); UNLOCK ( floppy ); UNLOCK ( harddisk ); 30

Typical Errors - Thread 2 LOCK ( floppy ); LOCK ( harddisk ); checksomething ( floppy, harddisk ); UNLOCK ( harddisk ); UNLOCK ( floppy ); 31

Typical Errors Deadlock. 32

Typical Errors void setuppaging () { LOCK ( pagedirectory ); insertpagetable (); insertpage (); fillpagewithdata (); UNLOCK ( pagedirectory ); } 33 // in some other file (!): void insertpagetable () { LOCK ( pagedirectory ); // do the actual work UNLOCK ( pagedirectory ); }

Thanks for your attention!