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

📄 limits.c

📁 BeginningLinuxProgramming example4
💻 C
字号:
//  1  Make the includes for all the functions we're going to be using in this program.#include <sys/types.h>#include <sys/resource.h>#include <sys/time.h>#include <unistd.h>#include <stdio.h>// 2  The void function writes a string to a temporary file 10000 times and then performs some arithmetic// to generate load on the CPU.void work(){    FILE *f;    int i;    double x = 4.5;    f = tmpfile();    for(i = 0; i < 10000; i++) {        fprintf(f,"Do some output\n");        if(ferror(f)) {            fprintf(stderr,"Error writing to temporary file\n");            exit(1);        }    }    for(i = 0; i < 1000000; i++)        x = log(x*x + 3.21);}//  3  The main function calls work and then uses the getrusage function to discover how much CPU time it has used. It displays this information on screen.int main(){    struct rusage r_usage;    struct rlimit r_limit;    int priority;    work();    getrusage(RUSAGE_SELF, &r_usage);    printf("CPU usage: User = %ld.%06ld, System = %ld.%06ld\n",        r_usage.ru_utime.tv_sec, r_usage.ru_utime.tv_usec,        r_usage.ru_stime.tv_sec, r_usage.ru_stime.tv_usec);//  4  Next, it calls getpriority and getrlimit to find out its current priority and file size limits respectively.    priority = getpriority(PRIO_PROCESS, getpid());    printf("Current priority = %d\n", priority);    getrlimit(RLIMIT_FSIZE, &r_limit);    printf("Current FSIZE limit: soft = %ld, hard = %ld\n",        r_limit.rlim_cur, r_limit.rlim_max);//  5  Finally, we set a file size limit using setrlimit and call work again, which fails because //  it attempts to create too large a file.    r_limit.rlim_cur = 2048;    r_limit.rlim_max = 4096;    printf("Setting a 2K file size limit\n");    setrlimit(RLIMIT_FSIZE, &r_limit);    work();    exit(0);}

⌨️ 快捷键说明

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