Advertisement
Guest User

Untitled

a guest
Apr 27th, 2017
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 13.05 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <sys/types.h>
  4. #include <sys/wait.h>
  5. #include "a2_helper.h"
  6. #include <unistd.h>
  7. #include <sys/types.h>
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <pthread.h>
  11. #include <string.h>
  12. #include <sys/ipc.h>
  13. #include <sys/sem.h>
  14. #include <semaphore.h>
  15.  
  16. #define THREADSP3 4
  17. #define MAXTHREADS 4
  18. #define THREADSP9 49
  19. #define THREADSP5 5
  20.  
  21. int sem_id_p3[THREADSP3];
  22. int sem_id_p9;
  23. int sem_id_p5[3];
  24. int t15s = 0;
  25. sem_t *sem1;
  26. sem_t *sem2;
  27.  
  28. pthread_mutex_t lock;
  29. pthread_cond_t cond;
  30. int nr_of_threads;
  31.  
  32. typedef struct _threadName {
  33.     int threadNo;
  34.     int threadId;
  35. }threadName;
  36.  
  37. void P(int semId, int semNr)
  38. {
  39.     struct sembuf op = { semNr, -1, 0 };
  40.  
  41.     semop(semId, &op, 1);
  42. }
  43.  
  44. void V(int semId, int semNr)
  45. {
  46.     struct sembuf op = { semNr, +1, 0 };
  47.  
  48.     semop(semId, &op, 1);
  49. }
  50.  
  51.  
  52. void* execThread3(void* threadParam)
  53. {
  54.  
  55.     threadName *thName = (threadName *)threadParam;
  56.     //info(BEGIN,thName->threadId, thName->threadNo);
  57.     P(sem_id_p3[thName->threadNo - 1], 0);
  58.  
  59.     if (thName->threadNo == 3)
  60.     {
  61.         P(sem_id_p5[2], 0);
  62.     }
  63.     info(BEGIN, thName->threadId, thName->threadNo);
  64.     if (thName->threadNo == THREADSP3)
  65.     {
  66.         info(END, thName->threadId, thName->threadNo);
  67.         V(sem_id_p3[THREADSP3 - 2], 0);
  68.     }
  69.     else
  70.     {
  71.         V(sem_id_p3[thName->threadNo], 0);
  72.         P(sem_id_p3[thName->threadNo - 1], 0);
  73.  
  74.         if (thName->threadNo == 3)
  75.         {
  76.             P(sem_id_p5[0], 0);
  77.         }
  78.         info(END, thName->threadId, thName->threadNo);
  79.         if (thName->threadNo == 3)
  80.         {
  81.             V(sem_id_p5[1], 0);
  82.         }
  83.         V(sem_id_p3[thName->threadNo - 2], 0);
  84.     }
  85.  
  86.     //V(sem_id_p3[(thName->threadNo)%5],0);
  87.     //info(END,thName->threadId, thName->threadNo);
  88.    
  89.     //semctl(sem_id_p3[0], 0, IPC_RMID, 0);
  90.     //semctl(sem_id_p3[1], 0, IPC_RMID, 0);
  91.     //semctl(sem_id_p3[2], 0, IPC_RMID, 0);
  92.     //semctl(sem_id_p3[3], 0, IPC_RMID, 0);
  93.     return NULL;
  94.  
  95. }
  96.  
  97. void* execThread5(void* threadParam)
  98. {
  99.     threadName *thName = (threadName *)threadParam;
  100.     if (thName->threadNo == 5)
  101.     {
  102.         P(sem_id_p5[1], 0);
  103.     }
  104.     info(BEGIN, thName->threadId, thName->threadNo);
  105.     if (thName->threadNo == 4)
  106.     {
  107.         V(sem_id_p5[2], 0);
  108.     }
  109.  
  110.  
  111.     if (thName->threadNo == 4)
  112.     {
  113.         V(sem_id_p5[0], 0);
  114.     }
  115.     info(END, thName->threadId, thName->threadNo);
  116.    
  117.    
  118.     //semctl(sem_id_p5[0], 0, IPC_RMID, 0);
  119.     //semctl(sem_id_p5[1], 0, IPC_RMID, 0);
  120.     //semctl(sem_id_p5[2], 0, IPC_RMID, 0);
  121.     return NULL;
  122. }
  123.  
  124. void* execThread9(void* threadParam)
  125. {
  126.  
  127.     // int nr_of_threads_local;
  128.     int can_close = 0, can_start = 0;    
  129.     threadName *thName = (threadName *)threadParam;
  130.    
  131.     do
  132.     {  
  133.         pthread_mutex_lock(&lock);
  134.         if(nr_of_threads < MAXTHREADS)
  135.         {
  136.             can_start = 1;
  137.             nr_of_threads++;
  138.             info(BEGIN, thName->threadId, thName->threadNo);
  139.             if(thName->threadNo == 15)
  140.             {
  141.                 t15s = 1;
  142.             }
  143.         }
  144.         pthread_mutex_unlock(&lock);
  145.     }while (! can_start);
  146.  
  147.     if (thName->threadNo == 15)
  148.     {
  149.         can_close = 0;
  150.         do
  151.         {
  152.             if (pthread_mutex_lock(&lock) != 0)
  153.             {
  154.                 printf("Cannot take the lock\n");
  155.                 exit(6);
  156.             }
  157.             if(nr_of_threads >= MAXTHREADS)
  158.             {
  159.                 t15s = 0;
  160.                 can_close = 1;
  161.                 nr_of_threads--;
  162.                 info(END, thName->threadId, thName->threadNo);
  163.             }
  164.             if (pthread_mutex_unlock(&lock) != 0)
  165.             {
  166.                 printf("Cannot release the lock\n");
  167.                 exit(5);
  168.             }
  169.         }while (! can_close);
  170.  
  171.     }
  172.     else
  173.     {
  174.         can_close = 0;
  175.         do{
  176.             pthread_mutex_lock(&lock);
  177.             if(t15s == 1)
  178.                 can_close = 0;
  179.             else
  180.                 can_close = 1;
  181.             pthread_mutex_unlock(&lock);
  182.         }while(! can_close);      
  183.         pthread_mutex_lock(&lock);
  184.         info(END, thName->threadId, thName->threadNo);
  185.         nr_of_threads--;
  186.         pthread_mutex_unlock(&lock);
  187.     }
  188.  
  189.   // if (pthread_cond_broadcast(&cond) != 0)
  190.   //   {
  191.   //       printf("Cannot wait for condition\n");
  192.   //       exit(4);
  193.   //   }
  194.  
  195.   //   if (pthread_mutex_unlock(&lock) != 0)
  196.   //   {
  197.   //       printf("Cannot release the lock\n");
  198.   //       exit(5);
  199.   //   }
  200.  
  201.    
  202.  
  203.     return NULL;
  204.  
  205. }
  206.  
  207. void process2()
  208. {
  209.     info(BEGIN, 2, 0);
  210.     info(END, 2, 0);
  211. }
  212.  
  213. void process3()
  214. {
  215.     //threads test
  216.     pthread_t th[THREADSP3];
  217.     threadName *thName;
  218.     int* state[MAXTHREADS];
  219.     thName = (threadName *)malloc(sizeof(threadName) * THREADSP3);
  220.  
  221.  
  222.     for (int i = 0; i < THREADSP3; i++)
  223.     {
  224.         sem_id_p3[i] = semget(IPC_PRIVATE, 1, IPC_CREAT | 0600);
  225.         semctl(sem_id_p3[i], 0, SETVAL, 0);
  226.         if (sem_id_p3[i] < 0) {
  227.             perror("Error creating the semaphore set");
  228.             exit(2);
  229.         }
  230.         thName[i].threadNo = i + 1;
  231.         thName[i].threadId = 3;
  232.         if (pthread_create(&th[i], NULL, execThread3, (void *)(&thName[i])) != 0)
  233.         {
  234.             exit(10);
  235.         }
  236.     }
  237.     V(sem_id_p3[0], 0);
  238.     for (int i = 0; i < THREADSP3; i++)
  239.     {
  240.         pthread_join(th[i], (void**)&state[i]);
  241.     }
  242.     //waitpid(childPid6,NULL,0);
  243. }
  244.  
  245. void process5()
  246. {
  247.     pthread_t th[THREADSP5];
  248.     int* state[THREADSP5];
  249.     threadName *thName;
  250.     thName = (threadName *)malloc(sizeof(threadName) * THREADSP5);
  251.  
  252.     for (int i = 0; i < THREADSP5; i++)
  253.     {
  254.         thName[i].threadNo = i + 1;
  255.         thName[i].threadId = 5;
  256.         if (pthread_create(&th[i], NULL, execThread5, (void *)(&thName[i])) != 0)
  257.         {
  258.             exit(10);
  259.         }
  260.     }
  261.  
  262.     for (int i = 0; i < THREADSP5; i++)
  263.     {
  264.         pthread_join(th[i], (void**)&state[i]);
  265.     }
  266.  
  267. }
  268.  
  269. void process9()
  270. {
  271.     //threads test
  272.     pthread_t th[THREADSP9];
  273.     threadName *thName;
  274.     int* state[THREADSP9];
  275.     thName = (threadName *)malloc(sizeof(threadName) * THREADSP9);
  276.     info(BEGIN, 9, 0);
  277.  
  278.     //sem_id_p9 = semget(IPC_PRIVATE, 1, IPC_CREAT | 0600);
  279.     //semctl(sem_id_p9, 0, SETVAL, 4);
  280.  
  281.     /*if (sem_id_p9 < 0) {
  282.     perror("Error creating the semaphore set");
  283.     exit(2);
  284.     }
  285.     */
  286.     if (pthread_mutex_init(&lock, NULL) != 0)
  287.     {
  288.         printf("Cannot initialize the lock\n");
  289.         exit(3);
  290.     }
  291.  
  292.     printf("1\n");
  293.  
  294.     // if (pthread_cond_init(&cond, NULL) != 0)
  295.     // {
  296.     //     printf("Cannot initialize the cond variable\n");
  297.     //     exit(4);
  298.     // }
  299.  
  300.     for (int i = 0; i < THREADSP9; i++)
  301.     {
  302.         thName[i].threadNo = i + 1;
  303.         thName[i].threadId = 9;
  304.         if (pthread_create(&th[i], NULL, execThread9, (void *)(&thName[i])) != 0)
  305.         {
  306.             exit(10);
  307.         }
  308.     }
  309.  
  310.     for (int i = 0; i < THREADSP9; i++)
  311.     {
  312.         pthread_join(th[i], (void**)&state[i]);
  313.     }
  314.  
  315.     info(END, 9, 0);
  316. }
  317.  
  318. void initSemahores()
  319. {
  320.     for (int k = 0; k< 3; k++)
  321.     {
  322.         sem_id_p5[k] = semget(IPC_PRIVATE, 1, IPC_CREAT | 0600);
  323.         semctl(sem_id_p5[k], 0, IPC_RMID, 0);
  324.         semctl(sem_id_p5[k], 0, SETVAL, 0);
  325.  
  326.         if (sem_id_p5[k] < 0) {
  327.             perror("Error creating the semaphore set");
  328.             exit(2);
  329.         }
  330.     }
  331. }
  332.  
  333.  
  334. int main() {
  335.     int childPid1 = 0;
  336.     int childPid2 = 0;
  337.     int childPid3 = 0;
  338.     int childPid4 = 0;
  339.     int childPid5 = 0;
  340.     int childPid6 = 0;
  341.     int childPid7 = 0;
  342.     int childPid8 = 0;
  343.  
  344.     //sem1 = semop("Sem1", O_CREAT | O_EXCL, 0644,)
  345.  
  346.     initSemahores();
  347.  
  348.     init();
  349.     info(BEGIN, 1, 0);
  350.     childPid1 = fork();
  351.     if (childPid1 < 0)
  352.     {
  353.         //error exit(1);
  354.     }
  355.     else
  356.     {
  357.         if (childPid1 > 0)
  358.         {
  359.             childPid2 = fork();
  360.             if (childPid2 < 0)
  361.             {
  362.                 //error exit(2);
  363.             }
  364.             else
  365.             {
  366.                 if (childPid2 >0)
  367.                 {
  368.                     childPid3 = fork();
  369.                     if (childPid3 < 0)
  370.                     {
  371.                         //error exit(3);
  372.                     }
  373.                     else
  374.                     {
  375.                         if (childPid3 >0)
  376.                         {
  377.                             childPid4 = fork();
  378.                             if (childPid4 < 0)
  379.                             {
  380.                                 //error exit(4);
  381.                             }
  382.                             else
  383.                             {
  384.                                 if (childPid4 >0)
  385.                                 {
  386.                                     waitpid(childPid1, NULL, 0);
  387.                                     waitpid(childPid2, NULL, 0);
  388.                                     waitpid(childPid3, NULL, 0);
  389.                                     waitpid(childPid4, NULL, 0);
  390.                                     info(END, 1, 0);
  391.                                 }
  392.                                 else
  393.                                 {
  394.                                     info(BEGIN, 7, 0);
  395.                                     info(END, 7, 0);
  396.                                 }
  397.                             }
  398.                         }
  399.                         else
  400.                         {
  401.                             info(BEGIN, 3, 0);
  402.                             childPid6 = fork();
  403.                             if (childPid6 < 0)
  404.                             {
  405.                                 //error exit(6);
  406.                             }
  407.                             else
  408.                             {
  409.                                 if (childPid6 >0)
  410.                                 {
  411.                                     process3();
  412.                                     waitpid(childPid6, NULL, 0);
  413.                                     info(END, 3, 0);
  414.  
  415.                                 }
  416.                                 else
  417.                                 {
  418.                                     info(BEGIN, 4, 0);
  419.                                     childPid7 = fork();
  420.                                     if (childPid7 < 0)
  421.                                     {
  422.                                         //error exit(7);
  423.                                     }
  424.                                     else
  425.                                     {
  426.                                         if (childPid7 >0)
  427.                                         {
  428.  
  429.                                             childPid8 = fork();
  430.                                             if (childPid8 < 0)
  431.                                             {
  432.                                                 //error exit(8);
  433.                                             }
  434.                                             else
  435.                                             {
  436.                                                 if (childPid8 >0)
  437.                                                 {
  438.                                                     //info(BEGIN, 4, 0);
  439.                                                     waitpid(childPid7, NULL, 0);
  440.                                                     waitpid(childPid8, NULL, 0);
  441.                                                     info(END, 4, 0);
  442.                                                 }
  443.                                                 else
  444.                                                 {
  445.                                                     printf("21312312\n" );
  446.                                                     process9();
  447.                                                     // info(BEGIN, 9, 0);
  448.                                                     // info(END, 9, 0);
  449.                                                 }
  450.                                             }
  451.                                         }
  452.                                         else
  453.                                         {
  454.                                             info(BEGIN, 6, 0);
  455.                                             info(END, 6, 0);
  456.                                         }
  457.  
  458.                                     }
  459.                                 }
  460.  
  461.                             }
  462.                         }
  463.                     }
  464.                 }
  465.                 else
  466.                 {
  467.                     info(BEGIN, 5, 0);
  468.                     childPid5 = fork();
  469.                     if (childPid5 < 0)
  470.                     {
  471.                         //error exit(5);
  472.                     }
  473.                     else
  474.                     {
  475.                         if (childPid5 >0)
  476.                         {
  477.                             process5();
  478.                             waitpid(childPid5, NULL, 0);
  479.                             info(END, 5, 0);
  480.                         }
  481.                         else
  482.                         {
  483.                             info(BEGIN, 8, 0);
  484.                             info(END, 8, 0);
  485.                         }
  486.                     }
  487.                 }
  488.             }
  489.         }
  490.         else
  491.         {
  492.             process2();
  493.         }
  494.     }
  495.  
  496.    
  497.     return 0;
  498. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement