Semaphores and Mailboxes - PowerPoint PPT Presentation

About This Presentation
Title:

Semaphores and Mailboxes

Description:

Semaphores and Mailboxes – PowerPoint PPT presentation

Number of Views:268
Avg rating:3.0/5.0
Slides: 19
Provided by: bina1
Learn more at: https://cse.buffalo.edu
Category:

less

Transcript and Presenter's Notes

Title: Semaphores and Mailboxes


1
Semaphores and Mailboxes
  • B. Ramamurthy

2
Critical sections and Semaphores
  • When multiples tasks are executing there may be
    sections where only one task could execute at a
    given time critical region or critical section
  • There may be resources which can be accessed only
    be one of the processes critical resource
  • Semaphores can be used to ensure mutual exclusion
    to critical sections and critical resources

3
Semaphores in exinu
  • include ltkernel.hgt
  • include ltqueue.hgt /lt queue.h must define
    of sem queues /
  • / Semaphore state definitions /
  • define SFREE 0x01 /lt this semaphore is
    free /
  • define SUSED 0x02 /lt this semaphore is
    used /
  • / type definition of "semaphore" /
  • typedef ulong semaphore
  • / Semaphore table entry /
  • struct sentry
  • char state /lt the state SFREE
    or SUSED /
  • short count /lt count for this
    semaphore /
  • queue queue /lt requires q.h.
    /

4
Semaphores in exinu (contd.)
  • extern struct sentry semtab
  • /
  • isbadsem - check validity of reqested
    semaphore id and state
  • _at_param s id number to test NSEM is declared
    to be 100 in kernel.h
  • A system typically has a predetermined
    limited number of semaphores
  • /
  • define isbadsem(s) (((ushort)(s) gt NSEM)
    (SFREE semtabs.state))
  • / Semaphore function declarations /
  • syscall wait(semaphore)
  • syscall signal(semaphore)
  • syscall signaln(semaphore, short)
  • semaphore newsem(short)
  • syscall freesem(semaphore)
  • syscall scount(semaphore)

5
Definition of Semaphores functions
  • static semaphore allocsem(void)
  • /
  • newsem - allocate and initialize a new
    semaphore.
  • _at_param count - number of resources available
    without waiting.
  • example count 1 for mutual exclusion lock
  • _at_return new semaphore id on success, SYSERR on
    failure
  • /
  • semaphore newsem(short count)
  • irqmask ps
  • semaphore sem
  • ps disable() /
    disable interrupts /
  • sem allocsem() /
    request new semaphore /
  • if ( sem ! SYSERR count gt 0 ) /
    safety check /
  • semtabsem.count count
    / initialize count /
  • restore(ps)
    / restore interrupts /
  • return sem
    / return semaphore id /

6
Semaphore newsem contd.
  • /
  • allocsem - allocate an unused semaphore and
    return its index.
  • Scan the global semaphore table for a free
    entry, mark the entry
  • used, and return the new semaphore
  • _at_return available semaphore id on success,
    SYSERR on failure
  • /
  • static semaphore allocsem(void)
  • int i 0
  • while(i lt NSEM) /
    loop through semaphore table /
  • /
    to find SFREE semaphore /
  • if( semtabi.state SFREE )
  • semtabi.state SUSED
  • return i
  • i
  • return SYSERR

7
Semaphore wait()
  • /
  • wait - make current process wait on a
    semaphore
  • _at_param sem semaphore for which to wait
  • _at_return OK on success, SYSERR on failure
  • /
  • syscall wait(semaphore sem)
  • irqmask ps
  • struct sentry psem
  • pcb ppcb
  • ps disable() / disable
    interrupts /
  • if ( isbadsem(sem) ) / safety
    check /
  • restore(ps)
  • return SYSERR
  • ppcb proctabcurrpid / retrieve
    pcb from process table /
  • psem semtabsem / retrieve
    semaphore entry /
  • if( --(psem-gtcount) lt 0 ) / if
    requested resource is unavailable /

8
Semaphore wait()
  • ppcb-gtsem sem / record semaphore id in pcb /
  • enqueue(currpid, psem-gtqueue)
  • resched() / place
    in wait queue and reschedule /
  • restore(ps) / restore
    interrupts /
  • return OK

9
Semaphore signal()
  • /signal - signal a semaphore, releasing one
    waiting process, and block
  • _at_param sem id of semaphore to signal
  • _at_return OK on success, SYSERR on failure
  • /
  • syscall signal(semaphore sem)
  • irqmask ps
  • register struct sentry psem
  • ps disable() / disable
    interrupts /
  • if ( isbadsem(sem) ) / safety
    check /
  • restore(ps)
  • return SYSERR
  • psem semtabsem /
    retrieve semaphore entry /
  • if ( (psem-gtcount) lt 0 ) / release
    one process from wait queue /
  • ready(dequeue(psem-gtqueue),
    RESCHED_YES)
  • restore(ps) / restore
    interrupts /

10
Semaphore usage
  • Problem 1
  • Create 3 tasks that each sleep for a random time
    and update a counter.
  • Counter is the critical resources shared among
    the processes.
  • Only one task can update the counter at a time so
    that counter value is correct.
  • Problem 2
  • Create 3 tasks task 1 updates the counter by 1
    and then signal task 2 that updates the counter
    by 2 and then signals task 3 to update the
    counter by 3.

11
Problem 1
  • include lt..gt
  • //declare semaphore
  • semaphore mutex1 newsem(1)
  • int counter 0
  • //declare functions proc1,proc1, proc3
  • ready(create((void )proc1, INITSTK, INITPRIO,
    PROC1",, 2, 0, NULL), RESCHED_NO)
  • ready(create((void )proc2, INITSTK, INITPRIO,
    PROC2",, 2, 0, NULL), RESCHED_NO)
  • ready(create((void )proc3, INITSTK, INITPRIO,
    PROC3",, 2, 0, NULL), RESCHED_NO)

12
Problem 1 multi-tasks
  • void proc1()
  • while (1)
  • sleep (rand()10)
  • wait(mutex1)
  • counter
  • signal(mutex1)
  • void proc2()
  • while (1)
  • sleep (rand()10)
  • wait(mutex1)
  • counter
  • signal(mutex1)
  • //similarly proc3
  • Simulation of this

13
Problem 1
Task 1
Task 2
Counter1
Task 3
14
Problem 2
  • semaphore synch12 newsem(0)
  • semaphore synch23 newsem(0)
  • semaphore synch31 newsem(0)
  • ready(create((void )proc1, INITSTK, INITPRIO,
    PROC1",, 2, 0, NULL), RESCHED_NO)
  • ready(create((void )proc2, INITSTK, INITPRIO,
    PROC2",, 2, 0, NULL), RESCHED_NO)
  • ready(create((void )proc3, INITSTK, INITPRIO,
    PROC3",, 2, 0, NULL), RESCHED_NO)
  • signal(synch31)

15
Task flow
16
UART and UART Drivers
Inside the system
Outside
Actual UART Buffer (input, output) Registers (Cont
rol, status)
UART Driver
17
Reverse Engineer UART read, write and intr
(interrupt)
18
UART Operation and code (contd.)
  • We will look at the sequence diagrams for other
    UART operations and read the code.
  • Lets look at initialize.c
Write a Comment
User Comments (0)
About PowerShow.com