6564533 Semaphores

Embed Size (px)

Citation preview

  • 7/28/2019 6564533 Semaphores

    1/9

    Semaphores

  • 7/28/2019 6564533 Semaphores

    2/9

    Sleeping Barber

    Hypothetical barber shop

    1 barber

    1 barber chair

    A number of chairs for waiting customers

    When the barber has no customers, he sleeps

    When a customer arrives, he either

    Sits in the barber chair and wakes the barber Sits in a vacant waiting chair

    Leaves if the barber shop is full

  • 7/28/2019 6564533 Semaphores

    3/9

    What does this have to do with

    anything?

    Analogy for a classic CS problem

    Barber and customers are processes or

    Tasks

    Inter-process communication and

    synchronization

    Most common solution is to use

    semaphores

  • 7/28/2019 6564533 Semaphores

    4/9

    Sleeping Barber Semaphores

    Customer arrives and looks for an empty seat.

    He either

    Sits in an empty seat and signals the barber with a

    semaphore Leaves if there are no empty seats

    If the barber was asleep, a waiting customers

    semaphore wakes him up

    If the barber has multiple customers waiting, he

    uses a semaphore to signal them when hes

    finished with a previous customer

  • 7/28/2019 6564533 Semaphores

    5/9

    Semaphores - Definition

    A semaphore is a protected variable (or

    abstract data type) and constitutes the

    classic method for restricting access to

    shared resources (e.g. storage) in amultiprogramming environment.

    Basically, simple inter-process or inter-

    task communication

  • 7/28/2019 6564533 Semaphores

    6/9

    How will we use them?

    Semaphores are known to Salvo as

    events

    salvocfg.h

    #define OSEVENTS (# of semaphores youll use)

    #define OSTASKS (# of tasks youll use)

    Semaphores are defined in main.h

    main.h #define (semaphore name) OSECBP(# of semaphore)

    #define BINSEM_CMD_CHAR_P OSECBP(1)

  • 7/28/2019 6564533 Semaphores

    7/9

    How will we use them? (2)

    In the main loop, we must create the event (just likecreating tasks) main.c

    OSCreateBinSem(semaphore name, number);

    OSCreateBinSem(BINSEM_CMD_CHAR_P, 0);

    Now we can have Tasks wait for this semaphore In a task (or anywhere you want to wait)

    OS_WaitBinSem(semaphore name, how long to wait, label);

    OS_WaitBinSem(BINSEM_CMD_CHAR_P, OSNO_TIMEOUT,label);

    We can also have Tasks or other parts of the programsend out this semaphore Anywhere you want to send the signal

    OSSignalBinSem(semaphore name);

    OSSignalBinSem(BINSEM_CMD_CHAR_P);

  • 7/28/2019 6564533 Semaphores

    8/9

    Sleeping Barber

    main.h has the lines

    #define CUSTOMER_WAITING OSECBP(1)

    #define BARBER_READY OSECBP(2)

    Barber task

    for(;;){

    OS_WaitBinSem(CUSTOMER_WAITING,

    OSNO_TIMEOUT, label);

    wakeup();cuthair(customer);

    OSSignalBinSem(BARBER_READY);

    sleep();

    }

    Customer task

    for(;;){

    If (chair available){

    OSSignalBinSem(CUSTOMER_WAITING);

    sit(chair);}

    If (sitting && next){

    OS_WaitBinSem(BARBER_READY,

    OSNO_TIMEOUT, label);

    sit(barber chair);

    OSSignalBinSem(CUSTOMER_WAITING);

    }}

  • 7/28/2019 6564533 Semaphores

    9/9

    Todays Lab

    Want the Cubesat kit to spit out over the debug channelany characters it receives over the radio. I received blah

    When this is working over debug, try transmitting it

    If the character is a t, transmit the onboard temperaturereading

    Well be passing out sensors and data sheets Figure out how to use them

    Design a circuit to use them

    Write the code to use them

    Can also begin pseudo-coding your rovers TaskDoCmds

    If (cmd==w) { move_forward(); }