linux任务管理器

时间:2022-01-29 03:41:12

在redhat2.4.20-8下编译通过:

由于本程序窗口部分由glad工具编制,源代码不易贴出,现只给出部分回调函数的代码:

1,“退出”按钮回调函数:

void
on_button9_clicked                      (GtkButton        *button,
                                         gpointer          user_data)
{
gtk_main_quit();
}

2,“关机”按钮回调函数:

void
on_button10_clicked                     (GtkButton        *button,
                                         gpointer          user_data)
{
system("halt");
}

3,“重新启动”按钮回调函数:

void
on_button11_clicked                     (GtkButton        *button,
                                         gpointer          user_data)
{
system("reboot");
}

4,“主机名”字符块回调函数:

void
on_label38_realize                      (GtkWidget        *widget,
                                         gpointer          user_data)
{
FILE * fp;
char buf[256];
fp=fopen("/proc/sys/kernel/hostname","r");
fgets(buf,256,fp);
fclose(fp);
gtk_label_set_text(widget,buf);
}

5,“CPU型号”字符块回调函数:

void
on_label45_realize                      (GtkWidget        *widget,
                                         gpointer          user_data)
{

FILE * fp;
int i,j=0;
char buf[256],buf1[256];
    fp=fopen("/proc/cpuinfo","r");
for(i=0;i<5;i++){
   fgets(buf,256,fp);
}
for(i=0;i<256;i++){
   if(buf[i]==':') break;
}
i+=2;
for(;i<256;i++){
   if(buf[i]==' ') break;
}
i+=1;
while(i<256){
   if(buf[i]==' '){
    i++;
   }
   else{
    buf1[j]=buf[i];
    j++;
    i++;
   }
}
fclose(fp);
   gtk_label_set_text(widget,buf1);//cpu型号
}

6,“CPU主频”回调函数:

void
on_label46_realize                      (GtkWidget        *widget,
                                         gpointer          user_data)
{

FILE * fp;
int i,j=0;
char buf[256],buf1[256];
    fp=fopen("/proc/cpuinfo","r");
for(i=0;i<7;i++){
   fgets(buf,256,fp);
}
for(i=0;i<256;i++){
   if(buf[i]==':') break;
}
i+=2;
j=0;
for(;i<256;i++){
   if(buf[i]==' ') break;
   else{
    buf1[j]=buf[i];
    j++;
   }
}
buf1[j]='/0';
fclose(fp);
   gtk_label_set_text(widget,buf1);//cpu型号
}

7,“系统版本号”回调函数:

void
on_label39_realize                      (GtkWidget        *widget,
                                         gpointer          user_data)
{
FILE * fp;
char buf[256];
fp=fopen("/proc/sys/kernel/osrelease","r");
fgets(buf,256,fp);
fclose(fp);
gtk_label_set_text(widget,buf);
}

8,“系统运行时间”回调函数:

void timeout_label40 (GtkWidget *widget,gpointer user_data){

FILE * fp;
int i,j;
char buf[64],buf1[64];
fp=fopen("/proc/uptime","r");
fgets(buf,64,fp);
j=0;
for(i=0;i<64;i++){
   if(buf[i]==' ') break;
   else {
    buf1[j]=buf[i];
    j++;
   }
}
buf1[j]='/0';
fclose(fp);

gtk_label_set_text(widget,buf1);
}

void
on_label40_realize                      (GtkWidget        *widget,
                                         gpointer          user_data)
{
gtk_timeout_add(200,timeout_label40,widget);
}

9,“CPU运行时间”回调函数:

void timeout_label42 (GtkWidget *widget,gpointer user_data)
{

FILE * fp;
int i,j;
char buf[64],buf1[64];
fp=fopen("/proc/uptime","r");
fgets(buf,64,fp);
for(i=0;i<64;i++){
   if (buf[i]==' ') break;
}
i++;
j=0;
for(;i<64;i++){
   if(buf[i]==' ') break;
   else {
    buf1[j]=buf[i];
    j++;
   }
}
buf1[j]='//';
buf1[j+1]='0';
fclose(fp);
gtk_label_set_text(widget,buf1);
}

void
on_label42_realize                      (GtkWidget        *widget,
                                         gpointer          user_data)
{
gtk_timeout_add(200,timeout_label42,widget);
}

10,“内存信息”回调函数:

void
on_label51_realize                      (GtkWidget        *widget,
                                         gpointer          user_data)
{
FILE * fp;
int i,j;
char buf[256],buf1[256];
fp=fopen("/proc/meminfo","r");
fgets(buf,256,fp);
fgets(buf,256,fp);
for(i=0;i<256;i++){
   if (buf[i]==':') break;
}
i++;
for(;i<256;i++){
   if (buf[i]!=' ') break;
}
j=0;
for(;i<256;i++){
   if(buf[i]==' ') break;
   else {
    buf1[j]=buf[i];
    j++;
   }
}
buf1[j]='/0';
fclose(fp);
gtk_label_set_text(widget,buf1);
}

11,“SWAP信息”回调函数:

void
on_label53_realize                      (GtkWidget        *widget,
                                         gpointer          user_data)
{
FILE * fp;
int i,j;
char buf[256],buf1[256];
fp=fopen("/proc/meminfo","r");
fgets(buf,256,fp);
fgets(buf,256,fp);
fgets(buf,256,fp);
for(i=0;i<256;i++){
   if (buf[i]==':') break;
}
i++;
for(;i<256;i++){
   if (buf[i]!=' ') break;
}
j=0;
for(;i<256;i++){
   if(buf[i]==' ') break;
   else {
    buf1[j]=buf[i];
    j++;
   }
}
buf1[j]='/0';
fclose(fp);
gtk_label_set_text(widget,buf1);
}

12,“内存使用信息”回调函数:

void timeout_label52 (GtkWidget   *widget,gpointer user_data)
{
FILE * fp;
int i,j;
char buf[256],buf1[256];
fp=fopen("/proc/meminfo","r");
fgets(buf,256,fp);
fgets(buf,256,fp);
for(i=0;i<256;i++){
   if (buf[i]==':') break;
}
i++;
for(;i<256;i++){
   if (buf[i]!=' ') break;
}
for(;i<256;i++){
   if (buf[i]==' ') break;
}
for(;i<256;i++){
   if (buf[i]!=' ') break;
}
j=0;
for(;i<256;i++){
   if(buf[i]==' ') break;
   else {
    buf1[j]=buf[i];
    j++;
   }
}
buf1[j]='/0';
fclose(fp);
gtk_label_set_text(widget,buf1);
}
void
on_label52_realize                      (GtkWidget        *widget,
                                         gpointer          user_data)
{
gtk_timeout_add(200,timeout_label52,widget);
}

13,“SWAT使用信息”回调函数:

void timeout_label54 (GtkWidget *widget, gpointer user_data)
{
FILE * fp;
int i,j;
char buf[256],buf1[256];
fp=fopen("/proc/meminfo","r");
fgets(buf,256,fp);
fgets(buf,256,fp);
fgets(buf,256,fp);
for(i=0;i<256;i++){
   if (buf[i]==':') break;
}
i++;
for(;i<256;i++){
   if (buf[i]!=' ') break;
}
for(;i<256;i++){
   if (buf[i]==' ') break;
}
for(;i<256;i++){
   if (buf[i]!=' ') break;
}
j=0;
for(;i<256;i++){
   if(buf[i]==' ') break;
   else {
    buf1[j]=buf[i];
    j++;
   }
}
buf1[j]='/0';
fclose(fp);
gtk_label_set_text(widget,buf1);
}

void
on_label54_realize                      (GtkWidget        *widget,
                                         gpointer          user_data)
{
gtk_timeout_add(200,timeout_label54,widget);
}

14,“进程显示”函数(此函数写在main.c中):

void procc_see(GtkWidget * window)
{
   GtkWidget *widget,*view;
   GtkTextBuffer * buffer;
   FILE *fp;
   GtkTextIter p;
   int i;
   char *buf=malloc(2048*sizeof(char));

   fp=popen("ps -l -x","r");
   /*for(i=0;i<12288;i++)
   {
     fscanf(fp,"%c",&buf[i]);
     if(buf[i]=='/0')break;
   }
   view = lookup_widget(window,"textview1");
   buffer=gtk_text_buffer_new(NULL);
  gtk_text_view_set_buffer(GTK_TEXT_VIEW(view), buffer);
   gtk_text_buffer_set_text(buffer,buf,-1);*/

   widget=lookup_widget(window,"textview1");
   buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(widget));
   buffer = gtk_text_buffer_new(gtk_text_buffer_get_tag_table(buffer));
  gtk_text_view_set_buffer(GTK_TEXT_VIEW(widget), buffer);
   g_object_unref(G_OBJECT(buffer));
   gtk_text_buffer_get_end_iter(buffer, &p);
   while((i = fread(buf, 1, sizeof(buf), fp)) > 0) {
GError *err = NULL;
gsize br, bw;
gchar *text;
if(!(text = g_locale_to_utf8(buf, i, &br, &bw, &err))) {
      g_printerr("Failed locale to UTF-8 conversion: %s/n", err->message);
      g_clear_error(&err);
      break;
}
gtk_text_buffer_insert(buffer, &p, text, bw);
g_free(text);
     }
   pclose(fp);
}

15,“查看系统类型”回调函数:和前面“查看系统版本”的回调函数是一样的,只不过要用到的文件不同。

不做赘述。

最后,用命令:./autogen.sh和make install 编译。

说明:此进程管理器实现的功能较少,不过,用此模式编写的程序很容易扩充,主要是回调函数的编写,相关内容请查看有关书籍或Google!