Question

In: Computer Science

Write a program to simulate the Distributed Mutual Exclusion in ‘C’.

  1. Write a program to simulate the Distributed Mutual Exclusion in ‘C’.

Solutions

Expert Solution

// Filename: peterson_spinlock.c 

#include <stdio.h> 
#include <pthread.h> 
#include"mythreads.h" 

int flag[2];  
const int MAX = 199; 
int ans = 0;
int turn; 

void lock_init() 
{ 

        flag[0] = flag[1] = 0; 
        turn = 0; 
} 
 
void lock(int self) 
{  
        flag[self] = 1; 
        turn = 1-self;  
        while (flag[1-self]==1 && turn==1-self) ; 
} 

void unlock(int self) 
{ 

        flag[self] = 0; 
} 

 
void* func(void *s)
{ 
        int i = 0; 
        int self = (int *)s; 
        printf("Thread Entered: %d\n", self); 

        lock(self); 
 
        for (i=0; i<MAX; i++) 
                ans++; 

        unlock(self); 
} 

// Driver code 
int main() 
{ 
        pthread_t p1, p2; 
        lock_init(); 
 
        pthread_create(&p1, NULL, func, (void*)0); 
        pthread_create(&p2, NULL, func, (void*)1); 

        pthread_join(p1, NULL); 
        pthread_join(p2, NULL); 

        printf("Actual Count: %d | Expected Count: %d\n", 
        ans, MAX*2); 

        return 0; 
} 

NoteApproch*

// Use Filename: peterson_spinlock.c
// Initialize lock by reseting the both the threads to acquire the locks. And, giving turn to one of them.
// Executed before entering critical section
// Set flag[self] = 1 saying you want to acquire lock
// But, first give the other thread the chance to
// acquire lock

// Wait until the other thread looses the desire
// to acquire lock or it is your turn to get the lock.
// Executed after leaving critical section
// You do not desire to acquire lock in future.
// This will allow the other thread to acquire the lock.
// A Sample function run by two threads created in main()
// Critical section (Only one thread can enter here at a time)

For Driver code
// Initialized the lock then fork 2 threads

// Create two threads (both run func)
// Wait for the threads to end.


Related Solutions

*****For C++ Program***** Overview For this assignment, write a program that uses functions to simulate a...
*****For C++ Program***** Overview For this assignment, write a program that uses functions to simulate a game of Craps. Craps is a game of chance where a player (the shooter) will roll 2 six-sided dice. The sum of the dice will determine whether the player (and anyone that has placed a bet) wins immediately, loses immediately, or if the game continues. If the sum of the first roll of the dice (known as the come-out roll) is equal to 7...
Consider the four distributed mutual exclusion algorithms Permission Based ---#1: Centralized Mutual Exclusion ---#2: Decentralized Mutex...
Consider the four distributed mutual exclusion algorithms Permission Based ---#1: Centralized Mutual Exclusion ---#2: Decentralized Mutex Algorithm ---#3: Distributed Mutual Exclusion #4: A Token Ring Algorithm Discuss their relative fault tolerance – basically the effect of processor crashes on the performance of the algorithm. You don’t have to come up with a 1-2-3-4 ordering.
please write in c using linux or unix Write a program that will simulate non -...
please write in c using linux or unix Write a program that will simulate non - preemptive process scheduling algorithm: First Come – First Serve Your program should input the information necessary for the calculation of average turnaround time including: Time required for a job execution; Arrival time; The output of the program should include: starting and terminating time for each job, turnaround time for each job, average turnaround time. Step 1: generate the input data (totally 10 jobs) and...
Please write in C using linux or unix. Write a program that will simulate non -...
Please write in C using linux or unix. Write a program that will simulate non - preemptive process scheduling algorithm: First Come – First Serve Your program should input the information necessary for the calculation of average turnaround time including: Time required for a job execution; Arrival time; The output of the program should include: starting and terminating time for each job, turnaround time for each job, average turnaround time. Step 1: generate the input data (totally 10 jobs) and...
Write a c++ Program To simulate the messages sent by the various IoT devices, a text...
Write a c++ Program To simulate the messages sent by the various IoT devices, a text le called device data.txt is provided that contains a number of device messages sent, with each line representing a distinct message. Each message contains a device name, a status value and a Unix epoch value which are separated by commas. Your c++ program will read this le line by line. It will separate the message into device name, status value and epoch value and...
In C, write a program that will simulate the operations of the following Round-Robin Interactive scheduling...
In C, write a program that will simulate the operations of the following Round-Robin Interactive scheduling algorithm. It should implement threads for the algorithm plus the main thread using a linked list to represent the list of jobs available to run. Each node will represent a Job with the following information: int ProcessID int time time needed to finish executing The thread representing the scheduler algorithm should continue running until all jobs end. This is simulated using the time variable...
Write a C/C++ program that simulate a menu based binary numbercalculator. This calculate shall have...
Write a C/C++ program that simulate a menu based binary number calculator. This calculate shall have the following three functionalities:Covert a binary string to corresponding positive integersConvert a positive integer to its binary representationAdd two binary numbers, both numbers are represented as a string of 0s and 1sTo reduce student work load, a start file CSCIProjOneHandout.cpp is given. In this file, the structure of the program has been established. The students only need to implement the following three functions:int binary_to_decimal(string...
There are three ways to provide mutual exclusion in a system. Explain how mutual exclusion can...
There are three ways to provide mutual exclusion in a system. Explain how mutual exclusion can be guaranteed using hardware approach and software approach.
software approach to mutual exclusion
Consider the following program which provides a software approach to mutual exclusion:Integer array control [1: N]; integer kWhere 1 ≤ k ≤ N, and each element of “control” is either 0, 1, Or 2. All elements of “control” are initially zero; the initial valueof k is immaterial.The program of the ith process (1 ≤ i ≤ N) isbegin integer j;L0: control [i] := l;LI: for j:=k step l until N, l step l until k dobeginif j = i then...
mutual exclusion is Lamport’s bakery
A software approach to mutual exclusion is Lamport’s bakery algorithm [LAMP74], so called because it is based on the practice in bakeries and other shops in which every customer receives a numbered ticket on arrival, allowing each to be served in turn. The algorithm is as follows:boolean choosing[n];int number[n];while (true) {choosing[i] = true;number[i] = 1 + getmax(number[], n);choosing[i] = false;for (int j = 0; j < n; j++){while (choosing[j]) { };while ((number[j] != 0) && (number[j],j) < (number[i],i)) { };}/*...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT