Advertisement
hejmus

pipa 2stronna

Apr 17th, 2011
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.11 KB | None | 0 0
  1. #include <unistd.h>
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <pthread.h>
  5.  
  6. void* thinput(void* input)
  7. {
  8.     char b;
  9.     while (1)
  10.     {
  11.         read((int)input,&b,1);
  12.         write(0,&b,1);
  13.     }
  14. }
  15.  
  16. void* thoutput(void* output)
  17. {
  18.     char b;
  19.     while (1)
  20.     {
  21.         read(1,&b,1);
  22.         write((int)output,&b,1);
  23.     }
  24. }
  25.  
  26. int main(int argc, char** argv)
  27. {
  28.     int fdin[2],fdout[2];
  29.    
  30.     if (argc==3) //xterm child
  31.     {
  32.         int input=atoi(argv[2]);
  33.         int output=atoi(argv[1]);
  34.         printf("input: %d | output: %d\n",input,output);
  35.         pthread_t t1,t2;
  36.        
  37.         pthread_create(&t1,NULL,&thinput,(void*)input);
  38.         pthread_create(&t1,NULL,&thoutput,(void*)output);
  39.         while (1);
  40.     }
  41.  
  42.     pipe(fdin);
  43.     pipe(fdout);
  44.    
  45.     if (fork()>0) //parent
  46.     {
  47.         printf("pt:R:%d W:%d | R:%d W:%d\n",fdin[0],fdin[1],fdout[0],fdout[1]);
  48.         pthread_t t1,t2;   
  49.         pthread_create(&t1,NULL,thinput,(void*)fdin[0]);
  50.         pthread_create(&t1,NULL,thoutput,(void*)fdout[1]);
  51.         while (1);
  52.     }
  53.     else //child
  54.     {
  55.         char argbuf[30],buf2[30];
  56.         sprintf(argbuf,"%d",fdin[1]);
  57.         sprintf(buf2,"%d",fdout[0]);
  58.         execlp("/usr/bin/xterm","xterm","-e",argv[0],argbuf,buf2,(char*)0);
  59.     }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement