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



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

Process definition Concurrency Process status Process attributes PROCESES 1.3

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

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

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

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

CSC 2405: Computer Systems II

Linux Syslog Messages in IBM Director

Programmation Systèmes Cours 7 IPC: FIFO

Lecture 25 Systems Programming Process Control

Chapter 6, The Operating System Machine Level

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

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

Shared Address Space Computing: Programming

1 Posix API vs Windows API

Performance Evaluation and Optimization of A Custom Native Linux Threads Library

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

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

ARUNNING INSTANCE OF A PROGRAM IS CALLED A PROCESS. If you have two

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

Shared Memory Introduction

Linux Kernel Architecture

Processes and Non-Preemptive Scheduling. Otto J. Anshus

Operating Systems and Networks

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

System Calls and Standard I/O

Green Telnet. Making the Client/Server Model Green

Unix System Calls. Dept. CSIE

Operating Systems. Privileged Instructions

Time Limit: X Flags: -std=gnu99 -w -O2 -fomitframe-pointer. Time Limit: X. Flags: -std=c++0x -w -O2 -fomit-frame-pointer - lm

Program 5 - Processes and Signals (100 points)

Introduction. What is an Operating System?

Lab 4: Socket Programming: netcat part

Lecture 22: C Programming 4 Embedded Systems

Win32 API Emulation on UNIX for Software DSM

OS Thread Monitoring for DB2 Server

Creating a Simple, Multithreaded Chat System with Java

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

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

Debugging with TotalView

Far-western University Central Office, Mahendranagar Operating System

Lecture 17. Process Management. Process Management. Process Management. Inter-Process Communication. Inter-Process Communication

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

Jorix kernel: real-time scheduling

Kernel Synchronization and Interrupt Handling

I/O Device and Drivers

Real Time Programming: Concepts

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

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

Lecture 25 Symbian OS

CS161: Operating Systems

Monitors, Java, Threads and Processes

Named Pipes, Sockets and other IPC

This presentation explains how to monitor memory consumption of DataStage processes during run time.

Lecture 16: System-Level I/O

Massachusetts Institute of Technology 6.005: Elements of Software Construction Fall 2011 Quiz 2 November 21, 2011 SOLUTIONS.

Outline of this lecture G52CON: Concepts of Concurrency

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

Computer Lab Software Fault-tolerance: Task Process Pairs

How ToWrite a UNIX Daemon

Distributed Systems [Fall 2012]

CS 377: Operating Systems. Outline. A review of what you ve learned, and how it applies to a real operating system. Lecture 25 - Linux Case Study

An Implementation Of Multiprocessor Linux

Lesson-3 CASE STUDY OF AN EMBEDDED SYSTEM FOR SMART CARD

Comp151. Definitions & Declarations

TFTP Usage and Design. Diskless Workstation Booting 1. TFTP Usage and Design (cont.) CSCE 515: Computer Network Programming TFTP + Errors

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

TUTORIAL FOR INITIALIZING BLUETOOTH COMMUNICATION BETWEEN ANDROID AND ARDUINO

Chapter 11: Input/Output Organisation. Lesson 06: Programmed IO

COS 318: Operating Systems

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

CS355 Hw 3. Extended Shell with Job Control

Priority Inversion Problem and Deadlock Situations

First-class User Level Threads

SYSTEM ecos Embedded Configurable Operating System

Chapter 1 FUNDAMENTALS OF OPERATING SYSTEM

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

Lecture 5. User-Mode Linux. Jeff Dike. November 7, Operating Systems Practical. OSP Lecture 5, UML 1/33

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

Distributed Locking. User Guide. 2006, 2016 Zumasys, Inc.

CS414 SP 2007 Assignment 1

COS 318: Operating Systems. I/O Device and Drivers. Input and Output. Definitions and General Method. Revisit Hardware

Cloud9 Parallel Symbolic Execution for Automated Real-World Software Testing

Implementing and testing tftp

Introduction to Java

Why Threads Are A Bad Idea (for most purposes)

Recursion C++ DSP Toolkit Libraries DM6437 Cross Development

Below is a diagram explaining the data packet and the timing related to the mouse clock while receiving a byte from the PS-2 mouse:

System Administration

Operating Systems Concepts: Chapter 7: Scheduling Strategies

Last Class: Semaphores

1.00 Lecture 1. Course information Course staff (TA, instructor names on syllabus/faq): 2 instructors, 4 TAs, 2 Lab TAs, graders

Apache Thrift and Ruby

Shared Memory Segments and POSIX Semaphores 1

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

Transcription:

Outline Review Inter process communication Signals Fork Pipes FIFO Spotlights 1

6.087 Lecture 14 January 29, 2010 Review Inter process communication Signals Fork Pipes FIFO Spotlights 2

Review: multithreading Race conditions non-determinism in thread order. can be prevented by synchronization atomic operations necessary for synchronization Mutex: Allows a single thread to own it Semaphores: Generalization of mutex, allows N threads to acquire it at a time. P(s) : acquires a lock V(s) : releases lock sem_init(),sem_destroy() sem_wait(),sem_trywait(),sem_post() Other problems: deadlock, starvation 2

Sockets <sys/socket.h> enables client-server computing Client: connect() Server: bind(),listen(),accept() I/O: write(),send(),read(),recv() 3

6.087 Lecture 14 January 29, 2010 Review Inter process communication Signals Fork Pipes FIFO Spotlights 4

Preliminaries Each process has its own address space. Therefore, individual processes cannot communicate unlike threads. Interprocess communication: Linux/Unix provides several ways to allow communications signal pipes FIFO queues shared memory semaphores sockets 4

<signals.h> Unix/Linux allows us to handle exceptions that arise during execution (e.g., interrupt, floating point error, segmentation fault etc.). A process recieves a signal when such a condition occurs. void ( signal(int sig,void( handler)(int )))( int ) determines how subsequent signals will be handled. pre-defined behavior: SIG_DFL (default), SIG_IGN (ignore) returns the previous handler. 5

<signal.h> Valid signals: SIGABRT SIGFPE SIGILL SIGINT SIGSEGV SIGTERM SIGBUS SIGQUIT abnormal termination floating point error illegal instruction interrupt segmentation fault termination request bus error quit The two signals SIGSTOP,SIGKILL cannot be handled. 6

<signal.h> int raise( int sig) can be used to send signal sig to the program. Notes: There can be race conditions. signal handler itself can be interrupted. use of non-reentrant functions unsafe. sigprocmask can be used to prevent interruptions. handler is reset each time it is called. 7

Example #include < s t d i o. h> void sigproc ( ) { s i g n a l ( SIGINT, sigproc ) ; / / p r i n t f ( " you have pressed c t r l c \ n " ) ; void quitproc ( ) { p r i n t f ( " c t r l \\ pressed to q u i t " ) ; e x i t ( 0 ) ; / normal e x i t s t a t u s / main ( ) { s i g n a l ( SIGINT, sigproc ) ; signal ( SIGQUIT, quitproc ) ; p r i n t f ( c t r l c disabled use c t r l \\ to q u i t n ) ; for ( ; ; ) ; / i n f i n i t e loop / 8

Fork pid_t fork (void) fork() is a system call to create a new process In the child process, it returns 0 In the parent process, it returns the PID (process id) of the child. The child PID can be used to send signals to the child process. returns -1 on failure (invalid PID) 9

Example #include < s t d l i b. h> #include < s t d i o. h> i n t main ( ) { / some code / p i d _ t pid= f o r k ( ) ; i n t i ; i f ( pid ) { for ( i =0; i <5; i ++){ sleep ( 2 ) ; p r i n t f ( " parent process:%d \ n ", i ) ; else { for ( i =0; i <5; i ++){ sleep ( 1 ) ; p r i n t f ( " c h i l d process:%d \ n ", i ) ; / end c h i l d / / end main / parent process :0 c h i l d process :1 c h i l d process :2 parent process :1 c h i l d process :3 c h i l d process :4 parent process :2 parent process :3 parent process :4 10

Fork fork() makes a full copy of the parents address space. pid_t getpid() returns PID of the current process. pid_t getppid() returns PID of the parent process. wait(int ) is used to wait for the child to finish. waitpid () is used to wait for a specific child. Zombies: the child process can exit before the parent stray process is marked as <defunct> preap can be used to reap zombie processes. 11

Pipes Pipes are used in unix to redirect output of one command to another. Pipes also allow parent processes to communicate with its children. Examples ls more - displays results of ls one screen at a time cat file.txt sort -displays contents of file.txt in sorted order int pipe(int FILEDES[2]) A pipe can be thought of as a pair of file descriptors no physical file is associated with the file descriptor one end is opened in write mode. other end is opened in read mode. 12

Example / source : http : / / beej. us / guide / #include <stdio. h> #include < s t d l i b. h> #include <errno. h> #include <sys / types. h> #include <unistd. h> / ipc / i n t main ( void ) { i n t pfds [ 2 ] ; char buf [ 3 0 ] ; pipe ( pfds ) ; i f (! f o r k ( ) ) { p r i n t f ( " CHILD : w r i t i n g to the pipe \ n " ) ; w r i t e ( pfds [ 1 ], " t e s t ", 5 ) ; p r i n t f ( " CHILD : e x i t i n g \ n " ) ; e x i t ( 0 ) ; else { p r i n t f ( "PARENT: reading from pipe \ n " ) ; read ( pfds [ 0 ], buf, 5 ) ; p r i n t f ( "PARENT: read \"%s \ " \ n ", buf ) ; wait (NULL ) ; return 0; 13

FIFO FIFO queues may be thought of as named pipes. Multiple processes can read and write from a FIFO. Unlike pipes, the processes can be unrelated. FIFOs can be created using mknod system call. int mknod (const char path,mode_t mode,dev_t dev) <sys/stat.h> contains the declaration for mknod. mknod used to create special files - devices,fifos etc. mode can have special bits such as S_IFIFO 0644 dev is interpreted based on the mode. Example: mknod("myfifo", S_IFIFO 0644, 0); 14

Example / source : h t t p : / / beej. us / guide / #include < s t d i o. h> #include < s t d l i b. h> #include < s t r i n g. h> #include <sys / s t a t. h> #include < u n i s t d. h> #define FIFO_NAME " f i f o " i n t main ( void ) { char s [ 3 0 0 ] ; i n t num, fd ; mknod (FIFO_NAME, S_IFIFO 0666, 0 ) ; p r i n t f ( " w a i t i n g f o r readers... \ n " ) ; fd = open (FIFO_NAME, O_WRONLY) ; p r i n t f ( " got a reader \ n " ) ; while ( gets ( s ),! f e o f ( s t d i n ) ) { num = w r i t e ( fd, s, s t r l e n ( s ) ) ; i f (num == 1) p e r r o r ( " w r i t e " ) ; else p r i n t f ( " wrote %d bytes \ n ", num ) ; return 0; #include < s t d i o. h> #include < s t d l i b. h> #include <errno. h> #include < s t r i n g. h> #include < f c n t l. h> #include <sys / types. h> #include <sys / s t a t. h> #include < u n i s t d. h> #define FIFO_NAME " f i f o " i n t main ( void ) { char s [ 3 0 0 ] ; i n t num, fd ; mknod (FIFO_NAME, S_IFIFO 0666, 0 ) ; p r i n t f ( " w a i t i n g f o r w r i t e r s... \ n " ) ; fd = open (FIFO_NAME, O_RDONLY) ; p r i n t f ( " got a w r i t e r \ n " ) ; do { num = read ( fd, s, 300); i f (num == 1) p e r r o r ( " read " ) ; else { s [num ] = \0 ; p r i n t f ( " read %d bytes :\"% s \ " \ n ", num, s ) ; while (num > 0 ) ; return 0; 15

6.087 Lecture 14 January 29, 2010 Review Inter process communication Signals Fork Pipes FIFO Spotlights 16

Project spotlights Face finding with opencv Barcode scanner ImageIC Image2DXF Library database Simple Audio Visualizer Non-linear oscillator NoteDeluxe CUDA Visual mouse Wallpaper downloader 16

MIT OpenCourseWare http://ocw.mit.edu 6.087 Practical Programming in C January (IAP) 2010 For information about citing these materials or our Terms of Use,visit: http://ocw.mit.edu/terms.