#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
// we'll be setting up an array of four descriptors
#define PARENT_READ 0
#define CHILD_WRITE 1
#define CHILD_READ 2
#define PARENT_WRITE 3
int main()
{
int fd[4];
char msg[256];
pid_t pid;
pipe(fd); // [0] = parent-read, [1] = child-write
pipe(fd+2); // [0] = child-read, [1] = parent-write
if((pid = fork()) == 0)
{
// child process.
close(fd[PARENT_READ]);
close(fd[PARENT_WRITE]);
read(fd[CHILD_READ], msg, sizeof(msg));
write
(fd
[CHILD_WRITE
], msg
, strlen(msg
)+1);
close(fd[CHILD_READ]);
close(fd[CHILD_WRITE]);
return EXIT_SUCCESS;
}
// parent process
close(fd[CHILD_READ]);
close(fd[CHILD_WRITE]);
strcpy(msg
, "HI!! YOU THERE"); write
(fd
[PARENT_WRITE
], msg
, strlen(msg
)+1); read(fd[PARENT_READ], msg, sizeof(msg));
printf("\nACK RECEIVED %s\n", msg
);
close(fd[PARENT_READ]);
close(fd[PARENT_WRITE]);
return EXIT_SUCCESS;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRsaWIuaD4KI2luY2x1ZGUgPHN0cmluZy5oPgojaW5jbHVkZSA8dW5pc3RkLmg+CiNpbmNsdWRlIDxzeXMvdHlwZXMuaD4KCi8vIHdlJ2xsIGJlIHNldHRpbmcgdXAgYW4gYXJyYXkgb2YgZm91ciBkZXNjcmlwdG9ycwojZGVmaW5lIFBBUkVOVF9SRUFEICAgICAwCiNkZWZpbmUgQ0hJTERfV1JJVEUgICAgIDEKI2RlZmluZSBDSElMRF9SRUFEICAgICAgMgojZGVmaW5lIFBBUkVOVF9XUklURSAgICAzCgppbnQgbWFpbigpCnsKICAgIGludCBmZFs0XTsKICAgIGNoYXIgbXNnWzI1Nl07CiAgICBwaWRfdCBwaWQ7CgogICAgcGlwZShmZCk7ICAgICAgIC8vIFswXSA9IHBhcmVudC1yZWFkLCBbMV0gPSBjaGlsZC13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