Process.c 17.1 KB
Newer Older
Hisham Muhammad's avatar
Hisham Muhammad committed
1
2
/*
htop - Process.c
Hisham Muhammad's avatar
Hisham Muhammad committed
3
(C) 2004-2015 Hisham H. Muhammad
Hisham Muhammad's avatar
Hisham Muhammad committed
4
5
6
7
Released under the GNU GPL, see the COPYING file
in the source distribution for its full text.
*/

Hisham Muhammad's avatar
Hisham Muhammad committed
8
#include "Process.h"
Hisham Muhammad's avatar
Hisham Muhammad committed
9
#include "Settings.h"
10

Hisham Muhammad's avatar
Hisham Muhammad committed
11
12
#include "CRT.h"
#include "String.h"
13
#include "RichString.h"
14
#include "Platform.h"
Hisham Muhammad's avatar
Hisham Muhammad committed
15
16
17
18
19
20
21

#include <stdio.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <sys/param.h>
#include <sys/stat.h>
#include <unistd.h>
Hisham Muhammad's avatar
Hisham Muhammad committed
22
#include <stdlib.h>
Hisham Muhammad's avatar
Hisham Muhammad committed
23
24
25
26
#include <signal.h>
#include <string.h>
#include <stdbool.h>
#include <pwd.h>
Hisham Muhammad's avatar
Hisham Muhammad committed
27
#include <time.h>
Hisham Muhammad's avatar
Hisham Muhammad committed
28
#include <assert.h>
Hisham Muhammad's avatar
Hisham Muhammad committed
29

sherpya's avatar
sherpya committed
30
31
32
33
34
#ifdef __ANDROID__
#define SYS_ioprio_get __NR_ioprio_get
#define SYS_ioprio_set __NR_ioprio_set
#endif

35
// On Linux, this works only with glibc 2.1+. On earlier versions
Hisham Muhammad's avatar
Hisham Muhammad committed
36
// the behavior is similar to have a hardcoded page size.
37
#ifndef PAGE_SIZE
Hisham Muhammad's avatar
Hisham Muhammad committed
38
#define PAGE_SIZE ( sysconf(_SC_PAGESIZE) )
39
#endif
Hisham Muhammad's avatar
Hisham Muhammad committed
40
#define PAGE_SIZE_KB ( PAGE_SIZE / ONE_K )
Hisham Muhammad's avatar
Hisham Muhammad committed
41
42

/*{
Hisham Muhammad's avatar
Hisham Muhammad committed
43
#include "Object.h"
44

Hisham Muhammad's avatar
Hisham Muhammad committed
45
#include <sys/types.h>
Hisham Muhammad's avatar
Hisham Muhammad committed
46

47
#define PROCESS_FLAG_IO 0x0001
48

49
50
51
52
53
54
55
56
57
58
59
60
61
62
typedef enum ProcessFields {
   PID = 1,
   COMM = 2,
   STATE = 3,
   PPID = 4,
   PGRP = 5,
   SESSION = 6,
   TTY_NR = 7,
   TPGID = 8,
   MINFLT = 10,
   MAJFLT = 12,
   PRIORITY = 18,
   NICE = 19,
   STARTTIME = 21,
63
   PROCESSOR = 38,
64
65
66
67
68
69
70
71
72
73
   M_SIZE = 39,
   M_RESIDENT = 40,
   ST_UID = 46,
   PERCENT_CPU = 47,
   PERCENT_MEM = 48,
   USER = 49,
   TIME = 50,
   NLWP = 51,
   TGID = 52,
} ProcessField;
Hisham Muhammad's avatar
Hisham Muhammad committed
74
75
76
77

typedef struct Process_ {
   Object super;

Hisham Muhammad's avatar
Hisham Muhammad committed
78
   struct Settings_* settings;
Hisham Muhammad's avatar
Hisham Muhammad committed
79

80
   unsigned long long int time;
Hisham Muhammad's avatar
Hisham Muhammad committed
81
   pid_t pid;
82
83
   pid_t ppid;
   pid_t tgid;
Hisham Muhammad's avatar
Hisham Muhammad committed
84
85
   char* comm;
   int indent;
86
87
88
89

   int basenameOffset;
   bool updated;

Hisham Muhammad's avatar
Hisham Muhammad committed
90
91
   char state;
   bool tag;
Hisham Muhammad's avatar
Hisham Muhammad committed
92
   bool showChildren;
93
   bool show;
94
95
96
   unsigned int pgrp;
   unsigned int session;
   unsigned int tty_nr;
97
   int tpgid;
98
   uid_t st_uid;
Hisham Muhammad's avatar
Hisham Muhammad committed
99
   unsigned long int flags;
100
   int processor;
Hisham Muhammad's avatar
Hisham Muhammad committed
101
102
103
104
105

   float percent_cpu;
   float percent_mem;
   char* user;

Hisham Muhammad's avatar
Hisham Muhammad committed
106
107
   long int priority;
   long int nice;
108
   long int nlwp;
Hisham Muhammad's avatar
Hisham Muhammad committed
109
110
   char starttime_show[8];
   time_t starttime_ctime;
Hisham Muhammad's avatar
Hisham Muhammad committed
111

112
113
   long m_size;
   long m_resident;
Hisham Muhammad's avatar
Hisham Muhammad committed
114
115
116
117
118

   int exit_signal;

   unsigned long int minflt;
   unsigned long int majflt;
119
   #ifdef DEBUG
Hisham Muhammad's avatar
Hisham Muhammad committed
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
   long int itrealvalue;
   unsigned long int vsize;
   long int rss;
   unsigned long int rlim;
   unsigned long int startcode;
   unsigned long int endcode;
   unsigned long int startstack;
   unsigned long int kstkesp;
   unsigned long int kstkeip;
   unsigned long int signal;
   unsigned long int blocked;
   unsigned long int sigignore;
   unsigned long int sigcatch;
   unsigned long int wchan;
   unsigned long int nswap;
   unsigned long int cnswap;
   #endif

Hisham Muhammad's avatar
Hisham Muhammad committed
138
139
} Process;

Hisham Muhammad's avatar
Hisham Muhammad committed
140
141
142
143
144
145
typedef struct ProcessFieldData_ {
   const char* name;
   const char* title;
   const char* description;
   int flags;
} ProcessFieldData;
Hisham Muhammad's avatar
Hisham Muhammad committed
146

147
148
// Implemented in platform-specific code:
void Process_setupColumnWidths();
Hisham Muhammad's avatar
Hisham Muhammad committed
149
150
void Process_writeField(Process* this, RichString* str, ProcessField field);
long Process_compare(const void* v1, const void* v2);
151
152
153
154
155
void Process_delete(Object* cast);
bool Process_isThread(Process* this);
extern ProcessFieldData Process_fields[];
extern char* Process_pidFormat;
extern char* Process_tpgidFormat;
Hisham Muhammad's avatar
Hisham Muhammad committed
156

157
typedef Process*(*Process_New)(struct Settings_*);
158
159
160
161
162
163
164
165
typedef void (*Process_WriteField)(Process*, RichString*, ProcessField);

typedef struct ProcessClass_ {
   const ObjectClass super;
   const Process_WriteField writeField;
} ProcessClass;

#define As_Process(this_)              ((ProcessClass*)((this_)->super.klass))
166

Hisham Muhammad's avatar
Hisham Muhammad committed
167
}*/
168

169
170
static int Process_getuid = -1;

171
#define ONE_K 1024L
Hisham Muhammad's avatar
Hisham Muhammad committed
172
173
174
#define ONE_M (ONE_K * ONE_K)
#define ONE_G (ONE_M * ONE_K)

175
#define ONE_DECIMAL_K 1000L
176
#define ONE_DECIMAL_M (ONE_DECIMAL_K * ONE_DECIMAL_K)
177
#define ONE_DECIMAL_G (ONE_DECIMAL_M * ONE_DECIMAL_K)
178

179
void Process_humanNumber(RichString* str, unsigned long number, bool coloring) {
Hisham Muhammad's avatar
Hisham Muhammad committed
180
181
   char buffer[11];
   int len;
182
183
184
185
186
187
188
189
   
   int largeNumberColor = CRT_colors[LARGE_NUMBER];
   int processMegabytesColor = CRT_colors[PROCESS_MEGABYTES];
   int processColor = CRT_colors[PROCESS];
   if (!coloring) {
      largeNumberColor = CRT_colors[PROCESS];
      processMegabytesColor = CRT_colors[PROCESS];
   }
Hisham Muhammad's avatar
Hisham Muhammad committed
190
 
191
   if(number >= (10 * ONE_DECIMAL_M)) {
192
193
194
      #ifdef __LP64__
      if(number >= (100 * ONE_DECIMAL_G)) {
         len = snprintf(buffer, 10, "%4ldT ", number / ONE_G);
195
         RichString_appendn(str, largeNumberColor, buffer, len);
196
         return;
197
      } else if (number >= (1000 * ONE_DECIMAL_M)) {
198
         len = snprintf(buffer, 10, "%4.1lfT ", (double)number / ONE_G);
199
         RichString_appendn(str, largeNumberColor, buffer, len);
200
201
         return;
      }
202
      #endif
203
      if(number >= (100 * ONE_DECIMAL_M)) {
204
         len = snprintf(buffer, 10, "%4ldG ", number / ONE_M);
205
         RichString_appendn(str, largeNumberColor, buffer, len);
206
         return;
207
      }
208
      len = snprintf(buffer, 10, "%4.1lfG ", (double)number / ONE_M);
209
      RichString_appendn(str, largeNumberColor, buffer, len);
210
      return;
211
   } else if (number >= 100000) {
Hisham Muhammad's avatar
Hisham Muhammad committed
212
      len = snprintf(buffer, 10, "%4ldM ", number / ONE_K);
213
      RichString_appendn(str, processMegabytesColor, buffer, len);
214
      return;
215
   } else if (number >= 1000) {
Hisham Muhammad's avatar
Hisham Muhammad committed
216
      len = snprintf(buffer, 10, "%2ld", number/1000);
217
      RichString_appendn(str, processMegabytesColor, buffer, len);
Hisham Muhammad's avatar
Hisham Muhammad committed
218
      number %= 1000;
219
      len = snprintf(buffer, 10, "%03lu ", number);
220
      RichString_appendn(str, processColor, buffer, len);
221
      return;
Hisham Muhammad's avatar
Hisham Muhammad committed
222
   }
223
   len = snprintf(buffer, 10, "%5lu ", number);
224
   RichString_appendn(str, processColor, buffer, len);
Hisham Muhammad's avatar
Hisham Muhammad committed
225
226
}

227
void Process_colorNumber(RichString* str, unsigned long long number, bool coloring) {
228
   char buffer[14];
229
230
231
232
233
234
235
236
237
238
239

   int largeNumberColor = CRT_colors[LARGE_NUMBER];
   int processMegabytesColor = CRT_colors[PROCESS_MEGABYTES];
   int processColor = CRT_colors[PROCESS];
   int processShadowColor = CRT_colors[PROCESS_SHADOW];
   if (!coloring) {
      largeNumberColor = CRT_colors[PROCESS];
      processMegabytesColor = CRT_colors[PROCESS];
      processShadowColor = CRT_colors[PROCESS];
   }

240
241
   if (number > 10000000000) {
      snprintf(buffer, 13, "%11lld ", number / 1000);
242
243
244
      RichString_appendn(str, largeNumberColor, buffer, 5);
      RichString_appendn(str, processMegabytesColor, buffer+5, 3);
      RichString_appendn(str, processColor, buffer+8, 4);
245
   } else {
246
      snprintf(buffer, 13, "%11llu ", number);
247
248
249
250
      RichString_appendn(str, largeNumberColor, buffer, 2);
      RichString_appendn(str, processMegabytesColor, buffer+2, 3);
      RichString_appendn(str, processColor, buffer+5, 3);
      RichString_appendn(str, processShadowColor, buffer+8, 4);
251
252
253
   }
}

254
255
void Process_printTime(RichString* str, unsigned long long totalHundredths) {
   unsigned long long totalSeconds = totalHundredths / 100;
Hisham Muhammad's avatar
Hisham Muhammad committed
256

257
258
259
260
   unsigned long long hours = totalSeconds / 3600;
   int minutes = (totalSeconds / 60) % 60;
   int seconds = totalSeconds % 60;
   int hundredths = totalHundredths - (totalSeconds * 100);
Hisham Muhammad's avatar
Hisham Muhammad committed
261
   char buffer[11];
262
263
   if (hours >= 100) {
      snprintf(buffer, 10, "%7lluh ", hours);
Hisham Muhammad's avatar
Hisham Muhammad committed
264
265
      RichString_append(str, CRT_colors[LARGE_NUMBER], buffer);
   } else {
266
267
268
269
270
271
272
273
      if (hours) {
         snprintf(buffer, 10, "%2lluh", hours);
         RichString_append(str, CRT_colors[LARGE_NUMBER], buffer);
         snprintf(buffer, 10, "%02d:%02d ", minutes, seconds);
      } else {
         snprintf(buffer, 10, "%2d:%02d.%02d ", minutes, seconds, hundredths);
      }
      RichString_append(str, CRT_colors[DEFAULT_COLOR], buffer);
Hisham Muhammad's avatar
Hisham Muhammad committed
274
275
276
   }
}

277
static inline void Process_writeCommand(Process* this, int attr, int baseattr, RichString* str) {
278
   int start = RichString_size(str);
279
   RichString_append(str, attr, this->comm);
Hisham Muhammad's avatar
Hisham Muhammad committed
280
   if (this->settings->highlightBaseName) {
281
      int finish = RichString_size(str) - 1;
282
283
      if (this->basenameOffset != -1)
         finish = (start + this->basenameOffset) - 1;
284
285
286
287
      int colon = RichString_findChar(str, ':', start);
      if (colon != -1 && colon < finish) {
         finish = colon;
      } else {
288
         for (int i = finish - start; i >= 0; i--) {
289
290
291
292
293
            if (this->comm[i] == '/') {
               start += i+1;
               break;
            }
         }
Hisham Muhammad's avatar
Hisham Muhammad committed
294
      }
295
      RichString_setAttrn(str, baseattr, start, finish);
Hisham Muhammad's avatar
Hisham Muhammad committed
296
297
298
   }
}

299
void Process_outputRate(RichString* str, char* buffer, int n, double rate, int coloring) {
Hisham Muhammad's avatar
Hisham Muhammad committed
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
   int largeNumberColor = CRT_colors[LARGE_NUMBER];
   int processMegabytesColor = CRT_colors[PROCESS_MEGABYTES];
   int processColor = CRT_colors[PROCESS];
   if (!coloring) {
      largeNumberColor = CRT_colors[PROCESS];
      processMegabytesColor = CRT_colors[PROCESS];
   }
   if (rate < ONE_K) {
      int len = snprintf(buffer, n, "%7.2f B/s ", rate);
      RichString_appendn(str, processColor, buffer, len);
   } else if (rate < ONE_K * ONE_K) {
      int len = snprintf(buffer, n, "%7.2f K/s ", rate / ONE_K);
      RichString_appendn(str, processColor, buffer, len);
   } else if (rate < ONE_K * ONE_K * ONE_K) {
      int len = snprintf(buffer, n, "%7.2f M/s ", rate / ONE_K / ONE_K);
      RichString_appendn(str, processMegabytesColor, buffer, len);
   } else {
      int len = snprintf(buffer, n, "%7.2f G/s ", rate / ONE_K / ONE_K / ONE_K);
      RichString_appendn(str, largeNumberColor, buffer, len);
319
320
321
   }
}

322
void Process_writeField(Process* this, RichString* str, ProcessField field) {
323
   char buffer[256]; buffer[255] = '\0';
Hisham Muhammad's avatar
Hisham Muhammad committed
324
   int attr = CRT_colors[DEFAULT_COLOR];
325
   int baseattr = CRT_colors[PROCESS_BASENAME];
326
   int n = sizeof(buffer) - 1;
Hisham Muhammad's avatar
Hisham Muhammad committed
327
   bool coloring = this->settings->highlightMegabytes;
Hisham Muhammad's avatar
Hisham Muhammad committed
328
329

   switch (field) {
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
   case PERCENT_CPU: {
      if (this->percent_cpu > 999.9) {
         snprintf(buffer, n, "%4d ", (unsigned int)this->percent_cpu); 
      } else if (this->percent_cpu > 99.9) {
         snprintf(buffer, n, "%3d. ", (unsigned int)this->percent_cpu); 
      } else {
         snprintf(buffer, n, "%4.1f ", this->percent_cpu);
      }
      break;
   }
   case PERCENT_MEM: {
      if (this->percent_mem > 99.9) {
         snprintf(buffer, n, "100. "); 
      } else {
         snprintf(buffer, n, "%4.1f ", this->percent_mem);
      }
      break;
   }
Hisham Muhammad's avatar
Hisham Muhammad committed
348
   case COMM: {
Hisham Muhammad's avatar
Hisham Muhammad committed
349
      if (this->settings->highlightThreads && Process_isThread(this)) {
350
351
352
         attr = CRT_colors[PROCESS_THREAD];
         baseattr = CRT_colors[PROCESS_THREAD_BASENAME];
      }
Hisham Muhammad's avatar
Hisham Muhammad committed
353
      if (!this->settings->treeView || this->indent == 0) {
354
         Process_writeCommand(this, attr, baseattr, str);
Hisham Muhammad's avatar
Hisham Muhammad committed
355
356
357
358
         return;
      } else {
         char* buf = buffer;
         int maxIndent = 0;
359
360
361
         bool lastItem = (this->indent < 0);
         int indent = (this->indent < 0 ? -this->indent : this->indent);

Hisham Muhammad's avatar
Hisham Muhammad committed
362
         for (int i = 0; i < 32; i++)
363
            if (indent & (1 << i))
Hisham Muhammad's avatar
Hisham Muhammad committed
364
               maxIndent = i+1;
365
          for (int i = 0; i < maxIndent - 1; i++) {
366
367
            int written;
            if (indent & (1 << i))
Hisham Muhammad's avatar
Hisham Muhammad committed
368
               written = snprintf(buf, n, "%s  ", CRT_treeStr[TREE_STR_VERT]);
Hisham Muhammad's avatar
Hisham Muhammad committed
369
            else
370
371
372
               written = snprintf(buf, n, "   ");
            buf += written;
            n -= written;
373
         }
Hisham Muhammad's avatar
Hisham Muhammad committed
374
375
         const char* draw = CRT_treeStr[lastItem ? (this->settings->direction == 1 ? TREE_STR_BEND : TREE_STR_TEND) : TREE_STR_RTEE];
         snprintf(buf, n, "%s%s ", draw, this->showChildren ? CRT_treeStr[TREE_STR_SHUT] : CRT_treeStr[TREE_STR_OPEN] );
Hisham Muhammad's avatar
Hisham Muhammad committed
376
         RichString_append(str, CRT_colors[PROCESS_TREE], buffer);
377
         Process_writeCommand(this, attr, baseattr, str);
378
         return;
Hisham Muhammad's avatar
Hisham Muhammad committed
379
380
      }
   }
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
   case MAJFLT: Process_colorNumber(str, this->majflt, coloring); return;
   case MINFLT: Process_colorNumber(str, this->minflt, coloring); return;
   case M_RESIDENT: Process_humanNumber(str, this->m_resident * PAGE_SIZE_KB, coloring); return;
   case M_SIZE: Process_humanNumber(str, this->m_size * PAGE_SIZE_KB, coloring); return;
   case NICE: {
      snprintf(buffer, n, "%3ld ", this->nice);
      attr = this->nice < 0 ? CRT_colors[PROCESS_HIGH_PRIORITY]
           : this->nice > 0 ? CRT_colors[PROCESS_LOW_PRIORITY]
           : attr;
      break;
   }
   case NLWP: snprintf(buffer, n, "%4ld ", this->nlwp); break;
   case PGRP: snprintf(buffer, n, Process_pidFormat, this->pgrp); break;
   case PID: snprintf(buffer, n, Process_pidFormat, this->pid); break;
   case PPID: snprintf(buffer, n, Process_pidFormat, this->ppid); break;
   case PRIORITY: {
      if(this->priority == -100)
         snprintf(buffer, n, " RT ");
      else
         snprintf(buffer, n, "%3ld ", this->priority);
      break;
   }
403
   case PROCESSOR: snprintf(buffer, n, "%3d ", Settings_cpuId(this->settings, this->processor)); break;
404
405
   case SESSION: snprintf(buffer, n, Process_pidFormat, this->session); break;
   case STARTTIME: snprintf(buffer, n, "%s", this->starttime_show); break;
Hisham Muhammad's avatar
Hisham Muhammad committed
406
407
   case STATE: {
      snprintf(buffer, n, "%c ", this->state);
408
409
410
411
412
413
414
415
      switch(this->state) {
          case 'R':
              attr = CRT_colors[PROCESS_R_STATE];
              break;
          case 'D':
              attr = CRT_colors[PROCESS_D_STATE];
              break;
      }
Hisham Muhammad's avatar
Hisham Muhammad committed
416
417
418
      break;
   }
   case ST_UID: snprintf(buffer, n, "%4d ", this->st_uid); break;
419
420
421
422
   case TIME: Process_printTime(str, this->time); return;
   case TGID: snprintf(buffer, n, Process_pidFormat, this->tgid); break;
   case TPGID: snprintf(buffer, n, Process_tpgidFormat, this->tpgid); break;
   case TTY_NR: snprintf(buffer, n, "%5u ", this->tty_nr); break;
Hisham Muhammad's avatar
Hisham Muhammad committed
423
   case USER: {
Hisham Muhammad's avatar
Hisham Muhammad committed
424
      if (Process_getuid != (int) this->st_uid)
Hisham Muhammad's avatar
Hisham Muhammad committed
425
         attr = CRT_colors[PROCESS_SHADOW];
426
      if (this->user) {
427
         snprintf(buffer, n, "%-9s ", this->user);
428
      } else {
429
         snprintf(buffer, n, "%-9d ", this->st_uid);
430
      }
431
432
433
      if (buffer[9] != '\0') {
         buffer[9] = ' ';
         buffer[10] = '\0';
Hisham Muhammad's avatar
Hisham Muhammad committed
434
435
436
437
438
439
440
441
442
      }
      break;
   }
   default:
      snprintf(buffer, n, "- ");
   }
   RichString_append(str, attr, buffer);
}

443
void Process_display(Object* cast, RichString* out) {
444
   Process* this = (Process*) cast;
Hisham Muhammad's avatar
Hisham Muhammad committed
445
   ProcessField* fields = this->settings->fields;
446
   RichString_prune(out);
447
   for (int i = 0; fields[i]; i++)
448
      As_Process(this)->writeField(this, out, fields[i]);
Hisham Muhammad's avatar
Hisham Muhammad committed
449
   if (this->settings->shadowOtherUsers && (int)this->st_uid != Process_getuid)
450
451
452
      RichString_setAttr(out, CRT_colors[PROCESS_SHADOW]);
   if (this->tag == true)
      RichString_setAttr(out, CRT_colors[PROCESS_TAG]);
453
   assert(out->chlen > 0);
454
455
}

456
void Process_done(Process* this) {
457
   assert (this != NULL);
458
   free(this->comm);
459
460
}

461
462
463
464
465
466
467
468
ProcessClass Process_class = {
   .super = {
      .extends = Class(Object),
      .display = Process_display,
      .delete = Process_delete,
      .compare = Process_compare
   },
   .writeField = Process_writeField,
469
470
};

471
void Process_init(Process* this, struct Settings_* settings) {
Hisham Muhammad's avatar
Hisham Muhammad committed
472
   this->settings = settings;
473
   this->tag = false;
Hisham Muhammad's avatar
Hisham Muhammad committed
474
   this->showChildren = true;
475
   this->show = true;
476
   this->updated = false;
477
   this->basenameOffset = -1;
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
   if (Process_getuid == -1) Process_getuid = getuid();
}

void Process_toggleTag(Process* this) {
   this->tag = this->tag == true ? false : true;
}

bool Process_setPriority(Process* this, int priority) {
   int old_prio = getpriority(PRIO_PROCESS, this->pid);
   int err = setpriority(PRIO_PROCESS, this->pid, priority);
   if (err == 0 && old_prio != getpriority(PRIO_PROCESS, this->pid)) {
      this->nice = priority;
   }
   return (err == 0);
}

494
495
496
497
498
499
bool Process_changePriorityBy(Process* this, size_t delta) {
   return Process_setPriority(this, this->nice + delta);
}

void Process_sendSignal(Process* this, size_t sgn) {
   kill(this->pid, (int) sgn);
500
501
}

502
long Process_pidCompare(const void* v1, const void* v2) {
Hisham Muhammad's avatar
Hisham Muhammad committed
503
504
   Process* p1 = (Process*)v1;
   Process* p2 = (Process*)v2;
505
506
507
   return (p1->pid - p2->pid);
}

508
long Process_compare(const void* v1, const void* v2) {
509
   Process *p1, *p2;
Hisham Muhammad's avatar
Hisham Muhammad committed
510
511
   Settings *settings = ((Process*)v1)->settings;
   if (settings->direction == 1) {
512
513
514
515
516
517
      p1 = (Process*)v1;
      p2 = (Process*)v2;
   } else {
      p2 = (Process*)v1;
      p1 = (Process*)v2;
   }
Hisham Muhammad's avatar
Hisham Muhammad committed
518
   switch (settings->sortKey) {
Hisham Muhammad's avatar
Hisham Muhammad committed
519
   case PERCENT_CPU:
520
      return (p2->percent_cpu > p1->percent_cpu ? 1 : -1);
Hisham Muhammad's avatar
Hisham Muhammad committed
521
   case PERCENT_MEM:
522
      return (p2->m_resident - p1->m_resident);
Hisham Muhammad's avatar
Hisham Muhammad committed
523
   case COMM:
524
      return strcmp(p1->comm, p2->comm);
525
526
527
528
529
530
531
532
533
534
   case MAJFLT:
      return (p2->majflt - p1->majflt);
   case MINFLT:
      return (p2->minflt - p1->minflt);
   case M_RESIDENT:
      return (p2->m_resident - p1->m_resident);
   case M_SIZE:
      return (p2->m_size - p1->m_size);
   case NICE:
      return (p1->nice - p2->nice);
535
536
   case NLWP:
      return (p1->nlwp - p2->nlwp);
537
538
539
540
541
542
543
544
   case PGRP:
      return (p1->pgrp - p2->pgrp);
   case PID:
      return (p1->pid - p2->pid);
   case PPID:
      return (p1->ppid - p2->ppid);
   case PRIORITY:
      return (p1->priority - p2->priority);
545
546
   case PROCESSOR:
      return (p1->processor - p2->processor);
547
548
   case SESSION:
      return (p1->session - p2->session);
549
550
551
552
553
554
   case STARTTIME: {
      if (p1->starttime_ctime == p2->starttime_ctime)
         return (p1->pid - p2->pid);
      else
         return (p1->starttime_ctime - p2->starttime_ctime);
   }
555
556
557
558
559
560
561
562
563
564
565
566
567
568
   case STATE:
      return (p1->state - p2->state);
   case ST_UID:
      return (p1->st_uid - p2->st_uid);
   case TIME:
      return ((p2->time) - (p1->time));
   case TGID:
      return (p1->tgid - p2->tgid);
   case TPGID:
      return (p1->tpgid - p2->tpgid);
   case TTY_NR:
      return (p1->tty_nr - p2->tty_nr);
   case USER:
      return strcmp(p1->user ? p1->user : "", p2->user ? p2->user : "");
Hisham Muhammad's avatar
Hisham Muhammad committed
569
   default:
570
      return (p1->pid - p2->pid);
Hisham Muhammad's avatar
Hisham Muhammad committed
571
572
   }
}