内容发布更新时间 : 2024/11/14 15:01:22星期一 下面是文章的全部内容请认真阅读。
附录:第四题源代码
#include
void GetInfo(char * path,char info []);
int GetStringTimes(char *target,char *befind);
void GetCpuMMInfo(char * path,char *addinfo,char info []); int GetTime (char* info);
void DevInfo(int i,char *m_info);
void value_changed_callback(GtkSpinButton *spinButton, gpointer data); void GetUptime(int i,char *m_info);
void select_row_callback(GtkWidget *clist,gint row,gint column,GdkEventButton *event,gpointer data); void GetProInfo(void);
static int timer; static int timer1; int a;
char process_text[40]; pid_t pid; int status;
GtkTextBuffer *buffer;
void thread(void) { int i;
for(i=0;i<3;i++)
printf(\}
void value_changed_callback(GtkSpinButton *spinButton, gpointer data) {
gint value = gtk_spin_button_get_value(spinButton); GString *text = g_string_new(\ g_string_sprintf(text, \
gtk_label_set_text(GTK_LABEL(data), text->str); a=value;
strcpy(process_text,text->str); printf(\ //GetProInfo(); }
void GetInfo(char * path,char info []) { int fd=open(path,O_RDONLY); int i=0; char store[2000]; read(fd,store,sizeof(store));
26
close(fd); char *p=store; while(*p!='\\0'&&i<2000&&*p!='\\n') { info[i]=*p; i++; p++; } info[i]='\\0'; }
int GetStringTimes(char *target,char *befind) { int i=0; char *p=target; while(p!='\\0'&&p!=NULL) { if((p=strstr(p,befind))!=NULL) { i++; p++; } } return i; }
void GetCpuMMInfo(char * path,char *addinfo,char info []) { int fd1=open(path,O_RDONLY); int i=0; char store1[2000]; read(fd1,store1,sizeof(store1)); close(fd1); char *p=store1; if((p=strstr(p,addinfo))==0) { return;//failed! printf(\ } while(*p!='\\0'&&i<2000&&*p!='\\n') { info[i]=*p; i++; p++; } info[i]='\\0'; }
int GetTime (char* info) { time_t rawtime; struct tm * timeinfo; time ( &rawtime ); timeinfo = localtime ( &rawtime ); //printf ( \ strcpy(info,asctime (timeinfo));
27
return 0; }
void DevInfo(int i,char *m_info) { //char m_info[300]; switch(i) { case 0: GetInfo(\ break; case 1: GetInfo(\ break; case 2: GetInfo(\ break; case 3: GetInfo(\ break; case 4: GetInfo(\ break; case 5: GetCpuMMInfo(\ break; case 6: GetTime(m_info); break; case 7: GetInfo(\ break; case 8: GetCpuMMInfo(\ break; case 9: GetCpuMMInfo(\ break; } }
void GetUptime(int i,char *m_info) {
char current[300]; int aa=0;
DevInfo(1,current); if(i==0) {
while(current[aa]!=' '&¤t[aa]!='\\0') {
aa++; }
current[aa]='\\0';
strcpy(m_info,current); }
else if(i==1) {
while(current[aa]!=' '&¤t[aa]!='\\0')
28
{
aa++; }
strcpy(m_info,current+aa); } else ; return; }
static void helloWorld (GtkWidget *wid, GtkWidget *win) {
GtkWidget *dialog = NULL;
dialog = gtk_message_dialog_new (GTK_WINDOW (win), GTK_DIALOG_MODAL, GTK_MESSAGE_INFO, GTK_BUTTONS_CLOSE, \
gtk_window_set_position (GTK_WINDOW (dialog), GTK_WIN_POS_CENTER); gtk_dialog_run (GTK_DIALOG (dialog)); gtk_widget_destroy (dialog); }
////////////////////////Draw Graph Function///////////////////////// void cb_quit() {
gtk_timeout_remove (timer); gtk_main_quit(); }
float cal_load (void) {
FILE * fp;
static long pre_user, pre_nice, pre_system, pre_idle; //in order to extend easily later, i defined four vars separately
long user, nice, system, idle;
long d_user, d_nice, d_system, d_idle; static long total; float scale; char buf[1024];
if ((fp = fopen (\ {
while (fgets (buf, sizeof(buf), fp)) {
if( *buf == 'c' && *(buf+1) == 'p') break; } fclose (fp); } else
return 0.0;
sscanf(buf, \
// calculate delta value
d_user = user - pre_user; d_nice = nice - pre_nice;
d_system = system - pre_system; d_idle = idle - pre_idle;
total = d_user + d_nice + d_system + d_idle;
29
if (total < 1) total = 1; scale = 1.0 / total;
pre_user = user; pre_nice = nice;
pre_system = system; pre_idle = idle;
return (d_user + d_nice + d_system) * scale; }
gint Repaint (gpointer da) {
GtkWidget * drawing_area = (GtkWidget *)da;
GdkDrawable * drawable = drawing_area->window;
GdkGC *gc_chart, *gc_text; GdkColor color;
static unsigned coory[201]; int da_width, da_height; int x; float load; char buf[30];
// set chart green
gc_chart = gdk_gc_new (drawing_area->window); color.red = 0;
color.green = 0xffff; color.blue = 0;
gdk_gc_set_rgb_fg_color(gc_chart, &color ); // set text red
gc_text = gdk_gc_new (drawing_area->window); color.red = 0xffff; color.green = 0; color.blue = 0;
gdk_gc_set_rgb_fg_color(gc_text, &color );
// getting size of drawing area
da_width = drawing_area->allocation.width; da_height = drawing_area->allocation.height;
gdk_draw_rectangle (drawable, drawing_area->style->white_gc,TRUE,0,0,da_width,da_height);
// chart cpu load
for (x=0; x<200; x++)
coory[x] = coory[x+1];
load = cal_load();
coory[x] = (int)(load * da_height);
for(x=0;x gdk_draw_line (drawable, gc_chart, x, da_height, x, da_height - coory[x+1]); } // following code for drawing text sprintf (buf, \ 30