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

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.
*****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...
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 Algoritm , code and output. In C++ In Operating Systems , Simulate with a program...
Write Algoritm , code and output. In C++ In Operating Systems , Simulate with a program to schedule disk in seek optimization.
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...
Programming Language: C++ Overview For this assignment, write a program that will simulate a single game...
Programming Language: C++ Overview For this assignment, write a program that will simulate a single 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 is equal to 7 or 11, the player wins immediately....
1) Write an 8051 C program that will simulate a traffic light. Green will turn on...
1) Write an 8051 C program that will simulate a traffic light. Green will turn on for 2 seconds, Yellow for 1 second and Red for 2 seconds. The green, yellow and red LEDs are connected to Ports P11, P12 and P13. 2) Write an 8051 C program that will turn on or turn off an LED using a switch. The LED is connected to Port P15 while the switch is connected to Port P11.
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT