Critical-Section Problem (CS130 notes)

版权声明:本文为博主原创文章,未经允许不得转载。 https://blog.csdn.net/qq_37592252/article/details/84986706

Critical-Section Problem

What is critical section?

A critical section is a sequence of code that atomically accesses shared state.

Solutions - Three Requirements

  • Mutal exclusion

At most one thread holds the lock.

  • Progress

If no thread holds the lock and any thread attempts to acquire the lock, then eventually some thread succeeds in acquiring the lock.

  • Bounded waiting

If thread T attempts to acquire a lock, then there exists a bound on the number of times other threads can successfully acquire the lock before T does.

Critical-Section Handling in OS

preeptive & non-preeptive

  • Preemptive

Allows preemption of process when running in kernel mode.

  • Non-preemptive

Runs until exits kernel mode, blocks, or voluntarily yields CPU.
Essentially free of race conditions in kernel mode

Peterson Algorithm

Suitable for two processes. Let’s say p i p_{i} and p j p_{j} .

int turn;
boolean flag[2];
/* Peterson Algorithm for Pi */
do {
	flag[i] = True;
	turn = j;
	while (flag[j] && turn == j);
	/* critical section */
	flag[i] = False;
	/* remainder section */
} while (True)

猜你喜欢

转载自blog.csdn.net/qq_37592252/article/details/84986706