原子操作:不可分割的操作。
原子:不可分割的最小单位。
原子操作的作用:解决竞争和冲突。
如tmpnam,其操作不原子。
程序中的重定向:dup,dup2
输出hello到指定文件
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#define FNAME "/tmp/out"
int main()
{
int fd;
close(1);
fd = open(FNAME, O_WRONLY|O_CREAT|O_TRUNC, 0600);
if (fd < 0)
{
perror("open()");
exit(1);
}
/************************************/
puts("hello");
exit(0);
}
运行查看结果:
dup函数
dup的用法
将oldfd拷贝一个副本,放到当前可用范围最小的位置上。
此时0到5都已被使用,当前可用范围最小的就是6号,然后dup(4),会把4号的副本放到6号的下标上,4号和6号指向的是同一个结构体。
代码
想法是关闭1号文件,然后dup(fd),这样就复制到了1号位置,puts就输出到了目标文件。
问题1:如果第一次open的时候,fd就是1,然后close(1),fd这个文件描述符就已经不成立了。
问题2:但是如果还有一个进程在跑,用同样的文件描述符表,执行了close(1),还没来得及dup复制,另一个进程开了一个文件,给到了1号,这样dup的结果就不是放到1号位置了。出问题的原因在于close和dup不是原子操作。
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#define FNAME "/tmp/out"
int main()
{
int fd;
fd = open(FNAME, O_WRONLY|O_CREAT|O_TRUNC, 0600);
if (fd < 0)
{
perror("open()");
exit(1);
}
close(1);
dup(fd);
close(fd);
/************************************/
puts("hello");
exit(0);
}
dup2函数
dup的用法
问题2的解决:将newfd作为oldfd的副本,首先将newfd关闭。
问题1的解决:如果fd本身是1怎么办。
older和newfd相同时,不执行close操作。
代码
虽然dup2中,oldfd和newfd相同时,不执行close,但是下面执行了close(fd),因此要增加一个判断,fd != 1。
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#define FNAME "/tmp/out"
int main()
{
int fd;
fd = open(FNAME, O_WRONLY|O_CREAT|O_TRUNC, 0600);
if (fd < 0)
{
perror("open()");
exit(1);
}
// close(1);
// dup(fd);
dup2(fd, 1);
if(fd != 1) close(fd);
/************************************/
puts("hello");
exit(0);
}