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
 Linux Kernel 2.6...4813 
 Trees...666 
 Go Web ...582 
 Максвелл 3...568 
 Clickhouse...557 
 Ethreal 4...542 
 Ext4 FS...529 
 William Gropp...522 
 Secure Programming for Li...510 
 Gary V.Vaughan-> Libtool...499 
 Steve Pate 1...498 
 C++ Patterns 3...497 
 Ethreal 3...490 
 Rodriguez 6...488 
 Assembler...468 
 Ethreal 1...465 
 DevFS...461 
 Ulrich Drepper...438 
 Стивенс 9...437 
 MySQL & PosgreSQL...431 
 
  01.01.2024 : 3621733 посещений 

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 , создается файл , который может быть использован любым процессом.
Оставьте свой комментарий !

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

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