-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathc 175 hw..py
More file actions
55 lines (42 loc) · 1.66 KB
/
c 175 hw..py
File metadata and controls
55 lines (42 loc) · 1.66 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
import matplotlib.pyplot as plt
import psutil
# Create an empty dictionary to store the application names and their CPU usage
app_name_dict = {}
# Set the count variable to 0
count = 0
# Iterate through the processes
for process in psutil.process_iter():
# Increment the count variable
count += 1
# Check if the count is less than or equal to 6
if count <= 6:
# Get the name of the application
name = process.name()
# Get the CPU usage of the application
cpu_usage = process.cpu_percent()
# Print the application name and CPU usage
print(f"{name} - {cpu_usage}%")
# Update the dictionary with the application name and CPU usage
app_name_dict.update({name: cpu_usage})
# Get the application name with maximum CPU usage
keymax = max(app_name_dict, key=app_name_dict.get)
print(f"Application with maximum CPU usage: {keymax}")
# Get the application name with minimum CPU usage
keymin = min(app_name_dict, key=app_name_dict.get)
print(f"Application with minimum CPU usage: {keymin}")
# Create a list of application names with maximum and minimum CPU usage
name_list = [keymax, keymin]
print(name_list)
# Get the maximum and minimum CPU usage values from the dictionary
app = app_name_dict.values()
max_app = max(app)
min_app = min(app)
# Create a list of maximum and minimum CPU usage values
max_min_list = [max_app, min_app]
print(max_min_list)
# Plot the bar chart
plt.figure(figsize=(6, 6))
plt.xlabel("Application")
plt.ylabel("CPU Usage")
plt.bar(name_list, max_min_list, width=0.5, color=['green', 'blue'])
plt.show()