Question
Implement locks for OS/161 The first part is to implement the locking primitive for OS161. The interface for the lock structure is defined in file
Implement locks for OS/161
The first part is to implement the locking primitive for OS161. The interface for the lock structure is defined in file synch.h. Stubbed code is in synch.c. These should be sleep locks and not spin locks, in the sense that a waiting thread will sleep, rather than spin, until the lock is available.
We are expected to do the above in C language via a linux terminal, thank you in advance.
Here are the two files:
-----------------------------------------------------------------------------------------> sync.h
/* * Header file for synchronization primitives. */ #ifndef _SYNCH_H_ #define _SYNCH_H_ /* * Dijkstra-style semaphore. * Operations: * P (proberen): decrement count. If the count is 0, block until * the count is 1 again before decrementing. * V (verhogen): increment count. * * Both operations are atomic. * * The name field is for easier debugging. A copy of the name is made * internally. */ struct semaphore { char *name; volatile int count; }; struct semaphore *sem_create(const char *name, int initial_count); void P(struct semaphore *); void V(struct semaphore *); void sem_destroy(struct semaphore *); /* * Simple lock for mutual exclusion. * Operations: * lock_acquire - Get the lock. Only one thread can hold the lock at the * same time. * lock_release - Free the lock. Only the thread holding the lock may do * this. * lock_do_i_hold - Return true if the current thread holds the lock; * false otherwise. * * These operations must be atomic. You get to write them. * * When the lock is created, no thread should be holding it. Likewise, * when the lock is destroyed, no thread should be holding it. * * The name field is for easier debugging. A copy of the name is made * internally. */ struct lock { char *name; // add what you need here // (don't forget to mark things volatile as needed) }; struct lock *lock_create(const char *name); void lock_acquire(struct lock *); void lock_release(struct lock *); int lock_do_i_hold(struct lock *); void lock_destroy(struct lock *); /* * Condition variable. * * Note that the "variable" is a bit of a misnomer: a CV is normally used * to wait until a variable meets a particular condition, but there's no * actual variable, as such, in the CV. * * Operations: * cv_wait - Release the supplied lock, go to sleep, and, after * waking up again, re-acquire the lock. * cv_signal - Wake up one thread that's sleeping on this CV. * cv_broadcast - Wake up all threads sleeping on this CV. * * For all three operations, the current thread must hold the lock passed * in. Note that under normal circumstances the same lock should be used * on all operations with any particular CV. * * These operations must be atomic. You get to write them. * * These CVs are expected to support Mesa semantics, that is, no * guarantees are made about scheduling. * * The name field is for easier debugging. A copy of the name is made * internally. */ struct cv { char *name; // add what you need here // (don't forget to mark things volatile as needed) }; struct cv *cv_create(const char *name); void cv_wait(struct cv *cv, struct lock *lock); void cv_signal(struct cv *cv, struct lock *lock); void cv_broadcast(struct cv *cv, struct lock *lock); void cv_destroy(struct cv *); #endif /* _SYNCH_H_ */
----------------------------------------------------------------------------------------> sync.c
/* * Synchronization primitives. * See synch.h for specifications of the functions. */ #include
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started