-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathkernel.py
More file actions
244 lines (211 loc) · 10.2 KB
/
Copy pathkernel.py
File metadata and controls
244 lines (211 loc) · 10.2 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# coding=utf-8
import signal
from colorama import init
from time import sleep
from shell import Shell
from file_manager import FileManager
from memory_manager import MemoryManager
from process_manager import ProcessManager
from settings.config import Config
import os
import threading
import matplotlib
class Kernel:
def __init__(self):
self.my_shell = Shell()
self.my_file_manager = FileManager(Config.storage_block_size,
Config.storage_track_num,
Config.storage_sec_num)
self.my_memory_manager = MemoryManager(
mode=Config.memory_management_mode,
page_size=Config.memory_page_size,
page_number=Config.memory_page_number,
physical_page=Config.memory_physical_page_number)
self.my_process_manager = ProcessManager(self.my_memory_manager)
self.is_monitoring = False
# start process manager
self.my_process_manager_run_thread = threading.Thread(
target=self.my_process_manager.start_manager)
self.my_process_manager_run_thread.start()
# printer Threading
self.IOdevice_thread = threading.Thread(
target=self.my_process_manager.io_device_handler)
self.IOdevice_thread.start()
# signal.signal(signal.SIGINT, self.my_shell.deblock)
# monitoring all resources
def monitoring(self, interval=1):
self.is_monitoring = True
self.my_file_manager.disk.disk_monitoring = True
while self.is_monitoring:
self.my_process_manager.resource_monitor()
self.my_memory_manager.memory_watching()
self.my_file_manager.draw_disk_speed()
sleep(interval)
def report_error(self, cmd, err_msg=''):
print('[error %s] %s' % (cmd, err_msg))
if err_msg == '':
self.display_command_description(cmd_list=[cmd])
# command: man [cmd1] [cmd2] ...
def display_command_description(self, cmd_list):
command_to_description = {
'man': 'manual page, format: man [command1] [command2] ...',
'ls': 'list directory contents, format: ls [-a|-l|-al] [path]',
'cd': 'change current working directory, format: cd [path]',
'rm':
'remove file or directory recursively, format: rm [-r|-f|-rf] path',
'mkdir': 'create directory, format: mkdir path',
'mkf':
'create common file, format: mkf path type size, e.g. mkf my_file crwx 300',
'dss': 'display storage status, format: dss',
'dms': 'display memory status, format: dms',
'exec': 'execute file, format: exec path, e.g. exec test',
'chmod':
'change mode of file, format: chmod path new_mode, e.g. chmod test erwx',
'ps': 'display process status, format: ps',
'rs': 'display resource status, format: rs',
'mon':
'start monitoring system resources, format: mon [-o], use -o to stop',
'td': 'tidy and defragment your disk, format: td',
'kill': 'kill process, format: kill pid',
'exit': 'exit MiniOS'
}
if len(cmd_list) == 0:
cmd_list = command_to_description.keys()
for cmd in cmd_list:
if cmd in command_to_description.keys():
print(cmd, '-', command_to_description[cmd])
else:
self.report_error(cmd=cmd, err_msg='no such command')
def run(self):
while True:
# a list of commands split by space or tab
current_file_list = self.my_file_manager.ls(method='get')
command_split_list = self.my_shell.get_split_command(
cwd=self.my_file_manager.current_working_path,
file_list=current_file_list)
# this indicates user push Enter directly, then nothing to happen
if len(command_split_list) == 0:
continue
for command_split in command_split_list:
if len(command_split) == 0: # an empty command
continue
tool = command_split[0] # tool name, e.g. ls, cd, ...
argc = len(command_split) # argument count
if tool == 'man':
self.display_command_description(
cmd_list=command_split[1:])
elif tool == 'ls':
if argc >= 2:
if command_split[1][0] == '-':
mode = command_split[1]
path_list = command_split[2:]
if len(path_list) == 0:
path_list = ['']
else:
mode = ''
path_list = command_split[1:]
for path in path_list:
self.my_file_manager.ls(dir_path=path, mode=mode)
else:
self.my_file_manager.ls()
elif tool == 'cd':
if argc >= 2:
self.my_file_manager.cd(dir_path=command_split[1])
else:
self.my_file_manager.cd(dir_path=os.sep)
elif tool == 'rm':
if argc >= 2:
if command_split[1][0] == '-':
mode = command_split[1]
path_list = command_split[2:]
if len(path_list) == 0:
self.report_error(cmd=tool)
else:
mode = ''
path_list = command_split[1:]
for path in path_list:
self.my_file_manager.rm(file_path=path, mode=mode)
else:
self.report_error(cmd=tool)
elif tool == 'chmod':
if argc >= 3:
self.my_file_manager.chmod(file_path=command_split[1],
file_type=command_split[2])
else:
self.report_error(cmd=tool)
elif tool == 'mkf':
if argc >= 4:
self.my_file_manager.mkf(file_path=command_split[1],
file_type=command_split[2],
size=command_split[3])
else:
self.report_error(cmd=tool)
elif tool == 'mkdir':
if argc >= 2:
for path in command_split[1:]:
self.my_file_manager.mkdir(dir_path=path)
else:
self.report_error(cmd=tool)
elif tool == 'dss':
self.my_file_manager.display_storage_status()
elif tool == 'dms':
self.my_memory_manager.display_memory_status()
# self.my_shell.block(func=self.my_memory_manager.display_memory_status)
elif tool == 'exec':
if argc >= 2:
path_list = command_split[1:]
for path in path_list:
my_file = self.my_file_manager.get_file(
file_path=path, seek_algo=Config.seek_algo)
if my_file:
if my_file['type'][3] == 'x':
self.my_process_manager.create(
exefile=my_file)
else:
self.report_error(
cmd=tool,
err_msg='no execution permission')
else:
self.report_error(cmd=tool)
else:
self.report_error(cmd=tool)
elif tool == 'ps':
self.my_process_manager.print_process_status()
# self.my_shell.block(func=self.my_process_manager.process_status)
elif tool == 'rs':
self.my_process_manager.print_resource_status()
# self.my_shell.block(func=self.my_process_manager.resource_status)
elif tool == 'mon':
if argc >= 2 and command_split[1] == '-o':
print('Stop monitoring')
self.is_monitoring = False
self.my_file_manager.disk.disk_monitoring = False
else:
# start monitoring
print('Start monitoring')
monitor_thread = threading.Thread(
target=self.monitoring)
monitor_thread.daemon = True
monitor_thread.start()
elif tool == 'td':
self.my_file_manager.tidy_disk()
elif tool == 'kill':
if argc >= 2:
for pid in command_split[1:]:
pid_to_kill = int(pid)
kill_res = self.my_process_manager.kill(
pid=pid_to_kill)
if kill_res:
self.my_memory_manager.free(pid=pid_to_kill)
else:
self.report_error(cmd=tool)
elif tool == 'exit':
self.my_process_manager.is_running = False
exit(0)
else:
self.report_error(cmd=tool, err_msg='no such command')
if __name__ == '__main__':
init(autoreset=True)
matplotlib.use('Agg')
my_kernel = Kernel()
my_kernel.run()