-
Notifications
You must be signed in to change notification settings - Fork 1
/
ll.c
152 lines (140 loc) · 2.98 KB
/
ll.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
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
#include "ll.h"
Node *temp, *last;
Node *listptr=NULL;
// Private functions
void delete_beg(){
Node *t=listptr;
listptr=listptr->next;
free(t);
}
void delete_last(){
Node *t=listptr;
while(t->next!=last) t=t->next;
last=t;
t=t->next;
last->next=NULL;
free(t);
}
char getState(int id){
char buff[1000];
char c;
int d, i;
struct stat state;
snprintf(buff, 1000, "/proc/%d/stat", id);
FILE* fp = fopen(buff, "r");
if (fp) {
if(!fscanf(fp, "%d %s %c", &d, &buff, &c)) {
printf("%d Not accessible\n", d);
perror("Error");
return -1;
}else{
return c;
}
}
}
void printState(char st){
if(st=='T')
printf("Stopped");
else if(st=='S')
printf("Running");
else if(st=='R')
printf("Running");
else
printf("Unknown");
}
// Public Functions
void createListNode(process n){
Node *newnode = malloc ( sizeof(Node) );
newnode->data.inbg = n.inbg; // Handle copy
newnode->data.id = n.id; // Handle copy
newnode->data.num = n.num; // Handle copy
strcpy(newnode->data.name, n.name);
newnode->next = NULL;
temp=last;
if(listptr==NULL)
listptr = newnode;
else
temp->next = newnode;
temp=newnode;
last=newnode;
}
int count(){
Node *t;
int n=0;
t = listptr;
while(t!=NULL){
n++;
t = t->next;
}
return n;
}
int deleteNodewithid(int id){
Node *t=listptr, *t1;
if(listptr->data.id==id)
delete_beg();
else if(last->data.id==id)
delete_last();
else{
while(t->next->data.id!=id && t->next!=last) t=t->next;
if(t->next->data.id!=id)
printf("Child already killed\n");
else{
t1=t->next;
t->next=t1->next;
free(t1);
}
}
}
Node *getNodewithid(int id){
Node *t=listptr, *t1;
while(t!=NULL){
if(t->data.id==id)
return t;
t=t->next;
}
return NULL;
}
int getidwithNum(int num){
Node *t=listptr, *t1;
while(t!=NULL){
if(t->data.num==num)
return t->data.id;
t=t->next;
}
return -1;
}
void printProcess(){
int i=0;
Node *t=listptr;
if(t==NULL)
printf("No Process in bg\n");
else{
while(t->next!=NULL){
char st=getState(t->data.id);
printf("[%d] ", t->data.num);
printState(st);
printf(" %s with id=%d\n", t->data.name, t->data.id);
t=t->next;
}
char st=getState(t->data.id);
printf("[%d] ", t->data.num);
printState(st);
printf(" %s with id=%d\n", t->data.name, t->data.id);
}
}
int deleteAllNodes(){
Node *t=listptr;
if(t==NULL)
return 1;
int i=0;
while(t!=NULL){
int id=t->data.id;
kill(id, SIGKILL);
//printf("[%d] %s with pid %d Stopped\n", i++, t->data.name, t->data.id);
t=t->next;
//free(t);
// t=listptr;
}
return 0;
// listptr=temp=last=NULL;
}