Advertisement
microsista

Untitled

Dec 7th, 2019
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.65 KB | None | 0 0
  1. ////////////////PISARZ///////////////////////////
  2. #include <stdio.h>
  3. #include <signal.h>
  4. #include <unistd.h>
  5. #include <fcntl.h>
  6. #include <string.h>
  7.  
  8. void signalHandler(int signalNumber, siginfo_t* senderInfo, void* dummy)
  9. {
  10. sleep(2);
  11. FILE* input = fopen("/etc/passwd", "r");
  12. char tmp[200];
  13. int output = open("bufor", O_WRONLY | O_CREAT, 777);
  14. while(fgets(tmp, 200, input))
  15. {
  16. write(output, tmp, strlen(tmp)+1);
  17. memset(tmp, 0, 200);
  18. kill(senderInfo->si_pid, SIGUSR2);
  19. sleep(1);
  20. lseek(output, 0L, 0);//ustawia kursor pliku wyjsciowego na poczatek
  21. //write(output, tmp, 200); // zapisuje tmp do pliku wyjsciowego
  22. //lseek(output, 0L, 0); //ustawia kuror pliku wyjsciowego na poczatek
  23. }
  24. fclose(input);
  25. close(output);
  26. remove("bufor");
  27. }
  28.  
  29. int main()
  30. {
  31. struct sigaction signalInterface;
  32. signalInterface.sa_sigaction = &signalHandler;
  33. signalInterface.sa_flags = SA_SIGINFO;
  34.  
  35. if(sigaction(SIGUSR1, &signalInterface, NULL) < 0)
  36. {
  37. perror("sigaction");
  38. return 1;
  39. }
  40.  
  41. printf("Moj ID: %d\n", getpid());
  42.  
  43. while(1);
  44.  
  45. return 0;
  46. }
  47. ///////////////////CZYTELNIK///////////////////////
  48. #include <stdio.h>
  49. #include <signal.h>
  50. #include <unistd.h>
  51. #include <fcntl.h>
  52. #include <string.h>
  53.  
  54. int pisarzID;
  55.  
  56. void signalHandler(int syg)
  57. {
  58. int input = open("bufor", O_RDONLY);
  59. char tmp[200];
  60. read(input, tmp, 200);
  61. printf("%s", tmp);
  62. //kill(pisarzID, SIGUSR1);
  63. fflush(stdout);
  64. close(input);
  65. }
  66.  
  67. int main()
  68. {
  69. signal(SIGUSR2, signalHandler);
  70.  
  71. printf("Wprowadz numer ID pisarza: ");
  72. scanf("%d", &pisarzID);
  73. printf("\n");
  74.  
  75. kill(pisarzID, SIGUSR1);
  76.  
  77. while(1);
  78.  
  79. return 0;
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement