Skip to content

Commit

Permalink
done
Browse files Browse the repository at this point in the history
  • Loading branch information
Hakstar committed Aug 17, 2020
0 parents commit 14d644b
Show file tree
Hide file tree
Showing 15 changed files with 781 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Project exclude paths
/cmake-build-debug/
8 changes: 8 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions .idea/encodings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions .idea/test_shell.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
cmake_minimum_required(VERSION 3.17)
project(test_shell)

set(CMAKE_CXX_STANDARD 14)

add_executable(test_shell main.cpp PCB.cpp PCB.h RCB.cpp RCB.h util/linklist.cpp util/linklist.h util/queue.cpp util/queue.h shell.cpp shell.h)
80 changes: 80 additions & 0 deletions PCB.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
//
// Created by Alex on 2020/8/13.
//

#include "PCB.h"

PCB::~PCB() = default;

PCB::PCB(int pid, int father_pid, int priority)
: pid(pid),
father_pid(father_pid),
priority(priority),
statue(0) {

}

int PCB::get_pid() {
return this->pid;
}

int PCB::get_priority() {
return this->priority;
}

std::map<int, int> PCB::get_resources() {
return this->resources;
}

int PCB::get_statue() {
return this->statue;
}


std::list<int> PCB::get_childrenPCBs() {
return this->childrenPCBs;
}

void PCB::set_priority(int new_priority) {
this->priority = new_priority;
}

void PCB::set_statue(int new_statue) {
this->statue = new_statue;
}

void PCB::add_childProcess(int c_pid) {
this->childrenPCBs.push_back(c_pid);
}

void PCB::set_resources(int rid, int num) {
this->resources[rid] = num;
}

void PCB::delete_childProcess(int c_pid) {
this->childrenPCBs.remove(c_pid);
}

int PCB::get_father_pid() {
return this->father_pid;
}

void PCB::set_waitRid(int rid) {
this->waitRid = rid;
}

void PCB::set_waitRid_num(int n) {
this->waitRid_num = n;
}

int PCB::get_waitRid_num() {
return this->waitRid_num;
}

int PCB::get_waitRid() {
return this->waitRid;
}




50 changes: 50 additions & 0 deletions PCB.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
//
// Created by Alex on 2020/8/13.
//

#ifndef TEST_SHELL_PCB_H
#define TEST_SHELL_PCB_H

#include <list>
#include <map>

class PCB {
public:
PCB(int pid, int fatherProcess, int priority);// 创建PCB需要 pid 父进程 优先级
// 修改获取PCB数据接口
int get_pid();// 获取进程pid
int get_priority(); // 获取进程优先级
std::map<int, int> get_resources(); // 获取进程已获得的资源
int get_statue();// 获取进程当前状态
int get_father_pid();// 获取父进程pid
std::list<int> get_childrenPCBs();// 获取子进程pid
int get_waitRid_num();// 获取等待资源数量
int get_waitRid();// 获取等待资源的rid

void set_priority(int new_priority); // 修改进程优先级
void set_statue(int new_statue);// 修改进程状态
void add_childProcess(int c_pid);// 增加子进程
void set_resources(int rid, int num);// 修改拥有资源
void delete_childProcess(int c_pid);// 删除子进程
void set_waitRid(int rid);// 修改等待资源rid
void set_waitRid_num(int n);// 修改等待资源数目
~PCB();

private:
int pid;// 进程id
int priority;// 进程优先级
std::map<int, int> resources{{1, 0},
{2, 0},
{3, 0},
{4, 0}};// 拥有资源
int father_pid;// 父进程
std::list<int> childrenPCBs;// 子进程链表
int statue;// 进程状态(0 :就绪 -1 :阻塞 1:运行)

int waitRid = 0;// 等待资源的rid
int waitRid_num = 0;// 等待资源数目

};


#endif //TEST_SHELL_PCB_H
42 changes: 42 additions & 0 deletions RCB.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
//
// Created by Alex on 2020/8/13.
//

#include "RCB.h"
#include <list>

RCB::RCB(int rid, int num)
: rid(rid),
total(num),
available(num) {
}

RCB::~RCB() = default;

int RCB::get_available() {
return this->available;
}

int RCB::get_rid() {
return this->rid;
}

std::list<int> RCB::get_wait_list() {
return this->wait_list;
}

int RCB::get_total() {
return this->total;
}

void RCB::set_available(int n) {
this->available = n;
}

void RCB::remove_wait_pid(int pid) {
this->wait_list.remove(pid);
}

void RCB::add_wait_pid(int pid) {
this->wait_list.push_back(pid);
}
31 changes: 31 additions & 0 deletions RCB.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//
// Created by Alex on 2020/8/13.
//

#ifndef TEST_SHELL_RCB_H
#define TEST_SHELL_RCB_H

#include <list>
#include "PCB.h"

class RCB {
public:
RCB(int rid, int num);// 初始化RCB需要rid
int get_available();// 获取剩余资源数量
int get_rid();// 获取资源rid
int get_total();// 获取资源总数
void set_available(int n);// 设置可获得资源数量
std::list<int> get_wait_list();// 获取等待该资源的进程pid链表
void remove_wait_pid(int pid);// 删除等待进程
void add_wait_pid(int pid);// 添加等待进程
~RCB();

private:
int rid;// 资源rid
int total;// 资源总数
int available;// 当前可用资源
std::list<int> wait_list;// 等待该资源的进程链表
};


#endif //TEST_SHELL_RCB_H
120 changes: 120 additions & 0 deletions main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
#include <iostream>
#include "shell.h"
#include <string>
#include <vector>
#include <cstdlib>
#include <sstream>

constexpr size_t HASH_STRING_PIECE(const char *string_piece, size_t hashNum = 0) {
return *string_piece ? HASH_STRING_PIECE(string_piece + 1, (hashNum * 131) + *string_piece) : hashNum;
}

size_t CALC_STRING_HASH(const std::string &str) {
// 获取string对象得字符串值并传递给HAHS_STRING_PIECE计算,获取得返回值为该字符串HASH值
return HASH_STRING_PIECE(str.c_str());
}

constexpr size_t operator "" _HASH(const char *string_pice, size_t) {
return HASH_STRING_PIECE(string_pice);
}

// 根据指令选择执行函数
void run_command(std::vector<std::string> command, shell *shell);

int main() {
std::string ins;
shell os = shell();
std::vector<std::string> res;
std::cout << "please input the command" << std::endl;
while (getline(std::cin, ins)) {
if (ins == "\n")break;
std::string tep;
std::stringstream input(ins);
while (input >> tep) res.push_back(tep);
run_command(res, &os);
res.clear();
}
}

void run_command(std::vector<std::string> command, shell *shell) {
if (command.empty()) {
std::cout << "error : Invalid command, Please check input parameters" << std::endl;
}
switch (CALC_STRING_HASH(command[0])) {
case "init"_HASH:
if (command.size() != 1) {
std::cout << "error : Invalid command, Please check input parameters" << std::endl;
} else {
shell->init_Process();
}
break;
case "cr"_HASH:
if (command.size() != 3) {
std::cout << "error : Invalid command, Please check input parameters" << std::endl;
} else {
int pid = atoi(command[1].c_str());
int priority = atoi(command[2].c_str());
shell->create_process(pid, priority);
}
break;
case "de"_HASH:
if (command.size() != 2) {
std::cout << "error : Invalid command, Please check input parameters" << std::endl;
} else {
int pid = atoi(command[1].c_str());
shell->destroy_process(pid);
}
break;
case "req"_HASH:
if (command.size() != 3) {
std::cout << "error : Invalid command, Please check input parameters" << std::endl;
} else {
int rid = atoi(command[1].c_str());
int n = atoi(command[2].c_str());
shell->request_resource(rid, n);
}
break;
case "rel"_HASH:
if (command.size() != 3) {
std::cout << "error : Invalid command, Please check input parameters" << std::endl;
} else {
int rid = atoi(command[1].c_str());
int n = atoi(command[2].c_str());
shell->release_resource(rid, n);
}
break;
case "to"_HASH:
if (command.size() != 1) {
std::cout << "error : Invalid command, Please check input parameters" << std::endl;
} else {
shell->time_out();
}
break;
case "list"_HASH:
if (command.size() != 2) {
std::cout << "error : Invalid command, Please check input parameters" << std::endl;
} else {
if (command[1] == "ready") {
shell->list_ready_process();
} else if (command[1] == "block") {
shell->list_block_process();
} else if (command[1] == "res") {
shell->list_available_res();
} else {
std::cout << "error : Invalid command, Please check input parameters" << std::endl;
}
}
break;
case "pr"_HASH:
if (command.size() != 2) {
std::cout << "error : Invalid command, Please check input parameters" << std::endl;
} else {
int pid = atoi(command[1].c_str());
shell->print_PCB(pid);
}
break;
default:
std::cout << "error : Invalid command, Please check input parameters" << std::endl;
break;
}
}
Loading

0 comments on commit 14d644b

Please sign in to comment.