1. (10 points) Consider the following snapshot of a system: Process

1. (10 points) Consider the following snapshot of a system:
Process
Allocation
Max
Available
A
B
C
D
A
B
C
D
A
B
C
D
P0
0
0
1
2
1
0
1
2
2
5
2
0
P1
1
0
0
0
1
7
5
0
P2
1
3
5
4
2
3
5
6
P3
0
6
3
2
0
6
5
2
P4
0
0
1
4
0
6
5
6
1. a. What is the content of the matrix Need?
Process
Allocation
Max
Need
A
B
C
D
A
B
C
D
A
B
C
D
P0
0
0
1
2
1
0
1
2
1
0
0
0
P1
1
0
0
0
1
7
5
0
0
7
5
0
P2
1
3
5
4
2
3
5
6
1
0
0
2
P3
0
6
3
2
0
6
5
2
0
0
2
0
P4
0
0
1
4
0
6
5
6
0
6
4
2
1. b. Is the system in a safe state? Why?
Yes, because there are enough resources available to complete at least 2 processes (P1 and P3) which,
when completed, free enough resources to allow the completion of all processes.
1. c. If a request from process P1 arrives for (0, 4, 2, 0), can the request be granted immediately?
Yes it can. This would leave the system with the state:
Process
Allocation
Need
Available
A
B
C
D
A
B
C
D
A
B
C
D
P0
0
0
1
2
1
0
0
0
2
1
0
0
P1
1
4
2
0
0
3
3
0
P2
1
3
5
4
1
0
0
2
P3
0
6
3
2
0
0
2
0
P4
0
0
1
4
0
6
4
2
Which would allow completion in this order:
Complete P0:
Process
Allocation
Need
Available
A
B
C
D
A
B
C
D
A
B
C
D
P1
1
4
2
0
0
3
3
0
2
1
1
2
P2
1
3
5
4
1
0
0
2
P3
0
6
3
2
0
0
2
0
P4
0
0
1
4
0
6
4
2
Complete P2:
Process
Allocation
Need
Available
A
B
C
D
A
B
C
D
A
B
C
D
P1
1
4
2
0
0
3
3
0
3
4
6
6
P3
0
6
3
2
0
0
2
0
P4
0
0
1
4
0
6
4
2
Complete P3:
Process
Allocation
Need
Available
A
B
C
D
A
B
C
D
A
B
C
D
P1
1
4
2
0
0
3
3
0
3
10
9
8
P4
0
0
1
4
0
6
4
2
Complete P4:
Process
P1
Allocation
Need
Available
A
B
C
D
A
B
C
D
A
B
C
D
1
4
2
0
0
3
3
0
3
10
10
12
Complete P1:
Process
Allocation
A
B
C
Need
D
A
B
Available
C
D
A
B
C
D
4
14
12
12
2. (10 points) Consider a swapping system in which memory consists of the following hole sizes in
memory order:
H1
H2
H3
H4
H5
H6
H7
H8
H9
16K
14K
4K
20K
18K
7K
9K
12K
15K
Which hole is taken for successive segment requests of
A
B
C
12K
10K
9K
for first fit? Now repeat the question for best fit, worst fit, and next fit.
First Fit:
H1
H2
H3
H4
H5
H6
H7
H8
H9
16
14
4
20
18
7
9
12
15
H1
H2
H3
H4
H5
H6
H7
H8
H9
4
14
4
20
18
7
9
12
15
H1
H2
H3
H4
H5
H6
H7
H8
H9
4
4
4
20
18
7
9
12
15
H1
H2
H3
H4
H5
H6
H7
H8
H9
4
4
4
11
18
7
9
12
15
A → H1
B → H2
C → H4
Best Fit:
H1
H2
H3
H4
H5
H6
H7
H8
H9
16
14
4
20
18
7
9
12
15
H1
H2
H3
H4
H5
H6
H7
H8
H9
16
14
4
20
18
7
9
0
15
H1
H2
H3
H4
H5
H6
H7
H8
H9
16
4
4
20
18
7
9
0
15
H1
H2
H3
H4
H5
H6
H7
H8
H9
16
4
4
20
18
7
0
0
15
H1
H2
H3
H4
H5
H6
H7
H8
H9
16
14
4
20
18
7
9
12
15
H1
H2
H3
H4
H5
H6
H7
H8
H9
16
14
4
8
18
7
9
12
15
H1
H2
H3
H4
H5
H6
H7
H8
H9
16
14
4
8
8
7
9
12
15
H1
H2
H3
H4
H5
H6
H7
H8
H9
5
14
4
8
18
7
9
12
15
A → H8
B → H2
C → H7
Worst Fit:
A → H4
B → H5
C → H1
Next Fit:
H1
H2
H3
H4
H5
H6
H7
H8
H9
16
14
4
20
18
7
9
12
15
H1
H2
H3
H4
H5
H6
H7
H8
H9
4
14
4
20
18
7
9
12
15
H1
H2
H3
H4
H5
H6
H7
H8
H9
4
4
4
20
18
7
9
12
15
H1
H2
H3
H4
H5
H6
H7
H8
H9
4
4
4
11
18
7
9
12
15
A → H1
B → H2
C → H4
3. (20 points) In the class we mentioned briefly the readers-writers problem with writers priority. The
problem can be solved in guarded commands as follows:
void reader()
{
when ( writers == 0 ) [
readers++;
]
void writer()
{
[writers++;]
when ( (readers == 0) && (active_writers == 0) )[
active_writers++;
]
//read
}
//write
[readers--;]
}
[writers--;
active_writers--;]
Here writers represents the number of threads that are either writing or waiting to write. The variable
active_writers represents the number of threads ( 0 or 1 ) that are currently writing.
Implement the solution using either POSIX threads or SDL threads. Again simulate the tasks by
reading from and writing to a file named counter.txt as in problem 4 of Homework 3.
My previous solution to this problem from problem 4 of Homework 3 implemented writer priority:
//
//
//
//
//
CSE 460 Homework3
name: Noah Rivera
date: 19 Feb, 2015
file: readwrite.cpp
comp: g++ -o readwrite readwrite.cpp -lSDL -lpthread
#include <iostream>
#include <fstream>
#include <sstream>
#include <SDL/SDL.h>
#include <SDL/SDL_thread.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
using namespace std;
SDL_mutex *mutex;
SDL_cond *reader_queue;
SDL_cond *writer_queue;
int readers = 0;
int writers = 0;
int reader(void* data) {
while(true) { // Read indefinitely
// Random delay until read
SDL_Delay(rand() % 3000);
SDL_LockMutex(mutex);
// Wait if there is a writer
while(writers != 0)
SDL_CondWait(reader_queue, mutex);
// Include this thread as current reader
++readers;
SDL_UnlockMutex ( mutex );
// Open the file
ifstream file("counter.txt");
if (file.good()) {
// Get the current counter value
int counter;
file >> counter;
// Print the counter value
cout << *((string*)data) << " with counter value " << counter << endl;
// Close the file
file.close();
} else // Print error
cout << *((string*)data) << " unable to read counter" << endl;
SDL_LockMutex(mutex);
// Exclude this thread from current readers and signal waiting writers if appropriate
if (--readers == 0 )
SDL_CondSignal (writer_queue);
}
SDL_UnlockMutex(mutex);
}
int writer (void* data) {
while(true) { // write indefinitely
// Random delay until write
SDL_Delay(rand() % 3000);
SDL_LockMutex(mutex);
// Wait for readers and writers
while(readers != 0 && writers != 0)
SDL_CondWait(writer_queue, mutex);
// Set writer as active
++writers;
SDL_UnlockMutex(mutex);
// Write
int counter = -1;
// Get the current counter value
ifstream file_read("counter.txt");
if (file_read.good()) {
file_read >> counter;
file_read.close();
}
// Increase counter and write it
++counter;
ofstream file_write("counter.txt", std::ofstream::trunc);
file_write << counter;
file_write.close();
// Print counter value
cout << *((string*)data) << " with counter value " << counter << endl;
SDL_LockMutex (mutex);
// Deactivate writer
--writers;
// Wake up next writers
SDL_CondSignal(writer_queue);
// Wake up all readers
SDL_CondSignal(reader_queue);
SDL_UnlockMutex (mutex);
}
}
int main () {
SDL_Thread* reader_thread[20];
SDL_Thread* writer_thread[3];
mutex = SDL_CreateMutex();
reader_queue = SDL_CreateCond();
writer_queue = SDL_CreateCond();
// Create writers
for(int i = 0; i < 3; ++i) {
stringstream ss;
ss << "writer " << i;
string* name = new string(ss.str());
writer_thread[i] = SDL_CreateThread(writer, (void*)name);
}
// Create readers
for(int i = 0; i < 20;
stringstream ss;
ss << "reader " <<
string* name = new
reader_thread[i] =
}
++i) {
i;
string(ss.str());
SDL_CreateThread (reader, (void*)name);
// Wait indefinitely for a thread
SDL_WaitThread(writer_thread[0], NULL);
SDL_DestroyCond(reader_queue);
SDL_DestroyCond(writer_queue);
SDL_DestroyMutex(mutex);
}
return 0;