Advertisement
Guest User

Untitled

a guest
Jan 29th, 2020
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.38 KB | None | 0 0
  1. Lista 8
  2. #include <stdio.h>
  3. #include <unistd.h>
  4. #include <sys/types.h>
  5. #include <fcntl.h>
  6. #include <sys/wait.h>
  7.  
  8. int main()
  9. {
  10.  
  11. int potokFd[2];
  12.  
  13. pipe(potokFd);
  14.  
  15. pid_t childPid=fork();
  16.  
  17. if(childPid < 0)
  18. {
  19.  
  20. puts("Nie utworzono procesu potomnego!");
  21.  
  22. return -1;
  23.  
  24. }
  25. if(childPid==0)
  26. {
  27. close(0); //zamkniecie fd dla wejscia i wyjscia
  28.  
  29. close(potokFd[1]);
  30.  
  31. dup(potokFd[0]);
  32.  
  33. close(potokFd[0]);
  34.  
  35. execlp("display", "display", NULL);
  36. }
  37.  
  38. //if(childPid>0)
  39. //{
  40. close(potokFd[0]);
  41. int deskryptor, licznik ;
  42. char nazwaPliku[100], bufor[128];
  43.  
  44. printf("Podaj nazwe pliku: ");
  45. scanf("%s", nazwaPliku);
  46.  
  47. deskryptor = open (nazwaPliku, O_RDONLY);
  48.  
  49. while((licznik=read(deskryptor, bufor, 128)) > 0)
  50. {
  51. write(potokFd[1], bufor, licznik);
  52. }
  53.  
  54. close(potokFd[1]);
  55.  
  56. //}
  57. wait(NULL);
  58.  
  59. return 0;
  60. }
  61.  
  62.  
  63.  
  64. Lista 9
  65.  
  66. #include <stdio.h>
  67. #include <unistd.h>
  68. #include <fcntl.h>
  69. #include <sys/types.h>
  70. #include <sys/stat.h>
  71.  
  72.  
  73. int main(){
  74.  
  75. int potok_fd;
  76. int deskryptor;
  77. int licznik;
  78. char bufor[8];
  79.  
  80. char nazwaPliku[100];
  81. char *FIFO = "../potok";
  82.  
  83. mkfifo(FIFO, 0666);
  84. while(1)
  85. {
  86. potok_fd = open(FIFO, O_WRONLY);
  87.  
  88. printf("Plik do odczytu: ");
  89. scanf("%s", nazwaPliku);
  90. deskryptor = open(nazwaPliku, O_RDONLY);
  91.  
  92. while ((licznik=read(deskryptor, bufor, 8)) > 0)
  93. {
  94. write(potok_fd, bufor, licznik);
  95. }
  96.  
  97. close(potok_fd);
  98.  
  99. }
  100. return 0;
  101. }
  102.  
  103.  
  104.  
  105. Lista 10
  106.  
  107. #include <stdio.h>
  108. #include <unistd.h>
  109. #include <fcntl.h>
  110. #include <sys/stat.h>
  111. #include <sys/types.h>
  112. #include <sys/mman.h>
  113.  
  114.  
  115.  
  116.  
  117.  
  118. int main()
  119. {
  120.  
  121. char nazwaObrazu[100];
  122.  
  123.  
  124.  
  125. if(fork()==0)
  126. {
  127. sleep(4);
  128. execlp("display", "display", "-update", "1", "obrazek.jpg", NULL);
  129.  
  130. }
  131.  
  132. while(1)
  133. {
  134. printf("Podaj nazwe obrazu: ");
  135. scanf("%s", nazwaObrazu);
  136.  
  137. int fd;
  138. int fdOdwz;
  139.  
  140. fd=open(nazwaObrazu, O_RDWR);
  141.  
  142. fdOdwz = open("obrazek.jpg", O_RDWR | O_CREAT, S_IWUSR | S_IRUSR);
  143.  
  144. struct stat danePliku;
  145.  
  146. fstat(fd, &danePliku);
  147.  
  148. truncate("obrazek.jpg", danePliku.st_size);
  149.  
  150.  
  151. char * adrMap;
  152.  
  153. adrMap = mmap( NULL , danePliku.st_size, PROT_READ | PROT_WRITE, MAP_SHARED, fdOdwz, 0 );
  154.  
  155. read(fd, adrMap, danePliku.st_size);
  156.  
  157. msync( adrMap, danePliku.st_size, MS_SYNC);
  158.  
  159. munmap( adrMap, danePliku.st_size);
  160.  
  161.  
  162.  
  163.  
  164. close(fd);
  165.  
  166.  
  167.  
  168.  
  169.  
  170. }
  171.  
  172. return 0;
  173. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement