Advertisement
Guest User

Untitled

a guest
Jan 21st, 2018
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.36 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <signal.h>
  4. #include <sys/types.h>
  5. #include <sys/ipc.h>
  6. #include <sys/msg.h>
  7. #include <string.h>
  8. #include <stdlib.h>
  9. #include <sys/ioctl.h>
  10.  
  11. FILE *file;
  12. #define WRITE 1
  13. #define READ 0
  14. #define DEFAULT_LEN 70
  15. char temp[DEFAULT_LEN];
  16. int pdes[2];
  17. char str[50];
  18.  
  19.  
  20. void proces1p(){
  21.  
  22. if(fork()==0)
  23.  
  24. while(1){
  25. if(fgets(temp,DEFAULT_LEN,file)){
  26. file=fopen(str,"r");
  27.  
  28. write(pdes[WRITE], temp, DEFAULT_LEN);
  29.  
  30. fclose(file);
  31. }
  32. sleep(1);
  33. }
  34.  
  35.  
  36. }
  37.  
  38. void proces1c(){
  39.  
  40. if(fork()==0)
  41. //pid1=getpid();
  42. {
  43.  
  44.  
  45.  
  46. while(1){
  47. char stri[100];
  48. fflush(stdin);
  49. usleep(1);
  50. gets(stri);
  51.  
  52. write(pdes[WRITE], temp, DEFAULT_LEN);
  53.  
  54.  
  55. sleep(1);
  56. }
  57. fclose(file);
  58.  
  59. }
  60. }
  61.  
  62.  
  63. void wybor(){
  64. char wyb;
  65.  
  66. printf("podaj wybor");
  67. scanf("%c", &wyb);
  68. switch(wyb){
  69. case 'a':
  70. proces1c();
  71.  
  72. break;
  73. case 'b':
  74. scanf("%s",str);
  75. file=fopen(str,"r");
  76. proces1p();
  77. break;
  78.  
  79. }
  80.  
  81. }
  82.  
  83.  
  84. void proces2(){
  85.  
  86. char string[DEFAULT_LEN];
  87. int i;
  88. while(1){
  89. if (ioctl(pdes[0], FIONREAD, &i) != -1){
  90.  
  91. read(pdes[READ],&string,DEFAULT_LEN);
  92. //i=strtol(string,NULL, 16);
  93. //i=atoi(string);
  94. printf("Odczytalem: %s\n", string);
  95.  
  96. sleep(1);
  97. }
  98.  
  99. }
  100. }
  101.  
  102. void proces3(){
  103.  
  104.  
  105.  
  106.  
  107. }
  108.  
  109.  
  110. int main(int argc, char *argv[]){
  111.  
  112.  
  113. pipe(pdes);
  114.  
  115. wybor();
  116. if(fork()==0){
  117. //pid2=getpid();
  118. proces2();
  119. }
  120.  
  121.  
  122.  
  123. return 0;
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement