当前位置: 技术问答>linux和unix
dup重定向问题
来源: 互联网 发布时间:2016-07-13
本文导语: sub.c #include #include int main() { int i = 0; char szBuf[16]; while(i++ >= 0) { sleep(1); snprintf(szBuf, sizeof(szBuf), "i = %dn", i); ...
sub.c
gcc -o a sub.c
file: fat.c
gcc -o b fat.c
[root@localhost test]# ./b &
[1] 11718
[root@localhost test]# ll
total 44
-rwxrwxrwx 1 root root 5055 Nov 2 04:51 a
-rwxrwxrwx 1 root root 0 Nov 2 04:46 a.txt
-rwxrwxrwx 1 root root 5254 Nov 2 04:53 b
-rw-rw-rw- 1 root root 356 Nov 2 04:49 fat.c
-rw-rw-rw- 1 root root 227 Nov 2 04:44 sub.c
[root@localhost test]# ll
total 44
-rwxrwxrwx 1 root root 5055 Nov 2 04:51 a
-rwxrwxrwx 1 root root 0 Nov 2 04:46 a.txt
-rwxrwxrwx 1 root root 5254 Nov 2 04:53 b
-rw-rw-rw- 1 root root 356 Nov 2 04:49 fat.c
-rw-rw-rw- 1 root root 227 Nov 2 04:44 sub.c
[root@localhost test]#
为什么b.txt没有内容?
#include
#include
int main()
{
int i = 0;
char szBuf[16];
while(i++ >= 0)
{
sleep(1);
snprintf(szBuf, sizeof(szBuf), "i = %dn", i);
write(1, szBuf, strlen(szBuf));
fsync(1);
}
return 0;
}
gcc -o a sub.c
file: fat.c
#include
#include
#include
#include
int main()
{
int iFd;
int iPid;
iPid = fork();
if (iPid == 0)
{
iFd = open("a.txt", O_CREAT, 0777);
close(0);
close(1);
close(2);
dup(iFd);
dup(iFd);
dup(iFd);
execl("./a", "a", NULL);
}
else
{
while(1)
{
sleep(1);
}
}
return 0;
}
gcc -o b fat.c
[root@localhost test]# ./b &
[1] 11718
[root@localhost test]# ll
total 44
-rwxrwxrwx 1 root root 5055 Nov 2 04:51 a
-rwxrwxrwx 1 root root 0 Nov 2 04:46 a.txt
-rwxrwxrwx 1 root root 5254 Nov 2 04:53 b
-rw-rw-rw- 1 root root 356 Nov 2 04:49 fat.c
-rw-rw-rw- 1 root root 227 Nov 2 04:44 sub.c
[root@localhost test]# ll
total 44
-rwxrwxrwx 1 root root 5055 Nov 2 04:51 a
-rwxrwxrwx 1 root root 0 Nov 2 04:46 a.txt
-rwxrwxrwx 1 root root 5254 Nov 2 04:53 b
-rw-rw-rw- 1 root root 356 Nov 2 04:49 fat.c
-rw-rw-rw- 1 root root 227 Nov 2 04:44 sub.c
[root@localhost test]#
为什么b.txt没有内容?
|
iFd = open("a.txt", O_CREAT|O_RDWR, S_IRUSR |
20 S_IWUSR |
21 S_IRGRP |
22 S_IWGRP |
23 S_IROTH |
24 S_IWOTH);
open的时候,flag可读可写。
20 S_IWUSR |
21 S_IRGRP |
22 S_IWGRP |
23 S_IROTH |
24 S_IWOTH);
open的时候,flag可读可写。
|
查找了资料,仔细思考了半小时,无果,呵呵。会继续思考的。
|
顶下,没看明白
|
先顶一下lz。 一起想。。
|
mark 一下, 明天早晨来,呵呵。
200 分啊。
200 分啊。