-
Notifications
You must be signed in to change notification settings - Fork 0
/
commands.cpp
77 lines (60 loc) · 1.79 KB
/
commands.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
#include<iostream>
#include<unistd.h>
#include"commands.h"
#include"buffer.h"
// Hashmap to check whether thread is alive or not
unordered_map<thread::id,bool> threadAlive;
// set thread is alive
void setAlive(thread::id id){
threadAlive[id] = true;
}
// set thread is die ( its works is done )
void setDie(thread::id id){
threadAlive[id] = false;
}
// function to addblk to hashqueue
void addblk(string blk_num, string status){
int blknum = stoi(blk_num);
int sts = stoi(status);
if(searchHQ(blknum)){
std::cout<<"Block already in HashQueue\n";
return;
}
// if the status of the block is unlocked or marked as delayed write
if(sts==1 || sts==3){
addToHQ(blknum,1,sts);
cout<<"Block Added !!\n";
if(sts==1){
// if status of block is unlocked then add to tail in the freelist
addToTailFL(blknum,1,sts);
}else{
// if status of block is marked as delayed write then add to head in the freelist
addToHeadFL(blknum,1,sts);
}
}else{
cout<<"\nERROR\n";
}
sleep(1);
}
// Display the IDs of the thread that are still alive ( waiting for a block to be assign)
void displayPL(){
cout<<"\n---------------PROCESS_LIST---------------\n";
for(auto i=threadAlive.begin() ; i!=threadAlive.end() ; i++){
if(i->second){
std::cout<<i->first<<" ";
}
}
std::cout<<"\n\n";
}
// function to print the defined data structures
void echo(string arg){
if(arg=="hashqueue"){
displayHQ(); // print Hashqueue
}else if(arg=="freelist"){
displayFL(); // print free list
}else if(arg=="processlist"){
displayPL(); // print Process list
}else{
printf("Invalid Command !!\n");
}
}