REAL TIME SYSTEMS. Piotr MALECKI



Similar documents
Shared Memory Introduction

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

OS: IPC I. Cooperating Processes. CIT 595 Spring Message Passing vs. Shared Memory. Message Passing: Unix Pipes

Programmation Systèmes Cours 9 Memory Mapping

Intel P6 Systemprogrammering 2007 Föreläsning 5 P6/Linux Memory System

Lecture 24 Systems Programming in C

System Calls Related to File Manipulation

64 Bit File Access. 1.0 Introduction. 2.0 New Interfaces. 2.1 New User Visible Types. Adam Sweeney

System Calls and Standard I/O

CSI 402 Lecture 13 (Unix Process Related System Calls) 13 1 / 17

Unix System Calls. Dept. CSIE

Shared Memory Segments and POSIX Semaphores 1

Tunable Base Page Size

Configuring CoreNet Platform Cache (CPC) as SRAM For Use by Linux Applications

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

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

LOW LEVEL FILE PROCESSING

1. Computer System Structure and Components

Operating System Structure

Mutual Exclusion using Monitors

Algorytm przydzielania bloku

Angels (OpenSSL) and D(a)emons. Athula Balachandran Wolfgang Richter

Hierarchy storage in Tachyon.

1 Operating Systems Prof. Dr. Marc H. Scholl DBIS U KN Summer Term IPC may often be used for both

Operating Systems and Networks

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

Lecture 16: System-Level I/O

CSE 544 Principles of Database Management Systems. Magdalena Balazinska Fall 2007 Lecture 5 - DBMS Architecture

Automatic Script Generation Based on User-System Interactions

Between Mutual Trust and Mutual Distrust: Practical Fine-grained Privilege Separation in Multithreaded Applications

Programmation Systèmes Cours 7 IPC: FIFO

The Linux Virtual Filesystem

An Implementation Of Multiprocessor Linux

Operating Systems. Privileged Instructions

Chapter 6, The Operating System Machine Level

Carnegie Mellon Virtual Memory of a Linux Process Process-specific data structs (ptables, Different for

I/O Device and Drivers

Linux Process Scheduling Policy

Have both hardware and software. Want to hide the details from the programmer (user).

What is an RTOS? Introduction to Real-Time Operating Systems. So what is an RTOS?(contd)

Chapter 3: Operating-System Structures. System Components Operating System Services System Calls System Programs System Structure Virtual Machines

Process definition Concurrency Process status Process attributes PROCESES 1.3

Jorix kernel: real-time scheduling

Generalised Socket Addresses for Unix Squeak

NDK: NOVELL NSS AUDIT

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

Creating Synergies on the Cloudbox. Mohan Sundar Samsung Electronics

Objectives. Chapter 2: Operating-System Structures. Operating System Services (Cont.) Operating System Services. Operating System Services (Cont.

REAL TIME OPERATING SYSTEMS. Lesson-10:

ProTrack: A Simple Provenance-tracking Filesystem

Computer Systems II. Unix system calls. fork( ) wait( ) exit( ) How To Create New Processes? Creating and Executing Processes

REAL TIME OPERATING SYSTEM PROGRAMMING-II: II: Windows CE, OSEK and Real time Linux. Lesson-12: Real Time Linux

Kernel Synchronization and Interrupt Handling

C++ Class Library Data Management for Scientific Visualization

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

Performance Evaluation and Optimization of A Custom Native Linux Threads Library

IT304 Experiment 2 To understand the concept of IPC, Pipes, Signals, Multi-Threading and Multiprocessing in the context of networking.

Department of Electrical Engineering and Computer Science MASSACHUSETTS INSTITUTE OF TECHNOLOGY Operating System Engineering: Fall 2005

UNIX File Management (continued)

6.828 Operating System Engineering: Fall Quiz II Solutions THIS IS AN OPEN BOOK, OPEN NOTES QUIZ.

RTI Monitoring Library Getting Started Guide

Operating System Structures

Review from last time. CS 537 Lecture 3 OS Structure. OS structure. What you should learn from this lecture

Virtualization Technology. Zhiming Shen

CS 356 Lecture 23 and 24 Software Security. Spring 2013

Last Class: OS and Computer Architecture. Last Class: OS and Computer Architecture

OpenMosix Presented by Dr. Moshe Bar and MAASK [01]

Chapter 11: File System Implementation. Operating System Concepts with Java 8 th Edition

The Real-Time Operating System ucos-ii

Operating System Components

Lab 4: Socket Programming: netcat part

Embedded Programming in C/C++: Lesson-1: Programming Elements and Programming in C

How To Understand How A Process Works In Unix (Shell) (Shell Shell) (Program) (Unix) (For A Non-Program) And (Shell).Orgode) (Powerpoint) (Permanent) (Processes

Lesson Objectives. To provide a grand tour of the major operating systems components To provide coverage of basic computer system organization

Network Programming with Sockets. Process Management in UNIX

Networks. Inter-process Communication. Pipes. Inter-process Communication

Green Telnet. Making the Client/Server Model Green

Making Linux Safe for Virtual Machines

Introduction. What is an Operating System?

Lecture 1 Operating System Overview

Systemnahe Programmierung KU

OPERATING SYSTEMS STRUCTURES

User Mode Linux Linux inside Linux inside Linux inside...

Assignment 5: Adding and testing a new system call to Linux kernel

Chapter 12 File Management. Roadmap

Chapter 12 File Management

A Survey of Parallel Processing in Linux

Xen and XenServer Storage Performance

SYSTEM ecos Embedded Configurable Operating System

1. Introduction to the UNIX File System: logical vision

Chapter 12: Multiprocessor Architectures. Lesson 09: Cache Coherence Problem and Cache synchronization solutions Part 1

RTAI API Inter Process Communication

EXPLOITING SHARED MEMORY TO IMPROVE PARALLEL I/O PERFORMANCE

Transcription:

SHARED MEMORY and MEMORY MAPPED FILES ALLOW PROCESSES TO COMMUNICATE PROCESSES SHARE PORTIONS OF THEIR ADDRESS SPACE WHEN ONE WRITES, THA DATA IS IMMEDIATELY AVAILABLE TO OTHERS COMMUNICATION IS FAST NO SYSTEM CALLS, NO COPYING TO BUFFERS, NO OVERHEAD MECHANISMS FOR MEMORY-MAPPED FILES ALLOW TO ASSOCIATE A PROCESS'S ADDRESS SPACE DIRECTLY WITH A FILE. DATA MOVEMENT BETWEEN FILE AND MEMORY DOES NOT NEEDS FILE I/O FUNCTIONS (like ReadFile, WriteFile, SetFilePointer, or the others) WITH MEMORY-MAPPED FILES THE PROGRAM CAN MAINTAIN DYNAMIC DATA STRUCTURES AND MEMORY BASED ALGORITHMS CAN PROCESS FILE DATA. MEMORY MAPPING CAN SIGNIFICANTLY SPEED UP SEQUENTIAL FILE PROCESSING AND WHAT IS PERHAPS MORE IMPORTANT PROVIDES A MECHANISM FOR MEMORY SHARING BETWEEN PROCESSES. WHEN ONE USES A SHARED, MAPPED FILE, THE CHANGES INITIATED BY A SINGLE PROCESS OR MULTIPLE PROCESSES ARE REFLECTED BACK TU THE FILE. OTHER PROCESSES USING THE SAME PATH AND OPENING THE CONNECTION TO THE MEMORY OBJECT HAVE A SHARED MAPPING OF THE FILE. IF THE MAPPING ALLOW IT, THE DATA WRITTEN INTO THE FILE THROUGH THE ADDRESS SPACE OF ONE PROCESS APPEARS IN THE ADDRESS SPACE OF ALL PROCESSES MAPPING THE SAME PORTION OF THE FILE. MEMORY-MAPPED OBJECTS ARE PERSISTENT. THEIR NAMES AND CONTENTS REMAIN UNTIL ALL PROCESSES THAT HAVE ACCESSED THE OBJECT UNLINK THE FILE. 1/15

GENERAL SCHEME OF MEMORY OBJECT USAGE GET A FILE DESCRIPTOR: CALL TO THE OPEN OR SHM_OPEN FUNCTION MAP THE OBJECT USING MMAP FUNCTION (AND THE FILE DESCRIPTOR) UNMAP THE OBJECT USING THE MUNMAP FUNCTION CLOSE THE OBJECT WITH THE CLOSE FUNCTION REMOVE THE SHARED-MEMORY OBJECT WITH A CALL TO THE SHM_UNLINK FUNCTION OR - OPTIONALLY REMOVE A MEMORY-MAPPED FILE WITH A CALL TO THE UNLINK FUNCTION THE UNLINK AND SHM_UNLINK REMOVE (DELETE) THE FILE AND ITS CONTENTS THEREFORE TO SAVE A SHARED FILE, CLOSE THE FILE BUT NOT UNLINK IT! (THE MEMEORY-MAPPED FILES MAY BE USED WITHOUT MEMORY SHARING BUT IN THE CONTEXT OF THE INTER PROCESS COMMUNICATION IT DOES NOT HAVE MUCH SENS). 2/15

... BUT A DIGRESSION... The mmap() function allows access to resources via address space manipulations, instead of read()/ write(). Once a file is mapped, all a process has to do to access it is use the data at the address to which the file was mapped. So, using pseudo-code to illustrate the way in which an existing program might be changed to use mmap(), the following: fildes = open(...) lseek(fildes, some_offset) read(fildes, buf, len) /* Use data in buf. */ becomes: fildes = open(...) address = mmap(0, len, PROT_READ, MAP_PRIVATE, fildes, some_offset) /* Use data at address. */ 3/15

BACK TO SHARED MEMORY OBJECTS AND MEMORY MAPPED FILES... Functions: shm_open - opens a shared memory object, returns a file descriptor shm_unlink - removes the name of the shared memory object int shm_open(const char *name, int oflag, mode_t mode); O_FLAGS: O_RDONLY - open for read access only O_RDWR - open for read and write access O_CREAT - create the shared-memory object, if it does not already exist O_EXCL - create an exclusive connection (when or-ed with O_CREAT) O_TRUNC - truncate to zero length 4/15

MEMORY-MAPPING FUNCTIONS mmap - maps the memory object into memory mprotect modifies protection of memory objects msync - synchronizes a memory-mapped object munmap unmaps a previously mapped region Child processes inherit the address space and all mapped regions of the parent. Once the object is opened, the child process can map it with the mmap function to establish a map reference. (If object is already mapped, the child also inherits the mapped region). URELATED processes CAN ALSO use the object, but must first call the open or shm_open function to establish connection to the shared memory. Side remark: open is often done outside a real time section of a application 5/15

MEMORY-MAPPING FUNCTIONS --- prot, flags PROT_READ - data can be read PROT_WRITE - data can be written PROT_EXEC - data can be executed PROT_NONE - data cannot be accessed flags: MAP_SHARED - specifies that modifications made to the mapped file region are immediately visible to the other processes which are mapped to the same region and also use the MAP_SHARED flag. Changes to the region are written to the file. MAP_PRIVATE - modification to the region are not visible to other processes whether or not the other processes used MAP_SHARED or MAP_PRIVATE. Modifications to the region are not written to the file. MAP_FIXED - the call to the mmap function can specify the address and subsequent calls can use MAP_FIXED to request the same address in other process. No matter what flag is specified, a mapped region is never placed at address zero or at address, where it would overlap with an existing region. 6/15

FILE FUNCTIONS used with MEMORY-MAPPED FUNCTIONS fchmod - changes permissions on files fcntl - controls operations on files and memory objects flock - locks a file as shared or exclusive fstat - provides information about file status ftruncate - sets the length of a memory object One can apply a lock to a shared memory object or mapped file by variety of file control functions, including fcntl i flock... Note that the locks applied with these functions are for files, not file descriptors. That means that under most circumstances, file locks are not inherited across the fork. If a parent process holds a lock on a file and the parent process forks, the child process will inherit the file description, but not the lock on the file. The msync function synchronizes the caching operations of a memory-mapped file or shared memory region. Using this function ensure that modified pages in mapped region are transfered to the file's underlying storage device or control the visibility of modifications with respect to the system operations. 7/15

CONTROLLING MEMORY-MAPPED FUNCTIONS (cont.) Flags used on the msync function specify whether the cache flush is to be synchronous (MS_SYNC), asynchronous (MS_ASYNC), or invalidated (MS_INVALIDATE). Either the MS_SYNC or MS_ASYNC flag can be specified, but not both. When MS_SYNC flag is used, the msync function does not return until all write operations are complete and integrity of the data is assured. All previous modifications to the mapped region are visible to processes using the read parameter. When one uses MS_ASYNC flag, the msync function returns immediately after all of the write opertaions are scheduled. When using msync, one should use pages within the same address and length as specified in the call to the mmap function to ensure that the entire mapped region is synchronized. (Similarly: mprotect, which changes the file protection). Note that use of the mprotect modifies access only to the specified region. 8/15

MEMORY LOCKING AND UNLOCKING FUNCTIONS SOME REAL TIME APPLICATIONS MAY LOCK AND UNLOCK MEMORY AS THE APPLICATION RUNS MEMORY LOCKING APPLIES TO A PROCESS'S ADDRESS SPACE. ONLY PAGES MAPPED INTO A PROCESS'S ADDRESS SPACE CAN BE LOCKED INTO MEMORY. mlock LOCKS A SPECIFIED REGION OF A PROCESS'S ADDRESS SPACE mlockall LOCKS ALL PROCESS'S ADDRESS SPACE munlock munlockall SUPERUSER PRIVILAGES ARE REQUIRED TO CALL THAT mlock ( addr, size) munlock ( addr, size) ADDRESS MUST BE ON A PAGE BOUNDARY. IF ADDRESS AND SIZE ARGUMENTS SPECIFY AN AREA SMALLER THAN A PAGE, THE KERNEL ROUNDS UP THE AMOUNT OF LOCKED MEMORY TO THE NEXT PAGE. REPEATED CALLS TO mlock COULD REQUEST MORE PHYSICAL MEMORY THAN AVAILABLE. PROCESS MUST WAIT! FOR REALTIME APPLICATIONS IT MAY UN-TOLERABLE. IF PROCESS REQUESTS MORE LOCKED MEMORY THAN WILL EVER BE AVAILABLE = ERROR 9/15

MEMORY LOCKING AND UNLOCKING FUNCTIONS (cont.) LOCKING AND UNLOCKING ENTIRE PROCESS SPACE mlockall ( MCL_CURRENT) mlockall ( MCL_FUTURE) mlockall ( MCL_CURRENT MCL_FUTURE) IF MCL_CURRENT IS SPECIFIED, ALL CURRENTLY MAPPED PAGES OF PROCESS'S ADDRESS SPACE ARE MEMORY-RESIDENT AND LOCKED. subsequent GROWTH IN ANY AREA OF THE SPECIFIED REGION IS NOT LOCKED! IF MCL_FUTURE IS SPECIFIED, ALL FUTURE PAGES ARE LOCKED IN MEMORY. IF BOTH FLAGS ARE SPECIFIED (Ored) THEN THE CURRENT PAGES ARE LOCKED AND SUBSEQUENT GROWTH IS AUTOMATICALLY LOCKED TOO. 10/15

Shared memory - /* Example of an exclusive access of write and read processes to the sharedmemory region ensured by use of a semaphore */ /* writer.c */ #include <sys/types.h> #include <stdio.h> #include <errno.h> #include <fcntl.h> #include <unistd.h> #include <sys/mman.h> #include <semaphore.h> #include <sys/stat.h> char char shm_fn[] = my_shm ; sem_fn[] = my_sem ; /***** WRITER *****/ main(){ caddr_t shmptr; unsigned int mode; int shmdes, index; sem_t *semdes; int SHM_SIZE; 11/15

mode = S_IRWXU S_IRWXG; /* Open the shared memory object */ if ((shmdes = shm_open(shm_fn, O_CREAT O_RDWR O_TRUNC, mode )) == -1) { perror( shm_open failure ); exit(); /* Preallocate a shared memory area */ SHM_SIZE = sysconf( SCPAGE_SIZE ); if( ftruncate(shmdes, SHM_SIZE) == -1) { perror( ftruncate failure 0; exit(); if(( shmptr = mmap(0, SHM_SIZE, PROT_WRITE PROT_READ, MAP_SHARED, shmdes, 0)) == (caddr_t) -1) { perror( mmap failure ); exit(); /* Create a semaphore in locked state */ sem_des=sem_open(sem_fn, O_CREAT, 0644, 0); if(sem_des == (void*) -1) { perror( sem_open failure ); exit(); 12/15

/* Access to the shared memory area */ for (index=0; index<100; index++) {printf( write %d into the shared memory shmpr[%d]\n, index*2, index); shmptr[index]=index*2; /* Release the semaphore lock */ sem_post (semdes); munmap(shmptr, SHM_SIZE); /* Close the shared memory object */ close(shmdes); /* Close the Semaphore */ sem_close(semdes); /* Delete the shared memory object */ shm_unlink(shm_fn); /************************************************************************/ /******* reader.c *************************************/ #include <sys/types.h> #include <errno.h> #include <fcntl.h> 13/15

#include #include #include <sys/mman.h> <semaphore.h> <sys/stat.h> char char shm_fn[] = my_shm ; sem_fn[] = my_sem ; /***** READER *****/ main(){ caddr_t shmptr; int shmdes, index; sem_t *semdes; int SHM_SIZE; /* Open the shared memory object */ SHM_SIZE = sysconf( SCPAGE_SIZE ); if ((shmdes = shm_open(shm_fn, O_RDWR, 0 )) == -1) { perror( shm_open failure ); exit(); if(( shmptr = mmap(0, SHM_SIZE, PROT_WRITE PROT_READ, MAP_SHARED, shmdes, 0)) == (caddr_t) -1) { perror( mmap failure ); exit(); 14/15

/* Open the Semaphore */ sem_des=sem_open(sem_fn, 0, 0644, 0); if(sem_des == (void*) -1) { perror( sem_open failure ); exit(); /* Lock the Semaphore */ if(!sem_wait(semdes)) { /* Access to the shared memory area */ for(index=0; index<100; indexx++) printf( The shared memory shmptr[%d] = %d\n, index, shmptr[index]); /* Release the semaphore lock */ sem_post(shmdes); munmap(shmptr, SHM_SIZE); /* Close the shared memory objest */ close(shmdes); /* Close the Semaphore */ sem_close(semdes); sem_unlink(sem_fn); 15/15