Question
CODING GUIDES( PYTHON ONLY ): 1. Use of proper function for each module. 2. No PLAGIARISM. 3. Write SIMPLE Code with COMMENTS. 4. Unique Self
CODING GUIDES( PYTHON ONLY):
1. Use of proper function for each module.
2. No PLAGIARISM.
3. Write SIMPLE Code with COMMENTS.
4. Unique Self written code.
Topic: Process Synchronization using Semaphores
-Sample Code
Q. Program creates two threads: one to increment the value of a shared variable and second to decrement the value of the shared variable. Both the threads make use of semaphore variable so that only one of the threads is executing in its critical section.
Process Synchronization using mutex locks:
TASK:
IMPORTANT NOTE:
1) Documentation of each module along with code file.
2) Use of proper function for each module.
3) No PLAGIARISM- CODE SHOULD NOT BE AVAILAIBLE ON OTHER SITES
4) Write SIMPLE CODE WITH COMMENTS AND NOT COPIED CODE.
KINDLY FOLLOW ALL ABOVE INSTRUCTIONS, THANKYOU...
CODE IN PYTHON import threading import time def funl() : global shared \# make the shared variable global semaphore. acquire() \# acquire the semaphore x= shared \# thread 1 reads value of shared variable print("Threadl reads the value as", x ) x+=1 \# thread 1 increments its value print("Local updation by Threadl:", x) time.sleep(0.01) \# thread 1 is preempted by thread 2 shared =x \# thread one updates the value of shared variable \# start threads threadl. start() threadi. start() \# wait for threads to finish thread1.join() thread2.join() \# prints the 1 ast undated value of shared variable print("Final value of shared is", shared) Output Threadl reads the value as 1 Local updation by Thread1: 2 Value of shared variable updated by Threadl is: 2 Thread 2 reads the value as 2 Local updation by Thread2: 1 Value of shared variable updated by Thread2 is: 1 Final value of shared is 1 The process initializes the semaphore variable s to ' 1 ' using the sem_inito function. The initial value is set to ' 1 ' because binary semaphore is used here. If you have multiple instances of the resource then counting semaphores can be used. Next, the process creates two threads. threadI acquires the semaphore variable by calling sem_waito. Next, it executes statements in its critical section part. We use sleep(1) function to preempt threadl and start thread2. This simulates a real-life scenario. Now, when thraed2 executes sem_waitO it will not be able to do so because threadl is already in the critical section. Finally, threadl calls sem_post() function. Now thread2 will be able to acquire s using sem_waitO. This ensures synchronization among threads. CODE IN PYTHON imporl Llireading import time def fun1(): global shared \# make the shared variable global print("Threadl trying to acquire lock") lock.acquire() \# thread 1 acquires the lock. Now thread 2 will not be able to acquire the lock until it is unlocked by thread 1 print ("Threadl acquired lock") x= shared \# thread 1 reads value of shared variable print " Threadl reads the value of shared variable as", x ) x+=1 \# thread 1 increments its value print ("Local updation by Threadl: ", x ) time. sleep(1) \# thread 1 is preempted by thread 2 shared =x \# thread one updates the value of shared variable print("Value of shared variable updated by Threadl is:", shared) lock.release() \#release the lock print ("Threadl released the lock") def fun2(): global shared \# make the shared variable global print("Thread2 trying to acquire lock") lock, acquire () print("Thread2 acquired lock") y= shared \# thread 2 reads value of shared variable print ("Thread2 reads the value as", y ) y=1 \# thread 2 increments its value print ("Local updation by Thread2: ", y ) time.sleep(1) \# thread 2 is preempted by thread 1 shared = y \# thread two updates the value of shared variable print("Value of shared variable updated by Thread2 is:", shared) lock.release () Write a program to achieve synchronization between multiple threads. The threads try to acquire a resource that has two instancesStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started