胜利老师yyds
书上有些有错误,这里代码都已调好。

7-1

#include <unistd.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<stdlib.h>
#include<stdio.h>
int main(void) {int fdl,fd2,fd3,nbytes;int flags=O_CREAT|O_TRUNC|O_WRONLY;char buf[10];if ((fdl=open ("rdwr.c", O_RDONLY, 0644)) <0) {perror("open rdwr.c");exit(EXIT_FAILURE);} if ((fd2=open("/dev/null", O_WRONLY)) <0) {perror("open /dev/null");exit(EXIT_FAILURE);}if ((fd3=open("/tmp/foo.bar", flags, 0644)) <0) {perror("open /tmp/foo.bar");close(fdl);close(fd2);exit(EXIT_FAILURE);} while((nbytes=read (fdl, buf,10)) >0) {if(write(fd2, buf, 10)<0)perror("write /dev/null");if(write(fd3,buf,nbytes)<0)perror("write /tmp/foo.bar");write(STDOUT_FILENO, buf, 10);}close(fdl);close(fd2);close(fd3);exit(EXIT_SUCCESS);
}

7-2

#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
struct person {char name[10];char id[10];off_t pos;
}people[]= {{"Zhangsan","123456",0},{"Lisi","246800",10240},{"Wangwu","135791",81920}
};
int main(int argc,char **argv){int fd,i,j;if(argc<2) {fprintf(stderr,"usage :%s file\n",argv[0]);return 1;}fd=open(argv[1],O_RDWR|O_CREAT|O_TRUNC,0666);if(fd<0) {fprintf(stderr, "%s:%s:cannot open for read/write:%s\n",argv[0],argv[1], strerror(errno));return 1;}j=sizeof(people)/sizeof(people[0]);for(i=0; i<j;i++) {if(lseek(fd,people[i].pos,SEEK_SET) <0 ) {fprintf(stderr,"%s:%s:seek error:% s \n", argv[0],argv[1],strerror(errno));close(fd);return 1;}if(write(fd,&people[i],sizeof(people[i]))!=sizeof(people[i])) {fprintf(stderr,"%s :%s :write error:%s\n",argv[0],argv[1], strerror(errno));close(fd);return 1;}}close(fd);return 0;
}

7-3

#include<stdio.h>
#include<errno.h>
#include<unistd.h>
#include<sys/types.h>
#include<stdlib.h>
int main(int argc,char **argv) {pid_t pid, old_ppid, new_ppid;pid_t child, parent;parent=getpid();if((child=fork())<0) {fprintf(stderr, "%s :fork of child failed :%s\n", argv[0], strerror(errno));exit(1);} else if(child==0) {old_ppid=getppid();sleep(2); new_ppid=getppid();} else {sleep(1);exit(0);}printf("Original parent:%d\n",parent);printf("Child: %d\n", getpid());printf("Child's old ppid:%d\n",old_ppid);printf("Child's new ppid:%d\n",new_ppid);exit(0);
}

7-4

#include <sys/types.h>
#include <sys/wait.h>
#include <signal.h>
#include <stdlib.h>
#include <stdio.h>
int main(void) {pid_t child;int status, retval;if ((child=fork()) <0) {perror("fork");exit(EXIT_FAILURE);}printf("Child's PID:%d\n", child);if (child==0) {sleep(20);exit(EXIT_SUCCESS);} else {if((waitpid(child,&status,WNOHANG))==0) {retval=kill(child, SIGKILL);if(retval) {puts ("kill failed\n");perror("kill");waitpid (child, &status, 0);} else printf("%d killed\n", child);}}exit(EXIT_SUCCESS);
}

7-5

#include <unistd.h>
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include<string.h>
int main(int argc, char **argv) {static const char mesg[] ="Happy New Years to you!";char buf[BUFSIZ];size_t rcount,wcount;int p_fd[2];size_t n;if (pipe(p_fd)< 0) {fprintf(stderr,"%s:pipe failed:%s\n",argv[0],strerror(errno));exit(1);}printf("Read end=fd %d,write end=fd %d\n",p_fd[0], p_fd[1]);n=strlen(mesg);if ((wcount=write(p_fd [1],mesg,n))!=n) {fprintf(stderr,"%s:write failed:%s\n",argv[0],strerror(errno));exit(1);}if((rcount=read(p_fd[0],buf,BUFSIZ))!=wcount) {fprintf(stderr, "%s: read failed: %s\n",argv[0],strerror(errno));exit(1);}buf[rcount]='\0';printf("Read <%s>from pipe\n",buf);close(p_fd[0]);close(p_fd[1]);return 0;
}

7-6

#include <unistd.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BUFSZ 512
struct msg {long type;char text[BUFSZ];
};
int main(void) {int qid;key_t key;int lenl,len2;struct msg pmsg_w,pmsg_r;key=IPC_PRIVATE;if((qid=msgget(key, IPC_CREAT|0666))<0) {perror("msgget:create");exit (EXIT_FAILURE);}puts("Enter message to post:");if((fgets(pmsg_w.text,BUFSZ,stdin))==NULL) {puts("Wrong,no message to post.");exit (EXIT_FAILURE);}pmsg_w.type=10;lenl=strlen(pmsg_w.text);if((msgsnd(qid,&pmsg_w,lenl,IPC_NOWAIT))<0) {perror("msgsnd");exit(EXIT_FAILURE);}puts("message posted.");puts("**************");len2=msgrcv(qid,&pmsg_r,BUFSZ,10,IPC_NOWAIT|MSG_NOERROR);if(len2>0) {pmsg_r.text[len2]='\0';printf("reading queue id=%05d\n",qid);printf("message type=%05ld\n",pmsg_r.type);printf("message length=%d bytes\n",len2);printf("message text=%s\n",pmsg_r.text);} else {perror("msgrcv");exit(EXIT_FAILURE);}exit(EXIT_SUCCESS);
}

7-7

#include <unistd.h>
#include <sys/types.h>
#include <wait.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
int main(void){pid_t pid; int num;
if((pid=fork())<0){perror("fork"); exit(EXIT_FAILURE);
}
else if(pid==0)
sleep(30);
else {printf("Sending SIGCHLD to %d\n",pid);
num=kill(pid,SIGCHLD);
if(num<0) perror("kill:SIGCHLD");
else printf("%d still alive\n",pid); printf("Killing %d\n",pid); if((kill(pid, SIGTERM))<0)
perror("kill:SIGTERM");
waitpid(pid,NULL,0);
}
exit(EXIT_SUCCESS);
}

7-8

#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
int main(int argc,char **argv) {int i,fdl,fd2,nbytes;char buf[10];if(argc<3) {fprintf(stderr, "usage:%s origin destination\n"), argv[0];return 1;}if((fdl=open(argv[1],O_RDONLY,0644))<0) {fprintf(stderr, "cannot open %s for reading\n", argv[1]);exit(EXIT_FAILURE);}if((fd2=open(argv[2],O_WRONLY))<0) {fprintf(stderr,"cannot open %s for writing\n",argv[2]);exit(EXIT_FAILURE);}while((nbytes=read(fdl,buf,10))>0) {if(write(fd2,buf,nbytes)<0) {fprintf(stderr,"%s writing error!\n",argv[2]);exit(EXIT_FAILURE);}for(i = 0; i < 10; i++)buf[i]='\0';}close(fdl);close(fd2);system("echo ");system("echo显示当前目录--`pwd`--的内容");system("ls ");exit(EXIT_SUCCESS);
}

exam7-8

#!/bin/bash
#系统调用、C程序和shell 脚本的交叉调用
#这是shell 脚本
echo "今天是`date`"
if(($#!=2)); then
echo "exam7-8 的用法: exam7-8 文件1 文件2 "
exit 1
elif [ ! -f $1 -o ! -f $2 ]; then
echo "输入文件名有误!"
exit 2
else
#调用C程序rdwr
./rdwr $1 $2
fi
echo "下面是文件2--$2--的内容"
cat $2

Linux第七章相关代码相关推荐

  1. Linux 第七章-磁盘分区和格式化

    Linux 第七章 磁盘与文件系统管理 1.磁盘分区与格式化 ​ 在Linux中,当现有的硬盘分区不能满足要求时,就需要对硬盘中的分区进行重新的规划与调整,有时候还需要添加新的硬盘来扩展存储空间. 1 ...

  2. 网安--第七章 恶意代码分析与防治

    第7章 恶意代码分析与防治 内容提要 ◎ 恶意代码的发展史和恶意代码长期存在的原因 ◎ 恶意代码实现机理.定义以及攻击方法 ◎ 恶意代码生存技术.隐藏技术,介绍网络蠕虫的定义以及结构 ◎ 恶意代码防范 ...

  3. linux第七章《档案与目录管理》重点回顾

    转载于:https://www.cnblogs.com/wubingshenyin/p/4514969.html

  4. LINUX 第七章 Squid配置

    见附件.... 转载于:https://blog.51cto.com/ywf860712/295745

  5. Linux第七章服务器硬件及RAID配置实战

    服务器硬件及RAID配置实战 文章目录 服务器硬件及RAID配置实战 一.RAID磁盘阵列介绍, 1.RAID 0磁盘阵列介绍 2.RAID 1磁盘阵列介绍 3.RAID 5磁盘阵列介绍: 4.RAI ...

  6. Linux第七章:6.管道 | 与 grep命令查找

    一.管道 1.管道符号: | 2.管道的作用:管道的作用是将 前面命令的输出 作为后面命令的输入 二.grep 查找命令 1.grep是查找指令,支持模糊搜索 2.grep选项: | grep -n ...

  7. 江苏自考计算机组成原理多少分及格,自考《计算机组成原理》基本概念第七章...

    1. CRT:阴极射线管,显示器的一种. 2. LCD:液晶显示器. 3. CCD:电荷耦合器件,用于图像输入. 4. MIDI:音乐器材数字化接口,规定电子乐器与计算机之间传递信息的方式. 5. 分 ...

  8. Linux内核分析 读书笔记 (第七章)

    第七章 链接 1.链接是将各种代码和数据部分收集起来并组合成为一个单一文件的过程,这个文件可被加载(或被拷贝)到存储器并执行. 2.链接可以执行于编译时,也就是在源代码被翻译成机器代码时:也可以执行于 ...

  9. 鸟哥的Linux私房菜(基础篇)- 第七章、Linux 文件与目录管理

    第七章.Linux文件与目录管理 最近升级日期:2009/08/26 在第六章我们认识了Linux系统下的文件权限概念以及目录的配置说明.在这个章节当中,我们就直接来进一步的操作与管理文件与目录吧!包 ...

最新文章

  1. 金九银十铁12,看完弄懂,工资少说加 5K
  2. 教你以 4G 的速度克隆 Github 项目!
  3. AndroidStudio更新时报错:Connection Error,Temp directory inside installation
  4. 我们可以无损放大一个Transformer模型吗?
  5. 2015/Province_Java_A/3/九数分三组
  6. 现代软件工程系列 结对编程 (II) 电梯调度程序
  7. LaTeX中巨算符下面输入两行内容的方法
  8. data image java,类 java.awt.image.DataBuffer 的使用 (Java 2 Platform SE 6)
  9. 又拍云引领云CDN加速 或成互联网刚性需求
  10. 怎么快速将Excel文件转为DBF格式文件
  11. 得力人脸识别考勤机密码设置_人脸指纹混合识别考勤机得力怎么使用
  12. python经纬度转换为平面坐标的算法_Python经纬度坐标转换为距离及角度的实现
  13. 金融中需要的计算机知识,金融笔试中的综合知识一般会考什么?
  14. 2021-10-18记录 MediaTek MT7620A 平台对应的类型
  15. 2020年小学几年级有计算机课,2019-2020年最新小学信息技术三年级上册第1课初识计算机备课教学参考.doc...
  16. webpack打包工具的基本使用
  17. kernel: SLUB: Unable to allocate memory on node -1 (gfp=0x20)
  18. 彻底解密C++宽字符
  19. 苹果4是android吗,呵呵!原来苹果手机有这么多缺点,你想换安卓吗?
  20. uni App 支付宝小程序分享代码

热门文章

  1. 淘宝封杀返现模式 淘宝客返利网站模式遇挑战
  2. 【系统分析师之路】2007年下系统分析师上午综合知识真题
  3. jadx卡死解决方案
  4. UniAPP离线车牌实时扫描识别
  5. 国培计算机音乐教学设计作业,2017国培计划教学设计
  6. uni-app在华为应用市场上架审核无法通过,涉及个人信息:IMEI用户数据收集问题
  7. 仿QQ空间登录,解决键盘挡住输入框的问题
  8. 【audio】耳机插拔/线控按键识别流程
  9. 【论文阅读】APdrawing GAN (CVPR19)
  10. 用C语言编写的商品库存管理系统