Vit*_*olt 5 c io shell process fgets
我正在尝试一些用于 shell 实现的 C 代码,发现在 fork 一个进程后, fgets() 返回重复的行,这是我无法理解的,我将非常感谢任何帮助。
我的问题是:分叉是否会更改父进程中任何打开文件的偏移量?这似乎发生在我的程序中。
根据下面的答案 @Vadim Ponomarev 和我的理解: fgets() 不是线程安全的(或者严格来说,它是线程安全的,但是分叉一个进程会导致标准输入以某种方式初始化,从而导致共享文件偏移量的更改) )。
代码如下:
int main() {
char buf[200];
int r;
pid_t pid = 0;
while(getcmd(buf, 200, pid) >= 0) {
fprintf(stderr, "current pid: %d\n", getpid());
pid = fork();
// Without forking the fgets() reads all lines normally
if(pid == 0)
exit(0);
wait(&r);
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
getcmd() 函数只是一个包装器:
int
getcmd(char *buf, int nbuf, pid_t pid)
{
memset(buf, 0, nbuf);
if (fgets(buf, nbuf, stdin) == NULL) {
fprintf(stderr, "EOF !!!\n");
return -1;
}
fprintf(stderr, "pid: %d -- getcmd buf ======= --> %s\n", getpid(), buf);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我还有一个输入文件temp,其中包含一些随机文本:
line 1
line 2
line 3
Run Code Online (Sandbox Code Playgroud)
编译后,我运行a.out < temp,输出显示打印了 6 行,并且通常有些行是重复的。但如果我删除该行
pid = fork()
...
Run Code Online (Sandbox Code Playgroud)
然后输出就正常了(只是一一显示所有行,这意味着 fgets() 被调用了 3 次)。
知道出了什么问题吗?
输出(这就是得到的):
pid: 10361 -- getcmd buf ======= --> line1
current pid: 10361
pid: 10361 -- getcmd buf ======= --> line2
current pid: 10361
pid: 10361 -- getcmd buf ======= --> line3
current pid: 10361
pid: 10361 -- getcmd buf ======= --> line2
current pid: 10361
pid: 10361 -- getcmd buf ======= --> line3
current pid: 10361
pid: 10361 -- getcmd buf ======= --> line3
current pid: 10361
EOF !!!
Run Code Online (Sandbox Code Playgroud)
我希望看到这个:
current pid: 10361
pid: 10361 -- getcmd buf ======= --> line1
current pid: 10361
pid: 10361 -- getcmd buf ======= --> line2
current pid: 10361
pid: 10361 -- getcmd buf ======= --> line3
EOF
Run Code Online (Sandbox Code Playgroud)
可以编译的版本供参考:
#include <stdio.h>
#include <stdlib.h>
#include <wait.h>
#include <zconf.h>
#include <unistd.h>
#include <memory.h>
int
getcmd(char *buf, int nbuf, pid_t pid)
{
memset(buf, 0, nbuf);
if (fgets(buf, nbuf, stdin) == NULL) {
fprintf(stderr, "EOF !!!\n");
return -1;
}
fprintf(stderr, "pid: %d -- getcmd buf ======= --> %s\n", getpid(), buf);
return 0;
}
int main() {
char buf[200];
int r;
pid_t pid = 0;
while(getcmd(buf, 200, pid) >= 0) {
fprintf(stderr, "current pid: %d\n", getpid());
pid = fork();
// Without forking the fgets() reads all lines normally
if(pid == 0)
exit(0);
wait(&r);
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
谢谢!
小智 4
似乎流的 libc 运行时初始化(stdin、stdout、stderr)包含一些更改当前 stdin 位置的内容:
> strace -f ./a.out < temp 2>&1 | less
....
write(2, "pid: 29487 -- getcmd buf ======="..., 45pid: 29487 -- getcmd buf ======= --> line 1
clone(child_stack=0,flags=CLONE_CHILD_CLEARTID|CLONE_CHILD_SETTID|SIGCHLD,child_tidptr=0x7f34940f19d0) = 29488
Process 29488 attached
[pid 29487] wait4(-1, <unfinished ...>
[pid 29488] lseek(0, -14, SEEK_CUR) = 7
[pid 29488] exit_group(0) = ?
[pid 29488] +++ exited with 0 +++
<... wait4 resumed> [{WIFEXITED(s) && WEXITSTATUS(s) == 0}], 0, NULL) = 29488
Run Code Online (Sandbox Code Playgroud)请注意子进程中的 lseek(0, -14, SEEK_CUR) (pid 29488)
结果,在我的环境(openSUSE Leap 42.2,glibc-2.22-4.3.1)中,程序无限循环,根本没有 EOF
在示例中将 fgets() 更改为 read()
....
if (read(0, buf, nbuf) == 0) {
....
while(getcmd(buf, 7, pid) >= 0) {
....
Run Code Online (Sandbox Code Playgroud)程序按预期运行(三行和 EOF)
并再次运行 strace -f - 子进程中不再有 lseek() !
结论 - 似乎流函数(在 stdio.h 中声明)必须在多进程环境中非常谨慎地使用,因为它有很多副作用(如本例所示)