【发布时间】:2017-03-20 07:42:10
【问题描述】:
这是我的程序foo.c。
#include <stdio.h>
#include <unistd.h>
int main()
{
int i;
printf("foo\n");
write(0, "bar\n", 4);
return 0;
}
如果我在前台或后台运行程序,foo 和 bar 都会打印在终端上。
$ gcc foo.c
$ ./a.out
foo
bar
$ ./a.out &
[1] 2081
$ foo
bar
[1]+ Done ./a.out
但是当我通过Makefile 运行程序时,我看到bar 仅在程序在前台运行时打印。当程序在后台运行时,它不会在终端上打印出来。
这是我的Makefile 的样子。
fg:
gcc foo.c
./a.out
sleep 1
bg:
gcc foo.c
./a.out &
sleep 1
这是输出。
$ make fg
gcc foo.c
./a.out
foo
bar
sleep 1
$ make bg
gcc foo.c
./a.out &
sleep 1
foo
$
程序通过Makefile在后台运行时,为什么终端不打印bar?
【问题讨论】:
-
write(0,你确定要写信给stdout?
标签: c makefile terminal background system-calls