department of computer science southern illinois university edwardsville spring, 2010 dr. hiroshi...

11
Department of Computer Science Southern Illinois University Edwardsville Spring, 2010 Dr. Hiroshi Fujinoki E-mail: [email protected] CS 547/490 Network Programming Semaphore and Shared Memory IPC2.PPT/001

Upload: arron-dawson

Post on 17-Dec-2015

217 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: Department of Computer Science Southern Illinois University Edwardsville Spring, 2010 Dr. Hiroshi Fujinoki E-mail: hfujino@siue.edu CS 547/490 Network

Department of Computer ScienceSouthern Illinois University Edwardsville

Spring, 2010

Dr. Hiroshi FujinokiE-mail: [email protected]

CS 547/490 Network Programming

Semaphore and Shared Memory

IPC2.PPT/001

Page 2: Department of Computer Science Southern Illinois University Edwardsville Spring, 2010 Dr. Hiroshi Fujinoki E-mail: hfujino@siue.edu CS 547/490 Network

CS 547/490 Network Programming

What is a problem in “pipe”?

Not flexible - only FIFO queue is implemented

FIFO

Other Data Structures

• Stack • Tree • Heap

• Binary-Tree

• B-Tree

• List • Table

• Link-ListIPC2.PPT/002

Page 3: Department of Computer Science Southern Illinois University Edwardsville Spring, 2010 Dr. Hiroshi Fujinoki E-mail: hfujino@siue.edu CS 547/490 Network

CS 547/490 Network Programming

What is a problem in “pipe” (continued)?

Not flexible - only FIFO queue is implemented

A solution: Semaphore + Shared Memory

= similar to pipe, but almost any data structure can be implemented by “semaphore + shared memory”

IPC2.PPT/003

Page 4: Department of Computer Science Southern Illinois University Edwardsville Spring, 2010 Dr. Hiroshi Fujinoki E-mail: hfujino@siue.edu CS 547/490 Network

CS 547/490 Network Programming

Shared Memory:

• Shared memory is a piece of memory space that does not belong to any particular user process.

• Shared memory can be accessed (shared) by multiple processes.

• Shared memory is created by OS.

• Two processes can send and receive data through shared memory

• No limitation in the order data is accessed (and even bi-directional)

• Race condition is there (users are responsible for avoiding race condition)

IPC2.PPT/004

Page 5: Department of Computer Science Southern Illinois University Edwardsville Spring, 2010 Dr. Hiroshi Fujinoki E-mail: hfujino@siue.edu CS 547/490 Network

CS 547/490 Network Programming

OS

Allocate/Manage

Shared Memory

WRITE

READ

Process 2Process 1

Can access any addresswithin a shared memory

Property of Shared Memory (1) “Random Access” is possible

IPC2.PPT/005

Not FIFO any more

Page 6: Department of Computer Science Southern Illinois University Edwardsville Spring, 2010 Dr. Hiroshi Fujinoki E-mail: hfujino@siue.edu CS 547/490 Network

CS 547/490 Network Programming

OS

Allocate/Manage

Shared Memory

READ

WRITE

Process 2Process 1

Property of Shared Memory (2) No limitation in “direction”

WRITE

READ

IPC2.PPT/006

Page 7: Department of Computer Science Southern Illinois University Edwardsville Spring, 2010 Dr. Hiroshi Fujinoki E-mail: hfujino@siue.edu CS 547/490 Network

CS 547/490 Network Programming

OS

Shared Memory

Process 2Process 1

READ

WRITE

WRITE

READ

OS does NOT provideprotection against

race-condition

Property of Shared Memory (3) No protection from “Race Condition”

IPC2.PPT/007

Page 8: Department of Computer Science Southern Illinois University Edwardsville Spring, 2010 Dr. Hiroshi Fujinoki E-mail: hfujino@siue.edu CS 547/490 Network

Time Server

OS

CS 547/490 Network Programming

Application Example

WriterProcess

TimeKeeper

(Delta Time)

Client 1

Client 2

Client n

RemoteClient 1

RemoteClient 2

RemoteClient n

Shared Memory

12:59:59

IPC2.PPT/008

Page 9: Department of Computer Science Southern Illinois University Edwardsville Spring, 2010 Dr. Hiroshi Fujinoki E-mail: hfujino@siue.edu CS 547/490 Network

CS 547/490 Network Programming

(1) Multiple readers can simultaneously access to the clock

(2) When the time keeper (= writer) updates the time, no client can access

(3) Once the time keeper leaves, multiple clients can access to the clock

Access Rules to the Shared Memory

IPC2.PPT/009

Page 10: Department of Computer Science Southern Illinois University Edwardsville Spring, 2010 Dr. Hiroshi Fujinoki E-mail: hfujino@siue.edu CS 547/490 Network

CS 547/490 Network Programming

START

Writer?

accept

create shared memory

folk

TimeKeeper

YES

folk

Client

NO

IPC2.PPT/010

Page 11: Department of Computer Science Southern Illinois University Edwardsville Spring, 2010 Dr. Hiroshi Fujinoki E-mail: hfujino@siue.edu CS 547/490 Network

CS 547/490 Network Programming

IPC2.PPT/011

Time Keeper (Writer) Time Client (Reader)

reader_count = reader_count +1;

if (reader_count = 1) wait (writer);

wait (mutex);

signal (mutex);

reader_count = reader_count -1;if (reader_count = 0) signal (writer);

wait (mutex);

signal (mutex);

wait (writer);

signal (writer);

The Time KeeperUpdates the current time A reader reads

the current time

writer, mutex = binary (mutual exclusion) semaphore