I have been assigned the following question:
[i]"There are two user processes, U1 and U2 that generate documents to be sent to one of two network printers, P1 and P2. Each printer has its own print queue with a limited buer size of three documents. When one of the user processes wishes to print a document they send it to one of the two printers by writing it to one of the printer queues. If one printer queue is full then the other one is selected. If both printer queues are full then the user process is suspended until a printer becomes available. For the simulation, use integers to represent documents. (A sequential number generator is to be be re-used from the previous program, as can the random number generator.) Your program should show the state of each of the four processes (active or suspended); the generated activity; and the state of both printer queues, at each iteration of the main loop. Tune the random number weightings so that the queues can be seen filling up and emptying."[/i]
I have the following program at the moment:
[code]#include <stdio.h>
#include <stdlib.h>
#include "queue_int.h"
#define RAND_NO_SEED 128475
#define MAX_BUFF_LEN 3
#define P1_SND 0
#define P2_RLY 1
static int P1_suspended = 0;
static int P2_suspended = 0;
int simulated_activity()
{
int r = rand()%99;
if (r < 33)
return P1_SND;
else
return P2_RLY;
}
static int item = 1;
int next_item() { return item++; }
void printint(int i) { printf("%i",i); }
int main(int argc, char **argv)
{
queue_int *buff_12 = new_bounded_queue_int(MAX_BUFF_LEN);
queue_int *buff_23 = new_bounded_queue_int(MAX_BUFF_LEN);
srand(RAND_NO_SEED);
int a;
while(1) {
sleep(1);
if (P1_suspended) printf("\nP1 suspended\n");
else printf("\nP1 active\n");
printf("buff_12: "); queue_int_print(buff_12,printint);
if (P2_suspended) printf("\nP2 suspended\n");
else printf("\nP2 active\n");
if (P1_suspended) printf("\nP1 suspended\n");
else printf("\nP1 active\n");
printf("buff_23: "); queue_int_print(buff_23,printint);
if (P2_suspended) printf("\nP2 suspended\n");
else printf("\nP2 active\n");
a = simulated_activity();
printf("\nUser 1 simulated activity: ");
if (a==P1_SND) printf("P1_SND\n");
else if (a==P2_RLY) printf("P2_RLY\n");
switch( a ) {
case P1_SND:
if (queue_int_isfull(buff_12))
P1_suspended = 1;
else {
P1_suspended = 0;
queue_int_enqueue(buff_12,next_item());
}
break;
case P2_RLY:
if (queue_int_isempty(buff_12) || queue_int_isfull(buff_23))
P2_suspended = 1;
else {
P2_suspended = 0;
queue_int_enqueue(buff_23, queue_int_dequeue(buff_12));
}
break;
}
}
}[/code]
[b]It seems to work along the lines of the question. But, when the two queues are filled up, they do not empty (or dequeue).[/b]
Here is the library file, [i]queue_int.h[/i]:
[code]#ifndef QUEUE_int_H
#define QUEUE_int_H
typedef struct queue_int_implementation queue_int;
queue_int * new_bounded_queue_int(int max);
queue_int * new_unbounded_queue_int();
int queue_int_isempty(queue_int *q);
int queue_int_isfull(queue_int *q);
int queue_int_size(queue_int *q);
void queue_int_enqueue(queue_int *q, int x);
int queue_int_dequeue(queue_int *q);
int queue_int_front(queue_int *q);
void queue_int_print(queue_int *q, void (* item_print)(int item));
void queue_int_release(queue_int *q);
#endif[/code]
Please help!
They don't empty because the only time you call dequeue() is:
[cpp]
queue_int_dequeue(buff_12)
[/cpp]
So printer 2 is never going to be emptied (I don't see any calls to dequeue printer 2).
I doesn't really specify in the question when printers are supposed to process "print jobs". Are you just supposed to dequeue both printers at the end of an iteration to simulate "processed" jobs?
[QUOTE=Electroholic;43203064]They don't empty because the only time you call dequeue() is:
[cpp]
queue_int_dequeue(buff_12)
[/cpp]
So printer 2 is never going to be emptied (I don't see any calls to dequeue printer 2).
I doesn't really specify in the question when printers are supposed to process "print jobs". Are you just supposed to dequeue both printers at the end of an iteration to simulate "processed" jobs?[/QUOTE]
The output suggests a different situation. Both printers are filling up queues and are not dequeue-ing at all.
When any one of the printers are full, they should be emptied. So, effectively, they would not display as being full, but instead, at the most, have 2/3 jobs going. I suppose this would simulate "processed jobs."
Sorry, you need to Log In to post a reply to this thread.