Advertisement
hejmus

namedpipa 2stronna

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