I'm trying to implement an anonymous pipe without using system calls: pipe(), fork(), mkfifo(), open(), read(), write(), close() .
Basically i implemented the anonymous pipe with a shared memory in which two file descriptors (both of which have the shared memory id) one for reader and the other for writer, communicates via two semaphores The only situation which i didn't refer to is in case of fork.
my question would be how should i implement the fork call or alternatively how should i treat this situation.
i'm allowed to use pthread_atfork() and atexit() syscalls.
thanks a lot.
p.s
i'm attaching a regular short code for pipe in order to emphasize exactly how should my implementation work.
the code works!!! it's there just to illustrate the way my functions should work.
#include<unistd.h>
#include <stdio.h>
int main()
{
int pfd[2], rb;
char buff[100];
//I implemented this syscall by allocating shared memory
if (pipe(pfd)<0)
{
return -1;
}
//here is my problem. I don’t know how to treat this syscall
if (fork())
{
sleep(5);
//I implemented this syscall by two file descriptors which communicate via two
//semaphores
write(pfd[1], "hello world\n", sizeof("hello world\n"));
wait(NULL);
}
else
{
//I implemented this syscall by two file descriptors which communicate via two
//semaphores
rb = read(pfd[0], buff, sizeof(buff));
if (rb < 0)
perror("SON: read");
else
{
printf("SON: writing %d\n", rb);
write(1, buff, rb);
}
}
return 0;
}
Sincerely,
eli