c++c++11linked-listspooler

Implementing a printer spooler


We have an assignment in which we have to implement a printer spooler using linked lists. I have used the list from STL to implement everything. I can get the program to send the print command and maintain status of documents. The problem is I don't know how to mimic/send a dummy print command so the document gets printed. If I use Sleep() there is a delay in the whole program.while I want printing on backhand and others users to have access to the program. Any help will be appreciated regarding my problem thanks.


Solution

  • In a REAL print spooler, either there are multiple threads (or in Unix, maybe a fork) or the system uses a "wait for several objects" type approach, and when there is something to print, a portion of the document is sent to the printer, set up so that when that portion is "consumed", a waitable object is "ready". Then the spooler waits for something to happen (either a new job or the printed). When the wait is completed, it determines what happened. If it's a new print job, queue it, if it's "some printing completed", it checks if that's the last of the printing and completes the job, or sends more stuff to the printer.

    In pseudocdoe it may look something like this:

    for(;;)
    {
       what = wait_for_work();
       switch (what)
       {
         case new_job:
           add_new_job();
           break;
         case print_write_done:
           if (document_complete)
              remove_current_job();
           else
              send_more_from_current_job();
           break;
         case spooler_terminate:
           exit(0);
         default:
           print_error_message();
       }
    }
    

    Obviously, without seeing your code, it's hard to say how you should implement it. But one could have a timer to simulate the "document being printed in small chunks", say, 10KB is consumed every 100ms, for example.