Understanding a rudimentary solution to Critical Section Problem

  • Thread starter K29
  • Start date
  • Tags
    Section
In summary, the solution to the problem of two processes entering their critical sections alternately can be achieved by implementing a common variable, turn, which keeps track of whose turn it is. However, this solution may not be effective if one process's non-critical code is much longer or if it halts, causing the other process to be unable to enter its critical section. Therefore, error-handling code should be implemented in the critical section to ensure that the turn variable is flipped in case of any errors. Additionally, keeping the critical section code short is important to avoid potential deadlocks.
  • #1
K29
108
0
(Source: page 20 of this :http://www.ics.uci.edu/~bic/courses/JaverOS/ch2.pdf)

The problem is easily solved if we insist that p1 and p2 enter their critical sections alternately; one common variable, turn, can keep track of whose turn it is. This idea is implemented in our first algorithm below.
Code:
/* CS Algorithm: Try #1 */
int turn = 1;
cobegin
p1: while (1)
{
  while (turn==2) ; /*wait loop*/
  CS1; turn = 2; program1;
 }
//
p2: while (1)
{
  while (turn==1) ; /*wait loop*/
  CS2; turn = 1; program2;
 }
coend

Initially, turn is set to 1, which allows p1 to enter its CS. After exiting, the process sets turn
to 2, which now allows p2 to enter its CS, and so on. Unfortunately, if program1 were much longer than program2 or if p1 halted in program 1, this solution would hardly be satisfactory. One process well outside its critical section can prevent the other from entering its critical section, thus violating the requirement 1 stated above

The part I put in bold looks like nonsense.
Since program1 executes after the Critical Section and after turn gets set to 2, there is nothing stopping p2 from leaving its wait loop and entering its critical section, no matter what happens in program1. Looks like a perfectly fine solution to me.

Am I right? Am I not seeing something?(NOTE: p1 is the thread and program1 is the non-critical part of p1.)
 
Last edited:
Technology news on Phys.org
  • #2
It looks OK for the most part. Each thread flips the flag immediately after exiting its critical section, thus allowing the other thread to enter its own critical section. As you noted, if either thread failed to flip the flag (i.e., died during its critical section phase) that would deadlock the other thread. So error-handling code in the critical section should ensure that, should the thread crash, the flag is flipped anyway.
 
  • #3
And I guess it goes without saying that the code within the critical section should be kept as short as possible.
 

Related to Understanding a rudimentary solution to Critical Section Problem

What is the Critical Section Problem?

The Critical Section Problem is a common issue in computer science, where multiple processes or threads require access to a shared resource. It can lead to errors and inconsistencies if not properly managed.

What is a rudimentary solution to the Critical Section Problem?

A rudimentary solution to the Critical Section Problem is the use of mutual exclusion, where only one process or thread is allowed to access the shared resource at a time. This can be achieved through the use of locks, semaphores, or other synchronization mechanisms.

What are the drawbacks of using a rudimentary solution to the Critical Section Problem?

Rudimentary solutions to the Critical Section Problem can lead to issues such as deadlock, where processes or threads are stuck waiting for each other to release the shared resource. They can also negatively impact system performance, as only one process or thread can access the resource at a time.

How can the Critical Section Problem be solved more efficiently?

More efficient solutions to the Critical Section Problem involve implementing techniques such as optimistic concurrency control, where multiple processes or threads can access the shared resource simultaneously but conflicts are resolved when updates are made. Other techniques include using transactional memory or implementing a lock-free or wait-free algorithm.

Why is it important to understand rudimentary solutions to the Critical Section Problem?

The Critical Section Problem is a fundamental issue in computer science, and understanding rudimentary solutions is crucial for developing more efficient and robust solutions. It is also important for ensuring the proper functioning of multi-threaded or multi-process applications that rely on shared resources.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
2
Views
998
  • Programming and Computer Science
Replies
1
Views
6K
  • Introductory Physics Homework Help
Replies
6
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Linear and Abstract Algebra
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
Replies
1
Views
843
Back
Top