-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcpyu.py
162 lines (124 loc) · 5.33 KB
/
cpyu.py
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
# !/usr/bin/python
# -*- coding: utf-8 -*-
# import sys
import time
import random
MAX_PROCESS_LIMIT = 999
HARD_DRIVE_IO_TIME_MIN = 200
HARD_DRIVE_IO_TIME_MAX = 300
VIDEO_DRIVE_IO_TIME_MIN = 100
VIDEO_DRIVE_IO_TIME_MAX = 200
PRINTER_DRIVE_IO_TIME_MIN = 50
PRINTER_DRIVE_IO_TIME_MAX = 600
PROCESS_LOOP_MIN = 100
PROCESS_LOOP_MAX = 300
PROCESS_ALLOC_TIME_MAX = 50
IO_DEVICES = {
0: 'hd',
1: 'video',
2: 'printer'
}
PROCESS_STATES = {
0: 'blocked',
1: 'created',
2: 'destroyed',
3: 'ready',
4: 'running'
}
class Process(object):
""""
A process class created to be instanciated and used along the running simulation
"""
pid = None
state = None
time_total = None
time_remaining = None
# time_running_loop = None
# time_current_status = None
# time_ioing = None
# running_time = None
# ready_time = None
# state_time_ready = None
# state_time_running = None
# state_time_blocked = None
# state_time_destroyed = None
# Declarating basic control variables
process_total = 200
delay_time = 0
clock_ping = 1
# Declarating processes array list
processes = []
# Generating PID's to prevent repetition
pid_deck = list(range(0, 1000))
random.shuffle(pid_deck)
# Processing time counter var declaration
processing_time_zero = time.clock() # Simulate started time
processing_time_result = None # Simulate result total time
time_starting = time.time()
time_ending = None
time_resulting = None
# Checking the process number typed by user. Needs to be from 5 to 999!
if (process_total >= 5 and process_total <= 999):
while len(processes) < process_total:
""""
While the processes just created are minus than the process number typed by user
to simulate, do
"""
# Sorting rate is 20% to decide if a process will be created this time or not
create_process_sorting = random.randint(1, 5) # Shorting range to make more time matching
# If you get lucky we're gonna create the process right now...
if create_process_sorting <= 1:
p = Process
p.pid = pid_deck.pop()
p.state = PROCESS_STATES[1]
p.time_total = random.randint(100, 300)
processes.append(p)
print('CLOCK: a new process was just created with PID: %d & STATUS: %s & CLOCK TIME: %d' % (p.pid, p.state, p.time_total))
# Sorting rate is 1% to process take some I/O resource
will_ioing = random.randint(1, 100)
if will_ioing == 1:
# Setting process state to running and get ready to make I/O access
p.state = PROCESS_STATES[4]
print('I/O ALERT: The process with PID %d is gonna make an I/O access... Now state is: %s' % (p.pid, p.state))
# Sorting what is gonna be the I/O resource to access
io_dev = random.randint(0, 2)
# Getting HD resource
if io_dev == 0:
io_dev_time = random.randint(HARD_DRIVE_IO_TIME_MAX, HARD_DRIVE_IO_TIME_MAX)
# Getting Vide resource
elif io_dev == 1:
io_dev_time = random.randint(VIDEO_DRIVE_IO_TIME_MIN, VIDEO_DRIVE_IO_TIME_MAX)
# Getting printer resource
elif io_dev == 2:
io_dev_time = random.randint(PRINTER_DRIVE_IO_TIME_MIN, PRINTER_DRIVE_IO_TIME_MAX)
p.time_total = p.time_total + io_dev_time
print('I/O ALERT: The process with PID %d is being access %s I/O resource and it will take %d' % (p.pid, IO_DEVICES[io_dev], io_dev_time))
time.sleep(p.time_total / 100)
print('PROCESS ALERT: The process with PID %d has finished to access I/O resource and is being destroyed after %d' % (p.pid, p.time_total))
# Destroying process before make I/O resource access
p.status = PROCESS_STATES[2]
else:
# Setting remaining running loop time to the process
p.time_remaining = p.time_total - PROCESS_ALLOC_TIME_MAX
if p.time_remaining <= 0:
# Destoying the state because is all done!
p.state = PROCESS_STATES[2]
print('PROCESS ALERT: The process with PID %d is all done! Now state is: %s' % (p.pid, p.state))
else:
# Setting ready state to the process
p.status = PROCESS_STATES[3]
print('PROCESS ALERT: The process with PID %d does not make anyone I/O and has more %d to take running. Now state is: %s' % (p.pid, p.time_remaining, p.status))
else:
print('CLOCK: None process was created this time...')
# User friendly delay to enable better view of the screen informations
time.sleep(delay_time)
# Calculating process time running
processing_time_result = processing_time_zero + time.clock()
else:
print('You need to type processs number between 5 and 999! Try again... ;-)')
# Calcutating real time app running
time_ending = time.time()
time_resulting = time_ending - time_starting
print('Total started process: %d' % len(processes))
print('All process were created and took %f of processing time. They all are ready to work!' % processing_time_result)
print('The real time app running was %f' % time_resulting)