Question
In a multithreaded environment, which of the following
synchronization mechanisms is used to prevent race conditions?ÂSolution
A semaphore is a synchronization mechanism that is crucial for controlling access to shared resources in a multithreaded environment. It serves as a signaling mechanism that can be used to prevent race conditions, which occur when multiple threads attempt to modify shared data simultaneously, leading to unpredictable outcomes. Semaphores maintain a count of available resources and ensure that only a certain number of threads can access the critical section of code at any given time. When a thread wants to enter a critical section, it checks the semaphore's value; if it is greater than zero, the thread decrements it and proceeds; otherwise, it is blocked until the semaphore is released by another thread. This prevents multiple threads from executing critical sections concurrently, thereby eliminating race conditions and ensuring data consistency. Option B (Fork) - Fork is used to create new processes, not for synchronizing access to shared resources. Option C (Signal) - Signals are used for inter-process communication but do not provide the same level of resource control as semaphores. Option D (Context Switch) - Context switching refers to the operating system's ability to switch between processes and threads, which does not inherently prevent race conditions. Option E (Stack) - A stack is a data structure and does not serve any synchronization purpose in multithreading.
Arvind sold 40% more apples than Vishnu. Ratio of number of apples sold by Vishnu to number of apples sold by Bishwa is 4:7. Find the number of apples s...
If the numerator of a fraction is increased by 16.66 % and denominator decreased by 14.28 % then, it becomes the reciprocal of itself. Find the original...
In an election between two candidates, winner got 65% of total valid votes. If total number of votes cast were 2000 out of which 6% were invalid, then f...
In an exam of 200 marks, a student scores 136 marks. What percent of marks did he score?
Heena decided to save 15% of her salary as fixed deposit. Later, she changed her mind and saved Rs 4800 which was 80% of what she had decided earlier. ...
The income of P is 4/5th of the income of Q and income of R is 60% more than the income of Q. Income of Q is Rs.20000 and expenditure of P is...
In a school, 30% of the students are not participating in any competition. 80% of the students who are participating in the competition are girls. If 42...
Ravi spends 20% of his income on house rent, 30% on groceries, 50% of the remaining on entertainment, and saves the rest. If the amount spent on groceri...
- In a municipal election between two candidates, 12% of the total votes were declared invalid. Candidate 'X' secured 60% of the valid votes. If candidate 'Y...
Anil spends 20% of the monthly income on the reconstruction of his house, 15% on basic needs, 10% of the remaining on travelling. If he spends (100/9)% ...