#include <sys/resource.h>
int getpriority(int which, id_t who);
int setpriority(int which, id_t who, int priority);
int getrlimit(int resource, struct rlimit *r_limit);
int setrlimit(int resource, const struct rlimit *r_limit);
int getrusage(int who, struct rusage *r_usage);
priority = getpriority(PRIO_PROCESS, getpid());
01 #include <sys/types.h>
02 #include <sys/resource.h>
03 #include <sys/time.h>
04 #include <unistd.h>
05 #include <stdio.h>
06 #include <stdlib.h>
07 #include <math.h>
08
09 void work()
10 {
11 FILE *f;
12 int i;
13 double x = 4.5;
14
15 f = tmpfile();
16
17 for(i = 0; i < 10000; i++) {
18 fprintf(f, "Do some output\n");
19 if(ferror(f)) {
20 fprintf(stderr, "Error writing to temporary file\n");
21 exit(1);
22 }
23 }
24
25 for(i = 0; i < 1000000; i++)
26 x = log(x*x + 3.21);
27 }
28
29 int main()
30 {
31 struct rusage r_usage;
32 struct rlimit r_limit;
33 int priority;
34
35 work();
36 getrusage(RUSAGE_SELF, &r_usage);
37
38 printf("CPU usage: User = %ld.%06ld, System = %ld.%06ld\n",
39 r_usage.ru_utime.tv_sec, r_usage.ru_utime.tv_usec,
40 r_usage.ru_stime.tv_sec, r_usage.ru_stime.tv_usec);
41
42 priority = getpriority(PRIO_PROCESS, getpid());
43 printf("Current priority = %d\n", priority);
44
45 getrlimit(RLIMIT_FSIZE, &r_limit);
46 printf("Current FSIZE limit: soft = %ld, hard = %ld\n",
47 r_limit.rlim_cur, r_limit.rlim_max);
48
49 r_limit.rlim_cur = 2048;
50 r_limit.rlim_max = 4096;
51 printf("Setting a 2K fil size limit\n");
52 setrlimit(RLIMIT_FSIZE, &r_limit);
53
54 work();
55 exit(0);
56 }
[root@localhost C]# ./resource_limits.exe
CPU usage: User = 0.108006, System = 0.000000
Current priority = 0
Current FSIZE limit: soft = -1, hard = -1
Setting a 2K fil size limit
File size limit exceeded
[root@localhost C]# nice ./resource_limits.exe
CPU usage: User = 0.124007, System = 0.000000
Current priority = 10
Current FSIZE limit: soft = -1, hard = -1
Setting a 2K fil size limit
File size limit exceeded
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <sys/resource.h>
#define doit(name) pr_limits(#name, name)
static void pr_limits (char *, int);
int main(void)
{
#ifdef RLIMIT_AS
doit(RLIMIT_AS);
#endif
doit(RLIMIT_CORE);
doit(RLIMIT_CPU);
doit(RLIMIT_DATA);
doit(RLIMIT_FSIZE);
#ifdef RLIMIT_LOCKS
doit(RLIMIT_LOCKS);
#endif
#ifdef RLIMIT_MEMLOCK
doit(RLIMIT_MEMLOCK);
#endif
doit(RLIMIT_NOFILE);
#ifdef RLIMIT_NPROC
doit(RLIMIT_NPROC);
#endif
#ifdef RLIMIT_RSS
doit(RLIMIT_RSS);
#endif
#ifdef RLIMIT_SBSIZE
doit(RLIMIT_SBSIZE);
#endif
doit(RLIMIT_STACK);
#ifdef RLIMIT_VMEM
doit(RLIMIT_VMEM);
#endif
exit(0);
}
static void pr_limits(char *name, int resource)
{
struct rlimit limit;
if(getrlimit(resource, &limit) < 0) {
perror ("getrlimit");
exit (EXIT_FAILURE);
}
printf ("%-14s ", name);
if (limit.rlim_cur == RLIM_INFINITY)
printf ("(infinite) ");
else
printf ("%10lld ", limit.rlim_cur);
if (limit.rlim_max == RLIM_INFINITY)
printf ("%10lld ", limit.rlim_max);
putchar ((int)'\n');
}
[beyes@localhost libfunc]$ ./printlimit
RLIMIT_AS (infinite) 8589934591
RLIMIT_CORE 4294967296 8589934591
RLIMIT_CPU (infinite) 8589934591
RLIMIT_DATA (infinite) 8589934591
RLIMIT_FSIZE (infinite) 8589934591
RLIMIT_LOCKS (infinite) 8589934591
RLIMIT_MEMLOCK 4295032832
RLIMIT_NOFILE 4294968320
RLIMIT_NPROC 4294968320
RLIMIT_RSS (infinite) 8589934591
RLIMIT_STACK 4303355904 8589934591
欢迎光临 曲径通幽论坛 (http://www.groad.net/bbs/) | Powered by Discuz! X3.2 |