🦄 refactor(C13, C14, C115): 更新了linux系统开发部分的结构

This commit is contained in:
lzy
2024-05-16 20:36:01 +08:00
parent cb969dfd41
commit 60d670d130
53 changed files with 1407 additions and 1246 deletions

51
C13-IO/sys/dup.c Normal file
View File

@@ -0,0 +1,51 @@
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#define FNAME "/tmp/out"
int main(int argc, char **argv)
{
int fd;
/***
* bug:
* 1. 假如默认没有1这里有bug。
* 2. 假如这个非原子操作的操作的过程,有可能被别的进程影响。
*/
fd = open(FNAME, O_WRONLY | O_CREAT | O_TRUNC, 0600);
if (fd < 0)
{
perror("open");
exit(1);
}
// close(1);
// dup(fd);
dup2(fd, 1);
// 使用 dup2 原子操作
// 且dup2如果两个参数相等不会做任何事
// !!!
if (1 != fd)
close(fd);
#if 0
close(1);
fd = open(FNAME, O_WRONLY | O_CREAT | O_TRUNC, 0600);
if (fd < 0)
{
perror("open");
exit(1);
}
#endif
/********************************************************************/
puts("hello!");
// TODO: 将sdtout回到终端
exit(0);
}