当前位置: 首页 > news >正文

Linux系统编程练习、作业

8.13练习

练习1:

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <string.h>
void fun()
{printf("hahaha\n");
}
int main(int argc, char const *argv[])
{pid_t pid;pid = fork();char buf[6];if (pid == 0){usleep(1);memset(buf, 0, sizeof(buf));printf("efg\n");scanf("%s", buf);exit(*buf);}printf("abc\n");int status;pid_t wpid;wpid = wait(&status);switch (WEXITSTATUS(status)){case 'a':printf("hello\n");break;case 'b':printf("world\n");break;default:break;}atexit(fun);return 0;
}

练习2

#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <sys/wait.h>
int main(int argc, char const *argv[])
{int fd;pid_t pid1;int i = 0;char buf[8];fd = open("a.txt", O_RDWR | O_CREAT, 0664);pid1 = fork();if (pid1 == 0){while (i < 25){write(fd, "嘻嘻\n", 8);printf("写入第%d个\n", i);sleep(1);i++;}exit(0);}pid_t pid2;pid2 = fork();ssize_t rd;if (pid2 == 0){while (i < 25){memset(buf, 0, sizeof(buf));rd = read(fd, buf, sizeof(buf));printf("读取到:%s", buf);sleep(1);i++;}exit(0);}wait(NULL);wait(NULL);return 0;
}

8.13作业

作业1:

stdio.c,打开log.txt文件描述符,使用dup2(新,旧)使用fgets(line,sizeof(line),stdin)从标准输入逐行读取到line中用printf写入到重定向标准输出指向的log.txt文件中。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>#define LOG "log.txt"
int main(int argc, char const *argv[])
{int log_fd = open(LOG, O_RDWR | O_CREAT | O_APPEND, 0664);dup2(log_fd, STDOUT_FILENO);char line[256];while (1){fgets(line, sizeof(line), stdin);printf("键盘输入的文本内容是:%s\n", line);fflush(stdout);}return 0;
}

execl_stdio.c

#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>
#include <stdlib.h>int main(int argc, char const *argv[])
{pid_t vpid;vpid=vfork();if(vpid==0){execl("./std","std",NULL);exit(0);}wait(NULL);return 0;
}

作业2:

out_std.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char const *argv[])
{char buf[256];while (1){fgets(buf, sizeof(buf), stdin);printf("被执行文件的键盘输入为:%s\n", buf);fflush(stdout);}return 0;
}

out_execl.c

#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/wait.h>
#define OUT_LOG "out_log.txt"int main(int argc, char const *argv[])
{int out_fd = open(OUT_LOG, O_RDWR | O_CREAT | O_APPEND, 0664);pid_t pid;pid = fork();if (pid == 0){dup2(out_fd, STDOUT_FILENO);close(out_fd);  execl("./out_std", "out_std", NULL);}close(out_fd);  wait(NULL);return 0;
}

http://www.dtcms.com/a/335037.html

相关文章:

  • Flink Stream API 源码走读 - 总结
  • 差分约束.
  • 腾讯混元大模型:实现3D打印产品生成的自动化平台
  • [Python 基础课程]继承
  • [Linux] RAID存储技术
  • 【102页PPT】电子行业数字化解决方案(附下载方式)
  • 容器化部署:用Docker封装机器翻译模型与服务详解
  • 服务器可以ping通,但部署的网站打不开
  • MyBatis 的 SQL 拦截器:原理、实现与实践
  • 基于Spring Boot的快递物流仓库管理系统 商品库存管理系统
  • OpenStack Neutron中的L2 Agent与L3 Agent:新手友好指南
  • Nginx蜘蛛请求智能分流:精准识别爬虫并转发SEO渲染服务
  • RemoteCtrl-初步的网络编程框架搭建
  • Linux 多线程:线程回收策略 线程间通信(互斥锁详解)
  • Easytier异地组网与Nginx反向代理
  • 昇腾AI自学Day2-- 深度学习基础工具与数学
  • 楼宇自控系统赋能建筑全维度管理,实现环境、安全与能耗全面监管
  • 计算分组内时间列的最大差值
  • 【AI论文】NextStep-1:迈向大规模连续令牌自回归图像生成
  • Warning: Unable to create personal MATLAB work folder:E:\绯荤粺榛樿\鏂囨。\MATLAB
  • 1083. 数列极差问题
  • 【深度学习】基于ESRNet模型的图像超分辨率训练
  • pytest介绍(python测试框架)(@pytest.mark.parametrize、@pytest.fixtures)
  • ClaudeCode使用指南
  • 鲁老师深度学习笔记(1)—最大似然估计
  • Flutter Provider 模式实现:基于 InheritedWidget 的状态管理实现
  • 93、23种设计模式之抽象工厂模式
  • 【读论文】医疗AI大模型:百川开源Baichuan-M2
  • 23. CommonJS 和 ES6 Module 区别
  • 19.3 Transformers量化模型极速加载指南:4倍推理加速+75%显存节省实战