【发布时间】:2012-07-06 14:02:18
【问题描述】:
鉴于此代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/wait.h>
#include <unistd.h>
#define BUF_SIZE 256
int main()
{
int fd1[2];
int fd2[2];
ssize_t numRead = -1;
// remark : working under the assumption that the messages are of equal length
const char* messageOne = "Hello world , I'm child number 1\n";
const char* messageTwo = "Hello world , I'm child number 2\n";
const unsigned int commLen = strlen(messageOne) + 1;
char buf[BUF_SIZE];
if (pipe(fd1) == -1)
{
printf("Error opening pipe 1!\n");
exit(1);
}
if (pipe(fd2) == -1)
{
printf("Error opening pipe 2!\n");
exit(1);
}
printf("Piped opened with success. Forking ...\n");
// child 1
switch (fork())
{
case -1:
printf("Error forking child 1!\n");
exit(1);
case 0:
printf("\nChild 1 executing...\n");
/* close reading end of first pipe */
if (close(fd1[0]) == -1)
{
printf("Error closing reading end of pipe 1.\n");
_exit(1);
}
/* close writing end of second pipe */
if (close(fd2[1]) == -1)
{
printf("Error closing writing end of pipe 2.\n");
_exit(1);
}
/* write to pipe 1 */
if (write(fd1[1], messageOne, commLen) != commLen)
{
printf("Error writing to pipe 1.\n");
_exit(1);
}
if (close(fd1[1]) == -1)
{
printf("Error closing writing end of pipe 1.\n");
_exit(1);
}
/* reding from pipe 2 */
numRead = read(fd2[0], buf, commLen);
if (numRead == -1)
{
printf("Error reading from pipe 2.\n");
_exit(1);
}
if (close(fd2[0]) == -1)
{
printf("Error closing reding end of pipe 2.\n");
_exit(1);
}
printf("Message received child ONE: %s", buf);
printf("Exiting child 1...\n");
_exit(0);
break;
default:
break;
}
// child 2
switch (fork())
{
case -1:
printf("Error forking child 2!\n");
exit(1);
case 0:
printf("\nChild 2 executing...\n");
/* close reading end of second pipe */
if (close(fd2[0]) == -1)
{
printf("Error closing reading end of pipe 2.\n");
_exit(1);
}
/* close writing end of first pipe */
if (close(fd1[1]) == -1)
{
printf("Error closing writing end of pipe 1.\n");
_exit(1);
}
/* read from the first pipe */
if (read(fd1[0], buf, commLen) == -1)
{
printf("Error reading from pipe 1.\n");
_exit(EXIT_FAILURE);
}
if (close(fd1[0]) == -1)
{
printf("Error closing reading end of pipe 1.\n");
_exit(EXIT_FAILURE);
}
/* write to the second pipe */
if (write(fd2[1], messageTwo, commLen) != commLen)
{
printf("Error writing to the pipe.");
_exit(EXIT_FAILURE);
}
if (close(fd2[1]) == -1)
{
printf("Error closing writing end of pipe 2.");
_exit(EXIT_FAILURE);
}
printf("Message received child TWO: %s", buf);
printf("Exiting child 2...\n");
_exit(EXIT_SUCCESS);
break;
default:
break;
}
printf("Parent closing pipes.\n");
if (close(fd1[0]) == -1)
{
printf("Error closing reading end of the pipe.\n");
exit(EXIT_FAILURE);
}
if (close(fd2[1]) == -1)
{
printf("Error closing writing end of the pipe.\n");
exit(EXIT_FAILURE);
}
if (close(fd2[0]) == -1)
{
printf("Error closing reading end of the pipe.\n");
exit(EXIT_FAILURE);
}
if (close(fd1[1]) == -1)
{
printf("Error closing writing end of the pipe.\n");
exit(EXIT_FAILURE);
}
printf("Parent waiting for children completion...\n");
if (wait(NULL) == -1)
{
printf("Error waiting.\n");
exit(EXIT_FAILURE);
}
if (wait(NULL) == -1)
{
printf("Error waiting.\n");
exit(EXIT_FAILURE);
}
printf("Parent finishing.\n");
exit(EXIT_SUCCESS);
}
这是两个使用 pipe() 的进程之间的简单对话。
输出是:
Piped opened with success. Forking ...
Parent closing pipes.
Parent waiting for children completion...
Child 2 executing...
Child 1 executing...
Message received child TWO: Hello world , I'm child number 1
Exiting child 2...
Message received child ONE: Hello world , I'm child number 2
Exiting child 1...
Parent finishing.
从上面可以看出,两个孩子正在使用fork 和pipe 与另一个说话。但是我想在没有管道的情况下做到这一点,这可能吗?如果是这样,请解释如何,我不想使用 pipe() ,我想要 simulate pipe() 。
谢谢
【问题讨论】:
-
为什么要这样做?使用
pipe()有什么问题? -
您可以使用
mkfifo。但这实际上只是调用pipe的一种更复杂的方式。 -
@robert:我想在没有
mkfifo()和pipe()的情况下这样做
标签: c linux pipe named-pipes