-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjobscommand.c
104 lines (82 loc) · 1.88 KB
/
jobscommand.c
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
#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<unistd.h>
#include<dirent.h>
#include<string.h>
#include<fcntl.h>
#include"shell.h"
typedef struct Filetime{
int pid;
long long time;
char name[100];
char state;
}filetime;
int compare(const void* a, const void* b){
filetime *filetimeA = (filetime*)a;
filetime *filetimeB = (filetime*)b;
return (filetimeA->time - filetimeB->time);
}
void jobscommand(){
filetime ftimearr[1000];
DIR *mydir;
struct dirent* myfile;
mydir = opendir("/proc");
int fd, t=0;
while((myfile = readdir(mydir))!=NULL){
if(myfile->d_name[0]>='1' && myfile->d_name[0]<='9'){
sscanf(myfile->d_name, "%d", &ftimearr[t].pid);
char finalpath[100];
finalpath[0]='\0';
strcat(finalpath, "/proc/");
strcat(finalpath, myfile->d_name);
strcat(finalpath, "/stat");
fd = open(finalpath, O_RDONLY);
char buffer[1000];
buffer[0]='\0';
read(fd, buffer, 1000);
int count=0, i=0;
// 3rd column contains the state
while(count!=2){
if(buffer[i]==' '){
count++;
}
i++;
}
ftimearr[t].state = buffer[i];
// 22nd column contains the time
while(count!=21){
if(buffer[i]==' '){
count++;
}
i++;
}
char timestring[20];
timestring[0]='\0';
int j=0;
while(buffer[i]!=' '){
timestring[j]=buffer[i];
i++;
j++;
}
timestring[j]='\0';
sscanf(timestring, "%lld", &ftimearr[t].time);
finalpath[0]='\0';
strcat(finalpath, "/proc/");
strcat(finalpath, myfile->d_name);
strcat(finalpath, "/cmdline");
fd = open(finalpath, O_RDONLY);
ftimearr[t].name[0]='\0';
read(fd, ftimearr[t].name, 100);
t++;
}
}
qsort(ftimearr, t, sizeof(filetime), compare);
for(int i=0;i<t;i++){
printf("[%d] ", i+1);
printf("%c ", ftimearr[i].state);
printf("%s ", ftimearr[i].name);
printf("%d\n", ftimearr[i].pid);
}
}