Opera&ng Systems ECE344 Lab assignment 1: - - PowerPoint PPT Presentation

opera ng systems ece344
SMART_READER_LITE
LIVE PREVIEW

Opera&ng Systems ECE344 Lab assignment 1: - - PowerPoint PPT Presentation

Opera&ng Systems ECE344 Lab assignment 1: Synchroniza5on Ding Yuan Overview of the lab assignment Task 1: implemen&ng synchroniza&on primi&ves


slide-1
SLIDE 1

Opera&ng ¡Systems ¡ ECE344 ¡

¡

Ding ¡Yuan ¡

Lab ¡assignment ¡1: ¡Synchroniza5on ¡

slide-2
SLIDE 2

Overview ¡of ¡the ¡lab ¡assignment ¡

  • Task ¡1: ¡implemen&ng ¡synchroniza&on ¡primi&ves ¡

– 1a: ¡implement ¡lock ¡ – 1b: ¡implement ¡condi&on ¡variable ¡

  • Task ¡2: ¡use ¡synchroniza&on ¡primi&ves ¡to ¡solve ¡problems ¡

– 2a: ¡Mice ¡and ¡cats ¡ – 2b: ¡traffic ¡lights ¡

Ding ¡Yuan, ¡ECE344 ¡Opera&ng ¡System 2 ¡

slide-3
SLIDE 3

Lock ¡and ¡cond. ¡var. ¡

  • Needs atomic region

– Atomic region can be done in a similar way to semaphore

  • If you understand how semaphore is implemented, should be trivial!

– Cannot use semaphore to implement lock or cond. var.

Ding ¡Yuan, ¡ECE344 ¡Opera&ng ¡System ¡ 3 ¡

P(sem) ¡{ ¡ ¡ ¡Disable ¡interrupts; ¡ ¡ ¡while ¡(sem-­‑>count ¡== ¡0) ¡{ ¡ ¡ ¡ ¡ ¡thread_sleep(sem); ¡/* ¡current ¡thread ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡will ¡sleep ¡on ¡this ¡sem ¡*/ ¡ ¡ ¡} ¡ ¡ ¡sem-­‑>count-­‑-­‑; ¡ ¡ ¡Enable ¡interrupts; ¡ } ¡ V(sem) ¡{ ¡ ¡ ¡Disable ¡interrupts; ¡ ¡ ¡sem-­‑>count++; ¡ ¡ ¡thread_wakeup ¡(sem); ¡/* ¡this ¡will ¡wake ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡up ¡all ¡the ¡threads ¡wai&ng ¡on ¡this ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡sem. ¡Why ¡wake ¡up ¡all ¡threads? ¡*/ ¡ ¡ ¡ ¡ ¡Enable ¡interrupts; ¡ } ¡

slide-4
SLIDE 4

Synchroniza&on ¡problems ¡

  • How to start?

– First: write operation code – Next: carefully reason about all the possible interleaving and timing scenarios – Add synchronization

Ding ¡Yuan, ¡ECE344 ¡Opera&ng ¡System ¡ 4 ¡

slide-5
SLIDE 5

Mice ¡and ¡cats ¡

  • Two bowls, multiple cats and mice
  • Safety criteria:

– If a cat is eating at either dishes, no mouse can eat – If a mouse is eating at either dishes, no cat can eat

Ding ¡Yuan, ¡ECE344 ¡Opera&ng ¡System ¡ 5 ¡

slide-6
SLIDE 6

Opera&on ¡code ¡

Ding ¡Yuan, ¡ECE344 ¡Opera&ng ¡System ¡ 6 ¡

void sem_eat(const char *who, int num, int bowl, int iteration) { kprintf("%s: %d starts eating: bowl %d, iteration %d\n", who, num, bowl, iteration); clocksleep(1); kprintf("%s: %d ends eating: bowl %d, iteration %d\n", who, num, bowl, iteration); }

  • void mousesem(void * p, unsigned long mousenumber) {

int bowl, iteration; for (iteration = 0; iteration < 4; iteration++) { sem_eat (”mouse", mousenumber, bowl, iteration); } }

  • int catmousesem(.. ..) {

for (index = 0; index < NCATS; index++) thread_fork("catsem Thread”, NULL, index, catsem, NULL);

  • for (index = 0; index < NMICE; index++)

thread_fork(”mousesem Thread”, NULL, index, mousesem, NULL); }

slide-7
SLIDE 7

About ¡starva&on ¡

– You ¡do ¡not ¡need ¡to ¡consider ¡priority ¡or ¡ starva&on ¡

  • e.g., ¡mice ¡can ¡prevent ¡cat ¡from ¡ea&ng ¡

– Since ¡cats/mice ¡will ¡eventually ¡finish ¡ea&ng, ¡ won’t ¡starve ¡forever ¡

Ding ¡Yuan, ¡ECE344 ¡Opera&ng ¡System ¡ 7 ¡