• # minimiser l'utilisation du CPU

    Posté par . En réponse au message minimiser l'utilisation du CPU. Évalué à 1.

    bon même avec sched_yield ça ne marche pas !!! :( je poste tout mon code si une personne peut m'aider ... le plus important et dans la fonction int main(void)
    #include <stdlib.h>
    #include <curses.h>
    #include <time.h>
    #include <pthread.h>
    #include <unistd.h>
    #include <linux/unistd.h> /* les macros _syscallX */
    #include <linux/kernel.h> /* pour struct sysinfo */
    #include <sys/utsname.h>
    #include <sched.h>
    #define ENTER		13
    #define ESCAPE		27
    #define PROC_MEM	"/proc/meminfo"
    #define PROC_CPU	"/proc/"
    #define PROC_HDD	"/proc/"
    // question forum http://linuxfr.org/forums/19/18977.html
    //setpriority(2), sched_yield(2)
    WINDOW *menubar, *memwin, *cpuwin, *hddwin, *sup_info;
    WINDOW *sub_mem, *sub_cpu, *sub_hdd;
    time_t d_time;
    struct sysinfo s_info;
    struct utsname u_name;
    typedef struct {
    	int processor;
    	char *vendor_id;
    	int cpu_family;
    	char *model_name;
    	float cpu_mhz;
    	int cache_size;
    	int cpuid_level;
    } CPU; 
    void init_curses(void) {
    	initscr();
    	start_color();
    	init_pair(1, COLOR_WHITE, COLOR_BLUE);
    	init_pair(2, COLOR_BLUE, COLOR_WHITE);
    	init_pair(3, COLOR_RED, COLOR_WHITE);
    	init_pair(4, COLOR_RED, COLOR_YELLOW);
    	init_pair(5, COLOR_GREEN, COLOR_BLUE);
    	init_pair(6, COLOR_RED, COLOR_BLUE);
    	curs_set(0);
    	noecho();
    	keypad(stdscr, TRUE);
    }
    void menu_bar(void) {
    	menubar = subwin(stdscr, 1,63, 26,0);
    	wbkgd(menubar, COLOR_PAIR(2));
    	wattron(menubar, COLOR_PAIR(3));
    	waddstr(menubar, "ESC");
    	wattroff(menubar, COLOR_PAIR(3));
    	waddstr(menubar, " : EXIT |");
    	wattron(menubar, COLOR_PAIR(3));
    	waddstr(menubar, u_name.sysname);
    	wattroff(menubar, COLOR_PAIR(3));
    	waddstr(menubar, "|");
    	wattron(menubar, COLOR_PAIR(3));
    	waddstr(menubar, u_name.machine);
    	wattroff(menubar, COLOR_PAIR(3));
    	waddstr(menubar, "|");
    	wattron(menubar, COLOR_PAIR(3));
    	waddstr(menubar, u_name.release);
    	wattroff(menubar, COLOR_PAIR(3));
    	waddstr(menubar, "| ");
    	wprintw(menubar, "%s", ctime(&d_time));
    }
    void set_title(void) {
    	wmove(memwin, 0, 2);
    	wattron(memwin, COLOR_PAIR(5));
    	waddstr(memwin, "Utilisation de la RAM");
    	wattroff(memwin, COLOR_PAIR(5));
    	wmove(cpuwin, 0, 2);
    	wattron(cpuwin, COLOR_PAIR(5));
    	waddstr(cpuwin, "Info CPU");
    	wattroff(cpuwin, COLOR_PAIR(5));
    	wmove(hddwin, 0, 2);
    	wattron(hddwin, COLOR_PAIR(5));
    	waddstr(hddwin, "Info Hard Disk Drive");
    	wattroff(hddwin, COLOR_PAIR(5));
    }
    void create_sub_win(void) {
    	sub_mem = subwin(memwin, 23, 29, 1, 1);
    //	sub_cpu = subwin(cpuwin, 5, 15, 1, 20);
    //	box(sub_cpu, ACS_VLINE, ACS_HLINE);
    //	sub_hdd = subwin(hddwin, 5, 15, 1, 15);
    //	box(sub_hdd, ACS_VLINE, ACS_HLINE);
    }
    void info_sup(void) {
    	sysinfo(&s_info);
    	werase(sup_info);
    	box(sup_info, ACS_VLINE, ACS_HLINE);
    	wmove(sup_info, 1, 1);
    	wprintw(sup_info, "- uptime : ");
    	wattron(sup_info, COLOR_PAIR(6));
    	wprintw(sup_info, "%d", s_info.uptime);
    	wattroff(sup_info, COLOR_PAIR(6));
    	wprintw(sup_info, " sec");
    	wmove(sup_info, 3, 1);
    	wprintw(sup_info, "- processus active : ");
    	wattron(sup_info, COLOR_PAIR(6));
    	wprintw(sup_info, "%d", s_info.procs);
    	wattroff(sup_info, COLOR_PAIR(6));
    	wrefresh(sup_info);
    }
    int main(void) {
    	int key, i=0;
    	FILE *meminfo;
    	char value[256];
    	struct tm up_time;
    	time(&d_time);
    	uname(&u_name);
    	init_curses();		// initialisation de curses
    	bkgd(COLOR_PAIR(1));
    	memwin = subwin(stdscr,26,33,0,0);
    	box(memwin, ACS_VLINE, ACS_HLINE);
    	cpuwin = subwin(stdscr, 10, 30, 0, 33);
    	box(cpuwin, ACS_VLINE, ACS_HLINE);
    	hddwin = subwin(stdscr, 11, 30, 10, 33);
    	box(hddwin, ACS_VLINE, ACS_HLINE);
    	sup_info = subwin(stdscr, 5, 30, 21, 33);
    	box(sup_info, ACS_VLINE, ACS_HLINE);
    	menu_bar();
    	set_title();
    	create_sub_win();
    	refresh();
    	// CODE A PLACER DANS UN THREAD
    	while(1) {
    		werase(sub_mem);
     	 if((meminfo = fopen(PROC_MEM, "r")) != NULL) {
     		 while(fgets(value, sizeof(value), meminfo)) {
     			 if(value[0] != '#') {
    					i+=1;
    					wmove(sub_mem, i, 1);
     	 wprintw(sub_mem, "- %s", value);
    				}
    			}
    			i=0;
     	fclose(meminfo);
    		}
    		info_sup();
    		wrefresh(sub_mem);
    		sched_yield();
    	}
    	// FIN DU CODE
    	getch();
    	delwin(cpuwin);
    	delwin(memwin);
    	delwin(menubar);
    	endwin();
    	return 0;
    }