You are on page 1of 6

OPERATING

SYSTEMS
CASE STUDY
PROJECT
CATS AND MICE PROBLEM:
Problem parameters:
1.No mouse should ever get eaten.

2.Only one mouse or one cat may eat from a given dish at any one time.
3.A cat or mouse that wants to eat should eventually be able to eat.
 Three variables are used: mutex, wrt, phil to implement solution
1.semaphore mutex, bowl; // semaphore mutex is used to ensure
mutual exclusion when phil is updated i.e. when any reader enters
or exit from the critical section and semaphore bowl is used by both
rats and cats.
2.int phil;  //    phil tells the number of processes(cats) in the critical
section, initially 0
 Rat process:
1.Rat requests the entry to critical section.
2.If allowed i.e. wait() gives a true value, it enters. If not allowed, it keeps on waiting.
3.It exits the critical section.
 Cat process:
1.Cat requests the entry to critical section.
2.If allowed:
1. it increments the count of number of cats inside the critical section. If this cat is
the first cat entering, it locks the bowl semaphore to restrict the entry of rats if
any rat is inside.
2. It then, signals room as any other cats is allowed to enter while others are
already eating.
3. After eating, it exits the critical section. When exiting, it checks if no more cat
is inside, it signals the semaphore bowl as now, rat can enter the critical section.
3.If not allowed, it keeps on waiting.
PROGRAM:
OUTPUT:

You might also like