【发布时间】:2026-01-14 03:15:01
【问题描述】:
我需要模拟Linux命令“cal -3”,并排显示3个月的日历。我现在需要的是让我的实现,使用管道,工作。我被告知我不能使用fork(),而是应该使用dup2()、write()、read() 和close() 来调用system("myCustomCommand") 三次。现在我的程序没有并排显示日历。
我正在尝试使用管道并遇到了问题。这是我正在尝试的:
int pfd[2];
int p; //for pipe
int d; //for dup2
const int BSIZE = 256;
char buf[BSIZE];
p = pipe(pfd);
if (p == -1) { perror("pipe"); exit(EXIT_FAILURE); }
if (p == 0)
{
d = dup2(pfd[1], 0);
close(pfd[1]);
nbytes = read (pfd[1], buf , BSIZE);
close(pfd[0]);
exit(EXIT_SUCCESS);
}
else
{
close(pfd[0]);
write(pfd[1], "test\n", BSIZE);
close(pfd[1]);
exit(EXIT_SUCCESS);
}
不幸的是,这段代码没有显示任何内容。你能帮我解决这个问题吗?
【问题讨论】:
标签: c linux command-line pipe