- 论坛徽章:
- 0
|
最近在看《编程之美》这本书,是电子工业出版社broadview发行的,里面有msra的一道面试题:
1. 如何通过命令行参数,让cpu的使用率保持在任意位置,如90%?
2. 如何让cpu的使用率表现为一条正xuan曲线?
3. 如果你的电脑是双核的,那么你的程序会有什么样的结果?为什么?
我先做第1题,貌似得到的cpu(MacBook Intel Dule Core 2)使用率有问题,请大家看看哈
http://trac.lcuc.org.cn/public/t ... ul_code/cpu_usage.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <errno.h>
#include <pthread.h>
#define PROC_STAT_FILE "/proc/stat"
#define DEFAULT_CPU_USAGE "50"
static int proc_stat_exists();
static double get_cpu_usage();
static void *get_cpu_usage_callback();
static void *set_cpu_usage_callback(void *usage);
static int
proc_stat_exists()
{
struct stat buf;
if (stat(PROC_STAT_FILE, &buf) < 0)
{
if (errno == ENOENT)
{
return 0;
}
}
return 1;
}
static double
get_cpu_usage()
{
double cur_usage = 0.00;
double user1, nice1, system1, idle1;
double user2, nice2, system2, idle2;
FILE *stream = NULL;
char *line = NULL;
int line_counter;
char *lineptr = NULL;
size_t allocated_size = 0;
size_t n = 0;
char *token = NULL;
const char *delim = " ";
int token_counter;
if (stream = fopen(PROC_STAT_FILE, "r"))
{
line_counter = 0;
/* TODO: Get the first line of /proc/stat */
while (getline(&lineptr, &n, stream) != -1 && !line_counter)
{
allocated_size = strlen(lineptr);
if (line = (char *) malloc(allocated_size))
{
memset(line, 0, allocated_size);
/* TODO: Get rid of '\0' at the end of line */
if (index(line, '\0'))
{
strncpy(line, lineptr, allocated_size - 1);
}
else
{
line = lineptr;
}
}
/* TODO: Get the user, nice, system and idle value */
token = strtok(line, delim);
token_counter = 0;
while (token)
{
switch (token_counter)
{
case 1:
user1 = (double) atoi(token);
break;
case 2:
nice1 = (double) atoi(token);
break;
case 3:
system1 = (double) atoi(token);
break;
case 4:
idle1 = (double) atoi(token);
break;
}
token = strtok(NULL, delim);
token_counter++;
}
line_counter++;
if (line)
{
free(line);
line = NULL;
lineptr = NULL;
}
}
}
if (stream)
{
fclose(stream);
stream = NULL;
}
sleep(2);
if (stream = fopen(PROC_STAT_FILE, "r"))
{
line_counter = 0;
while (getline(&lineptr, &n, stream) != -1 && !line_counter)
{
allocated_size = strlen(lineptr);
if (line = (char *) malloc(allocated_size))
{
memset(line, 0, allocated_size);
if (index(line, '\0'))
{
strncpy(line, lineptr, allocated_size - 1);
}
else
{
line = lineptr;
}
}
token = strtok(line, delim);
token_counter = 0;
while (token)
{
switch (token_counter)
{
case 1:
user1 = (double) atoi(token);
break;
case 2:
nice1 = (double) atoi(token);
break;
case 3:
system1 = (double) atoi(token);
break;
case 4:
idle1 = (double) atoi(token);
break;
}
token = strtok(NULL, delim);
token_counter++;
}
line_counter++;
if (line)
{
free(line);
line = NULL;
lineptr = NULL;
}
}
}
if (stream)
{
fclose(stream);
stream = NULL;
}
cur_usage = (user2 - user1) / (user2 + nice2 + system2 + idle2 - user1 - nice1 - system1 - idle1);
return cur_usage;
}
static void *
get_cpu_usage_callback()
{
printf("CPU usage is %.2f currently\n", get_cpu_usage());
return NULL;
}
static void *
set_cpu_usage_callback(void *usage)
{
double set_usage = (double) atoi(usage) / (double) 100;
printf("try to restrict CPU usage to %.2f ...\n", set_usage);
return NULL;
}
int
main(int argc, char **argv)
{
extern char *optarg;
int c;
char *usage = DEFAULT_CPU_USAGE;
pthread_t get_cpu_usage_thread;
pthread_t set_cpu_usage_thread;
if (!proc_stat_exists())
{
printf("proc/stat is not exists\n");
return -1;
}
while ((c = getopt(argc, argv, "v:")) != -1)
{
switch (c)
{
case 'v':
if (atoi(optarg) < 100 && atoi(optarg) > 0)
{
usage = optarg;
}
break;
}
}
pthread_create(&get_cpu_usage_thread, NULL, get_cpu_usage_callback, NULL);
pthread_create(&set_cpu_usage_thread, NULL, set_cpu_usage_callback, (void *) usage);
pthread_join(get_cpu_usage_thread, NULL);
pthread_join(set_cpu_usage_thread, NULL);
return 0;
}
程序显示cpu user usage一直是3%,连我故意提高cpu负荷,它依旧3% |
|