Facebook
From a, 9 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 837
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <sys/types.h>
  4. #include <sys/wait.h>
  5. #define MAXSTRS 5
  6.  
  7. int main()
  8. {
  9.   int cntr;
  10.   FILE *pipe_fp;
  11.   char *strings[MAXSTRS] = { "echo", "bravo", "alpha", "charlie", "delta"};
  12.   int i;
  13.   int fds[2];
  14.   pid_t pid;
  15.   int data_processed;
  16.   const char some_data[]="pewnego razu byl sobie jeden ....\n";
  17.   if(pipe(fds)==0)
  18.   {
  19.     pid=fork();
  20.     if(pid==(pid_t)0)
  21.     {
  22.       close(fds[1]);
  23.       dup2(fds[0],0);//STDIN_FILENO
  24.       execlp("sort","sort", 0);
  25.     }
  26.    
  27.  
  28.    
  29.     else
  30.     {
  31.       FILE *stream;
  32.       close(fds[0]);
  33.       stream=fdopen(fds[1],"w");
  34.      
  35.       for(i=0; i<MAXSTRS;i++) fprintf(stream,"%s\n", strings[i]);
  36.  
  37.      
  38.       fprintf(stream,"darek\n");
  39.       fprintf(stream,"adam\n");
  40.       fflush(stream);
  41.      
  42.       char b[40];
  43.       sprintf(b,"ls –l /proc/%d/fd", getpid());
  44.       system(b);
  45.  
  46.      
  47.       close(fds[1]);
  48.     }
  49.   }
  50.   return 0;
  51. }
  52.  
  53.  
  54. #include <stdio.h>
  55. #define MAXSTRS 5
  56. int main(void)
  57. {
  58.   int cntr;
  59.   FILE *pipe_fp;
  60.   char *strings[MAXSTRS] = { "echo", "bravo", "alpha", "charlie", "delta"};
  61.  
  62.   if (( pipe_fp = popen("sort", "w")) == NULL)
  63.   {
  64.     perror("popen");
  65.     exit(1);
  66.   }
  67.  
  68.   for(cntr=0; cntr<MAXSTRS; cntr++)
  69.   {
  70.     fputs(strings[cntr], pipe_fp);
  71.     fputc('\n', pipe_fp);
  72.   }
  73.   system("ps f");
  74.   pclose(pipe_fp); /* Zamknięcie potoku */
  75.   return(0);
  76. }
  77.  
  78.  
  79. #include <stdio.h>
  80. #include <unistd.h>
  81. #include <sys/types.h>
  82. #include <sys/wait.h>
  83. #include <errno.h>
  84. #include <string.h>
  85. #define MAXSTRS 5
  86. #define MAX 512
  87.  
  88. #define 10MEG (1024*1024*10)
  89. #define ROZMIAR_BUF PIPE_BUF
  90.  
  91. int main(int argc, char *argv[])
  92. {
  93.  
  94.   extern int errno;
  95.   char path[20];
  96.   char komunikat[MAX];
  97.   int infkolejka;
  98.   int open_mode=O_WRONLY;
  99.   int pipe_fd;
  100.   char bufor[ROZMIAR_BUF+1];
  101.  
  102.   if(argc==2)
  103.     strcpy(path,argv[1]);
  104.   else
  105.     strcpy(path,"/tmp/kolejka");
  106.  
  107.  
  108.     infkolejka=mkfifo(path,0777);
  109.    
  110.  
  111.   if(infkolejka==-1)
  112.     {
  113.       printf("Utworzenie kolejki %s\n",path);
  114.     }
  115.       fprintf(stderr,"Bład mkfifo %s numer %d\n",path,errno);
  116.  
  117.       pipe_fd = open(path, open_mode);
  118.    
  119.       if(pipe_fd!=-1)
  120.       {
  121.         while(wyslane<10MEG)
  122.           infkolejka = write(pipe_fd, bufor, ROZMIAR_BUF);
  123.        
  124.           if(infkolejka==-1)
  125.           {
  126.            
  127.            
  128.           }
  129.           wyslane+=infkolejka
  130.       }
  131.        
  132.  
  133.   else{
  134.     exit(1);
  135.   return 0;
  136. }
  137.  
  138.