Няшная / Говнокод #29090 Ссылка на оригинал

0

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20
  21. 21
  22. 22
  23. 23
  24. 24
  25. 25
  26. 26
  27. 27
  28. 28
  29. 29
  30. 30
  31. 31
  32. 32
  33. 33
  34. 34
  35. 35
  36. 36
  37. 37
  38. 38
  39. 39
  40. 40
  41. 41
  42. 42
  43. 43
  44. 44
  45. 45
  46. 46
  47. 47
  48. 48
  49. 49
  50. 50
  51. 51
  52. 52
  53. 53
  54. 54
  55. 55
  56. 56
  57. 57
  58. 58
  59. 59
  60. 60
  61. 61
  62. 62
  63. 63
  64. 64
  65. 65
  66. 66
  67. 67
  68. 68
  69. 69
  70. 70
  71. 71
  72. 72
  73. 73
  74. 74
  75. 75
  76. 76
  77. 77
  78. 78
  79. 79
  80. 80
  81. 81
  82. 82
  83. 83
  84. 84
  85. 85
  86. 86
  87. 87
  88. 88
  89. 89
  90. 90
  91. 91
void main() {
    ph_fork f1, f2, f3, f4, f5;

    f_arr[0] = &f1;
    f_arr[1] = &f2;
    f_arr[2] = &f3;
    f_arr[3] = &f4;
    f_arr[4] = &f5;

    philosopher ph1, ph2, ph3, ph4, ph5;

    ph_arr[0] = &ph1;
    ph_arr[1] = &ph2;
    ph_arr[2] = &ph3;
    ph_arr[3] = &ph4;
    ph_arr[4] = &ph5;

    f1.number = 1;
    sem_init(&f1.is_free, 0, 1);

    f2.number = 2;
    sem_init(&f2.is_free, 0, 1);

    f3.number = 3;
    sem_init(&f3.is_free, 0, 1);

    f4.number = 4;
    sem_init(&f4.is_free, 0, 1);

    f5.number = 5;
    sem_init(&f5.is_free, 0, 1);

    ph1.number = 1;
    ph1.ph_fork_amount = 0;
    ph1.ph_forks[0] = 1;
    ph1.ph_forks[1] = 2;
    ph1.times_eaten = 0;
    ph1.times_thought = 0;
    ph1.st = THINKING;
    pthread_create(&ph1.thread, NULL, routine, (void*)&ph1);

    ph2.number = 2;
    ph2.ph_fork_amount = 0;
    ph2.ph_forks[0] = 2;
    ph2.ph_forks[1] = 3;
    ph2.times_eaten = 0;
    ph2.times_thought = 0;
    ph2.st = THINKING;
    pthread_create(&ph2.thread, NULL, routine, (void*)&ph2);

    ph3.number = 3;
    ph3.ph_fork_amount = 0;
    ph3.ph_forks[0] = 3;
    ph3.ph_forks[1] = 4;
    ph3.times_eaten = 0;
    ph3.times_thought = 0;
    ph3.st = THINKING;
    pthread_create(&ph3.thread, NULL, routine, (void*)&ph3);

    ph4.number = 4;
    ph4.ph_fork_amount = 0;
    ph4.ph_forks[0] = 4;
    ph4.ph_forks[1] = 5;
    ph4.times_eaten = 0;
    ph4.times_thought = 0;
    ph4.st = THINKING;
    pthread_create(&ph4.thread, NULL, routine, (void*)&ph4);

    ph5.number = 5;
    ph5.ph_fork_amount = 0;
    ph5.ph_forks[0] = 5;
    ph5.ph_forks[1] = 1;
    ph5.times_eaten = 0;
    ph5.times_thought = 0;
    ph5.st = THINKING;
    pthread_create(&ph5.thread, NULL, routine, (void*)&ph5);

    pthread_join(ph1.thread, NULL);
    pthread_join(ph2.thread, NULL);
    pthread_join(ph3.thread, NULL);
    pthread_join(ph4.thread, NULL);
    pthread_join(ph5.thread, NULL);

    sem_destroy(&f1.is_free);
    sem_destroy(&f2.is_free);
    sem_destroy(&f3.is_free);
    sem_destroy(&f4.is_free);
    sem_destroy(&f5.is_free);

    printf("\nThe lunch has ended!\n--------\nRESULTS:\nPhilosopher 1 has eaten %d times and thought %d times\nPhilosopher 2 has eaten %d times and thought %d times\nPhilosopher 3 has eaten %d times and thought %d times\nPhilosopher 4 has eaten %d times and thought %d times\nPhilosopher 5 has eaten %d times and thought %d times\n", ph1.times_eaten, ph1.times_thought, ph2.times_eaten, ph2.times_thought, ph3.times_eaten, ph3.times_thought, ph4.times_eaten, ph4.times_thought, ph5.times_eaten, ph5.times_thought);
}

Решение задачи про обедающих философов, часть вторая.

Запостил: GDMaster GDMaster, (Updated )

Комментарии (2) RSS

  • круто! а можете пожалуйста сделать вариант для сорока семи фолосов?
    Сейчас просто знаете бывает много ядер у процессоров
    Ответить
    • У меня в контроллере нет никаких "философов". И ядро обычно одно.
      Ответить

Добавить комментарий

Я, guest, находясь в здравом уме и твердой памяти, торжественно заявляю:

    А не использовать ли нам bbcode?


    8