このコードを考えると:
#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);
}
これは、 を使用した 2 つのプロセス間の簡単な会話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.
上記からわかるように、2 人の子供は と を使用して互いに話し合っていfork
ますpipe
。しかし、パイプなしでこれを行いたいのですが、可能ですか? もしそうなら、その方法を説明しpipe()
てください pipe()
。
ありがとう