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
 MINIX...3057 
 Solaris...2933 
 LD...2904 
 Linux Kernel 2.6...2470 
 William Gropp...2180 
 Rodriguez 6...2012 
 C++ Templates 3...1945 
 Trees...1937 
 Kamran Husain...1866 
 Secure Programming for Li...1792 
 Максвелл 5...1710 
 DevFS...1694 
 Part 3...1683 
 Stein-MacEachern-> Час...1632 
 Go Web ...1624 
 Ethreal 4...1618 
 Arrays...1607 
 Стивенс 9...1603 
 Максвелл 1...1592 
 FAQ...1538 
 
  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 , создается файл , который может быть использован любым процессом.
Оставьте свой комментарий !

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

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