pieniakoskar

SO062

Apr 24th, 2018
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.27 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <fcntl.h>
  5. #include <sys/wait.h>
  6.  
  7. bool lpierwsza(int liczba){
  8. for(int i=2;i*i<=liczba;i++){
  9. if(liczba%i==0)
  10. return false;
  11. }
  12. return true;
  13. }
  14. int main()
  15. {
  16. char buff[10];
  17. int rd;
  18. int i, k;
  19. int f = fork();
  20.  
  21. int file1 = open("tmp1.txt", O_CREAT | O_RDWR | O_TRUNC);
  22. int file2 = open("tmp2.txt", O_CREAT | O_RDWR | O_TRUNC);
  23.  
  24. if(file1 == -1 || file2 == -1)
  25. {
  26. printf("Blad podczas odczytu pliku\n");
  27. return 0;
  28. }
  29. if(f > 0){
  30. i = 1;
  31. k = 99;
  32. }
  33. else{
  34. i = 100;
  35. k = 200;
  36. }
  37.  
  38. for(i; i <= k; i++){
  39. if(lpierwsza(i)){
  40. sprintf(buff, "%d\n", i);
  41. if(f > 0){
  42. write(file1, buff, sizeof(buff));
  43. }
  44. else{
  45. write(file2, buff, sizeof(buff));
  46. }
  47. }
  48. }
  49. wait(NULL);
  50. lseek(file1, 0, SEEK_SET);
  51. lseek(file2, 0, SEEK_SET);
  52.  
  53. while((rd = read(file1, buff, sizeof(buff))) > 0){
  54. printf("%s", buf);
  55. }
  56. while((rd = read(file2, buff, sizeof(buff))) > 0){
  57. printf("%s", buf);
  58. }
  59. close(file1);
  60. close(file2);
  61. return 0;
  62. }
Add Comment
Please, Sign In to add comment