Question: In this project, you will add synchronization implementation to SThreads, a simple user - space threading library. In the synchronization implementation, you are expected to
In this project, you will add synchronization implementation to SThreads, a simple userspace threading library. In the synchronization implementation, you are expected to implement the lock utilities, including readlock and writelock. Given your implementation, an application programmer can use your synchronization primitive to develop a multithreading application that accesses and updates shared data. In particular, an application programmer will use your readwrite lock mechanism to achieve the synchronization goal.
Get the source program folder sthread.tgz to extract: $ tar zxvf sthread.tgz The files provide the source, header, and Makefile for the SThreads library. The publicly available functions and datatypes are in the file sthread.h The library is used as follows:
Threads are manipulated using variables of type sthreadt
sthreadinit must be called exactly once, as the first thing in main It returns normally, and on error.
A new thread is created using the function:
int sthreadcreatesthreadt t sthreadmaint main, void arg
The first argument is where the sthreadt object is returned. The second is the function the new thread should run. The third argument is passed to this function. sthreadcreate returns normally and on error.
sthreadself returns the sthreadt associated with the currently running thread. sthreadsuspend puts the currently running thread to sleep, and sthreadwakesthreadt t wakes up a thread specified.
An example program:
#define REENTRANT
#include
#include
#include
#include
#include "sthread.h
int
threadmainvoid arg
int threadno intarg;
for ;;
printfthread d: Im going to sleep
threadno;
sthreadsuspend;
printfthread d: I woke up
threadno;
return ;
int
mainint argc, char argv
sthreadt thr thr;
if sthreadinit
fprintfstderrs: sthreadinit: s
argv strerrorerrno;
if sthreadcreate&thr threadmain, void
fprintfstderrs: sthreadcreate: s
argv strerrorerrno;
if sthreadcreate&thr threadmain, void
fprintfstderrs: sthreadcreate: s
argv strerrorerrno;
sleep;
sthreadwakethr;
sleep;
sthreadwakethr;
sleep;
sthreadwakethr;
sthreadwakethr;
sleep;
return ;
#define REENTRANT: This is necessary in any multithreaded program, as it tells subsequent include files to use reentrant versions of library functions and threadsafe variables. For example, the errno variable, since it is a global variable, is normally not threadsafe. A function in thread A can set it on error, and thread B can set it to something else before the caller in thread A ever gets to read the value. This is an example of a race condition. If you #define REENTRANT, though, errno is redefined to errnolocation which references a different address for each thread. An implementation of this function is provided in sthread.c
sleep: you are not allowed to use sleep or any other timer functions in your implementation for sync functions except the user applications, which is for demonstration purpose
testandsetbit: is used to immplement spinlocks, in which you repeatedly call testandsetbit and a noop in a tight loop, waiting for the test result to be schedyield is a good noop function to use within your spinlocks, you need to #include Note that you can use spinlocks to synchronize the access on your readerswriters locks' shared data structures, but not to implement the readerswriters locks' themselves. In other words, if a thread calls sthreadwritelock on an unavailable readwrite lock, it should suspend rather than spin until the lock is available.
Support muliple readers: your rwlock implementation should allow multiple readers to acquire the lock in read shared mode and concurrently execute. For example, if the lock is held by reader thread A another reader thread B can immediately acquire and share the lock with A if there is no other waiting thread write If the lock is held in write mode, no other threads can acquire the lock in read or write mode.
Bonus : Favor writer threads over readers: if there are threads waiting to acquire the lock in write mode, you should let one of these threads acquire the lock immediately when the lock becomes abailable. For example, the lock is held by a writer thread and a thread A is waiting to acquire the lock in read mode. Another thread B comes, trying to acquire the lock in write mode. In an implementation that favors writers over readers, thread B can acquire the lock in write mode and proceed if the previous write lock is released.
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
