实验四:进程管理(二)
实验内容:
编写一个程序,打印进程的如下信息:进程标识符,父进程标识符,真实用户ID,有效用户ID,真实用户组ID,有效用户组ID。并分析真实用户ID和有效用户ID的区别。
源代码及结果:
真实用户ID和有效用户ID的区别:
真实用户ID:这个ID就是我们登陆unix系统时的身份ID。
有效用户ID:定义了操作者的权限。有效用户ID是进程的属性,决定了该进程对文件的访问权限。
阅读如下程序,编译并运行,分析进程执行过程的时间消耗(总共消耗的时间和CPU消耗的时间),并解释执行结果。再编写一个计算密集型的程序替代grep,比较两次时间的花销。注释程序主要语句。
/*process using time */
#include
#include
#include
#include
#include
void time_print(char *,clock_t);
int main(void){
//取得进程运行相关的时间
clock_t start,end;
struct tms t_start,t_end;
start = times(&t_start);
system(“grep the /usr/doc/*/* > /dev/null 2> /dev/null”);
/*command >/dev/null的作用是将是command命令的标准输出丢弃,而标准错误输出还是在屏幕上。?一般来讲标准输出和标准错误输出都是屏幕,因此错误信息还是会在屏幕上输出。>/dev/null 2> /dev/null 标准输出与标准错误输出都会被丢弃*/
// 0 1 2 标准输入 标准输出 错误输出
// > 将信息放到该文件null中
end=times(&t_end);
time_print(“elapsed”,end-start);
puts(“parent times”);
time_print(“\tuser CPU”,t_end.tms_utime);
time_print(“\tsys CPU”,t_end.tms_stime);
puts(“child times”);
time_print(“\tuser CPU”,t_end.tms_cutime);
time_print(“\tsys CPU”,t_end.tms_cstime);
exit(EXIT_SUCCESS);
}
void time_print(char *str, clock_t time)
{
long tps = sysconf(_SC_CLK_TCK);
/*函数sysconf()的作用为将时钟滴答数转化为秒数,_SC_CLK_TCK 为定义每秒钟有多少个滴答的宏*/
printf(“%s: %6.2f secs\n”,str,(float)time/tps);
}
程序运行结果:
因为该程序计算量很小,故消耗的时间比较少,CPU消耗时间均为0.00secs不足为奇。而进程的执行时间等于用户CPU时间和系统CPU时间加从硬盘读取数据时间之和。
密集型的程序替代grep:
更改为计算密集型的之后就较容易观察出消耗时间的差异。
阅读下列程序,编译并多次运行,观察执行输出次序,说明次序相同(或不同)的原因;观察进程ID,分析进程ID的分配规律。总结fork()的使用方法。注释程序主要语句。
/* fork usage */
#include
#include
#include
int main(void)
{
pid_t child;
if((child=fork())==-1){
perror(“fork”);
exit(EXIT_FAILURE);
}else if(child==0){
puts(“in child”);
printf(“\tchild pid = %d\n”,getpid());
printf(“\tchild ppid = %d\n”,getppid());
exit(EXIT_SUCCESS);
}else{
puts(“in parent”);
printf(“\tparent pid = %d\n”,getpid());
printf(“\tparent ppid = %d\n”,getppid());
}
exit(EXIT_SUCCESS);
}