-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocess_container.cpp
More file actions
101 lines (90 loc) · 3.34 KB
/
process_container.cpp
File metadata and controls
101 lines (90 loc) · 3.34 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
#include "process_container.h"
process_container::process_container(QWidget *parent) : QWidget(parent)
{
H_layout_1 = new QHBoxLayout;
button = new QPushButton("Завершить");
button->setToolTip("Для завершения процесса вы должны выделить его PID и нажать на кнопку \"Завершить\"");
H_layout_1->addStretch();
H_layout_1->addWidget(button);
layout = new QVBoxLayout;
table = new QTableWidget;
update();
layout->addWidget(table);
layout->addLayout(H_layout_1);
this->setLayout(layout);
QTimer *timer = new QTimer;
timer->start(5000);
connect(timer, SIGNAL(timeout()), this, SLOT(update()));
connect(button, SIGNAL(clicked()), this, SLOT(kill()));
}
void process_container::update()
{
table->setColumnCount(4);
table->setRowCount(0);
QStringList list;
QStringList temp_list;
QString temp;
QString vms;
QString rss;
double temp_long = 0;
int bracket_1_index = 0;
int bracket_2_index = 0;
int brake_index = 0;
temp.clear();
list << "Name" << "PID" << "VMS" << "RSS";
table->setHorizontalHeaderLabels(list);
QDir *dir = new QDir("/proc");
list = dir->entryList(QStringList("*"), QDir::AllDirs);
foreach(QString str, list) {
if(str.toInt()) {
QFile file = {"/proc/" + str.toUtf8() + "/stat"}; //23-vms 24-rss pages (one page - 4kB)
if(!file.open(QIODevice::ReadOnly))
qDebug() << "PROCESS ERROR!" << "Unable to open entry";
vms = file.readAll();
bracket_1_index = vms.indexOf('(', 0);
bracket_2_index = vms.indexOf(')', 0);
brake_index = vms.indexOf(' ', bracket_1_index);
if(brake_index < bracket_2_index)
{
vms.remove(brake_index, 1);
}
temp_list = vms.split(' ');
vms = temp_list.value(22);
temp_long = vms.toDouble() / 1000;
vms = QString::number(uint64_t(temp_long));
rss = temp_list.value(23);
temp_long = rss.toDouble() * 4;
rss = QString::number(uint64_t(temp_long));
file.close();
file.setFileName("/proc/" + str.toUtf8() + "/comm");
if(!file.open(QIODevice::ReadOnly))
qDebug() << "PROCESS ERROR!" << "Unable to open entry";
temp = file.readAll();
int lastRow = table->rowCount();
QString icon = "/usr/share/icons/hicolor/32x32/apps/" + temp + ".png";
QFile file_icon(icon);
table->insertRow(lastRow);
table->setColumnWidth(0,150);
if(!file_icon.exists()) {
icon = "./binary.png";
}
table->setItem(lastRow, 0, new QTableWidgetItem(QPixmap(icon), temp));
table->setItem(lastRow, 1, new QTableWidgetItem(str));
table->setItem(lastRow, 2, new QTableWidgetItem(vms));
table->setItem(lastRow, 3, new QTableWidgetItem(rss));
}
else
{
continue;
}
}
}
void process_container::kill()
{
QList<QTableWidgetItem*> list = table->selectedItems();
QTableWidgetItem* item = list.value(0);
QString str = item->text();
qDebug() << str;
QProcess::execute("kill", QStringList() << str);
update();
}