鉴于此代码:
#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
and 和另一个说话pipe
。但是我想在没有管道的情况下做到这一点,这可能吗?如果是这样,请解释一下,我不想使用pipe()
我想要模拟 pipe()
的东西。
谢谢