ramblinjan
ramblinjan

Reputation: 6694

Why use a semaphore for each processor in a spooling simulation

I'm working on a project that simulates multiple processors handling commands and queuing strings to be printed via one spooler.

There are up to ten processors, each executing a series of jobs that have "compute" and "print" statements. Compute is just a mathematical process to take up time to simulate other work, while print transfers a short string to the spooler to be printed. There is one spooler, with one printer hooked up to the spooler. Each processor will handle a number of jobs before termination, all print statements from a specific job on a specific processor should print together (no interleaving of printing from individual jobs), and the spooler should never be blocked on a process that is computing.

I generally understand how to code this using semaphore and mutex structures, but a statement in the specifications confused me:

Try to maximize the concurrency of your system. (You might consider using an array of semaphores indexed by processor id.)

Is there a specific advantage I'm missing to using a semaphore for each individual process?

If further clarification is needed, let me know--I tried to describe the problem in a concise way.

EDIT: Another possibly important piece: each processor has a buffer that can hold up to ten strings for sending to the spooler. Could the sempahores for each process be for waiting when the buffer is full?

EDIT 2: A job can contain multiple compute and print statements mixed in with each other:
Job 1
Calculate 4
Print Foo
Calculate 2
Print Bar
End Job

Print statements within a job should all be printed in order (Foo and Bar should be printed sequentially without a print from another job/processor in between).

Upvotes: 2

Views: 459

Answers (1)

John Vint
John Vint

Reputation: 40256

The important information is here:

(no interleaving of printing from individual jobs),

This implies a new Semaphore(1) (if you are using Java).

And

and the spooler should never be blocked on a process that is computing.

If you had a semaphore that accepts one party this last piece would not be satisfied. An executing processor should not have to wait for another to complete, it can be done in parallel.

You can do this by creating a striped set of semaphores. You have it indexed by the processor ID so that each thread/processor would run without interleaving but without waiting for other processors to complete.

Semaphore[] semaphores = new Semaphore[Number_of_proessors];
//initialize all semaphore indexes

semaphores[Process.id].acquire();

//work    

semaphores[Process.id].release();

Upvotes: 2

Related Questions