25
Software Engineering Lecture 5 Multiprogramming and Scheduling ASPI8-4 Anders P. Ravn March 2004

Software Engineering Lecture 5 Multiprogramming and Scheduling ASPI8-4 Anders P. Ravn March 2004

  • View
    218

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Software Engineering Lecture 5 Multiprogramming and Scheduling ASPI8-4 Anders P. Ravn March 2004

Software Engineering

Lecture 5

Multiprogramming and Scheduling

ASPI8-4Anders P. Ravn March 2004

Page 2: Software Engineering Lecture 5 Multiprogramming and Scheduling ASPI8-4 Anders P. Ravn March 2004

Overview

1. Concurrent processes - Java Threads

2. Mutual exclusion

3. Semaphores

4. Monitors - Java synchronized, wait, notify

5. Ada rendezvous

Page 3: Software Engineering Lecture 5 Multiprogramming and Scheduling ASPI8-4 Anders P. Ravn March 2004

A kernel specification/* kernel.hInterface to a lightweight kernel that implements concurrent processes and a release primitive `pause'.Anders P. Ravn, DTU, Denmark 24 April [email protected]*/

typedef void (*Program)(void); /* A program text is a function without parameters*/ typedef void * Thread; /* identifier for a process */

extern Thread create(Program p,unsigned int stacksize); /* creates a process with a stack of the specified size and starts it executing the program. If there is insufficient memory, the result is NULL */

extern void pause(void); /* release the processor */

Page 4: Software Engineering Lecture 5 Multiprogramming and Scheduling ASPI8-4 Anders P. Ravn March 2004

Multiprogramming#include ”kernel.h”void process() { /* do something */ pause(); /* do something */}

void main() { Thread p1, p2;

p1 = create(&process,2000); /* p1 is started */ p2 = create(&process,1000); /* p2 is started */

/* the kernel will see to it that main is left when both p1 and p2 has exited */}

Page 5: Software Engineering Lecture 5 Multiprogramming and Scheduling ASPI8-4 Anders P. Ravn March 2004

A kernel implementation Itypedef unsigned long Register;

typedef struct x {struct x* next; Register esp;} Threaddescriptor;

static Threaddesriptor* ready; /* queue of threads linked cyclically; ready points to last, the first is current */

#define current ready->next

esp1 esp2 esp3

ready:

current

Page 6: Software Engineering Lecture 5 Multiprogramming and Scheduling ASPI8-4 Anders P. Ravn March 2004

A kernel implementation IIvoid pause() {Register sp; __asm__(” pushal movl %%esp,%sp"); /* sp -> |saved registers ... | eip return from call */DISABLE; /* scheduling */ current->esp = sp; ready = current; sp = current->esp;__asm__(" movl %sp,%%esp popal" );ENABLE; }

esp1 esp2 esp3

ready:

current

stack1 stack2

Page 7: Software Engineering Lecture 5 Multiprogramming and Scheduling ASPI8-4 Anders P. Ravn March 2004

A kernel implementation III

pause: pushl %ebp movl %esp,%ebp

pushal movl %esp,%ecx sp = espmovl ready,%eaxmovl (%eax),%edx current->esp movl %ecx,4(%edx) = spmovl %edx,ready ready = currentmovl (%edx),%edx movl 4(%edx),%ecx sp = current->espmovl %ecx,%esppopalleaveret

Page 8: Software Engineering Lecture 5 Multiprogramming and Scheduling ASPI8-4 Anders P. Ravn March 2004

A kernel implementation IV

pause:pushal movl ready,%eaxmovl (%eax),%edx current->esp movl %esp,4(%edx) = espmovl %edx,ready ready = currentmovl (%edx),%edx movl 4(%edx),%esp esp = current->esppopalret

Page 9: Software Engineering Lecture 5 Multiprogramming and Scheduling ASPI8-4 Anders P. Ravn March 2004

Java Threads

import java.awt.*;

class Process extends Thread {

public Customer(...){ ...}

public void run(){... // do something }

...

Process p1 = new Process(); p1.start();

...

Page 10: Software Engineering Lecture 5 Multiprogramming and Scheduling ASPI8-4 Anders P. Ravn March 2004

Shared Variablesclass Banking { /* shared variable balance and private withdrawals */ public int balance; public int[] wd; public Banking() { balance = 2000; wd = new int[2]; }

// Invariant: // balance+wd[0]+wd[1] == 2000}

Page 11: Software Engineering Lecture 5 Multiprogramming and Scheduling ASPI8-4 Anders P. Ravn March 2004

Critical Sectionclass Customer extends Thread { int id; Lock critical; Banking bank; public void run() { do { sleep(800-400*id); critical.enter(id); int local = bank.balance; sleep(shortdelay); bank.balance = local-1; critical.leave(id); bank.wd[id]++; } while (true); } }

Page 12: Software Engineering Lecture 5 Multiprogramming and Scheduling ASPI8-4 Anders P. Ravn March 2004

Semaphorepublic class Semaphore { int count; public Semaphore(int initial_value){ count= initial_value; } public synchronized void Wait(){ while(count == 0) wait(); --count; } public synchronized void signal(){ if (count++ == 0) notify(); }}

Page 13: Software Engineering Lecture 5 Multiprogramming and Scheduling ASPI8-4 Anders P. Ravn March 2004

Rendezvous

Page 14: Software Engineering Lecture 5 Multiprogramming and Scheduling ASPI8-4 Anders P. Ravn March 2004

Scheduling

1. Periodic processes – cyclic executive2. Fixed Priority Scheduling – Rate Monotonic3. Response Time Analysis4. Sporadic Processes5. Blocking and priority inversion6. Priority Ceiling protocols7. Real-Time Java

Page 15: Software Engineering Lecture 5 Multiprogramming and Scheduling ASPI8-4 Anders P. Ravn March 2004

Cyclic executive

loop wait 25msinterrupt; a(); b(); c(); wait 25ms interrupt; a(); b(); d(); e(); wait 25ms interrupt; a(); b(); c(); wait 25ms interrupt; a(); b(); d();end loop;

Process T (period) C (wcet)

a 25 10

b 25 8

c 50 5

d 50 4

e 100 2

Page 16: Software Engineering Lecture 5 Multiprogramming and Scheduling ASPI8-4 Anders P. Ravn March 2004

Utilization tests

Utilization U = C/T

Priority is rate (1/T) monotonic

U1 + ... + UN N( N2 – 1) 0.693 (FPS)

U1 + ... + UN 1 (Earliest Deadline First !?)

Liu & Layland JACM, 1973

Page 17: Software Engineering Lecture 5 Multiprogramming and Scheduling ASPI8-4 Anders P. Ravn March 2004

Response Time Analysis

Response time R= C + I -- Interference

Ii = Ri /TN CN + ... + Ri /Ti+1Ci+1 (FPS)

Joseph & Pandya Computer Journal 1986

Page 18: Software Engineering Lecture 5 Multiprogramming and Scheduling ASPI8-4 Anders P. Ravn March 2004

Sporadic Processes

Deadline D < T

Priority is deadline (1/D) monotonic

Page 19: Software Engineering Lecture 5 Multiprogramming and Scheduling ASPI8-4 Anders P. Ravn March 2004

Blocking

Critical Regions V and Q locked by eg a semaphore.

d(Q,V): EEEEBQ-----------BQQVVEE

c(V) : EEVV----VVEE

b() : ------------EEEE

a(Q) : EEQQ----------------QQQQQQ------EE

Priority Inversion

Page 20: Software Engineering Lecture 5 Multiprogramming and Scheduling ASPI8-4 Anders P. Ravn March 2004

Response Time Analysis

Response time R= C + B + I

K = (k1,..., km): resources used by a process of lower priority and by a process with a higher or equal prority

Bi = Ck1 + ... + Ckm

Page 21: Software Engineering Lecture 5 Multiprogramming and Scheduling ASPI8-4 Anders P. Ravn March 2004

Immediate Ceiling Protocol

A resource uses the maximual priority of any process using it.

K = (k1,..., km): resources used by a process of lower priority and by a process with a higher or equal prority

Bi = max C(k), k K

Page 22: Software Engineering Lecture 5 Multiprogramming and Scheduling ASPI8-4 Anders P. Ravn March 2004

Blocking ICPPd(Q,V): EEEEBQ-----------BQQVVEE

c(V) : EEVV----VVEE

b() : ------------EEEE

a(Q) : EEQQ----------------QQQQQQ------EE

-------------------

d(Q,V): BBEEEEQQVVEE

c(V) : BBBBBB----------EEVVVVEE

b() : BBBBBB------------------EEEE

a(Q) : EEQQQQQQQQ----------------------EE

Page 23: Software Engineering Lecture 5 Multiprogramming and Scheduling ASPI8-4 Anders P. Ravn March 2004

Real-Time Java

public class Periodic extends RealTimeThread{

public Periodic(PriorityParameters pp, PeriodicParameters p) { ... } public void run(){ for (;;) { ... waitForNextPeriod(); } }}

Page 24: Software Engineering Lecture 5 Multiprogramming and Scheduling ASPI8-4 Anders P. Ravn March 2004

PeriodicParameters

public class Periodicparameters ... {

public PeriodicParameters( HighResolutionTime start, RelativeTime period, RelativeTime cost, RelativeTime deadline, AsyncEventHandler overrunhandler, AsyncEventHandler misshandler){ ... }}

Page 25: Software Engineering Lecture 5 Multiprogramming and Scheduling ASPI8-4 Anders P. Ravn March 2004

And more

http://www.rtj.org