Assignment: Use threads to implement Peterson\'s algorithm for mutual exclusion.
ID: 3801922 • Letter: A
Question
Assignment:
Use threads to implement Peterson's algorithm for mutual exclusion. Demonstrate that the algorithm works using two threads. One of the threads should continually print "a". The other thread should continually print "b". Each time a thread prints, it should update a character counter (which is protected using Peterson's algorithm). After 30 characters have been printed, the thread that prints next should print a new line and reset the character counter to 0. All access to the character counter should reside in a critical section protected by Peterson's algorithm. All code that does not require mutual exclusion should reside outside of critical sections.
Note:- you can code in any programming language you want.
Finally, you will write a report explaining your implementation and addressing the following question:
Does Peterson’s Algorithm properly implement Mutual Exclusion of the critical code section?
Be sure to address each of the six requirements for Mutual Exclusion.
1. Mutual exclusion must be enforced: Only one process at a time is allowed into its critical section, among all processes that have critical sections for the same resource or shared object.
2. A process that halts in its noncritical section must do so without interfering with other processes.
3. It must not be possible for a process requiring access to a critical section to be delayed indefinitely: no deadlock or starvation.
4. When no process is in a critical section, any process that requests entry to its critical section must be permitted to enter without delay.
5. No assumptions are made about relative process speeds or number of processors.
6. A process remains inside its critical section for a finite time only
Explanation / Answer
#include<stdio.h>
#include<conio.h>
#include<process.h
#include<pthread.h>
#include "mythreads.h"
int flg[6];
int trn;
const int MAXIMUM= 1e9;
int anser = 0;
void lock_init()
{
flg[0] = flg[1] = 0;
turn = 0;
}
void lock(int self)
{
flg[self]=1;
trn = 1-self;
__sync_synchronize();
while (flg[1-self]==1 && trn==1-self)
sched_yield();
}
// Executed after leaving critical section
void unlock(int self)
{
flg[self]=0;
}
// A Sample function run by two threads created
void* function(void *s)
{
int x = 0;
int self = (int *)s;
printf("Thread Entered: %d ",self);
lock(self);
// Critical section (Only one thread
// can enter here at a time)
for (x=0; x<MAXIMUM; x++)
anser++;
unlock(self);
}
// Driver code
int main()
{
pthread_t p1, p2;
// Initialize the lock
lock_init();
Pthread_create(&p1, NULL, function, (void*)0);
Pthread_create(&p2, NULL, function, (void*)1);
Pthread_join(p1, NULL);
Pthread_join(p2, NULL);
printf("Actual Count: %d | Expct Count:"
" %d ",ans,MAXIMUM*2);
return 0;
}
#ifndef __MYTHREADS_h__
#define __MYTHREADS_h__
#include<stdio.h>
#include<conio.h>
#include<process.h
#include<pthread.h>
#include <assert.h>
#include <sched.h>
void Pthread_mutex_lock(pthread_mutex_t *n)
{
int rc = pthread_mutex_lock(n);
assert(rc == 0);
}
void Pthread_mutex_unlock(pthread_mutex_t *n)
{
int rc = pthread_mutex_unlock(n);
assert(rc == 0);
}
void Pthread_create(pthread_t *thread, const pthread_attr_t *attr,
void *(*start_routine)(void*), void *arg)
{
int rc = pthread_create(thread, attr, start_routine, arg);
assert(rc == 0);
}
void Pthread_join(pthread_t thread, void **value_ptr)
{
int rc = pthread_join(thread, value_ptr);
assert(rc == 0);
}
#endif // __MYTHREADS_h__
Output:
Thread Entered: 1
Thread Entered: 0
Actual Count: 2000000000 | Expct Count: 2000000000
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.