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
 Assembler...2335 
 Advanced Bash Scripting G...2149 
 Ethreal 4...1682 
 Secure Programming for Li...1455 
 CPAN-> FAQ...1285 
 Intel 386...1077 
 Ethreal 1...1077 
 Go Web ...1062 
 Тренажёр...979 
 Trees...970 
 Alg1...940 
 Максвелл 3...922 
 William Gropp...889 
 Ext4 FS...868 
 Rust...861 
 C + UNIX...787 
 Go...757 
 Mod_parrot...733 
 Benchmark...726 
 System...705 
 
  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 , создается файл , который может быть использован любым процессом.
Оставьте свой комментарий !

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

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