Professional Documents
Culture Documents
Distributed Systems Processes
Distributed Systems Processes
Distributed Systems Processes
1 Processes
3 Inter-Process Communication
6 Virtualization
Introduction
Distributed Programming
Distributed programming is about processes which communicate over a
network
Obvious requirement: good knowledge in how to handle processes
locally
Processes
Basic idea
We build virtual processors in software, on top of physical processors:
Processor: Provides a set of instructions along with the capability of
automatically executing a series of those instructions.
Thread: A minimal software processor in whose • a series of instructions can
be executed. Saving a thread context implies stopping the current execution
and saving all the data needed to continue the execution at a later stage.
Process: A software processor in whose context one or more threads may
be executed. Executing a thread, means executing a series of instructions in
the context of that thread.
Processes
Processes
After testing for fork() ’s return value, the two processes may diverge:
p i t t pid ;
pid = fork ( ) ;
i f ( p i d <0)
{
p e r r o r ( ” Fork e r r o r ” ) ; e x i t ( 1 ) ;
}
i f ( p i d ==0)
{
p r i n t f ( ” I am t h e c h i l d p r o c e s s \n” ) ;
while (1) putchar (”c” ) ;
} else {
p r i n t f ( ” I am t h e p a r e n t p r o c e s s \n” ) ;
w h i l e ( 1 ) p u t c h a r ( ”p” ) ;
}
With fork(), children always execute the same program as their parent
exec() allows one process to switch to another program
All code/data for that process are destroyed
Environment variables and file descriptors are kept identical
New code is loaded, then started (from the beginning)
execl()example :
execv ()example :
Stopping a Process
Signal Handling
Signal Handling
#i n c l u d e <s i g n a l . h>
v o i d (∗ s i g n a l ( i n t signum , v o i d (∗ s i g h a n d l e r ) ( i n t ) ) ) ( i n t ) ;
Example:
#i n c l u d e <s i g n a l . h>
void myhandler ( i n t s i g ) {
p r i n t f ( ” I r e c e i v e d s i g n a l number %d !\ n” , s i g ) ;
s i g n a l ( sig , myhandler ) ;
}
i n t main ( ) {
v o i d (∗ o l d h a n d l e r ) ( i n t ) ;
o l d h a n d l e r = s i g n a l ( SIGINT , m y h a n d l e r ) ;
s i g n a l ( SIGUSR1 , m y h a n d l e r ) ;
...
}
Inter-Process Communication
Pipes
#i n c l u d e <u n i s t d . h>
i n t pipe ( i n t fd [ 2 ] ) ;
Pipes
i f ( p i p e ( f d )<0) e x i t ( 1 ) ;
pid = fork ( ) ;
i f ( p i d ==0) /∗ c h i l d ∗/
{
c l o s e ( fd [ 0 ] ) ;
/∗ c l o s e r e a d e r ∗/
write ( fd [ 1 ] , ” hello , world ! ” ,14);
}
e l s e /∗ p a r e n t ∗/
{
c l o s e ( f d [ 1 ] ) ; /∗ c l o s e w r i t e r ∗/
i f ( r e a d ( f d [ 0 ] , buf , 6 4 ) > 0 )
p r i n t f ( ” R e c e i v e d : %s \n” , b u f ) ;
w a i t p i d ( p i d , NULL , 0 ) ;
}
}
Pipes
#i n c l u d e <u n i s t d . h>
s o r t f o o . t x t | u n i q | wc
Shared Memory
Shared Memory
To destroy a segment:
#i n c l u d e <s y s / i p c . h>
#i n c l u d e <s y s /shm . h>
i n t s h m c t l ( i n t shmid , i n t cmd , s t r u c t s h m i d d s ∗ b u f ) ;
Shared memory segments stay persistent even after all processes have
died!
You must destroy them in your programs
The ipcs command shows existing segments (and semaphores)
You can destroy them by hand with: ipcrmshm < id >
Shared Memory
Shared Memory
#i n c l u d e <s y s / t y p e s . h>
#i n c l u d e <s y s /shm . h>
v o i d ∗shmat ( i n t shmid , c o n s t v o i d ∗shmaddr , i n t s h m f l g ) ;
Shared Memory
To destroy a segment:
#i n c l u d e <s y s / i p c . h>
#i n c l u d e <s y s /shm . h>
i n t s h m c t l ( i n t shmid , i n t cmd , s t r u c t s h m i d d s ∗ b u f ) ;
Shared memory segments stay persistent even after all processes have
died!
You must destroy them in your programs
The ipcs command shows existing segments (and semaphores)
You can destroy them by hand with: ipcrmshm < id >
Shared Memory
Example
i n t main ( ) {
i n t shmid = shmget ( IPC PRIVATE , s i z e o f ( i n t ) , 0600);
i n t ∗ s h a r e d i n t = ( i n t ∗) shmat ( shmid , 0 , 0);
∗shared int = 42;
i f ( f o r k ()==0) {
p r i n t f ( ”The v a l u e i s : %d\n” , ∗ s h a r e d i n t );
∗shared int = 12;
shmdt ( ( v o i d ∗) s h a r e d i n t ) ;
}
else {
sleep (1);
p r i n t f ( ”The v a l u e i s : %d\n” , ∗ s h a r e d i n t );
shmdt ( ( v o i d ∗) s h a r e d i n t ) ;
s h m c t l ( shmid , IPC RMID , 0 ) ;
}
}
Threads in C:
Posix threads (pthreads) are standard among Unix systems
The operating system must have special support for threads
Programs must be linked with -lpthread
Threads in Java:
Threads are a native feature of Java: every virtual machine has thread
support
They are portable on any Java platform
Java threads can be mapped to operating system threads (native
threads) or emulated in user space (green threads)
Creating a Pthread
To create a pthread:
#i n c l u d e <p t h r e a d . h>
i n t p t h r e a d c r e a t e ( pthread t ∗thread , p t h r e a d a t t r t ∗attr ,
v o i d ∗(∗ s t a r t r o u t i n e ) ( v o i d ∗ ) , v o i d ∗ a r g ) ;
thread: thread id
attr: attributes (i.e., options)
start routine: function that the thread will execute
arg: parameter to be passed to the thread
To initialize a pthread attribute:
int pthread attr init ( pthread attr t ∗attr );
Stopping a Pthread
#i n c l u d e <p t h r e a d . h>
void pthread exit ( void ∗ r e t v a l ) ;
#i n c l u d e <p t h r e a d . h>
i n t p t h r e a d j o i n ( p t h r e a d t th , v o i d ∗∗ t h r e a d r e t u r n ) ;
#i n c l u d e <p t h r e a d . h>
v o i d ∗ f u n c ( v o i d ∗param ) {
i n t ∗p = ( i n t ∗) param ;
p r i n t f ( ”New t h r e a d : param=%d\n” ,∗ p ) ;
r e t u r n NULL ;
}
i n t main ( ) {
pthread t id ;
pthread attr t attr ;
i n t x = 42;
void pthread exit ( void ∗ r e t v a l ) ;
p t h r e a d a t t r i n i t (& a t t r ) ;
p t h r e a d c r e a t e (& i d , &a t t r , f u n c , ( v o i d ∗) &x ) ;
p t h r e a d j o i n ( i d , NULL ) ;
}
Detached Threads
A “detached” thread:
Does not need to be pthread join()ed
Does not stop when its parent thread stops
By default, threads are “joinable” (i.e., “attached”)
To create a detached thread, set an attribute before creating the
thread:
pthread t id ;
pthread attr t attr ;
p t h r e a d a t t r i n i t (& a t t r ) ;
p t h r e a d a t t r s e t d e t a c h s t a t e (& a t t r , PTHREAD CREATE DETACHED ) ;
p t h r e a d c r e a t e (& i d , &a t t r , f u n c , NULL ) ;
p r i v a t e i n t argument ;
MyThread ( i n t a r g ) {
a r g ument = a r g ;
}
p u b l i c void run ( ) {
System . o u t . p r i n t l n ( ”New t h r e a d s t a r t e d ! a r g=” + argument ) ;
}
}
public c l a s s AnotherClass {
s y n c h r o n i z e d p u b l i c v o i d methodOne ( ) { . . . }
s y n c h r o n i z e d p u b l i c v o i d methodTwo ( ) { . . . }
p u b l i c v o i d methodThree ( )
{ ... }
}
public c l a s s AnotherClass {
p r i v a t e Mutex mutex ;
p u b l i c v o i d methodOne ( )
{ mutex . l o c k ( ) ; . . . ; mutex . u n l o c k ( ) ; }
p u b l i c v o i d methodTwo ( )
{ mutex . l o c k ( ) ; . . . ; mutex . u n l o c k ( ) ; }
p u b l i c v o i d methodThree ( ) { . . . }
}
. . . e x c e p t t h a t t h e Mutex c l a s s d o e s n o t e x i s t !
c l a s s Object {
void wait ( ) ;
/∗ b l o c k s t h e c a l l i n g t h r e a d ∗/
void notify ( ) ;
/∗ u n b l o c k s one t h r e a d b l o c k e d i n t h i s o b j e c t ∗/
v o i d n o t i f y A l l ( ) ; /∗ u n b l o c k s a l l t h r e a d s b l o c k e d i n t h e o b j e c t ∗/
...
};
wait(): causes current thread to wait until another thread invokes the
notify() method or the notifyAll() method for this object.
notify(): wakes up a single thread that is waiting on this object’s
monitor.
notifyAll(): wakes up all threads that are waiting on this object’s
monitor.
Wondimagegn D. (AAIT ) Distributed System Programming October 13, 2016 39 / 42
Java Thread Programming
This means that each object contains one (and no more) condition
variable
The wait(), notify() and notifyAll() methods must be called inside a
monitor
Virtualization
Program
Interface A
Program Implementation of
mimicking A on B
Interface A Interface B
(a) (b)
Architecture of VMs
Virtualization
Virtualization can take place at very different levels, strongly depending on
the interfaces as offered by various systems components:
Library
System calls