Incorrect Algorithm No. 2: Using Status Flags

CONCEPT: A shared Boolean array named flags contains a flag for each process. The flag values are BUSY when the process is in its critical section (using the resource), or FREE when it is not.

INITIALIZATION:

	typedef char boolean;
	...
	shared boolean flags[n - 1];
	...
	flags[] = FREE;
	...
	flags[] = FREE;
	...
ENTRY PROTOCOL (for Process ):
	/* wait while the other process is in its CS */
	while (flags[] == BUSY) {
	}
-->
	/* claim the resource */
	flags[] = BUSY;
EXIT PROTOCOL (for Process ):
	/* release the resource */
	flags[] = FREE;
ANALYSIS: Suppose Process is interrupted at precisely the point marked by the arrow in the exit protocol. At this point the process believes it has the right to proceed, but it has not yet set its flag. If Process executes its entry protocol before Process resumes, it will enter its CS. When Process resumes, it will enter as well. The mutual exclusion  requirement is violated.