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



Similar documents
Critical section problem (repetition)

Shared Memory Segments and POSIX Semaphores 1

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

System Calls and Standard I/O

BSc (Hons) Business Information Systems, BSc (Hons) Computer Science with Network Security. & BSc. (Hons.) Software Engineering

Illustration 1: Diagram of program function and data flow

Unix System Calls. Dept. CSIE

Testing Database Performance with HelperCore on Multi-Core Processors

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

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

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

System Calls Related to File Manipulation

The C Programming Language course syllabus associate level

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

Lecture 25 Systems Programming Process Control

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

Data Types in the Kernel

Module 816. File Management in C. M. Campbell 1993 Deakin University

How To Write Portable Programs In C

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

Shared Address Space Computing: Programming

Molecular Dynamics Simulations with Applications in Soft Matter Handout 7 Memory Diagram of a Struct

Linux Kernel Architecture

Lecture 24 Systems Programming in C

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

Interprocess Communication in Java

1 Abstract Data Types Information Hiding

1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++

Introduction. dnotify

Embedded Systems. Review of ANSI C Topics. A Review of ANSI C and Considerations for Embedded C Programming. Basic features of C

Program 5 - Processes and Signals (100 points)

Chapter 6, The Operating System Machine Level

SMTP-32 Library. Simple Mail Transfer Protocol Dynamic Link Library for Microsoft Windows. Version 5.2

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

Keil C51 Cross Compiler

NDK: NOVELL NSS AUDIT

Lecture 5: Java Fundamentals III

MAX = 5 Current = 0 'This will declare an array with 5 elements. Inserting a Value onto the Stack (Push)

Operating Systems and Networks

2 ASCII TABLE (DOS) 3 ASCII TABLE (Window)

Linux Kernel Networking. Raoul Rivas

Process definition Concurrency Process status Process attributes PROCESES 1.3

Parallelization: Binary Tree Traversal

Programming languages C

Sources: On the Web: Slides will be available on:

Output: struct treenode{ int data; struct treenode *left, *right; } struct treenode *tree_ptr;

A Survey of Parallel Processing in Linux

Mouse Drivers. Alan Cox.

Jorix kernel: real-time scheduling

Project No. 2: Process Scheduling in Linux Submission due: April 28, 2014, 11:59pm

Integrating VoltDB with Hadoop

The programming language C. sws1 1

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

Sistemi Operativi. Lezione 25: JOS processes (ENVS) Corso: Sistemi Operativi Danilo Bruschi A.A. 2015/2016

Green Telnet. Making the Client/Server Model Green

Virtuozzo Virtualization SDK

Generalised Socket Addresses for Unix Squeak

An Incomplete C++ Primer. University of Wyoming MA 5310

Session NM059. TCP/IP Programming on VMS. Geoff Bryant Process Software

The Linux Kernel: Process Management. CS591 (Spring 2001)

Priority Based Implementation in Pintos

DESIGN: A Program to Create Data Entry Programs

ASCII Encoding. The char Type. Manipulating Characters. Manipulating Characters

The following themes form the major topics of this chapter: The terms and concepts related to trees (Section 5.2).

Informatica e Sistemi in Tempo Reale

GDB Tutorial. A Walkthrough with Examples. CMSC Spring Last modified March 22, GDB Tutorial

Outline of this lecture G52CON: Concepts of Concurrency

Tutorial on C Language Programming

Dalhousie University CSCI 2132 Software Development Winter 2015 Lab 7, March 11

1 Posix API vs Windows API

Introduction to UNIX System Programming

CMPT 373 Software Development Methods. Building Software. Nick Sumner Some materials from Shlomi Fish & Kitware

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

## Remove any existing rules -D

Programmation Systèmes Cours 7 IPC: FIFO

An Introduction To Simple Scheduling (Primarily targeted at Arduino Platform)

Channel Access Client Programming. Andrew Johnson Computer Scientist, AES-SSG

TivaWare Utilities Library

Xillybus host application programming guide for Linux

Chapter 13 - The Preprocessor

Lecture 22: C Programming 4 Embedded Systems

Introduction to Java

What is COM/DCOM. Distributed Object Systems 4 COM/DCOM. COM vs Corba 1. COM vs. Corba 2. Multiple inheritance vs multiple interfaces

Implementing and testing tftp

REAL TIME SYSTEMS. Piotr MALECKI

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

Scoping (Readings 7.1,7.4,7.6) Parameter passing methods (7.5) Building symbol tables (7.6)

File Handling. What is a file?

Programming Languages CIS 443

How To Port A Program To Dynamic C (C) (C-Based) (Program) (For A Non Portable Program) (Un Portable) (Permanent) (Non Portable) C-Based (Programs) (Powerpoint)

Data Structure with C

/* File: blkcopy.c. size_t n

Visual Basic Programming. An Introduction

Fortify on Demand Security Review. Fortify Open Review

Lab 2 : Basic File Server. Introduction

OpenGL Insights. Edited by. Patrick Cozzi and Christophe Riccio

CPSC 2800 Linux Hands-on Lab #7 on Linux Utilities. Project 7-1

PetaLinux SDK User Guide. Application Development Guide

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

Transcription:

IPC Two processes will use shared memory to communicate and some mechanism for synchronise their actions. This is necessary because shared memory does not come with any synchronisation tools: if you can access it at all, you can access it anytime regardless of the other process. Semaphores were chosen for synchronisation (out of several options). Note that there is a much simpler way to implement a solution to the producer consumer (with confirmations) problem: two message queues. 1

References Beej s guide to shared memory Beej s guide to semaphores Linux guide (sketchy) Marshall s guide to shared memory Marshall s guide to semaphores Another attempt to explain shared memory 2

IPC identifier Each IPC object is identified by two labels: key: which is an external identifier. All the processes that want a given object must produce the one and only key that allows access to it. identifier: once a key was accepted, a process will refer to an IPC object by an internal id (an object will have a different id in each process using it). A key identifies uniquely an object in a particular IPC domain but can be used simultaneously in several domains. 3

There are two ways of getting a key: Invent one Pick any number such as 4561111 and use it. As long as nobody picks the same number, you are fine (the number above should be avoided: it is the telephone of the White House). Get one from the system A special system call ftok() exists solely to give you a unique key based on two arguments: key = ftok( char, char ) ; where the first argument is the path of any existing (and accessible) file in the system and the second argument is a number between 0 and 255 (every character has this property). domains. 4

Acquiring some shared memory You already have your favourite key and you want a shared memory area of size bytes with access rights shmflg. A call like this will create one: if ((shmid = shmget (key, size, shmflg )) == 1) { perror("shmget failed"); exit( 1); The access rights could be: 0666 (anybody can do anything) plus create or fail: shmflg = 0666 IPC CREAT IPC EXCL ; It could also be: 0640 (the owner can do anything, group members may read) plus create or fail: shmflg = 0640 IPC CREAT IPC EXCL ; 5

shmget() got you a shared memory identifier but it is not a pointer to a location in memory and thus is useless in itself. What you need is a pointer that you can use to access the memory area: shmptr = shmat( shmid, mychoice, 0 ) ; if( shmptr == (char ) 1 ) { perror( "shmat" ) ; exit( 1 ) ; The second argument mychoice is almost always 0; if not, it asks that the value returned by shmat be equal to this argument, if possible. The last argument gives access rights again. Now you can access the shared memory using the pointer provided by shmat(). 6

Here is some nonsensical code copied from Marshall: main() { char c; int shmid; key t key = 5678 ; char shm, s; if ((shmid = shmget(key, 27, IPC CREAT 0666)) < 0)... if ((shm = shmat(shmid, 0, 0)) == (char ) 1)... s = shm; for (c = 'a'; c <= 'z'; s++ = c++) ; s = NULL; while ( shm! = '*') sleep(1); 7

Creating a semaphore You cannot get one semaphore; you must ask for an array of them. The code below asks for nsems semaphores. if ((semid = semget(key, nsems, semflg)) == 1) { perror("semget failed"); exit( 1); You give a magic key which is the external name ( public name ) of your semaphore cluster and you provide flags that indicate what access permission you are willing to grant to users of this cluster. The standard permission is 0600 (I can read and write and nobody else can). The returned value is an identifier (not a pointer). A semaphore is a tightly controlled entity and you cannot simply access it directly as in: semid[2] = 0 ; This will not compile. If you want to set the third semaphore of the cluster identified by semid to 0, you must use the system call semctl() which has most obscure semantics. 8

Acquiring a semaphore (again) The key is the public name of the semaphore cluster you want; the same name will be used by all the processes that will share this semaphore cluster. Consider using the inode of the current directory (if you are there, it is accessible) to create 2 semaphores with the requirement that they must be brand new: key = ftok( ".", 'Q' ) ; semid = semget( key, 2, 0600 IPC CREAT IPC EXCL ) ; if( semid == 1) { perror( "semget refused" ); kill( getpid(), SIGINT ) ; The Q argument is an integer between 0 and 255 (as required). 9

Clean after your code void delete( int sig ) { printf( "Cleaning\n" ) ; shmctl( shmid, IPC RMID, 0 ) ; semctl( semid, IPC RMID, 0 ) ; exit( 0 ) ; void start( ) { signal( SIGINT, delete) ;...... 10

Semaphore operations Two operations are of real interest: semctl() allows to manipulate the values of semaphores. semop() provides the basic P and V operations on a semaphore. You can use semop() to define your own operations (such as a non blocking Poll()). 11

Basic use of semaphores P( semaphore ) ;... modify the shared memory... V( semaphore ) ; If the semaphore is properly initialised (to 1), the P operation will block any process that wants to touch the shared memory when another process is doing so. 12

void V( int s ) { struct sembuf S ; S.sem num = s ; S.sem op = 1 ; if( semop( semid, &S, 1 ) == 1 ) { perror( "V failed" ) ; kill( getpid(), SIGINT ) ; 13

P void P( int s ) { struct sembuf S ; S.sem num = s ; S.sem op = 1 ; while( semop( semid, &S, 1 ) == 1 ) { perror( "P failed" ) ; sleep( 1 ) ; This code assumes that semop() failed due to an interrupted system call ordue to a race condition (this is the purpose of the sleep()). It will not work if there is asynchronisation error; in such case, it will loop forever. 14

semun The system call semctl requires a union type called semun. Some systems have it defined in bits/sem.h (called inside sys/sem.h); other systems require that you define it yourself. #include<sys/sem.h> #ifdef SEM SEMUN UNDEFINED union semun { int val ; struct semid ds buf ; unsigned short array ; ; #endif // SEM SEMUN UNDEFINED Consult the file /usr/include/bits/sem.h for details. 15

Two non standard operations on semaphores: a non blocking Poll() and an initialisation function I(). int Poll( int s ) { return semctl( semid, s, GETVAL ) ; void I( int s ) { union semun arg ; arg.val = 1 ; // sets it to 1 if( semctl( semid, s, SETVAL, arg ) == 1 ) perror( "semctl" ) ; 16

struct VID { int code ; // the current state of this entry // = 0 empty (sem 0 == 1) // = 1 vid inside (sem 1 == 0) // = 2 confirmed vid (sem 1 == 0) pid t pid ; // Validator s pid int vid ; // the vid to be recorded ; int confirmation ; // passed back from Tallier 17

fh = fopen( "Booth pid pid", "w" ) ; if( fh == NULL ) perror( "Could not open the pid file" ) ; fprintf( fh, "%d\n", getpid() ) ; fclose( fh ) ; key = ftok( "/dev/null", 'B' ) ; if( (key t) key == 1 ) perror( "ftok" ) ; shmid = shmget( key,... shmptr = (struct VID )shmat( shmid, 0, 0 ) ; ) ; semid = semget( key, 2, 0600 IPC CREAT IPC EXCL 18