Chinaunix首页 | 论坛 | 博客
  • 博客访问: 535433
  • 博文数量: 142
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1452
  • 用 户 组: 普通用户
  • 注册时间: 2013-09-12 16:28
文章分类

全部博文(142)

文章存档

2016年(10)

2015年(60)

2014年(72)

我的朋友

分类: LINUX

2015-05-07 16:37:17


点击(此处)折叠或打开

  1. gwwu@hz-dev2.wgw.com:~/test/process>more setpriority.c
  2. #include <sys/time.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <unistd.h>
  6. #include <string.h>
  7. #include <errno.h>
  8. #include <sys/resource.h>
  9. int print_getpriority(int which,int who)
  10. {
  11.     int prio;

  12.     errno = 0;
  13.     prio = getpriority(which,who);
  14.     if(prio == -1 && errno != 0) {
  15.         printf("getpriority error\n");
  16.         return -1;
  17.     }
  18.     return prio;
  19.     //printf("nice value = %d\n", prio);
  20. }

  21. int main(int argc, char* argv[])
  22. {
  23.     int which,prio;
  24.     id_t who;

  25.     if (argc != 4 || strchr("pgu", argv[1][0]) == NULL) {
  26.         printf("%s {p|g|u} who priority\n"
  27.                 " set priority of: p=process;g=process group;"
  28.                 "u=process for user\n",argv[0]);
  29.         return 0;
  30.     }
  31.     printf("prio=%d\n",print_getpriority(PRIO_PROCESS,0));
  32.     which = (argv[1][0] == 'p') ? PRIO_PROCESS :(argv[1][0] == 'g') ? PRIO_PGRP:PRIO_USER;
  33.     who = atoi(argv[2]);
  34.     prio = atoi(argv[3]);

  35.     if(setpriority(which,who,prio) == -1) {
  36.         printf("setpriority error\n");
  37.         return -1;
  38.     }

  39.     printf("priority=%d\n",print_getpriority(which,who));

  40.     exit(0);
  41. }
编译运行:

点击(此处)折叠或打开

  1. gwwu@hz-dev2.wgw.com:~/test/process>gcc -g setpriority.c -o setpriority
  2. gwwu@hz-dev2.wgw.com:~/test/process>./setpriority p 0 5
  3. prio=0-------------------------默认进程的nice值为0
  4. priority=5 --------非特权进程可以修改自己或者是有效用户ID与目的进程的有效用户ID或者真实用户ID一致的目的进程的nice值


阅读(929) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~