Skip to content

Commit a6120d7

Browse files
committed
Add all 5 assignments
1 parent fc2c5f0 commit a6120d7

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+2152
-39
lines changed

assign1/Makefile

+9-6
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,20 @@
33
#
44
###################################
55
CC = g++ # use g++ for compiling c++ code or gcc for c code
6-
CFLAGS = -g -Wall -std=c++11 # compilation flags: -g for debugging. Change to -O or -O2 for optimized code.
6+
CFLAGS = -g -Wall -std=c++11 # compilation flags: -g for debugging. Change to -O3 for optimized code.
77
LIB = -lm # linked libraries
88
LDFLAGS = -L. # link flags
9-
PROG = prog1 # target executable (output)
10-
SRCS = main.cpp ReadyQueue.cpp # .c or .cpp source files.
9+
PROG = test1 test2 # target executables (output)
10+
SRCS = test1.cpp test2.cpp pcbtable.cpp readyqueue.cpp # .c or .cpp source files.
1111
OBJ = $(SRCS:.cpp=.o) # object files for the target. Add more to this and next lines if there are more than one source files.
1212

13-
all : $(PROG) depend
13+
all : $(PROG)
1414

15-
$(PROG): $(OBJ)
16-
$(CC) -o $(PROG) $(OBJ) $(LDFLAGS) $(LIB)
15+
test1: test1.o pcbtable.o readyqueue.o
16+
$(CC) -o test1 test1.o pcbtable.o readyqueue.o $(LDFLAGS) $(LIB)
17+
18+
test2: test2.o pcbtable.o readyqueue.o
19+
$(CC) -o test2 test2.o pcbtable.o readyqueue.o $(LDFLAGS) $(LIB)
1720

1821
.cpp.o:
1922
$(CC) -c $(CFLAGS) $< -o $@

assign1/PCB.cpp

-3
This file was deleted.

assign1/PCB.h

+106-9
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,27 @@
1-
#pragma once
1+
/**
2+
* Assignment 1: priority queue of processes
3+
* @file pcb.h
4+
* @author ??? (TODO: your name)
5+
* @brief This is the header file for the PCB class, a process control block.
6+
* @version 0.1
7+
*/
8+
//You must complete the all parts marked as "TODO". Delete "TODO" after you are done.
9+
// Remember to addPCB sufficient comments to your code
210

3-
// Remember to add comments to your code
4-
// ...
11+
#pragma once
12+
#include <iostream>
13+
using namespace std;
514

615
// enum class of process state
716
// A process (PCB) in ready queue should be in READY state
817
enum class ProcState {NEW, READY, RUNNING, WAITING, TERMINATED};
918

10-
/*
11-
Process control block(PCB) is a data structure representing a process in the system.
12-
A process should have at least an ID and a state(i.e.NEW, READY, RUNNING, WAITING or TERMINATED).
13-
It may also have other attributes, such as scheduling information (e.g. priority)
14-
*/
19+
/**
20+
* @brief A process control block (PCB) Process control block(PCB) is a data structure representing a process in the system.
21+
A process should have at least an ID and a state(i.e.NEW, READY, RUNNING, WAITING or TERMINATED).
22+
It may also have other attributes, such as scheduling information (e.g. priority)
23+
*
24+
*/
1525
class PCB {
1626
public:
1727
// The unique process ID
@@ -22,5 +32,92 @@ class PCB {
2232
// A process in the ReadyQueue should be in READY state
2333
ProcState state;
2434

25-
// TODO: Add constructor and other necessary functions for the PCB class
35+
/**
36+
* @brief Construct a new PCB object
37+
* @param id: each process has a unique ID
38+
* @param priority: the priority of the process in the range 1-50. Larger number represents higher priority
39+
* @param state the state of the process.
40+
*/
41+
PCB(unsigned int id = 0, unsigned int priority = 1, ProcState state = ProcState::NEW) {
42+
this->id = id;
43+
this->priority = priority;
44+
this->state = state;
45+
}
46+
47+
/**
48+
* @brief Destroy the PCB object.
49+
*
50+
*/
51+
~PCB() {}
52+
53+
/**
54+
* @brief Get the ID of the PCB.
55+
*
56+
* @return unsigned int: the ID of the PCB
57+
*/
58+
unsigned int getID() {
59+
return id;
60+
}
61+
62+
/**
63+
* @brief Get the priority of the PCB.
64+
*
65+
* @return unsigned int: the priority of the PCB
66+
*/
67+
unsigned int getPriority() {
68+
return priority;
69+
}
70+
71+
/**
72+
* @brief Get the state of the PCB.
73+
*
74+
* @return ProcState: the state of the PCB
75+
*/
76+
ProcState getState() {
77+
return state;
78+
}
79+
80+
/**
81+
* @brief Change the state of the PCB.
82+
* @param state
83+
*/
84+
void setState(ProcState state) {
85+
// TODO: addPCB your code here
86+
}
87+
88+
/**
89+
* @brief Change the priority of the PCB.
90+
* @param priority
91+
*/
92+
void setPriority(unsigned int priority) {
93+
// TODO: addPCB your code here
94+
}
95+
96+
/**
97+
* @brief Print the PCB.
98+
*
99+
*/
100+
void display() const {
101+
cout << "ID: " << id;
102+
cout << ", Priority: " << priority;
103+
cout << ", State: " ;
104+
switch(state) {
105+
case ProcState::NEW:
106+
cout << "NEW";
107+
break;
108+
case ProcState::READY:
109+
cout << "READY";
110+
break;
111+
case ProcState::RUNNING:
112+
cout << "RUNNING";
113+
break;
114+
case ProcState::WAITING:
115+
cout << "WAITING";
116+
break;
117+
case ProcState::TERMINATED:
118+
cout << "TERMINATED";
119+
break;
120+
}
121+
cout << endl;
122+
}
26123
};

assign1/ReadyQueue.cpp

+49-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,51 @@
11
#include <iostream>
2-
#include "ReadyQueue.h"
2+
#include "readyqueue.h"
33

4-
// TODO: Add your implementation of ReadyQueue functions here
4+
using namespace std;
5+
6+
//You must complete the all parts marked as "TODO". Delete "TODO" after you are done.
7+
// Remember to add sufficient comments to your code
8+
9+
10+
/**
11+
* @brief Constructor for the ReadyQueue class.
12+
*/
13+
ReadyQueue::ReadyQueue() {
14+
//TODO: add your code here
15+
}
16+
17+
/**
18+
* @brief Add a PCB representing a process into the ready queue.
19+
*
20+
* @param pcbPtr: the pointer to the PCB to be added
21+
*/
22+
void ReadyQueue::addPCB(PCB *pcbPtr) {
23+
//TODO: add your code here
24+
// When adding a PCB to the queue, you must change its state to READY.
25+
}
26+
27+
/**
28+
* @brief Remove and return the PCB with the highest priority from the queue
29+
*
30+
* @return PCB*: the pointer to the PCB with the highest priority
31+
*/
32+
PCB* ReadyQueue::removePCB() {
33+
//TODO: add your code here
34+
// When removing a PCB from the queue, you must change its state to RUNNING.
35+
}
36+
37+
/**
38+
* @brief Returns the number of elements in the queue.
39+
*
40+
* @return int: the number of PCBs in the queue
41+
*/
42+
int ReadyQueue::size() {
43+
//TODO: add your code here
44+
}
45+
46+
/**
47+
* @brief Display the PCBs in the queue.
48+
*/
49+
void ReadyQueue::displayAll() {
50+
//TODO: add your code here
51+
}

assign1/ReadyQueue.h

+50-16
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,64 @@
1-
// Remember to add comments to your code
2-
// ...
3-
1+
/**
2+
* Assignment 1: priority queue of processes
3+
* @file readyqueue.h
4+
* @author ??? (TODO: your name)
5+
* @brief ReadyQueue is a queue of PCB's that are in the READY state to be scheduled to run.
6+
* It should be a priority queue such that the process with the highest priority can be selected next.
7+
* @version 0.1
8+
*/
9+
//You must complete the all parts marked as "TODO". Delete "TODO" after you are done.
10+
// Remember to add sufficient comments to your code
411
#pragma once
512

6-
#include "PCB.h"
13+
#include "pcb.h"
714

815
/**
9-
* ReadyQueue is a queue of PCB's that are in the READY state to be scheduled to run.
10-
* It should be a priority queue here uch that the process with the highest priority
11-
* can be selected next.
16+
* @brief A queue of PCB's that are in the READY state to be scheduled to run.
17+
* It should be a priority queue such that the process with the highest priority can be selected next.
1218
*/
1319
class ReadyQueue {
20+
private:
21+
// TODO: add your private member variables here
22+
// choose a data structure for the ReadyQueue. No STL class is allowed.
23+
1424
public:
15-
// TODO: Implement the required functions. Add necessary member fields and functions
16-
// You may use different parameter signatures from the example below
25+
/**
26+
* @brief Construct a new ReadyQueue object
27+
*
28+
*/
29+
ReadyQueue();
30+
31+
/**
32+
* @brief Destructor
33+
*/
34+
~ReadyQueue() {}
35+
36+
// You may add additional member functions, but don't change the definitions of the following four member functions.
1737

18-
// add a PCB representing a process into the ready queue.
19-
void add(PCB* pcbPtr);
38+
/**
39+
* @brief Add a PCB representing a process into the ready queue.
40+
*
41+
* @param pcbPtr: the pointer to the PCB to be added
42+
*/
43+
void addPCB(PCB* pcbPtr);
2044

21-
// remove and return the PCB with the highest priority from the queue
22-
PCB* removeHighest();
45+
/**
46+
* @brief Remove and return the PCB with the highest priority from the queue
47+
*
48+
* @return PCB*: the pointer to the PCB with the highest priority
49+
*/
50+
PCB* removePCB();
2351

24-
// Returns the number of elements in the queue.
52+
/**
53+
* @brief Returns the number of elements in the queue.
54+
*
55+
* @return int: the number of PCBs in the queue
56+
*/
2557
int size();
2658

27-
// Prints the queue contents to standard output.
28-
void display();
59+
/**
60+
* @brief Display the PCBs in the queue.
61+
*/
62+
void displayAll();
2963

3064
};

assign1/main.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Remember to add comments to your code
1+
// Remember to addPCB comments to your code
22

33
#include <iostream>
44
#include <cstdlib>

assign1/pcbtable.cpp

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* Assignment 1: priority queue of processes
3+
* @file pcbtable.h
4+
* @author ??? (TODO: your name)
5+
* @brief This is the implementation file for the PCBTable class.
6+
* //You must complete the all parts marked as "TODO". Delete "TODO" after you are done.
7+
* // Remember to add sufficient comments to your code
8+
*/
9+
10+
#include "pcbtable.h"
11+
12+
/**
13+
* @brief Construct a new PCBTable object of the given size (number of PCBs)
14+
*
15+
* @param size: the capacity of the PCBTable
16+
*/
17+
PCBTable::PCBTable(int size) {
18+
// TODO: add your code here
19+
}
20+
21+
/**
22+
* @brief Destroy the PCBTable object. Make sure to delete all the PCBs in the table.
23+
*
24+
*/
25+
PCBTable::~PCBTable() {
26+
// TODO: add your code here
27+
}
28+
29+
/**
30+
* @brief Get the PCB at index "idx" of the PCBTable.
31+
*
32+
* @param idx: the index of the PCB to get
33+
* @return PCB*: pointer to the PCB at index "idx"
34+
*/
35+
PCB* PCBTable::getPCB(unsigned int idx) {
36+
// TODO: add your code here
37+
}
38+
39+
/**
40+
* @brief Add a PCB to the PCBTable.
41+
*
42+
* @param pcb: the PCB to add
43+
*/
44+
void PCBTable::addPCB(PCB *pcb, unsigned int idx) {
45+
// TODO: add your code here
46+
}

0 commit comments

Comments
 (0)