Chapter 3: Processes Wh a t i s a p r o c e s s ? Wh - - PowerPoint PPT Presentation

chapter 3 processes
SMART_READER_LITE
LIVE PREVIEW

Chapter 3: Processes Wh a t i s a p r o c e s s ? Wh - - PowerPoint PPT Presentation

Chapter 3: Processes Wh a t i s a p r o c e s s ? Wh a t i s p r o c e s s s c h e d u l i n g ? Wh a t a r e t h e c o m m o n o p e r a t i o n s o n p r o c e s s e


slide-1
SLIDE 1

1

Chapter 3: Processes

 Wh

a t i s a p r

  • c

e s s ?

 Wh

a t i s p r

  • c

e s s s c h e d u l i n g ?

 Wh

a t a r e t h e c

  • m

m

  • n
  • p

e r a t i

  • n

s

  • n

p r

  • c

e s s e s ?

 H

  • w

t

  • c
  • n

d u c t p r

  • c

e s s

  • l

e v e l c

  • m

m u n i c a t i

  • n

?

 H

  • w

t

  • c
  • n

d u c t c l i e n t

  • s

e r v e r c

  • m

m u n i c a t i

  • n

?

slide-2
SLIDE 2

2

Process Concept

 P

r

  • c

e s s

 i

s a p r

  • g

r a m i n e x e c u t i

  • n

 i

s a n i n s t a n c e

  • f

a c

  • m

p u t e r p r

  • g

r a m b e i n g s e q u e n t i a l l y e x e c u t e d

 p

r

  • c

e s s e x e c u t i

  • n

m u s t p r

  • g

r e s s i n s e q u e n t i a l f a s h i

  • n

 p

r

  • c

e s s i s a l s

  • c

a l l e d a j

  • b

 P

r

  • g

r a m V s . p r

  • c

e s s

 p

r

  • g

r a m i s a p a s s i v e e n t i t y ; p r

  • c

e s s i s a n a c t i v e e n t i t y

 p

r

  • g

r a m

  • n

l y c

  • n

t a i n s t e x t ; p r

  • c

e s s i s a s s

  • c

i a t e d w i t h c

  • d

e , d a t a , P C , h e a p , s t a c k , r e g i s t e r s , a n d

  • t

h e r i n f

  • r

m a t i

  • n

 p

r

  • g

r a m b e c

  • m

e s a p r

  • c

e s s w h e n a n e x e c u t a b l e fj l e i s l

  • a

d e d i n t

  • m

e m

  • r

y

 s

a m e p r

  • g

r a m e x e c u t e d m u l t i p l e t i m e s w i l l c

  • r

r e s p

  • n

d t

  • d

i fg e r e n t p r

  • c

e s s e a c h t i m e

slide-3
SLIDE 3

3

Process in Memory

slide-4
SLIDE 4

4

Process State

 D

u r i n g e x e c u t i

  • n

, t h e p r

  • c

e s s m a y b e i n

  • n

e

  • f

t h e f

  • l

l

  • w

i n g s t a t e s

 n

e w – p r

  • c

e s s i s b e i n g c r e a t e d

 r

u n n i n g – i n s t r u c t i

  • n

s a r e b e i n g e x e c u t e d

 w

a i t i n g – w a i t i n g f

  • r

s

  • m

e e v e n t t

  • c

c u r

 r

e a d y – w a i t i n g t

  • b

e a s s i g n e d a p r

  • c

e s s

  • r

 t

e r m i n a t e d – p r

  • c

e s s h a s fj n i s h e d e x e c u t i

  • n

 E

a c h p r

  • c

e s s

  • r

c a n

  • n

l y r u n

  • n

e p r

  • c

e s s a t a i n s t a n t .

slide-5
SLIDE 5

5

Diagram of Process State

slide-6
SLIDE 6

6

Process Control Block (PCB)

 P

C B i s r e p r e s e n t a t i

  • n
  • f

a p r

  • c

e s s i n a n

  • p

e r a t i n g s y s t e m .

 m

a i n t a i n s p r

  • c

e s s

  • s

p e c i fj c i n f

  • r

m a t i

  • n

 n

e c e s s a r y f

  • r

s c h e d u l i n g

 I

n f

  • r

m a t i

  • n

a s s

  • c

i a t e d w i t h e a c h p r

  • c

e s s

 p

r

  • c

e s s s t a t e

 p

r

  • g

r a m c

  • u

n t e r

 C

P U r e g i s t e r s

 C

P U s c h e d u l i n g i n f

  • r

m a t i

  • n

 m

e m

  • r

y

  • m

a n a g e m e n t i n f

  • r

m a t i

  • n

 a

c c

  • u

n t i n g i n f

  • r

m a t i

  • n

 I

/ O s t a t u s i n f

  • r

m a t i

  • n
slide-7
SLIDE 7

7

Process Control Block (PCB) (2)

slide-8
SLIDE 8

8

CPU Switch From Process to Process

slide-9
SLIDE 9

9

Process Representation in Linux

R e p r e s e n t e d b y t h e C s t r u c t u r e task_struct

pid t_pid; /* process identifier */ long state; /* state of the process */ unsigned int time_slice /* scheduling information */ struct task_struct *parent; /* this process’s parent */ struct list_head children; /* this process’s children */ struct files_struct *files; /* list of open files */ struct mm_struct *mm; /* address space of this process */

slide-10
SLIDE 10

10

Process Scheduling

 P

r

  • c

e s s s c h e d u l i n g s e l e c t s t h e p r

  • c

e s s t

  • r

u n

  • n

a C P U

 m

a x i m i z e s C P U u t i l i z a t i

  • n

i n a m u l t i p r

  • g

r a m m i n g O S

 p

r

  • v

i d e s i l l u s i

  • n
  • f

e a c h p r

  • c

e s s

  • w

n i n g t h e s y s t e m i n a t i m e

  • s

h a r e d O S

 T

e r m i n

  • l
  • g

y u s e d i n O S s c h e d u l e r s

 j

  • b

q u e u e – s e t

  • f

a l l p r

  • c

e s s e s i n t h e s y s t e m

 r

e a d y q u e u e – s e t

  • f

a l l p r

  • c

e s s e s r e s i d i n g i n m a i n m e m

  • r

y , r e a d y a n d w a i t i n g t

  • e

x e c u t e

 d

e v i c e q u e u e s – s e t

  • f

p r

  • c

e s s e s w a i t i n g f

  • r

a n I / O d e v i c e

 P

r

  • c

e s s e s m i g r a t e a m

  • n

g t h e v a r i

  • u

s q u e u e s

slide-11
SLIDE 11

11

Ready Queue And Various I/O Device Queues

slide-12
SLIDE 12

12

Representation of Process Scheduling

slide-13
SLIDE 13

13

Schedulers

 S

y s t e m s w i t h a p

  • s

s i b i l i t y

  • f

h u g e d e l u g e

  • f

j

  • b

r e q u e s t s m a y u s e m u l t i p l e s c h e d u l e r s .

 L

  • n

g

  • t

e r m s c h e d u l e r (

  • r

j

  • b

s c h e d u l e r )

 s

e l e c t s p r

  • c

e s s e s t

  • b

e b r

  • u

g h t i n t

  • t

h e r e a d y q u e u e

 c

  • n

t r

  • l

s t h e d e g r e e

  • f

m u l t i p r

  • g

r a m m i n g

 c

  • n

t r

  • l

s t h e m i x

  • f

a c t i v e C P U

  • b
  • u

n d a n d I / O

  • b
  • u

n d p r

  • c

e s s e s

 i

n v

  • k

e d i n f r e q u e n t l y

 c

a n a fg

  • r

d m

  • r

e t i m e t

  • m

a k e s e l e c t i

  • n

d e c i s i

  • n

 S

h

  • r

t

  • t

e r m s c h e d u l e r (

  • r

C P U s c h e d u l e r )

 s

e l e c t s t h e p r

  • c

e s s t

  • b

e e x e c u t e d n e x t a n d a l l

  • c

a t e s C P U

 i

n v

  • k

e d f r e q u e n t l y

 n

e c e s s a r y t

  • l

i m i t s c h e d u l i n g

  • v

e r h e a d

slide-14
SLIDE 14

14

Context Switch

 A

c

  • n

t e x t s w i t c h i s t h e p r

  • c

e s s

  • f

s t

  • r

i n g a n d r e s t

  • r

i n g t h e s t a t e ( c

  • n

t e x t )

  • f

t h e C P U s u c h t h a t m u l t i p l e p r

  • c

e s s e s c a n s h a r e a s i n g l e C P U r e s

  • u

r c e

 f

  • r

t i m e

  • s

h a r e d

  • r

m u l t i p r

  • g

r a m m i n g e n v i r

  • n

m e n t s

 c

  • n

t e x t

  • f

a p r

  • c

e s s r e p r e s e n t e d i n t h e P C B

 c

  • n

t e x t s w i t c h i n v

  • l

v e s a s t a t e s a v e

  • f

t h e c u r r e n t p r

  • c

e s s , a n d a s t a t e r e s t

  • r

e

  • f

t h e p r

  • c

e s s b e i n g r e s u m e d n e x t

 s

w i t c h f r

  • m

u s e r t

  • k

e r n e l m

  • d

e

  • r

v i c e

  • v

e r s a i s a m

  • d

e s w i t c h

 C

  • n

t e x t

  • s

w i t c h t i m e i s

  • v

e r h e a d

 t

h e s y s t e m d

  • e

s n

  • u

s e f u l w

  • r

k w h i l e s w i t c h i n g

 o

v e r h e a d d e p e n d s

  • n

h a r d w a r e s u p p

  • r

t

 S

u n U l t r a S P A R C p r

  • v

i d e s m u l t i p l e b a n k s

  • f

r e g i s t e r s

 I

n t e l x 8 6 p r

  • c

e s s

  • r

s a l s

  • p

r

  • v

i d e s

  • m

e s u p p

  • r

t

slide-15
SLIDE 15

15

Process Creation

 A

n y p r

  • c

e s s c a n c r e a t e

  • t

h e r p r

  • c

e s s e s d u r i n g i t s e x e c u t i

  • n

 o

p e r a t i n g s y s t e m s h a v e a p r i m

  • r

d i a l p r

  • c

e s s

 c

r e a t i n g p r

  • c

e s s c a l l e d p a r e n t p r

  • c

e s s

 n

e w p r

  • c

e s s c a l l e d c h i l d p r

  • c

e s s

 p

r

  • c

e s s e s i d e n t i fj e d a n d m a n a g e d v i a a p r

  • c

e s s i d e n t i fj e r ( p i d )

 R

e s

  • u

r c e s h a r i n g

  • p

t i

  • n

s

 p

a r e n t a n d c h i l d r e n s h a r e a l l r e s

  • u

r c e s

 c

h i l d r e n s h a r e s u b s e t

  • f

p a r e n t ’ s r e s

  • u

r c e s

 p

a r e n t a n d c h i l d s h a r e n

  • r

e s

  • u

r c e s

 E

x e c u t i

  • n
  • p

t i

  • n

s

 p

a r e n t a n d c h i l d r e n e x e c u t e c

  • n

c u r r e n t l y

 p

a r e n t w a i t s u n t i l c h i l d r e n t e r m i n a t e

slide-16
SLIDE 16

16

Process Creation (Cont)

 A

d d r e s s s p a c e

  • p

t i

  • n

s

 c

h i l d d u p l i c a t e

  • f

p a r e n t

 c

h i l d h a s a p r

  • g

r a m l

  • a

d e d i n t

  • i

t

 U

N I X e x a m p l e s

 f

  • r

k s y s t e m c a l l c r e a t e s n e w p r

  • c

e s s

 e

x e c s y s t e m c a l l u s e d a f t e r a f

  • r

k t

  • r

e p l a c e t h e p r

  • c

e s s ’ m e m

  • r

y s p a c e w i t h a n e w p r

  • g

r a m

slide-17
SLIDE 17

17

Process Creation Example on Unix

int main() { pid_t pid; /* fork another process */ pid = fork(); if (pid < 0) { /* error occurred */ fprintf(stderr, "Fork Failed"); exit(-1); } else if (pid == 0) { /* child process */ execlp("/bin/ls", "ls", NULL); } else { /* parent process */ /* parent will wait for the child to complete */ wait (NULL); printf ("Child Complete"); exit(0); } }

slide-18
SLIDE 18

18

Process Creation

 P

a r e n t w a i t i n g f

  • r

c h i l d p r

  • c

e s s t

  • fj

n i s h

slide-19
SLIDE 19

19

Process Termination

 P

r

  • c

e s s t e r m i n a t e s a f t e r e x e c u t i n g l a s t s t a t e m e n t

 c

a n e x p l i c i t l y i n v

  • k

e t h e e x i t s y s t e m c a l l t

  • t

e r m i n a t e

 O

S i m p l i c i t l y c a l l s e x i t

 c

h i l d c a n p a s s r e t u r n s t a t u s t

  • p

a r e n t ( v i a w a i t )

 p

r

  • c

e s s r e s

  • u

r c e s a r e d e a l l

  • c

a t e d b y

  • p

e r a t i n g s y s t e m

 P

a r e n t m a y t e r m i n a t e e x e c u t i

  • n
  • f

c h i l d r e n p r

  • c

e s s e s ( a b

  • r

t )

 c

h i l d h a s e x c e e d e d a l l

  • c

a t e d r e s

  • u

r c e s

 t

a s k a s s i g n e d t

  • c

h i l d i s n

  • l
  • n

g e r r e q u i r e d

 i

f p a r e n t i s e x i t i n g

 s

  • m

e

  • p

e r a t i n g s y s t e m d

  • n
  • t

a l l

  • w

c h i l d t

  • c
  • n

t i n u e i f i t s p a r e n t t e r m i n a t e s

 a

l l c h i l d r e n t e r m i n a t e d

  • c

a s c a d i n g t e r mi n a t i

  • n
slide-20
SLIDE 20

20

Multiprocess Architecture – Chrome Browser

 M

a n y w e b b r

  • w

s e r s r a n a s s i n g l e p r

  • c

e s s ( s

  • m

e s t i l l d

  • )

 I

f

  • n

e w e b s i t e c a u s e s t r

  • u

b l e , e n t i r e b r

  • w

s e r c a n h a n g

  • r

c r a s h

 G

  • g

l e C h r

  • m

e B r

  • w

s e r i s m u l t i p r

  • c

e s s w i t h 3 d i fg e r e n t t y p e s

  • f

p r

  • c

e s s e s :

 B

r

  • w

s e r p r

  • c

e s s m a n a g e s u s e r i n t e r f a c e , d i s k a n d n e t w

  • r

k I / O

 R

e n d e r e r p r

  • c

e s s r e n d e r s w e b p a g e s , d e a l s w i t h H T M L , J a v a s c r i p t . A n e w r e n d e r e r c r e a t e d f

  • r

e a c h w e b s i t e

  • p

e n e d

 R

u n s i n s a n d b

  • x

r e s t r i c t i n g d i s k a n d n e t w

  • r

k I / O , m i n i m i z i n g e fg e c t

  • f

s e c u r i t y e x p l

  • i

t s

 P

l u g

  • i

n p r

  • c

e s s f

  • r

e a c h t y p e

  • f

p l u g

  • i

n

slide-21
SLIDE 21

21

Interprocess Communication

 C

  • m

m u n i c a t i

  • n

w i t h i n t h e s a m e s y s t e m .

 P

r

  • c

e s s e s m a y n e e d t

  • c
  • p

e r a t e f

  • r

s e v e r a l r e a s

  • n

s

 i

n f

  • r

m a t i

  • n

s h a r i n g

 c

  • m

p u t a t i

  • n

s p e e d u p

 m

  • d

u l a r i t y

 c

  • n

v e n i e n c e

 C

  • p

e r a t i n g p r

  • c

e s s c a n a fg e c t

  • r

b e a fg e c t e d b y

  • t

h e r p r

  • c

e s s e s

 t

y p i c a l l y , b y s h a r i n g d a t a

 C

  • p

e r a t i n g p r

  • c

e s s e s n e e d i n t e r p r

  • c

e s s c

  • mmu

n i c a t i

  • n

( I P C )

slide-22
SLIDE 22

22

Producer-Consumer Problem

 C

  • m

m

  • n

p a r a d i g m f

  • r

c

  • p

e r a t i n g p r

  • c

e s s e s

 p

r

  • d

u c e r p r

  • c

e s s p r

  • d

u c e s i n f

  • r

m a t i

  • n

 c

  • n

s u m e r p r

  • c

e s s c

  • n

s u m e s t h e p r

  • d

u c e d i n f

  • r

m a t i

  • n

 P

r

  • c

e s s e s n e e d s y n c h r

  • n

i z a t i

  • n

 c

  • n

s u m e r c a n n

  • t

u s e i n f

  • r

m a t i

  • n

b e f

  • r

e i t i s p r

  • d

u c e d b y t h e p r

  • d

u c e r

 A

b s t r a c t i

  • n

m

  • d

e l s

 u

n b

  • u

n d e d

  • b

u fg e r p l a c e s n

  • p

r a c t i c a l l i m i t

  • n

t h e s i z e

  • f

t h e b u fg e r

 b

  • u

n d e d

  • b

u fg e r a s s u m e s t h a t t h e r e i s a fj x e d b u fg e r s i z e

slide-23
SLIDE 23

23

Models of IPC

 S

h a r e d m e m

  • r

y

 s

h a r e a r e g i

  • n
  • f

m e m

  • r

y b e t w e e n c

  • p

e r a t i n g p r

  • c

e s s e s

 r

e a d

  • r

w r i t e t

  • t

h e s h a r e d m e m

  • r

y r e g i

  • n

 f

a s t c

  • m

m u n i c a t i

  • n

 c

  • n

v e n i e n t c

  • m

m u n i c a t i

  • n

 M

e s s a g e p a s s i n g

 e

x c h a n g e m e s s a g e s ( s e n d a n d r e c e i v e )

 t

y p i c a l l y , m e s s a g e s d

  • n
  • t
  • v

e r w r i t e e a c h

  • t

h e r

 n

  • n

e e d f

  • r

c

  • n

fm i c t r e s

  • l

u t i

  • n

 t

y p i c a l l y , u s e d f

  • r

s e n d i n g s m a l l e r a m

  • u

n t s

  • f

d a t a

 s

l

  • w

e r c

  • m

m u n i c a t i

  • n

 e

a s y t

  • i

m p l e m e n t ( e v e n f

  • r

i n t e r

  • c
  • m

p u t e r c

  • m

m u n i c a t i

  • n

)

slide-24
SLIDE 24

24

Models of IPC (2)

message passing shared memory

slide-25
SLIDE 25

25

Message Passing

 A

n

  • t

h e r m e c h a n i s m f

  • r

i n t e r p r

  • c

e s s c

  • m

m u n i c a t i

  • n

 c

a n b e e m p l

  • y

e d f

  • r

c l i e n t

  • s

e r v e r c

  • m

m u n i c a t i

  • n

 M

e s s a g e p a s s i n g f a c i l i t y p r

  • v

i d e s a t l e a s t t w

  • p

e r a t i

  • n

s :

 s

e n d ( m e s s a g e ) a n d r e c e i v e ( m e s s a g e )

 I

f P a n d Q w i s h t

  • c
  • m

m u n i c a t e , t h e y n e e d t

  • :

 e

s t a b l i s h a c

  • m

m u n i c a t i

  • n

l i n k b e t w e e n t h e m

 e

x c h a n g e m e s s a g e s v i a s e n d / r e c e i v e

 I

m p l e m e n t a t i

  • n

i s s u e s

 h

  • w

a r e l i n k s e s t a b l i s h e d ?

 c

a n a l i n k b e a s s

  • c

i a t e d w i t h m

  • r

e t h a n t w

  • p

r

  • c

e s s e s ?

 h

  • w

m a n y l i n k s b e t w e e n e v e r y p a i r

  • f

c

  • m

m u n i c a t i n g p r

  • c

e s s e s ?

 w

h a t i s t h e c a p a c i t y

  • f

a l i n k ?

 fj

x e d

  • r

v a r i a b l e s i z e d m e s s a g e ?

 i

s t h e l i n k u n i d i r e c t i

  • n

a l

  • r

b i

  • d

i r e c t i

  • n

a l ?

slide-26
SLIDE 26

26

Message Passing – Naming

 D

i r e c t c

  • m

m u n i c a t i

  • n

 p

r

  • c

e s s e s m u s t n a m e e a c h

  • t

h e r e x p l i c i t l y :

 s

e n d ( P , m e s s a g e ) – s e n d a m e s s a g e t

  • p

r

  • c

e s s P

 r

e c e i v e ( Q , m e s s a g e ) – r e c e i v e a m e s s a g e f r

  • m

p r

  • c

e s s Q

 p

r

  • p

e r t i e s

  • f

c

  • m

m u n i c a t i

  • n

l i n k

 l

i n k s a r e e s t a b l i s h e d a u t

  • m

a t i c a l l y

 a

l i n k i s a s s

  • c

i a t e d w i t h e x a c t l y

  • n

e p a i r

  • f

c

  • m

m u n i c a t i n g p r

  • c

e s s e s

 b

e t w e e n e a c h p a i r t h e r e e x i s t s e x a c t l y

  • n

e l i n k

 d

i s a d v a n t a g e

 p

r

  • c

e s s i d e n t i fj e r s a r e h a r d

  • c
  • d

e d

slide-27
SLIDE 27

27

Message Passing – Naming (2)

 I

n d i r e c t c

  • m

m u n i c a t i

  • n

 m

e s s a g e s a r e d i r e c t e d a n d r e c e i v e d f r

  • m

m a i l b

  • x

e s ( a l s

  • r

e f e r r e d t

  • a

s p

  • r

t s )

 s

e n d ( A , m e s s a g e ) – s e n d a m e s s a g e t

  • m

a i l b

  • x

A

 r

e c e i v e ( A , m e s s a g e ) – r e c e i v e a m e s s a g e f r

  • m

m a i l b

  • x

A

 e

a c h m a i l b

  • x

h a s a u n i q u e i d

 p

r

  • c

e s s e s c a n c

  • m

m u n i c a t e

  • n

l y i f t h e y s h a r e a m a i l b

  • x

 p

r

  • p

e r t i e s

  • f

c

  • m

m u n i c a t i

  • n

l i n k

 l

i n k m a y b e a s s

  • c

i a t e d w i t h m a n y p r

  • c

e s s e s

 e

a c h p a i r

  • f

p r

  • c

e s s e s m a y s h a r e s e v e r a l c

  • m

m u n i c a t i

  • n

l i n k s

 l

i n k m a y b e u n i d i r e c t i

  • n

a l

  • r

b i

  • d

i r e c t i

  • n

a l

 m

u l t i p l e r e c e i v e r s m a y n e e d s y n c h r

  • n

i z a t i

  • n

 m

a i l b

  • x

c a n b e h e l d i n t h e p r

  • c

e s s a d d r e s s s p a c e

  • r

i n t h e k e r n e l

slide-28
SLIDE 28

28

Message Passing (3)

 S

y n c h r

  • n

i z a t i

  • n

 m

e s s a g e p a s s i n g m a y b e e i t h e r b l

  • c

k i n g ( s y n c h r

  • n
  • u

s )

  • r

n

  • n
  • b

l

  • c

k i n g ( a s y n c h r

  • n
  • u

s )

b l

  • c

k i n g s e n d h a s t h e s e n d e r b l

  • c

k u n t i l t h e m e s s a g e i s r e c e i v e d

b l

  • c

k i n g r e c e i v e h a s t h e r e c e i v e r b l

  • c

k u n t i l a m e s s a g e i s a v a i l a b l e

n

  • n
  • b

l

  • c

k i n g s e n d h a s t h e s e n d e r s e n d t h e m e s s a g e a n d c

  • n

t i n u e

n

  • n
  • b

l

  • c

k i n g r e c e i v e h a s t h e r e c e i v e r r e c e i v e a v a l i d m e s s a g e

  • r

n u l l

 B

u fg e r i n g – q u e u e

  • f

m e s s a g e s a t t a c h e d t

  • t

h e l i n k

 z

e r

  • c

a p a c i t y – m e s s a g e s

 S

e n d e r m u s t w a i t f

  • r

r e c e i v e r

 b

  • u

n d e d c a p a c i t y – fj n i t e l e n g t h

  • f

n m e s s a g e s

 S

e n d e r m u s t w a i t i f l i n k f u l l

 u

n b

  • u

n d e d c a p a c i t y – i n fj n i t e l e n g t h

 S

e n d e r n e v e r w a i t s

slide-29
SLIDE 29

29

Interprocess Communication in Unix

 P

r

  • v

i d e s m u l t i p l e m

  • d

e s

  • f

I P C

 p

i p e s

 F

I F O s ( n a m e s p i p e s )

 m

e s s a g e q u e u e s

 s

h a r e d m e m

  • r

y

 s

  • c

k e t s

slide-30
SLIDE 30

30

Pipes

 M

  • s

t b a s i c f

  • r

m

  • f

I P C

  • n

a l l U n i x s y s t e m s

 a

l s

  • p

r

  • v

i d e s a u s e f u l c

  • m

m a n d

  • l

i n e i n t e r f a c e

 C

  • n

d u i t f

  • r

t w

  • p

r

  • c

e s s e s t

  • c
  • m

m u n i c a t e

 o

r d i n a r y p i p e s r e q u i r e p a r e n t

  • c

h i l d r e l a t i

  • n

s h i p b e t w e e n c

  • m

m u n i c a t i n g p r

  • c

e s s e s

slide-31
SLIDE 31

31

Pipes

 I

s s u e s t

  • b

e a d d r e s s e d

 i

s c

  • m

m u n i c a t i

  • n

u n i d i r e c t i

  • n

a l

  • r

b i d i r e c t i

  • n

a l ?

 U

n i x p i p e s

  • n

l y a l l

  • w

u n i d i r e c t i

  • n

a l c

  • m

m u n i c a t i

  • n

 s

h

  • u

l d c

  • m

m u n i c a t i

  • n

p r

  • c

e s s e s b e r e l a t e d ?

 a

n

  • n

y m

  • u

s p i p e s c a n

  • n

l y b e c

  • n

s t r u c t e d b e t w e e n p a r e n t

  • c

h i l d

 c

a n p i p e s c

  • m

m u n i c a t e

  • v

e r a n e t w

  • r

k

 p

r

  • c

e s s e s m u s t b e c

  • n

t r

  • l

l e d b y t h e s a m e O S

 P

i p e s e x i s t

  • n

l y u n t i l t h e p r

  • c

e s s e s e x i s t

 p

r e

  • m

a t u r e p r

  • c

e s s e x i t m a y c a u s e d a t a l

  • s

s

 D

a t a c a n

  • n

l y b e c

  • l

l e c t e d i n F I F O

  • r

d e r

slide-32
SLIDE 32

32

Simple Example Using Pipes

#include <unistd.h> #include <stdio.h> #include <string.h> main() { char *s, buf[1024]; int fds[2]; s = "EECS 678 Spring 2009\n"; /* open a pipe. fd[0] is opened for reading, and fd[1] for writing.*/ pipe(fds); /* write to the write-end of the pipe */ write(fds[1], s, strlen(s)); /* This can be read from the other end of the pipe */ read(fds[0], buf, strlen(s)); printf("fds[0]=%d, fds[1]=%d\n", fds[0], fds[1]); write(1, buf, strlen(s)); }

slide-33
SLIDE 33

33

IPC Example Using Pipes

main() { char *s, buf[1024]; int fds[2]; s = "EECS 678 Spring 2009. Pipe program 2\n"; /* create a pipe */ pipe(fds); /* create a new process using fork */ if (fork() == 0) { /* child process. All file descriptors, including pipe are inherited, and copied.*/ write(fds[1], s, strlen(s)); exit(0); } /* parent process */ read(fds[0], buf, strlen(s)); write(1, buf, strlen(s)); }

slide-34
SLIDE 34

34

Pipes Used for Process Synchronization

main() { char *s, buf[1024]; int fds[2]; s = "EECS 678 Spring 2009. Pipe program 3\n"; /* create a pipe */ pipe(fds); if (fork() == 0) { /* child process. */ printf("Child line 1\n"); read(fds[0], s, strlen(s)); printf("Child line 2\n"); } else { /* parent process */ printf("Parent line 1\n"); write(fds[1], buf, strlen(s)); printf("Parent line 2\n"); } }

slide-35
SLIDE 35

35

Pipes Used in Unix Shells

 P

i p e s c

  • m

m

  • n

l y u s e d i n m

  • s

t U n i x s h e l l s

 o

u t p u t

  • f
  • n

e c

  • m

m a n d i s i n p u t t

  • t

h e n e x t c

  • m

m a n d

 e

x a m p l e : /bin/ps -ef | /bin/more

 How does the shell realize this command?

 c

r e a t e a p r

  • c

e s s t

  • r

u n ps -ef

 c

r e a t e a p r

  • c

e s s t

  • r

u n more

 c

r e a t e a p i p e f r

  • m

ps -ef t

  • more

 t

h e s t a n d a r d

  • u

t p u t

  • f

t h e p r

  • c

e s s t

  • r

u n ps -ef i s r e d i r e c t e d t

  • a

p i p e s t r e a m i n g t

  • t

h e p r

  • c

e s s t

  • r

u n more

 t

h e s t a n d a r d i n p u t

  • f

t h e p r

  • c

e s s t

  • r

u n more i s r e d i r e c t e d t

  • b

e t h e p i p e f r

  • m

t h e p r

  • c

e s s r u n n i n g ps -ef

slide-36
SLIDE 36

36

FIFO (Named Pipes)

 P

i p e w i t h a n a m e !

 M

  • r

e p

  • w

e r f u l t h a n a n

  • n

y m

  • u

s p i p e s

 n

  • p

a r e n t

  • s

i b l i n g r e l a t i

  • n

s h i p r e q u i r e d

 a

l l

  • w

b i d i r e c t i

  • n

a l c

  • m

m u n i c a t i

  • n

 F

I F O s e x i s t s e v e n a f t e r c r e a t i n g p r

  • c

e s s i s t e r m i n a t e d

 C

h a r a c t e r i s t i c s

  • f

F I F O s

 a

p p e a r a s t y p i c a l fj l e s

 o

n l y a l l

  • w

h a l f

  • d

u p l e x c

  • m

m u n i c a t i

  • n

 c

  • m

m u n i c a t i n g p r

  • c

e s s m u s t r e s i d e

  • n

t h e s a m e m a c h i n e

slide-37
SLIDE 37

37

Producer Consumer Example with FIFO

 P

r

  • d

u c e r C

  • d

e :

main() { char str[MAX_LENGTH]; int num, fd; mkfifo(FIFO_NAME, 0666); // create FIFO file printf("waiting for readers..."); fd = open(FIFO_NAME, O_WRONLY); // open FIFO for writing printf("got a reader !\n"); printf("Enter text to write in the FIFO file: "); fgets(str, MAX_LENGTH, stdin); while(!(feof(stdin))){ if ((num = write(fd, str, strlen(str))) == -1) perror("write"); else printf("producer: wrote %d bytes\n", num); fgets(str, MAX_LENGTH, stdin); } }

slide-38
SLIDE 38

38

Producer Consumer Example with FIFO (2)

 C

  • n

s u m e r c

  • d

e :

main() { char str[MAX_LENGTH]; int num, fd; mkfifo(FIFO_NAME, 0666); // make fifo, if not already present printf("waiting for writers..."); fd = open(FIFO_NAME, O_RDONLY); // open fifo for reading printf("got a writer !\n"); do{ if((num = read(fd, str, MAX_LENGTH)) == -1) perror("read"); else{ str[num] = '\0'; printf("consumer: read %d bytes\n", num); printf("%s", str); } }while(num > 0); }

slide-39
SLIDE 39

39

Message Passing in Unix

 L

i n u x u s e s i n d i r e c t c

  • m

m u n i c a t i

  • n
  • r

m a i l b

  • x

e s .

 Q

u e u e s c a n b e a s s

  • c

i a t e d w i t h m u l t i p l e p r

  • c

e s s e s

 s

y n c h r

  • n

i z a t i

  • n

m a y b e r e q u i r e d

 C

  • m

m u n i c a t i n g p r

  • c

e s s e s c a n u s e a n y n u m b e r

  • f

q u e u e s

 e

a c h q u e u e i s i d e n t i fj e d b y a u n i q u e i d e n t i fj e r

 C

a p a c i t y

  • f

t h e l i n k i s s y s t e m i n i t i a l i z e d

 c

a n b e

  • v

e r

  • r

i d d e n b y t h e u s e r

 M

e s s a g e s a r e

  • f

a fj x e d s i z e

 s

p e c i fj e d b y t h e b u fg e r l e n g t h

 E

a c h c

  • m

m u n i c a t i n g p r

  • c

e s s c a n s e n d a n d r e c e i v e f r

  • m

t h e s a m e q u e u e .

slide-40
SLIDE 40

40

Message Queue Example

int main() { /* identifjer for the message queue */ int queue_id; /* send and receive message bufgers */ struct msg_buf send_buf, recv_buf; /* create a message queue */ queue_id = msgget(0, S_IRUSR|S_IWUSR|IPC_CREAT); /* send a message to the queue */ send_buf.mtype = 1; strcpy(send_buf.bufger, "EECS 678 Class"); msgsnd(queue_id, (struct msg_buf *)&send_buf, sizeof(send_buf)); /* get the message from the queue */ msgrcv(queue_id, (struct msg_buf *)&recv_buf, sizeof(recv_buf), 0, 0); printf("%s\n", recv_buf.bufger); /* delete the message queue, and deallocate resources */ msgctl(queue_id, IPC_RMID, NULL); return 0; } struct msg_buf{ long mtype; char bufger[1000]; }

slide-41
SLIDE 41

41

Message Queues Example (2)

 M

e s s a g e p a s s i n g i n L i n u x i s d

  • n

e v i a m e s s a g e q u e u e s .

 m

s g g e t – c r e a t e a n e w m e s s a g e q u e u e

 r

e t u r n e x i s t i n g q u e u e i d e n t i fj e r i f i t e x i s t s

 m

s g s n d – s e n d a m e s s a g e t

  • t

h e q u e u e

 e

a c h m e s s a g e s h

  • u

l d b e i n a b u fg e r l i k e , struct msg_buf { long mtype; char mtext[1]; }

 nonblocking, unless no space in the queue

 m

s g r c v – r e c e i v e m e s s a g e f r

  • m

t h e q u e u e

 mtype

c a n b e u s e d t

  • g

e t s p e c i fj c m e s s a g e s

 m

s g c t l – p e r f

  • r

m c

  • n

t r

  • l
  • p

e r a t i

  • n

s s p e c i fj e d b y c m d

 s

e c

  • n

d a r g u m e n t , w e u s e i t t

  • t

e r m i n a t e q u e u e

slide-42
SLIDE 42

42

Memory Sharing in Unix

 M

u l t i p l e p r

  • c

e s s e s s h a r e s i n g l e c h u n k

  • f

m e m

  • r

y .

 I

m p l e m e n t a t i

  • n

p r i n c i p l e s

 u

n i q u e l y n a m i n g t h e s h a r e d s e g m e n t

 s

y s t e m

  • w

i d e

  • r

a n

  • n

y m

  • u

s n a m e

 s

p e c i f y i n g a c c e s s p e r m i s s i

  • n

s

 r

e a d , w r i t e , e x e c u t e

 d

e a l i n g w i t h r a c e c

  • n

d i t i

  • n

s

 a

t

  • m

i c , s y n c h r

  • n

i z e d a c c e s s

 M

  • s

t t h r e a d

  • l

e v e l c

  • m

m u n i c a t i

  • n

i s v i a s h a r e d m e m

  • r

y .

slide-43
SLIDE 43

43

Shared Memory Example

int main() { int segment_id; char *shared_memory; const int size = 4096; /* allocate and attach a shared memory segment */ segment_id = shmget(IPC_PRIVATE, size, S_IRUSR|S_IWUSR); shared_memory = (char *) shmat(segment_id, NULL, 0); /* write and print a message to the shared memory segment */ sprintf(shared_memory, "EECS 678 Spring 2009 Class"); printf("%s\n", shared_memory); /* detach and remove the shared memory segment */ shmdt(shared_memory); shmctl(segment_id, IPC_RMID, NULL); return 0; }

slide-44
SLIDE 44

44

Shared Memory Example (2)

 s

h m g e t – c r e a t e s h a r e d m e m

  • r

y s e g m e n t

 I

P C _ P R I V A T E s p e c i fj e s c r e a t i

  • n
  • f

n e w m e m

  • r

y s e g m e n t

  • f

s i z e r

  • u

n d e d t

  • t

h e s y s t e m p a g e s i z e

 a

c c e s s p e r m i s s i

  • n

s a s f

  • r

n

  • r

m a l fj l e a c c e s s

 r

e t u r n s i d e n t i fj e r

  • f

s h a r e d m e m

  • r

y s e g m e n t

 s

h m a t – a t t a c h s h a r e d m e m

  • r

y s e g m e n t

 m

u s t f

  • r

e v e r y p r

  • c

e s s w a n t i n g a c c e s s t

  • t

h e r e g i

  • n

 s

e g m e n t i d e n t i fj e d b y s e g m e n t _ i d

 s

y s t e m c h

  • s

e s a s u i t a b l e a t t a c h a d d r e s s

 s

h m c t l – p e r f

  • r

m s t h e c

  • n

t r

  • l
  • p

e r a t i

  • n

s p e c i fj e d b y c m d

 c

  • m

m a n d i s I P C _ R M I D t

  • r

e m

  • v

e s h a r e d s e g m e n t

 s

e e p r

  • g

r a m s h a r e d _ m e m

  • r

y 2 . c

 R

e a d m a n p a g e s !

slide-45
SLIDE 45

45

Unix Domain Sockets

 S

  • c

k e t s

 c

a n b e d e fj n e d a s a n e n d

  • p
  • i

n t f

  • r

c

  • m

m u n i c a t i

  • n

s

 t

w

  • w

a y c

  • m

m u n i c a t i

  • n

p i p e

 c

a n b e u s e d i n a v a r i e t y

  • f

d

  • m

a i n s , i n c l u d i n g I n t e r n e t

 U

n i x D

  • m

a i n S

  • c

k e t s

 c

  • m

m u n i c a t i

  • n

b e t w e e n p r

  • c

e s s e s

  • n

t h e s a m e U n i x s y s t e m

 s

p e c i a l fj l e i n t h e fj l e s y s t e m

 M

  • s

t l y u s e d f

  • r

c l i e n t

  • s

e r v e r p r

  • g

r a m m i n g

 c

l i e n t s e n d i n g r e q u e s t s f

  • r

i n f

  • r

m a t i

  • n

, p r

  • c

e s s i n g

 s

e r v e r w a i t i n g f

  • r

u s e r r e q u e s t s

 s

e r v e r p e r f

  • r

m i n g t h e r e q u e s t e d a c t i v i t y a n d s e n d i n g u p d a t e s t

  • c

l i e n t

 S

  • c

k e t c

  • m

m u n i c a t i

  • n

m

  • d

e s

 c

  • n

n e c t i

  • n
  • b

a s e d , T C P

 c

  • n

n e c t i

  • n
  • l

e s s , U D P

slide-46
SLIDE 46

46

Unix Domain Sockets – System Calls

 s

  • c

k e t ( )

  • c

r e a t e t h e U n i x s

  • c

k e t

 int socket(int domain, int type, int protocol);  domain is AF_UNIX

 bind ( ) - assign a name to a socket

 int bind(int sockfd, const struct sockaddr *my_addr,

socklen_t addrlen);

 my_addr is addrlen bytes long

 listen ( ) - listen to incoming client requests

 int listen(int sockfd, int backlog);  backlog specifjes the queue limit for incoming connctions

slide-47
SLIDE 47

47

Unix Domain Sockets – System Calls (2)

 accept ( ) - create a new connected socket

 int accept(int sockfd, struct sockaddr *addr,

socklen_t *addrlen);

 only for connection-based protocols

 recv ( ) - receive messages from socket

 ssize_t recv(int s, void *buf, size_t len, int flags);  message placed in buf

 close ( ) - close the socket connection

slide-48
SLIDE 48

48

Socket Example – Echo Server

 s

e e s

  • c

k e t _ s e r v e r . c

 s

e e s

  • c

k e t _ c l i e n t . c

slide-49
SLIDE 49

49

Communications in Client-Server Systems

 S

  • c

k e t s

 R

e m

  • t

e P r

  • c

e d u r e C a l l s

 R

e m

  • t

e M e t h

  • d

I n v

  • c

a t i

  • n

( J a v a )

slide-50
SLIDE 50

50

Remote Procedure Calls

 R

e m

  • t

e p r

  • c

e d u r e c a l l ( R P C ) a b s t r a c t s s u b r

  • u

t i n e c a l l s b e t w e e n p r

  • c

e s s e s

  • n

n e t w

  • r

k e d s y s t e m s

 s

u b r

  • u

t i n e e x e c u t e s i n a n

  • t

h e r a d d r e s s s p a c e

 u

s e s m e s s a g e p a s s i n g c

  • m

m u n i c a t i

  • n

m

  • d

e l

 m

e s s a g e s a r e w e l l

  • s

t r u c t u r e d

 R

P C d a e m

  • n
  • n

t h e s e r v e r h a n d l e s t h e r e m

  • t

e c a l l s

 C

l i e n t

  • s

i d e s t u b

 p

r

  • x

y f

  • r

t h e a c t u a l p r

  • c

e d u r e

  • n

t h e s e r v e r

 r

e s p

  • n

s i b l e f

  • r

l

  • c

a t i n g c

  • r

r e c t p

  • r

t

  • n

t h e s e r v e r

 r

e s p

  • n

s i b l e f

  • r

m a r s h a l l i n g t h e p r

  • c

e d u r e p a r a m e t e r s

 S

e r v e r

  • s

i d e s t u b

 r

e c e i v e s t h e m e s s a g e ; u n p a c k s t h e m a r s h a l l e d p a r a m e t e r s

 p

e r f

  • r

m s t h e p r

  • c

e d u r e

  • n

t h e s e r v e r , r e t u r n s r e s u l t

slide-51
SLIDE 51

51

Marshalling Parameters

slide-52
SLIDE 52

52

Execution of RPC

slide-53
SLIDE 53

53

Remote Method Invocation

 R

e m

  • t

e M e t h

  • d

I n v

  • c

a t i

  • n

( R M I )

 J

a v a m e c h a n i s m ( A P I ) t

  • p

e r f

  • r

m R P C s

 J

a v a r e m

  • t

e m e t h

  • d

p r

  • t
  • c
  • l

( J R M P )

  • n

l y a l l

  • w

s c a l l s f r

  • m
  • n

e J V M t

  • a

n

  • t

h e r J V M

 C

O R B A i s u s e d t

  • s

u p p

  • r

t c

  • m

m u n i c a t i

  • n

w i t h n

  • n
  • J

V M c

  • d

e

 c

l i e n t

  • b

t a i n s r e f e r e n c e t

  • r

e m

  • t

e

  • b

j e c t , a n d i n v

  • k

e s m e t h

  • d

s

  • n

t h e m