Advertisement
Guest User

Untitled

a guest
Apr 21st, 2014
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.67 KB | None | 0 0
  1. /*
  2. Предмет: ОСиСП
  3. Лабораторная №4
  4. Задание: Дерево процессов, обмен сигналами.
  5. Вариант:
  6. Таблица 1: - 7 === 1->(2,3,4,5,6) 6->(7,8)
  7. Таблица 2: - 6 === 1->2 SIGUSR1 2->(3,4) SIGUSR2 4->5 SIGUSR1 3->6 SIGUSR1 6->7 SIGUSR1 7->8 SIGUSR1 8->1 SIGUSR1
  8. Выполнил: Петрович Максим, гр. 251004
  9. */
  10.  
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <unistd.h>
  14. #include <sys/types.h>
  15. //#include <sys/wait.h>
  16. #include <signal.h>
  17. #include <sys/time.h>
  18. #include <string.h>
  19. #include <libgen.h>
  20. #include <errno.h>
  21.  
  22. char* progname;
  23. static struct timeval tv;
  24. pid_t pid[8];
  25.  
  26.  
  27. /* _fork() - создание процесса + контроль ошибки */
  28. pid_t _fork();
  29.  
  30. int main (int argc, char* argv[]) {
  31.  
  32. progname = basename(argv[0]);
  33. int i,j;
  34. pid_t p;
  35.  
  36. pid[0] = getpid(); // Родительский PID 0
  37.  
  38. p = _fork(); // PID 1
  39.  
  40. if (!p) {
  41. // PID 2-6
  42. for (i=2;i<=6;i++) {
  43. if (getppid() == pid[0]) {
  44. p = _fork();
  45. if (!p) {
  46. printf("I:%d,PID:%d\n", i, getpid());
  47. pid[i] = getpid();
  48.  
  49. if (i == 6) { // PID 6
  50. // PID 7-8
  51. printf("6->\n");
  52. for (j=7;j<=8;j++) {
  53. if (getpid() == pid[6]) {
  54. p = _fork();
  55. if (!p) {
  56. printf("J:%d, PID:%d\n", j, getpid());
  57. pid[j] = getpid();
  58. }
  59. }
  60. }
  61. }
  62. }
  63. }
  64. }
  65. }
  66.  
  67. while(1) {
  68. pause();
  69. }
  70.  
  71. }
  72.  
  73. pid_t _fork() {
  74. pid_t pid;
  75. pid = fork();
  76. if (pid != -1) {
  77. return pid;
  78. } else {
  79. fprintf(stderr, "%s: %s\n", progname, strerror(errno));
  80. exit(errno);
  81. }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement