I am reading William Stallings operating system book which gives this pseudo-Code for disabling interrupt to achieve Mutual Exclusion

while (true) {
 /* disable interrupts */;
 /* critical section */;
 /* enable interrupts */;
 /* remainder */;

But I don't get why there is a While loop in the code which is true this means that this part of the code repeats forever but why?


1 Answer 1


The while loop with true condition is just to ensure this process can enter and exit its critical section repeatedly. This usage is quite common when we are discussing concurrency programming.

Take a moment to reflect. How can you create an environment to demonstrate easily the effect of two or more different concurrent processes are, for example, contending for one shared resources? If you let each process just try to enter the critical section once, it might be hard to produce the various particular situations that could happen only to concurrent running processes. It may just happen all processes enter the critical section in order once and then exit. However, if we use a while(true) loop to run the processes long enough, it will be much more probable to observe all sorts of phenomenons.

  • $\begingroup$ Can you give an example? $\endgroup$
    – m.o
    Oct 27, 2018 at 17:22
  • $\begingroup$ This pattern of using while(true) is very common. You may just check a few more similar examples down that book. For example, the figure 5.6 "Mutual Exclusion Using Semaphores" of seventh edition. Or figure 5.9. Or figure 5.10. And several more. $\endgroup$
    – John L.
    Oct 28, 2018 at 1:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.