Printer Spooler Problem

The printer spooler problem is a standard problem of process synchronization. A printer spooler is a small application that manages the paper printing jobs sent from a computer to a printer or print server. It enables storing multiple print jobs within a print queue or a buffer where it’s retrieved by the printer or print server.

Let’s explain with an example

Suppose we have a network and only one printer is there in the network. All users in the network are using the same printer.

The Printer is a very slow-speed device as compared to the Processor. So, for printing, there is a spooler directory that keeps the entire file for printing through the Print Spooler program.

What is a Printer Spooler program?

It is software that is responsible for managing All printing files that exist in the printing spooler directory. The print spooler program executes all files on the printer from the printing spooler directory one by one.

How do you add printing files to the printing spooler directory?

If any process wants to print a document, then it will execute the four-line code to add the document to the spooler directory

LOAD Ri, M[IN]
STORE SD[Ri], “F-N”
INCR Ri
STORE M[IN], Ri

Let Suppose

“IN=0” is the shared variable that tells the position of the incoming document in the printing spooler directory.

R1 is the Register; SD is the directory, File1.doc, File2.doc, File3.doc, File4.doc, and File5.doc are files.

Then

LOAD R1, M[IN] // Load the IN value in register R1 so R1=0
STORE SD[R1], “File1.doc” // save File1.doc in SD position 0
INCR R1 // R1=R1+1= 0+1=1
STORE M[IN], R1 // Store R1 value in IN shared variable so IN=1

CASE: 01

When there is no interruption during the execution of the above four lines, then the printer spooler program works properly.  Let’s suppose there are two processes. Process P1 has registered R1 and File1.doc file. In the same way, P2 has registered R2 and File2.doc files.

But the case is when P1 completes the execution of 4 line codes, and then P2 will execute. In this way, the printer spooler program works perfectly as given below in the diagram. There will be no problem

printer spooler problem normal working

CASE: 02

Let’s suppose P1 and P2 are two processes.

P1 has an R1 register and a File1.doc file. In the same way, P2 has registered R2 and File2.doc files. Both processes share the IN variable and run parallel on the uniprocessor.

printer spooler problem in working

 

If P1 executes instructions 1, 2, and 3 and an interrupt occurs then OS saves the record of P1 in its PCB and CPU preempt to P2.

PCB Till now of P1 is,

  • first instruction Result is: R1=0
  • The second instruction Result is:  File1.doc is in Zero position of Spooler Directory (SD)
  • The third instruction Result is: R1=1

Note:  IN=0

(After the execution of the 4th instruction, the value of IN will be stored in R1. But here, the 4th is still pending due to interruption just after the 3rd instruction. So, IN=0)

When P2 executes the above four lines the result till now is 

  • first instruction Result is: R2=0
  • The second instruction Result is: File2.doc is in Zero position of Spooler Directory (SD)
  • The third instruction Result is R2=1
  • The fourth instruction Result is: IN=1

After P2, the CPU preempts back to P1 and executes the 4th instruction

  • Result of Fourth instruction: IN=1

CONCLUSION: File1.doc is replaced with File2.doc at position 1 of Process P2. File1 is lost because processes are not synchronized. In simple words, we can say that when shared variables are used in a printer spooler program, data may be overwritten in some cases. that’s why process synchronization is important. Solutions for such problems are semaphore, mutex, Peterson solution, etc.

Question:

If there are three files already in the main memory, then the value of IN=3 and P1, P2 executing parallelly. Then, find the results as given above in cases 1 and 2.