Distributed Systems Processes

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 42

Overview

1 Processes

2 Creating/Deleting Unix Processes

3 Inter-Process Communication

4 Posix Thread Programming

5 Java Thread Programming

6 Virtualization

Wondimagegn D. (AAIT ) Distributed System Programming October 13, 2016 1 / 42


Processes

Introduction

Distributed Programming
Distributed programming is about processes which communicate over a
network
Obvious requirement: good knowledge in how to handle processes
locally

Wondimagegn D. (AAIT ) Distributed System Programming October 13, 2016 2 / 42


Processes

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.

Wondimagegn D. (AAIT ) Distributed System Programming October 13, 2016 3 / 42


Processes

Threads and Distributed Systems


Multithreaded Web client
Hiding network latencies:
Web browser scans an incoming HTML page, and finds that more files
need to be fetched
Each file is fetched by a separate thread, each doing a (blocking) HTTP
request.
As files come in, the browser displays them.

Multiple request-response calls to other machines (RPC)


Hiding network latencies:
A client does several calls at the same time, each one by a different thread.
It then waits until all results have been returned.
Note: if calls are to different servers, we may have a linear speed-up.
Wondimagegn D. (AAIT ) Distributed System Programming October 13, 2016 4 / 42
Processes

Processes

One process is made of:


A process identifier (an integer)
One executing program
Memory used to execute the program
One program counter (indicates where in the program the process
currently is)
A number of signal handlers (tells the program what to do when
receiving signals)
One process can determine what is its own identifier:
#i n c l u d e <s y s / t y p e s . h>
#i n c l u d e <u n i s t d . h>
pid t getpid ( void ) ;

It can also determine the identifier of its parent:


pid t getppid ( void ) ;

Wondimagegn D. (AAIT ) Distributed System Programming October 13, 2016 5 / 42


Processes

Processes

Computers can execute programs


A process is one instance of a program while it is executing
For example: I am currently using acroread to display these slides
At the same time, there are many other programs executing: they are
other processes
The same program can be executed multiple times in parallel
Somebody else may log on my computer and start acroread
These are several separate processes, executing the same program
A process can only be created by another process
E.g., when I type a command, my shell process will create an acroread
process

Wondimagegn D. (AAIT ) Distributed System Programming October 13, 2016 6 / 42


Processes

The fork() Primitive

There is exactly one way of creating a process in Unix:


#i n c l u d e <s y s / t y p e s . h>
#i n c l u d e <u n i s t d . h>
pid t fork ( void ) ;

Wondimagegn D. (AAIT ) Distributed System Programming October 13, 2016 7 / 42


Creating/Deleting Unix Processes

The fork() Primitive

The child process is an exact copy of the parent


It is running the same program
Its program counter is at the same position within the program
I.e., just after the fork() call
Its memory area is an exact copy of the parent’s memory
Signal handlers and file descriptors are copied too
There is one way of distinguishing between the two processes:

Wondimagegn D. (AAIT ) Distributed System Programming October 13, 2016 8 / 42


Creating/Deleting Unix Processes

The fork() Primitive

There is one way of distingusing between the two processes:


fork() return 0 to the child process
fork() returns the childs pid to the parent (pid > 0)
fork() return -1 if an error occurred
Most often, programs need to check the return value from fork()

Wondimagegn D. (AAIT ) Distributed System Programming October 13, 2016 9 / 42


Creating/Deleting Unix Processes

The fork() Primitive

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” ) ;
}

Wondimagegn D. (AAIT ) Distributed System Programming October 13, 2016 10 / 42


Creating/Deleting Unix Processes

The Fork of Death

You must be careful when using fork()!


It is very easy to create very harmful programs
What does the following program do?
while (1)
{
fork ();
}

Wondimagegn D. (AAIT ) Distributed System Programming October 13, 2016 11 / 42


Creating/Deleting Unix Processes

The exec() Primitives

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)

Wondimagegn D. (AAIT ) Distributed System Programming October 13, 2016 12 / 42


Creating/Deleting Unix Processes

The exec() Primitives


There are 4 versions of the exec() primitive:
Depending if the program is searched in $PATH or if it is specified by
an absolute path
Depending if program parameters are passed as parameters to exec()
or as a single array:
absolute path. search in $PATH
n parameters execl() execlp()
1 array execl() execlp()

execl()example :

e x e c l ( ” / b i n / l s ” , ” l s ” , ”−l ” , ” /home/ wondimagegn ” , NULL ) ;

execv ()example :

c h a r ∗params [ 4 ] = {” l s ” , ”−l ” , ” /home/ wondimagegn ” , NULL} ;


e x e c v ( ” / b i n / l s ” , params ) ;

Wondimagegn D. (AAIT ) Distributed System Programming October 13, 2016 13 / 42


Creating/Deleting Unix Processes

Stopping a Process

A process stops when:


The main() function returns or
The program calls exit()
A process cannot directly kill another process
It can only send a signal to it
Depending on the destination’s signal handlers, it may stop (or do
something else)
By default, the signal SIGINT stops the process.This is what happens
when you type C

Wondimagegn D. (AAIT ) Distributed System Programming October 13, 2016 14 / 42


Creating/Deleting Unix Processes

Signal Handling

Signals can be sent by the system to a process


SIGSEGV: segmentation fault (non-authorized memory access)
SIGBUS: bus error (non-aligned memory access)
SIGPIPE: you tried to write in a pipe with no reader
SIGCHLD: one of your children process has stopped
There are two generic signals to be used by user programs: SIGUSR1
and SIGUSR2
You can get a complete list of signals with kill -l

Wondimagegn D. (AAIT ) Distributed System Programming October 13, 2016 15 / 42


Creating/Deleting Unix Processes

Signal Handling

To setup a signal handler:

#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 ) ;
...
}

Wondimagegn D. (AAIT ) Distributed System Programming October 13, 2016 16 / 42


Inter-Process Communication

Inter-Process Communication

In general, processes cannot influence each other


Each process is executed in isolation from the others
For example, one process cannot write into the memory of another
process. . .
But you can decide to make several processes communicate together:
Send a signal to another process
Pipes: a communication channel to transfer data
Shared memory: the same memory area is accessible to multiple
processes
Semaphores: perform synchronization (e.g., to regulate access to
shared memory)
All these IPCs only work between processes of the same computer!

Wondimagegn D. (AAIT ) Distributed System Programming October 13, 2016 17 / 42


Inter-Process Communication

Pipes

A pipe is a unidirectional communication channel between processes


All data written into it can be read at the other end
If you need bidirectional communication, use two separate pipes!
To create a pipe: pipe()

#i n c l u d e <u n i s t d . h>
i n t pipe ( i n t fd [ 2 ] ) ;

Wondimagegn D. (AAIT ) Distributed System Programming October 13, 2016 18 / 42


Inter-Process Communication

Pipes

Pipes are often used in combination with fork()


i n t main ( ) {
i n t pid , fd [ 2 ] ;
char buf [ 6 4 ] ;

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 ) ;
}
}

Wondimagegn D. (AAIT ) Distributed System Programming October 13, 2016 19 / 42


Inter-Process Communication

Pipes

Pipes are used for example for shell commands like:

#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

Pipes can only link processes which have a common ancestor


Because children processes inherit the file descriptors from their parent

Wondimagegn D. (AAIT ) Distributed System Programming October 13, 2016 20 / 42


Inter-Process Communication

Shared Memory

Shared memory allows multiple processes to have direct access to the


same memory area
They can interact through the shared memory segment
Shared memory segments must be created, then attached to a process
to be usable. Then, you must detach and destroy it afterwards.
When using shared memory, you must be very careful about race
conditions, and solve them thanks to semaphores.

Wondimagegn D. (AAIT ) Distributed System Programming October 13, 2016 21 / 42


Inter-Process Communication

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 >

Wondimagegn D. (AAIT ) Distributed System Programming October 13, 2016 22 / 42


Inter-Process Communication

Shared Memory

To create a shared memory 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 shmget ( k e y t key , i n t s i z e , i n t s h m f l g ) ;

key: rendezvous point key=IPC PRIVATE if it will be used by


children processes
size: size of the segment in bytes
shmflg: options (access control mask)
Return value: a shm identifier (or -1 for error)

Wondimagegn D. (AAIT ) Distributed System Programming October 13, 2016 23 / 42


Inter-Process Communication

Shared Memory

To attach a shared memory segment:

#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 ) ;

shmid: shared memory identifier (returned by shmget)


shmaddr: address where to attach the segment, or NULL if you don’t
care
shmflg: options (access control mask)
To detach a shared memory segment:
i n t shmdt ( c o n s t v o i d ∗shmaddr ) ;

shmaddr: segment address


Attention: shmdt() does not destroy the segment!

Wondimagegn D. (AAIT ) Distributed System Programming October 13, 2016 24 / 42


Inter-Process Communication

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 >

Wondimagegn D. (AAIT ) Distributed System Programming October 13, 2016 25 / 42


Inter-Process Communication

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 ) ;
}
}

Wondimagegn D. (AAIT ) Distributed System Programming October 13, 2016 26 / 42


Posix Thread Programming

Threads vs. Processes

Multi-process programs are expensive:


fork() needs to copy all the process’ memory, etc.
Inter-process communication is hard
Threads: ”lightweight processes”
One process contains several “threads of execution”
All threads execute the same program (but can be at different stages
within it)
All threads share process instructions, global memory, open files and
signal handlers
But each thread has its own thread ID, stack, program counter and
stack pointer, errno and signal mask
There are special synchronization primitives between threads of the
same process

Wondimagegn D. (AAIT ) Distributed System Programming October 13, 2016 27 / 42


Posix Thread Programming

Threads in C and Java

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)

Wondimagegn D. (AAIT ) Distributed System Programming October 13, 2016 28 / 42


Posix Thread Programming

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 );

Wondimagegn D. (AAIT ) Distributed System Programming October 13, 2016 29 / 42


Posix Thread Programming

Stopping a Pthread

A pthread stops when:


Its process stops
Its parent thread stops
Its start routine function returns
It calls pthread exit:

#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 ) ;

Like processes, stopped threads must be waited for:

#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 ) ;

Wondimagegn D. (AAIT ) Distributed System Programming October 13, 2016 30 / 42


Posix Thread Programming

Pthread Create/Delete Example

#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 ) ;
}

Wondimagegn D. (AAIT ) Distributed System Programming October 13, 2016 31 / 42


Posix Thread Programming

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 ) ;

You can also detach a thread later with pthread detach()

Wondimagegn D. (AAIT ) Distributed System Programming October 13, 2016 32 / 42


Posix Thread Programming

Pthread Synchronization with Mutex

Pthreads have two synchronization concepts: mutex and condition


variables
Mutex: mutual exclusion
#i n c l u d e <p t h r e a d . h>
int pthread mutexattr init ( pthread mutexattr t ∗attr );
i n t p t h r e a d m u t e x i n i t ( p t h r e a d m u t e x t ∗mutex ,
const pthread mutexattr t ∗mutexattr ) ;
i n t p t h r e a d m u t e x l o c k ( p t h r e a d m u t e x t ∗mutex ) ) ;
i n t p t h r e a d m u t e x t r y l o c k ( p t h r e a d m u t e x t ∗mutex ) ;
i n t p t h r e a d m u t e x u n l o c k ( p t h r e a d m u t e x t ∗mutex ) ;
i n t p t h r e a d m u t e x d e s t r o y ( p t h r e a d m u t e x t ∗mutex ) ;

Wondimagegn D. (AAIT ) Distributed System Programming October 13, 2016 33 / 42


Posix Thread Programming

Pthread Synchronization with Mutex

Pthreads have two synchronization concepts: mutex and condition


variables
Mutex: mutual exclusion

pthread mutex t array mutex ;


i n t add elem ( i n t elem ) {
int n;
p t h r e a d m u t e x l o c k (& a r r a y m u t e x ) ;
i f ( n b e l e m s ==32) { p t h r e a d m u t e x u n l o c k (& a r r a y m u t e x ) ; r e t u r n −1; }
a r r a y [ n b e l e m s ++] = e l e m ;
n = nbelems ;
p t h r e a d m u t e x u n l o c k (& a r r a y m u t e x ) ;
return (n );
}
i n t main ( ) {
pthread mutexattr t attr ;
p t h r e a d m u t e x a t t r i n i t (& a t t r ) ;
p t h r e a d m u t e x i n i t (& a r r a y m u t e x , &a t t r ) ;
...
p t h r e a d m u t e x d e s t r o y (& a r r a y m u t e x ) ;
}

Wondimagegn D. (AAIT ) Distributed System Programming October 13, 2016 34 / 42


Java Thread Programming

Creating Java Threads

A Java thread is a class which inherits from Thread


You must overload its run() method:
p u b l i c c l a s s MyThread e x t e n d s Thread
{

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 ) ;
}
}

To start the thread:

MyThread t = new MyThread ( 4 2 ) ;


t . start ();

Wondimagegn D. (AAIT ) Distributed System Programming October 13, 2016 35 / 42


Java Thread Programming

Stopping Java Threads

A Java thread stops when its run() method returns


You do not need to join() for a Java thread to finish

MyThread t = new MyThread ( 4 2 ) ;


t . start ();
...
t . join ();

Wondimagegn D. (AAIT ) Distributed System Programming October 13, 2016 36 / 42


Java Thread Programming

Java Thread Synchronization with Monitors

A monitor is similar to a mutex:

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 ( )
{ ... }
}

Each object contains one mutex, which is locked when entering a


synchronized method and unlocked when leaving
Two synchronized methods from the same object cannot be executing
simultaneously
Two different objects from the same class can be executing the same
synchronized method simultaneously

Wondimagegn D. (AAIT ) Distributed System Programming October 13, 2016 37 / 42


Java Thread Programming

Java Thread Synchronization with Monitors

So, the previous class is equivalent to:

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 !

Wondimagegn D. (AAIT ) Distributed System Programming October 13, 2016 38 / 42


Java Thread Programming

Condition Variables in Java


There is no real condition variable in Java
But you can explicitly block a thread
All Java classes inherit from class Object:

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

Condition Variables in Java

This means that each object contains one (and no more) condition
variable
The wait(), notify() and notifyAll() methods must be called inside a
monitor

Wondimagegn D. (AAIT ) Distributed System Programming October 13, 2016 40 / 42


Virtualization

Virtualization

Virtualization is becoming increasingly important:


Hardware changes faster than software
Ease of portability and code migration
Isolation of failing or attacked components

Program

Interface A
Program Implementation of
mimicking A on B
Interface A Interface B

Hardware/software system A Hardware/software system B

(a) (b)

Wondimagegn D. (AAIT ) Distributed System Programming October 13, 2016 41 / 42


Virtualization

Architecture of VMs

Virtualization
Virtualization can take place at very different levels, strongly depending on
the interfaces as offered by various systems components:

Library functions Application

Library
System calls

Privileged Operating system General


instructions instructions
Hardware

Wondimagegn D. (AAIT ) Distributed System Programming October 13, 2016 42 / 42

You might also like