Skip to content

Commit 506d526

Browse files
committed
first commit
0 parents  commit 506d526

17 files changed

+1819
-0
lines changed

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
*.user
2+
*.autosave
3+
*.qrc
4+
*.user.*

MyOS.pro

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#-------------------------------------------------
2+
#
3+
# Project created by QtCreator 2022-10-08T17:54:44
4+
#
5+
#-------------------------------------------------
6+
7+
QT += core gui
8+
9+
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
10+
11+
TARGET = MyOS
12+
TEMPLATE = app
13+
14+
# The following define makes your compiler emit warnings if you use
15+
# any feature of Qt which has been marked as deprecated (the exact warnings
16+
# depend on your compiler). Please consult the documentation of the
17+
# deprecated API in order to know how to port your code away from it.
18+
DEFINES += QT_DEPRECATED_WARNINGS
19+
20+
# You can also make your code fail to compile if you use deprecated APIs.
21+
# In order to do so, uncomment the following line.
22+
# You can also select to disable deprecated APIs only up to a certain version of Qt.
23+
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
24+
25+
CONFIG += c++11
26+
27+
SOURCES += \
28+
main.cpp \
29+
mainwindow.cpp \
30+
pcb.cpp \
31+
cpuscheduler.cpp \
32+
cpu.cpp \
33+
newprocessdialog.cpp
34+
35+
HEADERS += \
36+
mainwindow.h \
37+
pcb.h \
38+
cpuscheduler.h \
39+
cpu.h \
40+
newprocessdialog.h
41+
42+
FORMS += \
43+
mainwindow.ui \
44+
newprocessdialog.ui
45+
46+
# Default rules for deployment.
47+
qnx: target.path = /tmp/$${TARGET}/bin
48+
else: unix:!android: target.path = /opt/$${TARGET}/bin
49+
!isEmpty(target.path): INSTALLS += target
50+
51+
RESOURCES += \
52+
res.qrc

cpu.cpp

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#include "cpu.h"
2+
3+
CPU::CPU(QObject *parent) : QObject(parent)
4+
{
5+
6+
}
7+
8+
void CPU::exec1(PCB &p)
9+
{
10+
p.time--;
11+
int dec = 3;
12+
if(p.priority - dec >= 100)
13+
{
14+
p.priority-= dec;
15+
p.priority_pre-= dec;
16+
}
17+
else
18+
{
19+
p.priority = 100;
20+
p.priority_pre = 100;
21+
}
22+
}

cpu.h

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#ifndef CPU_H
2+
#define CPU_H
3+
4+
#include <pcb.h>
5+
#include <QObject>
6+
7+
class CPU : public QObject
8+
{
9+
Q_OBJECT
10+
public:
11+
explicit CPU(QObject *parent = nullptr);
12+
void exec1(PCB &p);
13+
14+
signals:
15+
16+
public slots:
17+
};
18+
19+
#endif // CPU_H

cpuscheduler.cpp

+115
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
#include "cpuscheduler.h"
2+
3+
CPUScheduler::CPUScheduler(QObject *parent) : QObject(parent)
4+
{
5+
6+
}
7+
8+
void CPUScheduler::newProcess(PCB* newP)
9+
{
10+
if(readyQueue->size() < 6)
11+
readyQueue->push(*newP);
12+
else
13+
jobQueue->push(*newP);
14+
}
15+
16+
void CPUScheduler::newProcess(string PID,int time,int priority,string father)
17+
{
18+
PCB* newP = new PCB(PID,time,priority,father);
19+
if(readyQueue->size() < 6)
20+
readyQueue->push(*newP);
21+
else
22+
{
23+
if(father == "")
24+
jobQueue->push(*newP);
25+
else
26+
{
27+
queue<PCB>* tempQ = new queue<PCB>;
28+
tempQ->push(*newP);
29+
while(!jobQueue->empty())
30+
{
31+
tempQ->push(jobQueue->front());
32+
jobQueue->pop();
33+
}
34+
queue<PCB>* a = jobQueue;
35+
jobQueue = tempQ;
36+
delete a;
37+
}
38+
39+
}
40+
}
41+
42+
void CPUScheduler::exec1()
43+
{
44+
if(readyQueue->empty())
45+
return;
46+
PCB tempE = readyQueue->top();
47+
if(tempE.suspend)
48+
return;
49+
cpu->exec1(tempE);
50+
readyQueue->pop();
51+
52+
//其余进程优先度增加
53+
int inc = 1;
54+
int size = (int)readyQueue->size();
55+
priority_queue<PCB>* tempQ = new priority_queue<PCB>;
56+
for(int i = 0;i < size;i++)
57+
{
58+
PCB temp2 = readyQueue->top();
59+
if(temp2.suspend)
60+
{
61+
tempQ->push(temp2);
62+
readyQueue->pop();
63+
continue;
64+
}
65+
if(temp2.priority + inc <= 140)
66+
{
67+
temp2.priority += inc;
68+
temp2.priority_pre += inc;
69+
}
70+
else
71+
{
72+
temp2.priority = 140;
73+
temp2.priority_pre = 140;
74+
}
75+
76+
tempQ->push(temp2);
77+
readyQueue->pop();
78+
}
79+
priority_queue<PCB>* a;
80+
a = readyQueue;
81+
readyQueue = tempQ;
82+
delete a;
83+
84+
//进程未完成push回 已完成push历史队列
85+
if(tempE.time > 0)
86+
{
87+
readyQueue->push(tempE);
88+
}
89+
else
90+
{
91+
//运行完毕队列
92+
free(tempE.father);
93+
historyQueue->push_back(tempE);
94+
}
95+
}
96+
97+
void CPUScheduler::free(string fatherPID)
98+
{
99+
priority_queue<PCB>* tempQ = new priority_queue<PCB>;
100+
101+
while(!readyQueue->empty())
102+
{
103+
PCB tempP = readyQueue->top();
104+
if(tempP.PID == fatherPID)
105+
{
106+
tempP.running_children--;
107+
qDebug()<<"father --";
108+
}
109+
tempQ->push(tempP);
110+
readyQueue->pop();
111+
}
112+
priority_queue<PCB>* a = readyQueue;
113+
readyQueue = tempQ;
114+
delete a;
115+
}

cpuscheduler.h

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#ifndef TEST_H
2+
#define TEST_H
3+
4+
#include <pcb.h>
5+
#include <queue>
6+
#include <cpu.h>
7+
#include <QObject>
8+
#include <QDebug>
9+
10+
class CPUScheduler : public QObject
11+
{
12+
Q_OBJECT
13+
public:
14+
explicit CPUScheduler(QObject *parent = nullptr);
15+
void newProcess(PCB* newP);
16+
void newProcess(string PID,int time,int priority,string father = "");
17+
void exec1();
18+
19+
CPU* cpu = new CPU();
20+
queue<PCB>* jobQueue = new queue<PCB>;
21+
priority_queue<PCB>* suspendQueue = new priority_queue<PCB>;
22+
priority_queue<PCB>* readyQueue = new priority_queue<PCB>;
23+
vector<PCB>* historyQueue = new vector<PCB>;
24+
25+
void free(string fatherPID);
26+
27+
signals:
28+
29+
public slots:
30+
};
31+
32+
#endif // TEST_H

cpuscheuler.cpp

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#include "cpuscheuler.h"
2+
3+
4+
void CPUScheuler::newProcess(string PID,int time,int priority,pstate state,PCB* father)
5+
{
6+
PCB* newP = new PCB(PID,time,priority,state,father);
7+
jobQueue.push(*newP);
8+
}
9+
10+
void CPUScheuler::exec()
11+
{
12+
PCB temp = readyQueue.top();
13+
cpu->exec(temp);
14+
readyQueue.pop();
15+
}

icon.png

2.26 KB
Loading

main.cpp

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#include "mainwindow.h"
2+
#include <QApplication>
3+
4+
int main(int argc, char *argv[])
5+
{
6+
QApplication a(argc, argv);
7+
MainWindow w;
8+
w.show();
9+
return a.exec();
10+
}

0 commit comments

Comments
 (0)