Search     or:     and:
 LINUX 
 Language 
 Kernel 
 Package 
 Book 
 Test 
 OS 
 Forum 
 iakovlev.org 
 Languages
 С
 GNU С Library 
 Qt 
 STL 
 Threads 
 C++ 
 Samples 
 stanford.edu 
 ANSI C
 Libs
 LD
 Socket
 Pusher
 Pipes
 Encryption
 Plugin
 Inter-Process
 Errors
 Deep C Secrets
 C + UNIX
 Linked Lists / Trees
 Asm
 Perl
 Python
 Shell
 Erlang
 Go
 Rust
 Алгоритмы
NEWS
Последние статьи :
  Тренажёр 16.01   
  Эльбрус 05.12   
  Алгоритмы 12.04   
  Rust 07.11   
  Go 25.12   
  EXT4 10.11   
  FS benchmark 15.09   
  Сетунь 23.07   
  Trees 25.06   
  Apache 03.02   
 
TOP 20
 Advanced Bash Scripting G...1859 
 Ethreal 4...1286 
 Secure Programming for Li...1060 
 CPAN-> FAQ...947 
 Intel 386...629 
 Тренажёр...506 
 Go Web ...489 
 Trees...421 
 Ethreal 1...416 
 Максвелл 3...390 
 Alg1...377 
 Rust...366 
 C + UNIX...346 
 Ext4 FS...342 
 Assembler...338 
 William Gropp...336 
 Mod_parrot...326 
 2.0-> Linux IP Networking...325 
 Benchmark...319 
 Rodriguez 6...316 
 
  01.01.2025 : 3803065 посещений 

iakovlev.org
Pipe - механизм взаимодействия между процессами. FIFO повторяет функционал pipe , но применительно через файловую систему . У pipe нет имени , у FIFO имя есть . В следующем примере родительский процесс пишет данные в pipe , которые позже читаются дочерними процессами .
 
 #include < sys/types.h >
 #include < unistd.h >
 #include < stdio.h >
 #include < stdlib.h >
 /* Read characters from the pipe and echo them to stdout. */
 void read_from_pipe (int file)
 {
   FILE *stream;
   int c;
   stream = fdopen (file, "r");
   while ((c = fgetc (stream)) != EOF)
   putchar (c);
   fclose (stream);
 }
 	      
  /* Write some random text to the pipe. */
 void write_to_pipe (int file)
 {
   FILE *stream;
   stream = fdopen (file, "w");
   fprintf (stream, "hello, world!\n");
   fprintf (stream, "goodbye, world!\n");
   fclose (stream);
 }
 intmain (void)
 {
   pid_t pid;
   int mypipe[2];
   /* Create the pipe. */
   if (pipe (mypipe))
   {
     fprintf (stderr, "Pipe failed.\n");
     return EXIT_FAILURE;
   }
   /* Create the child process. */
   pid = fork ();
   if (pid == (pid_t) 0)
   {
     /* This is the child process.    Close other end first. */
    close (mypipe[1]);
    read_from_pipe (mypipe[0]);
    return EXIT_SUCCESS;
   }
   else if (pid < (pid_t) 0)
   {
    /* The fork failed. */
    fprintf (stderr, "Fork failed.\n");
    return EXIT_FAILURE;
   }
   else
   {
    /* This is the parent process.    Close other end first. */
    close (mypipe[0]);
    write_to_pipe (mypipe[1]);
    return EXIT_SUCCESS;
   }
 }
 
В следующем примере показано , как получить данные из другого системного процесса , в данном случае more :
 
 #include < stdio.h >
 #include < stdlib.h >
 
 void
 write_data (FILE * stream)
 {
   int i;
   for (i = 0; i < 100; i++)
     fprintf (stream, "%d\n", i);
   if (ferror (stream))
   {
     fprintf (stderr, "Output to stream failed.\n");
     exit (EXIT_FAILURE);
   }
 }
 int main (void)
 {
   FILE *output;
   output = popen ("more", "w");
   if (!output)
   {
     fprintf (stderr,"incorrect parameters or too many files.\n");
     return EXIT_FAILURE;
   }
   write_data (output);
   if (pclose (output) != 0)
    {
      fprintf (stderr, "Could not run more or other error.\n");
    }
    return EXIT_SUCCESS;
    }
 
При создании FIFO , создается файл , который может быть использован любым процессом.
Оставьте свой комментарий !

Ваше имя:
Комментарий:
Оба поля являются обязательными

 Автор  Комментарий к данной статье