8.9

进程对 并发么?
AB ×
AC
AD
BC
BD
CD

8.10

A. fork
B. longjmp execve
C. setjmp

8.11

4次,画画进程图就行了

8.12

8次

8.13

x=4
x=3
x=2

8.14

3

8.15

5

8.16

counter = 2

8.17

hello 0 1 Bye 2 Bye
hello 1 0 Bye 2 Bye
hello 1 Bye 0 2 Bye

8.18

BD

8.19

2^n

8.20

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <unistd.h>
#include <string.h>
#include <ctype.h>
#include <setjmp.h>
#include <signal.h>
#include <dirent.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <errno.h>
#include <math.h>
#include <pthread.h>
#include <semaphore.h>
#include <sys/socket.h>
#include <netdb.h>
#include <netinet/in.h>
#include <arpa/inet.h>void unix_error(char *msg) /* Unix-style error */
{fprintf(stderr, "%s: %s\n", msg, strerror(errno));exit(0);
}void Execve(const char *filename, char *const argv[], char *const envp[])
{if (execve(filename, argv, envp) < 0)unix_error("Execve error");
}int main(int argc, char **argv, char **env) {Execve("/bin/ls", argv, env);return 0;
}

8.21

abc 或 bac

8.22

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <unistd.h>
#include <string.h>
#include <ctype.h>
#include <setjmp.h>
#include <signal.h>
#include <dirent.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <errno.h>
#include <math.h>
#include <pthread.h>
#include <semaphore.h>
#include <sys/socket.h>
#include <netdb.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define MAXARGS 128
#define MAXLINE 8192  /* Max text line length */extern char **environ; /* Defined by libc *//* Linux C中environ 变量是一个char** 类型,存储着系统的环境变量 */void unix_error(char *msg) /* Unix-style error */
{fprintf(stderr, "%s: %s\n", msg, strerror(errno));exit(0);
}void app_error(char *msg) /* Application error */
{fprintf(stderr, "%s\n", msg);exit(0);
}pid_t Wait(int *status)
{pid_t pid;if ((pid  = wait(status)) < 0)unix_error("Wait error");return pid;
}char *Fgets(char *ptr, int n, FILE *stream)
{char *rptr;if (((rptr = fgets(ptr, n, stream)) == NULL) && ferror(stream))app_error("Fgets error");return rptr;
}/* parseline - Parse the command line and build the argv array */
int parseline(char *buf, char **argv)
{char *delim;         /* Points to first space delimiter */int argc;            /* Number of args */int bg;              /* Background job? */buf[strlen(buf)-1] = ' ';  /* Replace trailing '\n' with space */while (*buf && (*buf == ' ')) /* Ignore leading spaces */buf++;/* Build the argv list */argc = 0;while ((delim = strchr(buf, ' '))) {argv[argc++] = buf;*delim = '\0';buf = delim + 1;while (*buf && (*buf == ' ')) /* Ignore spaces */buf++;}argv[argc] = NULL;if (argc == 0)  /* Ignore blank line */return 1;/* Should the job run in the background? */if ((bg = (*argv[argc-1] == '&')) != 0)argv[--argc] = NULL;return bg;
}pid_t Fork(void)
{pid_t pid;if ((pid = fork()) < 0)unix_error("Fork error");return pid;
}int mysystem(char *command) {char *Argv[MAXARGS];int status;if(Fork() == 0) {parseline(command, Argv);for(int i = 0; i < MAXARGS; i++) {if(Argv[i]==NULL) {break;}}execve("/bin/sh", Argv, environ);}Wait(&status);    /*等待子进程结束*/return WEXITSTATUS(status);
}int main(int argc, char **argv, char **env) {char str[12] = "/bin/sh -c ";char command[MAXLINE];char temp[MAXLINE-12];printf("\033[1;32m>\033[0m ");while(Fgets(temp, MAXLINE-12, stdin)) {strcpy(command, str);strcat(command, temp);printf("\033[1;32m mysystem 返回值为 %d \n\033[0m", mysystem(command));printf("\033[1;32m\n>\033[0m ");}return 0;
}

我并没有使用 “csapp.h” 头文件,而是用到什么就复制出什么,一方面可以清楚被封装函数的调用方法,另一方面可以学习作者是如何对函数进行封装的。
我做的有些麻烦,虽然使用了 parseline 函数,但是我的代码只支持不带参数的 command,比如 “/bin/sh ls -a”,就只能识别到 “/bin/sh ls”,可以修改 parseline 函数,不过我懒 (ಥ _ ಥ)

对于几种不同的输入,有不同的返回值:

USER@NAME:~# ./mysystem
> ls
123.py       公共     模板
.........mysystem 返回值为 0 > ./exit12mysystem 返回值为 12 > lllll
/bin/sh: 1: lllll: not foundmysystem 返回值为 127

exit12 程序非常简单,只有四行:

#include <stdio.h>
#include <stdlib.h>
int main() {exit(12);
}

8.23

一个进程的待处理信号不会排队,只会被丢弃。

8.24

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <unistd.h>
#include <string.h>
#include <ctype.h>
#include <setjmp.h>
#include <signal.h>
#include <dirent.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <errno.h>
#include <math.h>
#include <pthread.h>
#include <semaphore.h>
#include <sys/socket.h>
#include <netdb.h>
#include <netinet/in.h>
#include <arpa/inet.h>#define N 2
const char CONSTINT[10] = "HAHA";    //只读常量void unix_error(char *msg) /* Unix-style error */
{fprintf(stderr, "%s: %s\n", msg, strerror(errno));exit(0);
}pid_t Waitpid(pid_t pid, int *iptr, int options)
{pid_t retpid;if ((retpid  = waitpid(pid, iptr, options)) < 0) unix_error("Waitpid error");return(retpid);
}pid_t Fork(void)
{pid_t pid;if ((pid = fork()) < 0)unix_error("Fork error");return pid;
}int main()
{int status, i;pid_t pid;for (i = 0; i < N; i++)                       if ((pid = Fork()) == 0) { /* Child */     scanf("%s", &CONSTINT);        //访问只读常量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)) {//const char *s;    printf("child %d terminated by signal %d:", pid, WTERMSIG(status));fflush(stdout);psignal(WTERMSIG(status), NULL);}}if (errno != ECHILD)                          unix_error("waitpid error");exit(0);
}
USER@NAME:~# gcc -o P8-24 P8-24.c
USER@NAME:~# ./P8-24
123123
child 11345 terminated by signal 11: Segmentation fault
22
child 11344 terminated by signal 11: Segmentation fault

8.25

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <unistd.h>
#include <string.h>
#include <ctype.h>
#include <setjmp.h>
#include <signal.h>
#include <dirent.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <errno.h>
#include <math.h>
#include <pthread.h>
#include <semaphore.h>
#include <sys/socket.h>
#include <netdb.h>
#include <netinet/in.h>
#include <arpa/inet.h>sigjmp_buf buf;unsigned int Alarm(unsigned int seconds) {return alarm(seconds);
}void handler(int sig)
{siglongjmp(buf, 1);
}char *tfgets(char *s, int size, FILE *stream) {signal(SIGALRM, handler);if (!sigsetjmp(buf, 1)) {Alarm(5);fgets(s, size, stream);}else {return NULL;}signal(SIGALRM, SIG_DFL);return s;
}int main()
{while(1) {char s[50];char *temp = tfgets(s, 50, stdin);if(temp == NULL) {printf("超时\n\n");} else {printf("输入的字符串是:%s\n", temp);}}exit(0);
}

8.26

[第八章] 深入理解计算机系统第三版 家庭作业参考答案相关推荐

  1. [第六章] 深入理解计算机系统第三版 家庭作业参考答案

    6.22 磁道数 d 与 r - x * r 成正比 设 d = k(r - x * r) = kr(1 - x) 总容量 c = 2πxk(r^2)(1 - x) = 2πk(r^2)(x - x^ ...

  2. [第五章] 深入理解计算机系统第三版 家庭作业参考答案

    5.13 A. 画图: 关键路径为第三幅图加粗部分 B. 下界为浮点加法的延迟界限,CPE 为 3.00 C. 整数加法的延迟界限,CPE 为 1.00 D. 关键路径上只有浮点加法 5.14 voi ...

  3. [第三章] 深入理解计算机系统第三版 家庭作业参考答案

    人非圣贤孰能无过,欢迎大家提问与纠错 3.58 long decode2(long x, long y, long z) {y -= z;x *= y;return ((y << 63) ...

  4. 《数据库原理与应用》(第三版)习题参考答案

    第 1 章 数据库概述 1. 试说明数据.数据库.数据库管理系统和数据库系统的概念. 答:数据是描述事物的符号记录. 数据库是长期存储在计算机中的有组织的.可共享的大量数据的集合. 数据库管理系统是一 ...

  5. 《深入理解计算机系统》(原书第三版)家庭作业第三章(3.63)解答

    原题目: **3.63 伪代码分析: x rdi ;n rsi n=n-0x3c(60); if(n>5){-->4005c3 } else{ goto:*(8n+0x4006f8) n= ...

  6. 深入理解计算机系统(第三版)

    300M+ 超级清楚 这本书我觉得很棒 最近在自学它 很基础的东西 百度网盘:https://pan.baidu.com/s/1rUHIzjvPZFbWiia-NUHnhQ passwd: 2gsp ...

  7. 深入理解计算机系统_3e 第二章家庭作业 CS:APP3e chapter 2 homework

    初始完成日期:2017.9.26 许可:除2.55对应代码外(如需使用请联系 randy.bryant@cs.cmu.edu),任何人可以自由的使用,修改,分发本文档的代码. 本机环境: (有一些需要 ...

  8. 深入理解计算机系统(第二版)第四章知识整理

    深入理解计算机系统(第二版)笔记 第四章 处理器体系结构 4.1 Y86指令集体系结构 4.1.1 程序员可见的状态 4.1.2 Y86指令 4.1.3 指令编码 4.1.4 Y86异常 4.1.5 ...

  9. 深入理解计算机系统 -资料整理 高清中文版_在所不辞的博客-CSDN博客_深入理解计算机系统第四版pdf

    深入理解计算机系统 -资料整理 高清中文版_在所不辞的博客-CSDN博客_深入理解计算机系统第四版pdf

  10. 深入理解C#第三版部分内容

     最近,粗略的读了<深入理解C#(第三版)>这本技术书,书中介绍了C#不同版本之间的不同以及新的功能. 现在将部分摘录的内容贴在下面,以备查阅. C#语言特性: 1.C#2.0 C#2的主 ...

最新文章

  1. shell转换特殊的格式(%b)到时间戳  08/Dec/2016
  2. 简单的介绍一下怎样如何学习Java基础
  3. Android分级部门选择界面(二)
  4. 知识点讲解三:获取重定位后的网址
  5. LeetCode-剑指 Offer 11. 旋转数组的最小数字
  6. html flash 动画效果代码大全,flash动作代码大全
  7. base64下载excel (使用blob)
  8. python基础(part6)--容器类型之通用操作
  9. php动态添加查询,php动态添加url查询参数的方法,php动态url参数_PHP教程
  10. JQuery 表单校验
  11. css实现左(右)侧固定宽度,右(左)侧宽度自适应 ---清除浮动
  12. [Leetcode][第24题][JAVA][两两交还的链表中的节点][递归][三指针]
  13. DataTable随机复制一行给新的DataTable
  14. 支付宝核心工程师谈如何成为一名优秀的程序员?
  15. 讯为开发板的最小LINUX系统烧写及U盘的挂载及卸载
  16. 常用的织梦(dedecms)调用标签
  17. 我的第二个切换图片高亮显示,给力
  18. 黑群晖二合一已损毁_黑群晖DSM6.2硬盘引导二合一镜像以及安装方法
  19. IDEA Eval Reset 插件找不到
  20. win7 系统定时开关机

热门文章

  1. 基于matlab和proe的曲面建模,两个基础曲面建模实例教你Proe曲面建模技巧和思路...
  2. HTTP协议格式详解(总结)
  3. 数学分析—集合与映射
  4. 商用密码产品认证(型号)概述
  5. 模拟QQ上面的导航按钮
  6. 效果器的使用技巧-与调音台的无缝连接
  7. VB .NET-文本文件读写
  8. 云计算和虚拟化都要用到的核心技术 VXLAN 网络,你掌握了吗?
  9. java留言板源码_jsp留言板示例源码下载(入门级)
  10. centos7内网穿透(frp、zerotier)