Open In App

Printer Spooler Problem

Last Updated : 19 Oct, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Printer-Spooler is a well-known problem of process synchronization problem. Process Synchronization is one of the most essential features of the operating system. It is the task of coordinating the execution of processes in a way that no two processes can have access to the same shared data and resources.

There are two types of processes:

  • Independent Process: A process that cannot affect and is not affected by any other process is called Independent Process.
  • Cooperative Process: A process that can affect and is affected by other processes is called the Cooperative process.

Since cooperative processes share resources so some problems with process synchronization can occur. The printer spooler is one of these problems that is caused by process synchronization.

Printer Spooler Problem:

Consider a situation where there are multiple processes and only one printer for printing purposes as the printer is a slower device as compared to CPU and memory so the spooler comes into the picture. The spooler is a program that maintains a directory containing a list of files that the printer has to print. Spooler hands over the file to the printer one by one.

Every process that wants to keep its file in a spooler needs to execute this program called Spooler Program:

I1. LOAD  Ri  M[IN]
I2. STORE SD[Ri] "FILE_NAME"
I3. INCR Ri
I4. STORE M[IN] Ri

Here,

  • IN is a shared variable between processes that want to use the printer. It points to the position where the file can be inserted into the spooler. 
  • Ri is the register of process Pi.
  • SD is a spooler directory

Now there are two cases:

  • Only one process wants to use the printer.
  • More than one processes want to use the printer.

Case 1: (Single Process):

Process P1 wants to print the file “f1.docx”, so it starts executing the spooler program:

Single Process into execution

Single Process into execution

Here the process P1 has executed the spooler program successfully, and the file F1 is queued in the spooler directory for printing. 

Case 2: (Multiple processes):

Say some files are already present inside the spooler and the value of IN is initially 3.

Multiple Processes in execution

Here what happened is when P1 executes it stored f4.docx at 3 and when process P2 arrived it also stored f5.docx at the same location by overwriting f4.docx. Hence we suffered a loss of data. This loss of data happened due to non-synchronization between P1 and P2  since both processes were sharing the same spooler program.

How to resolve the printer-spooler problem?

Process Synchronization problems can be solved using Semaphores or Mutex.

Semaphores: A semaphore is an integer variable that can be accessed using only two functions: wait() and signal(). There are two types of semaphores: binary semaphores and counting semaphores.

Mutex: A mutex is a lock that provides mutual exclusion. When the mutex is used, then only one thread can work with the entire buffer. If the mutex is used in the printer spooler then only one program can print its file at a time.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads