Chapter 3 Process Scheduling - PowerPoint PPT Presentation

1 / 32
About This Presentation
Title:

Chapter 3 Process Scheduling

Description:

(or CPU scheduler) selects which process should be executed next and allocates CPU ... Just simply put every new process in memory for short-term scheduler ... – PowerPoint PPT presentation

Number of Views:28
Avg rating:3.0/5.0
Slides: 33
Provided by: csG7
Category:

less

Transcript and Presenter's Notes

Title: Chapter 3 Process Scheduling


1
Chapter 3 Process Scheduling
  • Bernard Chen
  • Spring 2007

2
Schedulers
  • Long-term scheduler
  • (or job scheduler) selects which processes
    should be brought into the ready queue
  • Short-term scheduler
  • (or CPU scheduler) selects which process
    should be executed next and allocates CPU

3
Schedulers
  • On some systems, the long-term scheduler maybe
    absent or minimal
  • Just simply put every new process in memory for
    short-term scheduler
  • The stability depends on physical limitation or
    self-adjustment nature of human users

4
Schedulers
  • Sometimes it can be advantage to remove process
    from memory and thus decrease the degree of
    multiprogrammimg
  • This scheme is called swapping

5
Addition of Medium Term Scheduling
6
Share Memory Parallelization System Example
  • m_set_procs(number) prepare number of child for
    execution
  • m_fork(function) childes execute function
  • m_kill_procs() terminate childs

7
Real Example
  • main(argc , argv)
  • int nprocs9
  • m_set_procs(nprocs) / prepare to launch this
    many processes /
  • m_fork(slaveproc) / fork out processes /
  • m_kill_procs() / kill activated
    processes /
  • void slaveproc()
  • int id
  • id m_get_myid()
  • m_lock()
  • printf(" Hello world from process d\n",id)
  • printf(" 2nd line Hello world from process
    d\n",id)
  • m_unlock()

8
  • include "stdio.h"
  • include "sys/types.h"
  • include "sys/times.h"
  • include "ulocks.h"
  • include "sys/param.h"
  • include "math.h"
  • define MAXTHRDS 6

9
Real Example
  • int array_size1000
  • int global_arrayarray_size
  • main(argc , argv)
  • int nprocs4
  • m_set_procs(nprocs) / prepare to launch this
    many processes /
  • m_fork(sum) / fork out processes /
  • m_kill_procs() / kill activated
    processes /
  • void sum()
  • int id
  • id m_get_myid()
  • for (iid(array_size/nprocs)
    ilt(id1)(array_size/nprocs) i)
  • global_arrayidarray_size/nprocsglobal_array
    i

10
Cooperating Processes
  • Independentprocess cannot affect or be affected
    by the execution of another process
  • Cooperatingprocess can affect or be affected by
    the execution of another process
  • Advantages of process cooperation
  • Information sharing
  • Computation speed-up
  • Modularity
  • Convenience

11
Interprocess Cpmmunication (IPC)
  • Two fundamental models
  • Share Memory
  • Message Passing

12
Communication Models
  • (a) MPI (b) Share memory

13
Shared-Memory Systems
  • Consider the producer-consumer problem
  • A producer process produce information that is
    consumed by customer process, for example, a web
    server produces HTML files and images which are
    consumed by client web browser

14
Shared-Memory Systems
  • The producer and consumer must be synchronized,
    so that consumer does not try to consume an item
    that has not yet been produced.
  • Two types of buffer can be used
  • Unbounded buffer
  • Bounded buffer

15
Shared-Memory Systems
  • Unbounded Buffer the consumer may have to wait
    for new items, but producer can always produce
    new items.
  • Bounded Buffer the consumer have to wait if
    buffer is empty, the producer have to wait if
    buffer is full

16
Bounded Buffer
  • define BUFFER_SIZE 6
  • Typedefstruct
  • . . .
  • item
  • item bufferBUFFER_SIZE
  • intin 0
  • intout 0

17
Bounded Buffer (producer iew)
  • while (true)
  • / Produce an item /
  • while (((in (in 1) BUFFER SIZE count)
    out)
  • / do nothing --no free buffers /
  • bufferin item
  • in (in 1) BUFFER SIZE

18
Bounded Buffer (Consumer view)
  • while (true)
  • while (in out)
  • // do nothing --nothing to consume
  • // until remove an item from the buffer
  • item bufferout
  • out (out 1) BUFFER SIZE
  • return item

19
Message-Passing Systems
  • A message passing facility provides at least two
    operations send(message), receive(message)

20
Message-Passing Systems
  • If 2 processes want to communicate, a
    communication link must exist
  • It has the following variations
  • Direct or indirect communication
  • Synchronize or asynchronize communication
  • Automatic or explicit buffering

21
Message-Passing Systems
  • Direct communication
  • send(P, message)
  • receive(Q, message)
  • Properties
  • A link is established automatically
  • A link is associated with exactly 2 processes
  • Between each pair, there exists exactly one link

22
Message-Passing Systems
  • Indirect communication the messages are sent to
    and received from mailbox
  • send(A, message)
  • receive(A, message)

23
Message-Passing Systems
  • Properties
  • A link is established only if both members of the
    pair have a shared mailbox
  • A link is associated with more than 2 processes
  • Between each pair, there exists a number of links

24
Message-Passing Systems
  • Mailbox sharing
  • P1, P2, andP3 share mailbox A
  • P1, sends P2 andP3 receive
  • Who gets the message?
  • Solutions
  • Allow a link to be associated with at most two
    processes
  • Allow only one process at a time to execute a
    receive operation
  • Allow the system to select arbitrarily the
    receiver. Sender is notified who the receiver was.

25
Message-Passing Systems
  • If the mailbox owned by process, it is easy to
    tell who is the owner and user.
  • And there is no confuse we send the message and
    who receives it.
  • When process terminates, the mailbox disappear

26
Message-Passing Systems
  • If the mailbox owned by OS, it requires the
    following functions
  • Create a new mailbox
  • Send and Receive message through the mailbox
  • Delete a mailbox

27
Message-Passing Systems
  • Synchronization synchronous and asynchronous
  • Blocking is considered synchronous
  • Blocking send has the sender block until the
    message is received
  • Blocking receive has the receiver block until a
    message is available

28
Message-Passing Systems
  • Non-blockingis considered asynchronous
  • Non-blocking send has the sender send the message
    and continue
  • Non-blocking receive has the receiver receive a
    valid message or null

29
Message-Passing Systems
  • Buffering Queue of messages attached to the
    link, there are 3 variations
  • Zero capacity 0 messages
  • Sender must wait for receiver
  • 2.Bounded capacity finite length of n
  • messages, sender must wait if link full
  • 3.Unbounded capacity infinite length
  • Sender never waits

30
MPI Program example
  • include "mpi.h"
  • include ltmath.hgt
  • include ltstdio.hgt
  • include ltstdlib.hgt
  • int main (int argc, char argv)
  • int id / Process rank /
  • int p / Number of processes
    /
  • int i,j
  • int array_size100
  • int arrayarray_size / or array
    and then use malloc or vector to increase the
    size /
  • int local_arrayarray_size/p
  • int sum0
  • MPI_Status stat
  • MPI_Comm_rank (MPI_COMM_WORLD, id)
  • MPI_Comm_size (MPI_COMM_WORLD, p)

31
MPI Program example
  • if (id0)
  • for(i0 iltarray_size i)
  • arrayii / initialize array/
  • for(i0 iltp i)
  • MPI_Send(arrayiarray_size/p,
    / Start from/
  • array_size/p,
    / Message size/
  • MPI_INT,
    / Data type/
  • i,
    / Send to which process/
  • MPI_COMM_WORLD)
  • else
  • MPI_Recv(local_array0,array_size/p,MPI_IN
    T,0,0,MPI_COMM_WORLD,stat)

32
MPI Program example
  • for(i0iltarray_size/pi)
  • sumarrayi
  • MPI_Reduce (sum, sum, 1, MPI_INT, MPI_SUM,
    0, MPI_COMM_WORLD)
  • if (id0)
  • printf("d ",sum)
Write a Comment
User Comments (0)
About PowerShow.com