MobyDicks

task9

Dec 10th, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.98 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <sched.h>
  3. #include <sys/mman.h>
  4. #include <stdlib.h>
  5. #include <unistd.h>
  6. #include <fcntl.h>
  7.  
  8. void itoa(char *buf, int value) { sprintf(buf, "%d", value); }
  9.  
  10. int main(void)
  11. {
  12. int i, pid, ppid, status;
  13. int fdrd, fdwr;
  14. char str1[10], str2[10];
  15. char c;
  16. struct sched_param shdprm;
  17.  
  18. if (mlockall((MCL_CURRENT | MCL_FUTURE)) < 0)
  19. perror("mlockall error");
  20.  
  21. pid = getpid();
  22. ppid = getppid();
  23.  
  24. shdprm.sched_priority = 1;
  25. if (sched_setscheduler(0, SCHED_RR, &shdprm) == -1)
  26. perror("SCHED_SETSCHEDULER_1");
  27.  
  28. if ((fdrd = open("infile.txt", O_RDONLY)) == -1)
  29. perror("Openning file");
  30.  
  31. if ((fdwr = creat("outfile.txt", 0666)) == -1)
  32. perror("Creating file");
  33.  
  34. itoa(str1, fdrd);
  35. itoa(str2, fdwr);
  36.  
  37. for (i = 0; i < 2; i++)
  38. if (fork() == 0)
  39. {
  40. shdprm.sched_priority = 50;
  41. if (sched_setscheduler(0, SCHED_RR, &shdprm) == -1)
  42. perror("SCHED_SETSCHEDULER_1");
  43. execl("son", "son", str1, str2, NULL);
  44. }
  45. system("ps -o uid,gid,ruid,pid,ppid,pgid,tty,vsz,stat,command > file.txt");
  46. if (close(fdrd) != 0)
  47. perror("Closing file");
  48.  
  49. for (i = 0; i < 2; i++)
  50. printf("process pid = %d finished\n", wait(&status));
  51. return 0;
  52. }
  53.  
  54. #include <sched.h>
  55. #include <sys/mman.h>
  56. #include <fcntl.h>
  57. #include <stdio.h>
  58.  
  59. int main(int argc, char *argv[])
  60. {
  61. if (mlockall((MCL_CURRENT | MCL_FUTURE)) < 0)
  62. perror("mlockall error");
  63. char c;
  64. int pid, ppid, buf;
  65. int fdrd = atoi(argv[1]);
  66. int fdwr = atoi(argv[2]);
  67. pid = getpid();
  68. ppid = getppid();
  69.  
  70. printf("son file decriptor = %d\n", fdrd);
  71. printf("son params: pid=%i ppid=%i\n", pid, ppid);
  72.  
  73. sleep(5);
  74.  
  75. for (;;)
  76. {
  77. if (read(fdrd, &c, 1) != 1)
  78. return 0;
  79. write(fdwr, &c, 1);
  80. printf("pid = %d: %c\n", pid, c);
  81. }
  82. return 0;
  83. }
Add Comment
Please, Sign In to add comment