Skip to content
This repository was archived by the owner on Sep 19, 2022. It is now read-only.

Commit 923d176

Browse files
committed
fixati i processi duplicati (finalmente)
1 parent 08f7f22 commit 923d176

2 files changed

Lines changed: 9 additions & 1 deletion

File tree

SimpleProcessMonitor

0 Bytes
Binary file not shown.

SimpleProcessMonitor.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,10 @@ int main(int argc, char *argv[])
248248
// array of pids
249249
int pids[max_processes];
250250

251+
// variables needed to fix bug where the last process is duplicated at the end of the list
252+
int processesToPrint = 0;
253+
int processesPrinted = 0;
254+
251255
// get all subfolders in /proc that start with a number and store them in an array
252256
DIR *dir;
253257
struct dirent *ent;
@@ -260,6 +264,7 @@ int main(int argc, char *argv[])
260264
{
261265
// if process is running, set element at index <pid> to 1
262266
pids[atoi(ent->d_name)] = 1;
267+
processesToPrint++;
263268
// printf("%s\n", ent->d_name);
264269
}
265270
}
@@ -281,7 +286,7 @@ int main(int argc, char *argv[])
281286
for (int i = 0; i < max_processes; i++)
282287
{
283288
// check if pid should be printed (also works as a duplicate check, kinda...)
284-
if (pids[i] == 1)
289+
if (pids[i] == 1 && processesPrinted < processesToPrint)
285290
{
286291
char filename[1000];
287292
sprintf(filename, "/proc/%d/stat", i);
@@ -484,6 +489,9 @@ int main(int argc, char *argv[])
484489
// print reset
485490
printf("\033[0m");
486491

492+
// variable needed to fix bug where the last process is duplicated at the end of the list
493+
processesPrinted++;
494+
// variable needed to fix bug where some processes duplicate exponentially
487495
pids[i] = 0;
488496
}
489497
}

0 commit comments

Comments
 (0)