多进程04-优先级&特权

228 阅读1分钟

进程调度-优先级

pr 进程优先级
ni nice值越小,优先级越高

  1. 实验:
    $nice = $argv[1];
    $count = 0;
    $start = time();
    $pid = pcntl_fork();
    
    if ($pid == 0){
        // 设置
        fprintf(STDOUT, "child process pid=%d, nice=%d\n", posix_getpid(), pcntl_getpriority(posix_getpid()));
        pcntl_setpriority($nice, posix_getpid(), PRIO_PROCESS);
        fprintf(STDOUT, "child process pid=%d, nice=%d\n", posix_getpid(), pcntl_getpriority(posix_getpid()));
        while (1){
            $count++;
            if (time() - $start > 5){
                break;
            }
        }
    }elseif ($pid > 0){
        fprintf(STDOUT, "parent process pid=%d, nice=%d\n", posix_getpid(), posix_getsid(posix_getpid()));
        while (1){
            $count++;
            if (time() - $start > 5){
                break;
            }
        }
    }
    
    fprintf(STDOUT, "proc:%d count:%d \n", posix_getpid(), $count);
    sleep(2);
    
  2. 测试
    • php demo4.php 5
    • php demo4.php 20
    • php demo4.php -10

suid特权进程

进程运行过程种需要特殊的权限,临时赋值

  1. suid,sgid给程序文件,程序文件启动后就是一个特权进程
  2. 这样无权限用户可以通过特权程序完成特殊操作。
  3. 特权进程也可在有需求时获取对应权限进行操作。特殊标识是s

image-20210601223420740.png 实验:

// chmod 4755 /usr/bin/php
// su other 
// php demo6.php

$file="aaa.txt";

$uid = posix_getuid();
$euid = posix_geteuid();
fprintf(STDOUT, "uid=%d, euid=%d \n", $uid, $euid);

posix_seteuid($euid);
posix_setuid($euid);

$uid = posix_getuid();
$euid = posix_geteuid();
fprintf(STDOUT, "uid=%d, euid=%d \n", $uid, $euid);

if (posix_access($file, POSIX_W_OK)){
    fprintf(STDOUT, "I can change file\n");
}else{
    fprintf(STDOUT, "I can't change file\n");
}

image-20210601232607262.png

image-20210601232616359.png