1、实际用户ID是用户登录时,由login程序设置的,login是一个超级用户进程。只有对程序文件设置了设置用户ID位时,exec函数才设置有效用户ID,即exec可以改变有效用户ID。保存的设置用户ID从有效用户ID复制得到。
实际用户ID和实际用户组ID:标识我是谁。也就是登录用户的uid和gid,比如我的Linux以simon登录,在Linux运行的所有的命令的实际用户ID都是simon的uid,实际用户组ID都是simon的gid(可以用id命令查看)。
有效用户ID和有效用户组ID:进程用来决定我们对资源的访问权限。一般情况下,有效用户ID等于实际用户ID,有效用户组ID等于实际用户组ID。当设置-用户-ID(SUID)位设置,则有效用户ID等于文件的所有者的uid,而不是实际用户ID;同样,如果设置了设置-用户组-ID(SGID)位,则有效用户组ID等于文件所有者的gid,而不是实际用户组ID。
普通用户可以将其有效用户ID设置为其实际用户ID或保存的设置用户ID。(自己设置自己)
//设置有效ID
#include <unistd.h>
int seteuid(uid_t uid);
int setegid(gid_t gid);
2、用户执行一个root程序
(1)程序属于root,并且其设置用户ID位已设置。
实际用户ID = 用户ID
有效用户ID = root
保存的设置用户ID = root
(2)先降低特权
实际用户ID = 用户ID
有效用户ID = 用户ID
保存的设置用户ID = root
(3)运行程序,当需要特权时
实际用户ID = 用户ID
有效用户ID = root
保存的设置用户ID = root
(4)一阶段处理结束,释放特权
实际用户ID = 用户ID
有效用户ID = root
保存的设置用户ID = root
(5)最后
实际用户ID = 用户ID
有效用户ID = 用户ID
保存的设置用户ID = 用户ID
3、解释器
//#!代表这是一个解释器文件,pathname 指定用到的解释器
#!pathname [可选参数]
#!/bin/sh
4、system函数形式实现命令行的功能
//ISO C定义了system函数
#include <stdlib.h>
int system(const char *cmdstring);
//system函数的实现
#include <sys/wait.h>
#include <errno.h>
#include <unistd.h>
int system(const char *cmdstring)
{
pid_t pid;
int status;
if(cmdstring == NULL)
return 1;
if((pid = fork()) < 0)
status = -1;
else if(pid == 0){
execl("/bin/sh", "sh", "-c", cmdstring, (char *)0);//执行另一个进程,执行成功会自动退出
_exit(127);//无此命令才会执行到这
}else{
while(waitpid(pid, &status, 0) < 0){
if(errno != EINTR){
status = -1;
break;
}
}
}
return(status);
}
5、进程统计
进程记录所需的各个数据都由内核保存在进程表中,并在一个新进程被创建的时候初始化。进程终止时写一个统计记录,exec不会创建一个新记录但是记录的名字会改变。
6、获取用户登录名
#include <unistd.h>
char *getlogin(void);
7、进程优先级,linux中子进程从父进程继承nice值
#error,当编译器遇到#error会停止编译并输出后面的内容
当进程共享CPU资源时,不同优先级会体现进程对CPU的占用率。如果是多核CPU进程无需竞争CPU资源时例外。
//进程只影响自己的nice(优先级值),把参数incr加到当前优先级上(降低优先级)
int nice(int incr);
//which为PRIO_PROCESS,PRIO_PGRP,PRIO_USER表示获取当前进程优先级,进程组优先级,实际用户ID所有作用进程中最高优先级,参数who选择需要查看的一个或多个进程,为0表示当前进程,当前进程组,当前用户
int getpriority(int which, id_t who);
//设置优先级,先把value加到NZERO(优先级最大值),再变为新的nice
int setpriprity(int which, id_t who, int value);
8、进程时间
#include <sys/times.h>
//返回墙上时间(以时钟滴答数为单位)
//用户CPU时间,系统CPU时间以及结束的子进程占用的时间保存在参数,结构体struct tms中
//得到的时间是相对于某一个时刻的相对时间,使用时应该算两个时间段调用该函数,再取差值。
clock_t times(struct tms *buf);
struct tms{
clock_t tms_utime;
clock_t tms_stime;
clock_t tms_cutime;
clock_t tms_cstime;
}
#include <sys/times.h>
#include "apue.h"
void pr_exit(int status);
static void do_cmd(char *);
static void pr_times(clock_t real, struct tms *tmsstart, struct tms *tmsend);
static void do_cmd(char *cmd)
{
struct tms tmsstart,tmsend;
clock_t start, end;
int status;
printf("\ncommand: %s\n", cmd);
if((start = times(&tmsstart)) == -1)
err_sys("times error");
if((status = system(cmd)) < 0)
err_sys("system() error");
if((end = times(&tmsend)) == -1)
err_sys("times error");
pr_times(end-start, &tmsstart, &tmsend);
pr_exit(status);
}
void pr_exit(int status)
{
if(WIFEXITED(status))
printf("nornal termination, exit status = %d", WEXITSTATUS(status));
if(WIFSIGNALED(status))
printf("abnornal termination, signal number = %d", WTERMSIG(status),
#ifdef WCOREDUMP
WCOREDUMP(status) ? "(core file gernrated)" : " ");
#else
" ");
#endif
else if(WIFSTOPPED(status))
printf("child stoped, signal number =%d\n",WSTOPSIG(status));
}
static void pr_times(clock_t real, struct tms *tmsstart, struct tms *tmsend)
{
static long clktck = 0;
if(clktck == 0)
if((clktck = sysconf(_SC_CLK_TCK)) < 0)
err_sys("sysconf error");
printf("real: %7.2f\n", real / (double)clktck);
printf("user: %7.2f\n", (tmsend->tms_utime - tmsstart->tms_utime) / (double)clktck);
printf("sys: %7.2f\n", (tmsend->tms_stime - tmsstart->tms_stime) / (double)clktck);
printf("child user: %7.2f\n", (tmsend->tms_cutime - tmsstart->tms_cutime) / (double)clktck);
printf("child sys: %7.2f\n", (tmsend->tms_cstime - tmsstart->tms_cstime) / (double)clktck);
}
int main(int argc, char *argv[])
{
int i;
setbuf(stdout, NULL);
for(i=0; i<argc;i++)
{
do_cmd(argv[i]);
}
exit(0);
}