Monday, November 29, 2010

Three processes are involved in printing a file (pictured below). Process A reads the file data from the disk to Buffer 1, Process B copies the data from Buffer 1 to Buffer 2, finally Process C takes the data from Buffer 2 and print it. Process A ------------- Process B ------------ Process C --------------------->| Buffer 1 |---------------------------------->| Buffer 2 |-----------------------------> Read from File ------------- copy ------------- print Assume all three processes operate on one (file) record at a time, both buffers' capacity are one record. Write a program to coordinate the three processes using semaphores.

semaphore empty1 = 1;
semaphore empty2 = 1;
semaphore full11 = 0;
semaphore full12 = 0;
Process_A () {
while(1) {
wait(empty1);
read(next_file(), Buffer_1);
signal(full1);
}
}
Process_B () {
while(1) {
wait(full1);
wait(empty2);
copy(Buffer_2, Buffer_1);
signal(empty1);
signal(full2);
}
}
Process_C () {
while(1) {
wait(full2);
print(Buffer_2);
signal(empty2);
}
}

No comments:

Post a Comment