SYSC3313 - Chapter 6 Part 3 Semaphores

0.0(0)
Studied by 0 people
call kaiCall Kai
Locked
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/5

encourage image

There's no tags or description

Looks like no tags are added yet.

Last updated 4:57 AM on 2/24/26
Name
Mastery
Learn
Test
Matching
Spaced
Call with Kai
Chat

No analytics yet

Send a link to your students to track their progress

6 Terms

1
New cards

Semaphore

Synchronization primitive based on a shared integer counter. P decrements when positive only and V increments.

2
New cards

Why use semaphores?

  • can do mutual exclusion but no ownership principle

  • task coordination

    • Signal between tasks

    • resource availability


3
New cards

Reader-Writers problem

Many tasks can access a shared resource at a time. Some read, some write. When writing, no other task can read or write.

Multiple readers allowed, but not multiple writers or both.

4
New cards

Readers-writers Solution

Use:

  • Reader semaphore for readcount (init to 1)

  • Writer semaphore for writing (init to 1)

  • reader count = 0 initially

Readers:

  1. Lock reader semaphore

  2. Increment reader count

  3. If first reader to enter, lock the writer semaphore

  4. Unlock reader semaphore and read

  5. Lock reader semaphore to update reader count

  6. If last reader, unlock writer semaphore

  7. Unlock reader semaphore

Writers:

  1. Unlock writer semaphore

  2. Write

  3. Lock writer semaphore


5
New cards

Semaphore functions

sem_wait(sem_t *sem): Post/decrement operation

sem_post(sem_t * sem): Increment operation

sem_init(sem_t *sem, int pshared, unsigned int val): Init semaphore to value val. pshared = 0 means thread shareable versus process shareable

sem_destroy(sem_t *sem) deallocate semaphore

6
New cards

Create a named semaphore

To use a semaphore across independent executables (processes)

const char* SEM_NAME

sem_t *sem = sem_open(SEM_NAME, O_CREAT, 0666, 1)