Embedded System Design

Similar documents
Data and Computer Communications. Tenth Edition by William Stallings

Java Basics 5 - Sockets. Manuel Oriol - May 4th, 2006

Piotr Mielecki Ph. D.

CS307 Operating Systems Processes

Processes. Process Concept. The Process. The Process (Cont.) Process Control Block (PCB) Process State

Operating Systems. Lecture 06. System Calls (Exec, Open, Read, Write) Inter-process Communication in Unix/Linux (PIPE), Use of PIPE on command line

CISC2200 Threads Spring 2015

Inter-Process Communication. Disclaimer: some slides are adopted from the book authors slides with permission 1

Network Programming Worksheet 2. Simple TCP Clients and Servers on *nix with C.

NETWORK PROGRAMMING. Instructor: Junaid Tariq, Lecturer, Department of Computer Science

CSE 421/521 - Operating Systems Fall 2011 Recitations. Recitation - III Networking & Concurrent Programming Prof. Tevfik Kosar. Presented by...

CSE 333 Section 8 - Client-Side Networking

Chapter 8: I/O functions & socket options

Introduction to Client-Server Model

Internet protocol stack. Internetworking II: Network programming. April 20, UDP vs TCP. Berkeley Sockets Interface.

Sockets. Dong-kun Shin Embedded Software Laboratory Sungkyunkwan University Embedded Software Lab.

Internetworking II: Network programming. April 20, 2000

ECE 598 Advanced Operating Systems Lecture 23

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

Process Synchronization

Operating Systems. VI. Threads. Eurecom. Processes and Threads Multithreading Models

CS 471 Operating Systems. Yue Cheng. George Mason University Fall 2017

ECE 650 Systems Programming & Engineering. Spring 2018

OS COMPONENTS OVERVIEW OF UNIX FILE I/O. CS124 Operating Systems Fall , Lecture 2

ECE 435 Network Engineering Lecture 2

CS 43: Computer Networks. 07: Concurrency and Non-blocking I/O Sep 17, 2018

ECE 435 Network Engineering Lecture 2

CSE 153 Design of Operating Systems Fall 2018

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

CSE 333 SECTION 3. POSIX I/O Functions

Client-server model The course that gives CMU its Zip! Network programming Nov 27, Using ports to identify services.

Inter-Process Communication. Disclaimer: some slides are adopted from the book authors slides with permission 1

Outline. OS Interface to Devices. System Input/Output. CSCI 4061 Introduction to Operating Systems. System I/O and Files. Instructor: Abhishek Chandra

Hyo-bong Son Computer Systems Laboratory Sungkyunkwan University

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

Sockets. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

CSE 333 SECTION 7. Client-Side Network Programming

Processes. Johan Montelius KTH

chat.h #ifndef _CHAT_H #define _CHAT_H #define LBUFFSIZE 128 #define CBUFFSIZE 331

A process. the stack

CS 471 Operating Systems. Spring 2016 Midterm Exam Time 2 & 1/2 hours. Open Book & Notes

Processes. CSE 2431: Introduction to Operating Systems Reading: Chap. 3, [OSC]

CSE 333 SECTION 7. C++ Virtual Functions and Client-Side Network Programming

Processes (Tasks) and operating systems. Why multiple processes? Example: engine control

518 Lecture Notes Week 3

Part I: Network protocols: HTTP and beyond

PA #2 Reviews. set_name, get_name, del_name. Questions? Will be modified after PA #4 ~

CS321: Computer Networks Socket Programming

Unix Network Programming Chapter 4. Elementary TCP Sockets 광운대학교컴퓨터과학과 정보통신연구실 석사과정안중현

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

Processes COMPSCI 386

LSN 13 Linux Concurrency Mechanisms

Problem 1: Concepts (Please be concise)

Computer Systems Laboratory Sungkyunkwan University

Interprocess Communication. Originally multiple approaches Today more standard some differences between distributions still exist

CS 3305 Intro to Threads. Lecture 6

socketservertcl a Tcl extension for using SCM_RIGHTS By Shannon Noe - FlightAware

Sockets. Dong-kun Shin Embedded Software Laboratory Sungkyunkwan University Embedded Software Lab.

CS321: Computer Networks Introduction to Application Layer

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

Definition Multithreading Models Threading Issues Pthreads (Unix)

Thread. Disclaimer: some slides are adopted from the book authors slides with permission 1

Lecture 4 Threads. (chapter 4)

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

Application Programming Interfaces

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

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

Multithreaded Programming

Operating Systems 2010/2011

CSE 333 SECTION 8. Sockets, Network Programming

EE 122: Sockets. Motivation. Sockets. Types of Sockets. Kevin Lai September 11, 2002

CSC209H Lecture 10. Dan Zingaro. March 18, 2015

CSE 333 Lecture 16 - network programming intro

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

MSc Integrated Electronics Networks Assignment. Investigation of TCP/IP Sockets and Ports. Gavin Cameron

Socket Programming for TCP and UDP

UNIX Network Programming. Overview of Socket API Network Programming Basics

Recitation 14: Proxy Lab Part 2

File I/0. Advanced Programming in the UNIX Environment

CSE 333 SECTION 6. Networking and sockets

Outline. Operating Systems. Socket Basics An end-point for a IP network connection. Ports. Network Communication. Sockets and the OS

Lecture 5 Overview! Last Lecture! This Lecture! Next Lecture! I/O multiplexing! Source: Chapter 6 of Stevens book!

CS370 Operating Systems

Process Management! Goals of this Lecture!

Part Two - Process Management. Chapter 3: Processes

Announcement (1) Due date for PA3 is changed (~ next week) PA4 will also be started in the next class. Not submitted. Not scored

Outline. Distributed Computer Systems. Socket Basics An end-point for a IP network connection. Ports. Sockets and the OS. Transport Layer.

Chapter 3. File I/O. System Programming 熊博安國立中正大學資訊工程學系

CMPS 105 Systems Programming. Prof. Darrell Long E2.371

Operating Systemss and Multicore Programming (1DT089)

CSC209: Software tools. Unix files and directories permissions utilities/commands Shell programming quoting wild cards files

CSC209: Software tools. Unix files and directories permissions utilities/commands Shell programming quoting wild cards files. Compiler vs.

Inter-Process Communication

Signal Example 1. Signal Example 2

Chapter 4: Processes. Process Concept

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

Introduction. Interprocess communication. Terminology. Shared Memory versus Message Passing

Chapter 4: Processes

CSCE 313 Introduction to Computer Systems. Instructor: Dezhen Song

CSE 333 SECTION 3. POSIX I/O Functions

Transcription:

Embedded System Design Lecture 9 Jaeyong Chung Robust Systems Laboratory Incheon National University

Inter-process Commnucation (IPC) Chung EPC6071 2

Inter-process Commnucation (IPC) A set of methods for the exchange of data among processes. Processes may be running on one or more computers connected by a network. Mechanism for processes to communicate and to synchronize their actions IPC methods are divided into methods for message passing (like SMS), synchronization, shared memory (like boards on web), remote procedure calls Main IPC methods File, Signal, Socket, Message Queue, Pipe, Semaphore, Shard memory Chung EPC6071 3

Message Passing Message system processes communicate with each other without resorting to shared variables. It can also provides synchronization Two operations read, write If P and Q wish to communicate, they need to: establish a communication link between them exchange messages via send/receive Message passing may be either blocking or nonblocking Blocking is considered synchronous Blocking send has the sender block until the message is received Blocking receive has the receiver block until a message is available Chung EPC6071 4

Blocking/Non-blocking Message passing may be either blocking or nonblocking Blocking is considered synchronous Blocking send has the sender block until the message is received Blocking receive has the receiver block until a message is available Non-blocking is considered asynchronous Non-blocking send has the sender send the message and continue Non-blocking receive has the receiver receive a valid message or null Chung EPC6071 5

More on File IO read Function Returns: number of bytes read if OK, 0 if end of file, - 1 on error #include <unistd.h> ssize_t read(int filedes, void *buff, size_t nbytes); Process/thread blocks until some data is read Note that 0 indicates end of file Chung EPC6071 6

More on File IO write Function #include <unistd.h> ssize_t write(int filedes, const void *buff, size_t nbytes); Returns: number of bytes written if OK, -1 on error Process/thread blocks until some data is written Chung EPC6071 7

(IPC Method 1) Pipe IPC method based on message passing A two-way data stream that is read character by character. For processes in the same machine Use read() and write() system calls Once communication links are set up, reading/writing data is the same as doing it to a file Every process has three default pipes Stdin (filedesc: 0), stdout (filedes: 1), stderr (filedesc:2 ) By default, these pipes are connected to terminal program Chung EPC6071 8

(IPC Method 1) Pipe Redirect stdout to a file./program > output.txt Redirect stderr to a file./program 2> errors.txt Redirect stderr to stdout./program 2>&1 Chung EPC6071 9

(IPC Method 1) Pipe Redirect stdout to stdin of another program./program1./program2 Chain of programs./program1./program2./program3 Chung EPC6071 10

Activity Create a file that contains all file names in CWD Create a file that contains the list of current processes owned by chung A program (./a.out) displays a lot of messages during the execution. Kick off the program so that it displays messages contatining WARNING string Chung EPC6071 11

(IPC Method 2) Socket Programming IPC method based on message passing A two-way data stream that is read character by character. For processes in different machines Use read() and write() system calls Once communication links are set up, reading/writing data is the same as doing it to a file Chung EPC6071 12

(IPC Method 2) Socket Programming int main(int argc, char *argv[]) { int sockfd, portno, n; struct sockaddr_in serv_addr; struct hostent *server; char buffer[256]; if (argc < 3) { fprintf(stderr,"usage %s hostname port\n", argv[0]); } exit(0); portno = atoi(argv[2]); sockfd = socket(af_inet, SOCK_STREAM, 0); if (sockfd < 0) error("error opening socket"); server = gethostbyname(argv[1]); if (server == NULL) { } fprintf(stderr,"error, no such host\n"); exit(0); serv_addr.sin_family = AF_INET; } bcopy((char *)server->h_addr, (char *)&serv_addr.sin_addr.s_addr, server->h_length); serv_addr.sin_port = htons(portno); if (connect(sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0) error("error connecting"); printf("please enter the message: "); bzero(buffer,256); fgets(buffer,255,stdin); n = write(sockfd,buffer,strlen(buffer)); if (n < 0) error("error writing to socket"); bzero(buffer,256); n = read(sockfd,buffer,255); if (n < 0) error("error reading from socket"); printf("%s\n",buffer); return 0; Chung EPC6071 13

Activity Write a function that must read a number of bytes desired from a socket, a pipe, a file, or so on. It blocks until the number of bytes is read Chung EPC6071 14

(IPC Method 3) Signal A limited form of inter-process communication used in Unix, Unix-like, and other POSIX-compliant operating systems A signal is an asynchronous notification sent to a process in order to notify it of an event that occurred When a signal is sent, the operating system interrupts the target process s normal flow of execution to deliver the signal. If the process has previously registered a signal handler, that routine is executed Analogous to an interrupt, but it is entirely a software creation kill() system calls $ kill 9 pid Chung EPC6071 15

(IPC Method 4) Shared Memory Processes have some memory in common Implemented by page sharing Synchronization using shared memory can be problematic Polling Race condition: The output is dependent on the sequence or timing of other uncontrollable events. [Silberschartz et al] Chung EPC6071 16

Blocking I/O Issues Case 1) I am writing a shooting-game program. I need to update positions of enemies and read inputs from keyboard at the same time. But reading from keyboard blocks the process Case 2) I am writing a chatting server. What happens if the server process waits for a client s message and it is not sent for a while? Solution 1: Non-blocking I/O Solution 2: Blocking I/O + Concurrent Programming Chung EPC6071 17

Non-blocking I/O Make read() return immediately If you have your own device driver, change your device in such way In general, Issues: int flags = fcntl(fd, F_GETFL, 0); fcntl(fd, F_SETFL, flags O_NONBLOCK); Busy-waiting: waste CPU time To relieve this, add sleep(), but this hurts response time Fairness Complexity: Applications play some of OS es roles Chung EPC6071 18

Concurrent Programming A CPU executes a program Multiple CPUs can execute a program in parallel This can be emulated in a single CPU system by timesharing May be considered as CPU virtualization A virtual CPU has its own PC value, register values, etc Multiple threads/processes can execute a program in parallel A thread is specified by PC value, register values, etc Chung EPC6071 19

Concurrent Programming Multi-processes (heavy) Each process has its own virtual address space Variables are not shared unless using shared memory IPC may complicate programming Multi-threading (light) Each thread share one virtual address space Global variables are shared To allow threads to access the same variables, synchronization through mutex is needed which complicates programming Race conditions are hard to debug Chung EPC6071 20

Activity Write an example of the output when the following program is executed by two threads and two processes int i = 0; void test() { } while(1) { } printf( %d\n, i++); 2 processes 2 threads Chung EPC6071 21

Activity Write an example of the output when the following program is executed by two threads ADR R2, i LDR R1, [R2] ADD R1, R1, #1 STR R1, [R2] 2 threads Chung EPC6071 22

Race Condition in Shared Memory Problem when two CPUs try to write the same location: CPU 1 reads flag and sees 0. CPU 2 reads flag and sees 0. CPU 1 sets flag to one and writes location. CPU 2 sets flag to one and overwrites location. Chung EPC6071 23

Atomic test-and-set Problem can be solved with an atomic test-and-set: single bus operation reads memory location, tests it, writes it. ARM test-and-set provided by SWP: ADR r0,semaphore LDR r1,#1 GETFLAG SWP r1,r1,[r0] BNZ GETFLAG Chung EPC6071 24

Critical Regions Critical region/section: section of code that cannot be interrupted by another process. Critical region is created by: Mutex in multi-threading Semaphores in multi-processing Chung EPC6071 25

Mutex in pthread #include <pthread.h> pthread_mutex_t count_mutex; long long count; Void increment_count() { pthread_mutex_lock(&count_mutex); // critical section begin count = count + 1; pthread_mutex_unlock(&count_mutex); // critical section end } long long get_count() { long long c; pthread_mutex_lock(&count_mutex); // critical section begin c = count; pthread_mutex_unlock(&count_mutex); // critical section end return (c); } Chung EPC6071 26

Semaphores in Linux void count() { sem_t *my_semaphore; my_semaphore = sem_open( /sem1, oflags); sem_wait(my_semaphore); // P // Do something; // critical section sem_post(my_semaphore); // V sem_close(my_semaphore); } Chung EPC6071 27