Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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 #include #include #include #include #include //////////////////////////////////////////////////////////// // // Semaphore. struct semaphore * sem_create(const char *namearg, int initial_count) { struct semaphore *sem; assert(initial_count >= 0); sem = kmalloc(sizeof(struct semaphore)); if (sem == NULL) { return NULL; } sem->name = kstrdup(namearg); if (sem->name == NULL) { kfree(sem); return NULL; } sem->count = initial_count; return sem; } void sem_destroy(struct semaphore *sem) { int spl; assert(sem != NULL); spl = splhigh(); assert(thread_hassleepers(sem)==0); splx(spl); /* * Note: while someone could theoretically start sleeping on * the semaphore after the above test but before we free it, * if they're going to do that, they can just as easily wait * a bit and start sleeping on the semaphore after it's been * freed. Consequently, there's not a whole lot of point in * including the kfrees in the splhigh block, so we don't. */ kfree(sem->name); kfree(sem); } void P(struct semaphore *sem) { int spl; assert(sem != NULL); /* * May not block in an interrupt handler. * * For robustness, always check, even if we can actually * complete the P without blocking. */ assert(in_interrupt==0); spl = splhigh(); while (sem->count==0) { thread_sleep(sem); } assert(sem->count>0); sem->count--; splx(spl); } void V(struct semaphore *sem) { int spl; assert(sem != NULL); spl = splhigh(); sem->count++; assert(sem->count>0); thread_wakeup(sem); splx(spl); } //////////////////////////////////////////////////////////// // // Lock. struct lock * lock_create(const char *name) { struct lock *lock; lock = kmalloc(sizeof(struct lock)); if (lock == NULL) { return NULL; } lock->name = kstrdup(name); if (lock->name == NULL) { kfree(lock); return NULL; } // add stuff here as needed return lock; } void lock_destroy(struct lock *lock) { assert(lock != NULL); // add stuff here as needed kfree(lock->name); kfree(lock); } void lock_acquire(struct lock *lock) { // Write this (void)lock; // suppress warning until code gets written } void lock_release(struct lock *lock) { // Write this (void)lock; // suppress warning until code gets written } int lock_do_i_hold(struct lock *lock) { // Write this (void)lock; // suppress warning until code gets written return 1; // dummy until code gets written } //////////////////////////////////////////////////////////// // // CV struct cv * cv_create(const char *name) { struct cv *cv; cv = kmalloc(sizeof(struct cv)); if (cv == NULL) { return NULL; } cv->name = kstrdup(name); if (cv->name==NULL) { kfree(cv); return NULL; } // add stuff here as needed return cv; } void cv_destroy(struct cv *cv) { assert(cv != NULL); // add stuff here as needed kfree(cv->name); kfree(cv); } void cv_wait(struct cv *cv, struct lock *lock) { // Write this (void)cv; // suppress warning until code gets written (void)lock; // suppress warning until code gets written } void cv_signal(struct cv *cv, struct lock *lock) { // Write this (void)cv; // suppress warning until code gets written (void)lock; // suppress warning until code gets written } void cv_broadcast(struct cv *cv, struct lock *lock) { // Write this (void)cv; // suppress warning until code gets written (void)lock; // suppress warning until code gets written }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Advances In Databases And Information Systems 25th European Conference Adbis 2021 Tartu Estonia August 24 26 2021 Proceedings Lncs 12843

Authors: Ladjel Bellatreche ,Marlon Dumas ,Panagiotis Karras ,Raimundas Matulevicius

1st Edition

3030824713, 978-3030824716

More Books

Students also viewed these Databases questions

Question

3. Is IBMs program really a mentoring program? Why or why not?

Answered: 1 week ago