CSAPP第⼋章家庭作业参考答案(CSAPP第三版系列) 宁码输入法8.12 这个程序会输出多少个“hello”输出⾏? 分析该程序doit()函数的⾏为,4个进程都会再返回到主程序,所以程序会输出8个“hello”输出⾏。
8.16 下⾯这个程序的输出是什么?
分析该程序,⼦进程将count减⼀,⽗进程将其加⼀后输出,所以输出是: counter = 1。
8.20使⽤execve编写⼀个叫做myls的程序,该程序的⾏为和/bin/ls程序的⼀样。
除冰车
程序如下:
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
连供系统int main(int argc,char *argv[],char *envp[])
{
if(execve("/bin/ls",argv,envp) == -1)
printf("execve error!\n");
return 0;
}
测试结果如下:
8.24 修改图8-18中的程序。
程序如下:
#include <unistd.h>
#include <sys/types.h>
废盐焚烧炉选型#include <sys/wait.h> #include <signal.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#define N 2
int main()
{
int status,i;
pid_t pid;
电子管话筒
for(i = 0;i < N;i++)
if((pid = fork()) == 0)
{
int *pt = 0x0;
*pt = 15213;
exit(100+i);
}
while((pid = waitpid(-1,&status,0)) > 0)
{
if(WIFEXITED(status))
printf("child %d terminated normally with exit status = %d\n",pid,WEXITSTATUS(status)); else if(WIFSIGNALED(status))
{
fprintf(stderr,"child %d terminated by signal %d",pid,WTERMSIG(status));
海水淡化
装置psignal(WTERMSIG(status)," ");}
else
printf("child %d terminated abnormally\n",pid);
}
if(errno != ECHILD)
printf("waitpid error\n");
exit(0);
}
测试结果如下: