-1

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

4

1 回答 1

0

fork不需要实现匿名管道,尽管可能需要对其进行测试。如果您在使用传统 Unix 的平台上fork,那么创建新进程的大多数方法都将涉及fork系统调用。您的替代方案是:

  • vfork紧随其后的是exec,假设您可以找到您的程序并提供适当的参数;
  • 从外部调用程序的另一个副本;或者
  • 线程(pthread_create)。
于 2012-07-04T12:08:50.823 回答