What's the difference between a mutex and a semaphore?
A mutex is owned by a thread/process. So once a thread locks it, then other threads/processes will either spin or block on the mutex. Whereas, semaphore allows one or more threads/processes to share the resource.
Mutex is binary semaphore
Showing posts with label semaphore. Show all posts
Showing posts with label semaphore. Show all posts
Jan 15, 2009
Semaphore implementation
Semaphore implementation
typedef struct
{
int value;
struct process* list;
} semaphore;
wait(semaphore S)
{
s.value—
if(s.value<0)
{
put the process p in S.list
block;//busy waiting, in the waiting list, not consume the cpu time
}
}
signal(semaphore S)
{
s.value++;
if(s.value<=0)
{
remove the process p from s.l;
wakeup(p);//in the ready list
}
}
The wait signal and wait process should be atomic, so in the one processor condition, we disable the interrupt during signal and wait
typedef struct
{
int value;
struct process* list;
} semaphore;
wait(semaphore S)
{
s.value—
if(s.value<0)
{
put the process p in S.list
block;//busy waiting, in the waiting list, not consume the cpu time
}
}
signal(semaphore S)
{
s.value++;
if(s.value<=0)
{
remove the process p from s.l;
wakeup(p);//in the ready list
}
}
The wait signal and wait process should be atomic, so in the one processor condition, we disable the interrupt during signal and wait
Subscribe to:
Posts (Atom)