⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 processes.c

📁 opensolaris下列出top进程
💻 C
字号:
#include "processes.h"/** * processes.c * create a list of processes and find the top $n processes */intupdate_process (process * task){  if (task == 0)    {      return -1;    }  glibtop_proc_time *time = g_malloc (sizeof (glibtop_proc_time));  glibtop_get_proc_time (time, task->pid);  task->diff = (time->utime - task->utime);  task->utime = time->utime;  task->touched = 1;  g_free (time);  return 0;}/** * look if process is in list already, * if not, append it to our list */intinsert_process (process * root, int pid){  int done = 0;  process *current = root;  while (current != NULL)    {      if (current->pid == pid)        {          update_process (current);          done = 1;          break;        }      current = current->next;    }  if (done == 0)    {      process *neu = malloc (sizeof (process));      glibtop_proc_time *time = g_malloc (sizeof (glibtop_proc_time));      glibtop_get_proc_time (time, pid);      glibtop_proc_state *state = g_malloc (sizeof (glibtop_proc_state));      glibtop_get_proc_state (state, pid);      neu->pid = pid;      neu->utime = time->utime;      strcpy (neu->cmd, state->cmd);      neu->touched = 1;      neu->next = 0;      neu->prev = 0;      neu->diff = 0;      g_free (time);      g_free (state);      while (root->next != NULL)        {          root = root->next;        }      root->next = neu;      neu->prev = root;    }  return 0;}intcleanup_touched (process * root){  while (root != NULL)    {      root->touched = 0;      root = root->next;    }  return 0;}/** * remove stopped tasks */intclean_list (process * root){  process *temp;  while (root != NULL)    {      if (root->touched == 0 && root->pid != 0)        {          root->prev->next = root->next;          if (root->next != NULL)            {              root->next->prev = root->prev;            }          temp = root->prev;          free (root);          root = temp;        }      root = root->next;    }  return 0;}process *create_processlist (){  glibtop_proclist *list = g_malloc (sizeof (glibtop_proclist));  unsigned *liste = glibtop_get_proclist (list, GLIBTOP_KERN_PROC_ALL, 0);  process *root = malloc (sizeof (process));  root->pid = 0;  root->utime = 0;  root->diff = 0;  root->next = NULL;  root->prev = NULL;  int i;  for (i = 0; i < list->number; i++)    {      insert_process (root, liste[i]);    }  g_free (liste);  g_free (list);  return root;}intupdate_processlist (process * root){  glibtop_proclist *list = g_malloc (sizeof (glibtop_proclist));  unsigned *liste = glibtop_get_proclist (list, GLIBTOP_KERN_PROC_ALL, 0);  int i;  for (i = 0; i < list->number; i++)    {      // update all processes      insert_process (root, liste[i]);    }  // remove old tasks  clean_list (root);  // remove "touched" marks  cleanup_touched (root);  g_free (liste);  g_free (list);  return 0;}intfind_top_processes (process * root, process ** max, int number){  // clear old list  int i;  for (i = 0; i < number; i++)    {      max[i] = 0;    }  // fill up the list  while (root != NULL)    {      if (root->diff == 0)        {          root = root->next;          continue;        }      // this process didn't have the cpu      // so we are save to ignore it      // sort in      // XXX: maybe we could optimize that      int j = 0;      while (j < number)        {          process *temp;          if (max[j] != NULL && max[j]->diff < root->diff)            {              temp = max[j];              max[j] = root;              if (j + 1 < number)                {                  max[j + 1] = temp;                }              break;            }          if (max[j] == NULL)            {              max[j] = root;              break;            }          j++;        }      root = root->next;    }  return number;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -