שאלה נוספת
בהנחה שאני רוצה להשתמש ב 2 pipes כדי לייצר תקשורת דו כיוונית בין 2 תהליכים
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
int main()
{
pid_t childId;
int fatherReadPipe[2];
int sonReadPipe[2];
char buf[PIPE_BUF];
pipe(fatherReadPipe);
pipe(sonReadPipe);
childId = fork();
switch (childId)
{
case -1:
{
printf("Error at fork\n");
exit(-1);
break;
}
case 0:
{
close(fatherReadPipe[0]);
printf("im process #%d, enter a message to send to father\n", getpid());
gets(buf);
write(fatherReadPipe[1], buf, strlen(buf) + 1);
read(sonReadPipe[0], buf, PIPE_BUF);
printf("father said:\n");
printf("%s\n", buf);
break;
}
default:
{
close(sonReadPipe[0]);
read(fatherReadPipe[0],buf,PIPE_BUF);
printf("im process #%d, this is a message my son sent me\n%s\n", getpid(),buf);
printf("enter a message to send to son\n");
gets(buf);
write(sonReadPipe[1],buf,strlen(buf) + 1);
break;
}
}
return 0;
}
האם קיימת דרך יותר יפה לעשות את זה? כלומר לא לעשות 2 מערכים ולהעביר את שתיהם ל pipe ואז לחסום צד אצל אבא צד אצל הבן?