implementando tubería anónima

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.

muchas gracias.

ps

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;
    }

Sinceramente,

Eli

preguntado el 04 de julio de 12 a las 10:07

thanks, but as i wrote in my question. the code works correctly and there is no need to change it -

in order to make an anonymous pipe work, we use fork. i'm trying to implement pipe with shared memory and semaphores. my question is how should i implement the fork call or alternatively how should i treat this situation. BTW, it's not my code. the code was taken from the recitation website and therefore should work fine(ignore the fact the first he shuts pfd[1]) motigeva.com/os/files/pipe1.c -

So you want to emulate pipe(2) using other OS-primitives / syscalls. Homework? -

@wildplasser: "Because (sizeof "string literal" == sizeof char*)" - a common and understandable mistake, but while string literals can easily decay to pointers they are not pointers and sizeof works properly on the actual array type (in both C and C++ FWIW), returning the size in bytes including the terminating NUL. Guaranteed compile-time, unlike strlen (I lodged a request with Sun to enhance their compiler to perform strlen on literals at compile time about 10 years ago - the Standard doesn't require it but GNU did it even back then). -

1 Respuestas

fork isn't necessary to implement an anonymous pipe, although it may be necessary to test it. If you're on a platform with conventional Unix fork, then most ways to create a new process will involve the fork syscall. Your alternatives are:

  • vfork seguido inmediatamente por exec, assuming you can find your program and provide appropriate args;
  • invoking another copy of your program externally; or
  • threading (pthread_create).

Respondido 04 Jul 12, 13:07

in case i tested and a fork has been created. what should i do? - Eli Katzav

@EliKatzav I'm not clear you understand what fork does. It creates another instance of a process with the same image and memory, with only the pid and the return from the fork call being different. - ecatmur

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.