-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscheduler.cpp
80 lines (70 loc) · 1.99 KB
/
scheduler.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include "scheduler.hpp"
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#include <vector>
typedef struct disk_io_request disk_io_request;
void* SCH_run(void* vec)
{
SCH_struct* str = (SCH_struct*) vec;
queue<disk_io_request*>* requests = str->requests;
pthread_mutex_t lock = str->lock;
int fd = str->fd;
vector<char*> vecs;
while(1)
{
pthread_mutex_lock(&lock);
if(isShutdown() && requests->size()==0){
pthread_mutex_unlock(&lock);
break;
}
if(requests->empty())
{
pthread_mutex_unlock(&lock);
continue;
}
disk_io_request* req = requests->front();
requests->pop();
pthread_mutex_unlock(&lock);
if(!req) continue;
if(req->op == io_READ)
{
pthread_mutex_lock(&req->lock);
lseek(fd, req->block_number*getBlockSize(), SEEK_SET);
read(fd, req->data, getBlockSize());
req->done = 1;
pthread_cond_signal(&req->waitFor);
pthread_mutex_unlock(&req->lock);
}
else if (req->op == io_WRITE)
{
pthread_mutex_lock(&req->lock);
lseek(fd, req->block_number*getBlockSize(), SEEK_SET);
write(fd, req->data, getBlockSize());
req->done = 1;
pthread_cond_signal(&req->waitFor);
pthread_mutex_unlock(&req->lock);
vecs.push_back(req->data);
if(vecs.size() % 300 == 0)
{
delete[] vecs.front();
vecs.erase(vecs.begin());
}
}
delete req;
}
//Writeback byte maps
lseek(fd, getFreeMapStart()*getBlockSize(), SEEK_SET);
for(int i = 0; i < getFreeMapSize(); i++)
{
write(fd, getFREE_MAP() + (i * getBlockSize()), getBlockSize());
}
lseek(fd, getInodeMapStart()*getBlockSize(), SEEK_SET);
for(int i = 0; i < getInodeMapSize(); i++)
{
write(fd, getINODE_MAP() + (i * getBlockSize()), getBlockSize());
}
exit(0);
return NULL;
}