Advertisement
IvoB1n

ios_1.3

Apr 23rd, 2020
595
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 11.90 KB
  1. // xbobro01 Ivan Bobrov 1BIT for IOS
  2. #include "proj2.h"
  3.  
  4. int load_res() {
  5.     // initializing semaphores
  6.     int ret = 0;
  7.     j_enter_sem = sem_open(J_ENTER_SEM, O_CREAT, 0666, OPENED);
  8.     if (j_enter_sem == SEM_FAILED) {
  9.         fprintf(stderr, "%s/n", "Failed to open semphore");
  10.         ret = 1;
  11.     }
  12.     j_immreg_sem = sem_open(J_IMMREG_SEM, O_CREAT, 0666, CLOSED);
  13.     if (j_immreg_sem == SEM_FAILED) {
  14.         fprintf(stderr, "%s/n", "Failed to open semphore");
  15.         ret = 1;
  16.     }
  17.     j_immexitwait_sem = sem_open(J_IMMEXITWAIT_SEM, O_CREAT, 0666, OPENED);
  18.     if (j_immexitwait_sem == SEM_FAILED) {
  19.         fprintf(stderr, "%s/n", "Failed to open semphore");
  20.         ret = 1;
  21.     }
  22.     imm_enter_sem = sem_open(IMM_ENTER_SEM, O_CREAT, 0666, CLOSED);
  23.     if (imm_enter_sem == SEM_FAILED) {
  24.         fprintf(stderr, "%s/n", "Failed to open semphore");
  25.         ret = 1;
  26.     }
  27.     imm_reg_sem = sem_open(IMM_REG_SEM, O_CREAT, 0666, CLOSED);
  28.     if (imm_reg_sem == SEM_FAILED) {
  29.         fprintf(stderr, "%s/n", "Failed to open semphore");
  30.         ret = 1;
  31.     }
  32.     imm_confwait_sem = sem_open(IMM_CONFWAIT_SEM, O_CREAT, 0666, CLOSED);
  33.     if (imm_confwait_sem == SEM_FAILED) {
  34.         fprintf(stderr, "%s/n", "Failed to open semphore");
  35.         ret = 1;
  36.     }
  37.     imm_exitwait_sem = sem_open(IMM_EXITWAIT_SEM, O_CREAT, 0666, CLOSED);
  38.     if (imm_exitwait_sem == SEM_FAILED) {
  39.         fprintf(stderr, "%s/n", "Failed to open semphore");
  40.         ret = 1;
  41.     }
  42.     act_change_sem = sem_open(ACT_CHANGE_SEM, O_CREAT, 0666, OPENED);
  43.     if (act_change_sem == SEM_FAILED) {
  44.         fprintf(stderr, "%s/n", "Failed to open semphore");
  45.         ret = 1;
  46.     }
  47.  
  48.     // allocating shared memory
  49.     int protection = PROT_READ | PROT_WRITE;
  50.     int visibility = MAP_SHARED | MAP_ANONYMOUS;
  51.     mem = mmap(NULL, sizeof(struct Actual_inf), protection, visibility,-1, 0);
  52.     if (mem == MAP_FAILED) {
  53.         fprintf(stderr, "%s/n", "Memory map creation failed");
  54.         ret = 1;
  55.     }
  56.  
  57.     // default structer variables setting
  58.     mem->PROCESS_ID = 0;
  59.     mem->IMM_COUNT = 0;
  60.     mem->IMM_W_CERT = 0;
  61.     mem->IMM_STARTED = 0;
  62.     mem->J_IN_B = 0;
  63.     mem->NB = 0;
  64.     mem->NC = 0;
  65.     mem->NE = 0;
  66.  
  67.     return(ret);
  68. }
  69.  
  70. int clean_res() {
  71.     int ret = 0;
  72.     // deleting semaphores
  73.     if(sem_close(j_enter_sem) == -1) {
  74.         fprintf(stderr, "%s %s\n", "Cannot close semaphore", J_ENTER_SEM);
  75.         ret = 1;
  76.     }
  77.     if(sem_close(j_immreg_sem) == -1) {
  78.         fprintf(stderr, "%s %s\n", "Cannot close semaphore", J_IMMREG_SEM);
  79.         ret = 1;
  80.     }
  81.     if(sem_close(j_immexitwait_sem) == -1) {
  82.         fprintf(stderr, "%s %s\n", "Cannot close semaphore", J_IMMREG_SEM);
  83.         ret = 1;
  84.     }
  85.     if(sem_close(imm_enter_sem) == -1) {
  86.         fprintf(stderr, "%s %s\n", "Cannot close semaphore", IMM_ENTER_SEM);
  87.         ret = 1;
  88.     }
  89.     if(sem_close(imm_reg_sem) == -1) {
  90.         fprintf(stderr, "%s %s\n", "Cannot close semaphore", IMM_REG_SEM);
  91.         ret = 1;
  92.     }
  93.     if(sem_close(imm_confwait_sem) == -1) {
  94.         fprintf(stderr, "%s %s\n", "Cannot close semaphore", IMM_CONFWAIT_SEM);
  95.         ret = 1;
  96.     }
  97.     if(sem_close(imm_exitwait_sem) == -1) {
  98.         fprintf(stderr, "%s %s\n", "Cannot close semaphore", IMM_EXITWAIT_SEM);
  99.         ret = 1;
  100.     }
  101.     if(sem_close(act_change_sem) == -1) {
  102.         fprintf(stderr, "%s %s\n", "Cannot close semaphore", ACT_CHANGE_SEM);
  103.         ret = 1;
  104.     }
  105.    
  106.     if (sem_unlink(J_ENTER_SEM) == -1) {
  107.         fprintf(stderr,"Cannot unlink semaphore 1!\n");
  108.         ret = 1;
  109.     }
  110.     if (sem_unlink(J_IMMREG_SEM) == -1) {
  111.         fprintf(stderr,"Cannot unlink semaphore 1!\n");
  112.         ret = 1;
  113.     }
  114.     if (sem_unlink(J_IMMEXITWAIT_SEM) == -1) {
  115.         fprintf(stderr,"Cannot unlink semaphore 1!\n");
  116.         ret = 1;
  117.     }
  118.     if (sem_unlink(IMM_ENTER_SEM) == -1) {
  119.         fprintf(stderr,"Cannot unlink semaphore 1!\n");
  120.         ret = 1;
  121.     }
  122.     if (sem_unlink(IMM_REG_SEM) == -1) {
  123.         fprintf(stderr,"Cannot unlink semaphore 1!\n");
  124.         ret = 1;
  125.     }
  126.     if (sem_unlink(IMM_CONFWAIT_SEM) == -1) {
  127.         fprintf(stderr,"Cannot unlink semaphore 1!\n");
  128.         ret = 1;
  129.     }
  130.     if (sem_unlink(IMM_EXITWAIT_SEM) == -1) {
  131.         fprintf(stderr,"Cannot unlink semaphore 1!\n");
  132.         ret = 1;
  133.     }
  134.     if (sem_unlink(ACT_CHANGE_SEM) == -1) {
  135.         fprintf(stderr,"Cannot unlink semaphore 1!\n");
  136.         ret = 1;
  137.     }
  138.  
  139.     // freeing shared memory
  140.     if(munmap(mem, sizeof(struct Actual_inf)) == -1) {
  141.         fprintf(stderr, "%s\n", "munmap failed");
  142.         ret = 1;
  143.     }
  144.  
  145.     return ret;
  146. }
  147.  
  148. void random_sleep(int ms) {
  149.     if (ms == 0) {
  150.         usleep(0);
  151.     } else {
  152.         // get random value and change it to microseconds
  153.         int sleep_time_microsec = (rand() % ms) * 1000;
  154.         usleep(sleep_time_microsec);
  155.     }
  156. }
  157.  
  158. int params_check(struct Params *params, char *endptr_ig, char *endptr_jg, char *endptr_it, char *endptr_jt) {
  159.     // check if params contains invalid symbols
  160.     if (strcmp("", endptr_ig)
  161.         || strcmp("", endptr_jg)
  162.         || strcmp("", endptr_it)
  163.         || strcmp("", endptr_jt)
  164.         )
  165.     {
  166.         fprintf(stderr, "%s\n", "Wrong argument type");
  167.         return 1;
  168.     }
  169.     else if (params->PI < 1 // check if arguments are in needed range
  170.         || (params->IG < 0 && params->IG > 2000)
  171.         || (params->JG < 0 && params->JG > 2000)
  172.         || (params->IT < 0 && params->IT > 2000)
  173.         || (params->JT < 0 && params->JT > 2000)
  174.         )
  175.     {
  176.         fprintf(stderr, "%s\n", "Argument out of range");
  177.         return 1;
  178.     }
  179.     return 0;
  180. }
  181.  
  182. void my_fprintf(FILE *file, char *person, int self_id, char *action) {
  183.     // case for immigrant
  184.     if (strcmp(person, IMM_NAME) == 0) {
  185.         sem_wait(act_change_sem);
  186.         if (strcmp(action, "enters") == 0) {
  187.             mem->NE++;
  188.             mem->NB++;
  189.         }
  190.         else if (strcmp(action, "checks") == 0) {
  191.             mem->NC++;
  192.         }
  193.         else if (strcmp(action, "leaves") == 0) {
  194.             mem->NB--;
  195.         }
  196.         fprintf(file, "%-10d: %s %-10d: %-20s: %-5d: %-5d: %-5d\n",
  197.         ++(mem->PROCESS_ID), IMM_NAME, self_id, action, mem->NE, mem->NC, mem->NB);
  198.         sem_post(act_change_sem);
  199.     }
  200.     // case for judge
  201.     else {
  202.         sem_wait(act_change_sem);
  203.         fprintf(file, "%-10d: %-14s: %-20s: %-5d: %-5d: %-5d\n",
  204.         ++(mem->PROCESS_ID), J_NAME, action, mem->NE, mem->NC, mem->NB);
  205.         sem_post(act_change_sem);
  206.     }
  207. }
  208.  
  209. void immigrant_process(FILE *file, struct Params *params) {
  210.     sem_wait(act_change_sem);
  211.     mem->IMM_COUNT++;
  212.     int self_id = mem->IMM_COUNT;
  213.     sem_post(act_change_sem);
  214.    
  215.     sem_wait(act_change_sem);
  216.     fprintf(file, "%-10d: %s %-10d: %-20s\n", ++(mem->PROCESS_ID), IMM_NAME, self_id, "starts");
  217.     mem->IMM_STARTED++;
  218.     sem_post(act_change_sem);
  219.        
  220.     sem_wait(act_change_sem);
  221.     if (mem->J_IN_B == 1) {
  222.         sem_post(act_change_sem);
  223.         sem_wait(j_enter_sem);
  224.         sem_post(j_enter_sem);
  225.     }
  226.     else {
  227.         sem_post(act_change_sem);
  228.     }
  229.    
  230.     my_fprintf(file, IMM_NAME, self_id, "enters");
  231.    
  232.     my_fprintf(file, IMM_NAME, self_id, "checks");
  233.    
  234.     if (mem->NE == mem->NC) {
  235.         sem_post(imm_reg_sem);
  236.     }
  237.  
  238.     sem_wait(imm_confwait_sem);
  239.    
  240.     my_fprintf(file, IMM_NAME, self_id, "wants certificate");
  241.  
  242.     random_sleep(params->IT);
  243.  
  244.     my_fprintf(file, IMM_NAME, self_id, "got certificate");
  245.    
  246.     /*
  247.     if (mem->IMM_W_CERT == params->PI) {
  248.         sem_post(imm_exitwait_sem);
  249.     }
  250.     */
  251.    sem_wait(act_change_sem);
  252.     if (mem->J_IN_B == 1) {
  253.         sem_post(act_change_sem);
  254.         sem_wait(j_enter_sem);
  255.         sem_post(j_enter_sem);
  256.     }
  257.     else {
  258.         sem_post(act_change_sem);
  259.     }
  260.     my_fprintf(file, IMM_NAME, self_id, "leaves");
  261.    
  262.     exit(0);
  263. }
  264.  
  265. void judge_process(FILE *file, struct Params *params) {
  266.     while(mem->IMM_W_CERT < params->PI) {
  267.         random_sleep(params->JG);
  268.        
  269.         sem_wait(act_change_sem);
  270.         fprintf(file, "%-10d: %-14s: %-20s\n", ++(mem->PROCESS_ID), J_NAME, "wants to enter");
  271.        
  272.         mem->J_IN_B = 1;
  273.         sem_wait(j_enter_sem);
  274.         sem_post(act_change_sem);
  275.         my_fprintf(file, J_NAME, 0, "enters");
  276.        
  277.        
  278.         if (mem->NE != mem->NC) {
  279.             my_fprintf(file, J_NAME, 0, "waits for imm");
  280.  
  281.             sem_wait(imm_reg_sem);
  282.         }
  283.        
  284.         my_fprintf(file, J_NAME, 0, "starts confirmation");
  285.  
  286.         random_sleep(params->JT);
  287.  
  288.         sem_wait(act_change_sem);
  289.         mem->IMM_W_CERT += mem->NC;
  290.         mem->NE = 0;
  291.         mem->NC = 0;
  292.         sem_post(act_change_sem);
  293.  
  294.         my_fprintf(file, J_NAME, 0, "ends confirmation");
  295.  
  296.         for (int i = 0; i < mem->NB; ++i) {
  297.             sem_post(imm_confwait_sem);
  298.         }
  299.        
  300.         random_sleep(params->JT);
  301.  
  302.  
  303.         sem_wait(act_change_sem);
  304.         fprintf(file, "%-10d: %-14s: %-20s: %-5d: %-5d: %-5d\n",
  305.         ++(mem->PROCESS_ID), J_NAME, "leaves", mem->NE, mem->NC, mem->NB);
  306.         mem->J_IN_B = 0;
  307.         sem_post(act_change_sem);
  308.         sem_post(j_enter_sem);
  309.         // for (int i = 0; i < mem->NB; ++i) {
  310.         //     sem_post(imm_exitwait_sem);
  311.         // }
  312.         // for (int i = 0; i < mem->IMM_STARTED; ++i) {
  313.         //     sem_post(imm_enter_sem);
  314.         // }
  315.        
  316.        
  317.     }
  318.  
  319.     sem_wait(act_change_sem);
  320.     fprintf(file, "%-10d: %-14s: %-20s\n", ++(mem->PROCESS_ID), J_NAME, "finishes");
  321.     sem_post(act_change_sem);
  322.    
  323.     exit(0);
  324. }
  325.  
  326. int main(int argv, char **argc) {
  327.     // argument validation
  328.     if (argv != num_of_args) {
  329.         fprintf(stderr, "%s\n", "You should enter 5 arguments");
  330.  
  331.         return 1;
  332.     }
  333.    
  334.     struct Params params;
  335.     char *endptr_ig, *endptr_jg, *endptr_it, *endptr_jt;
  336.     params.PI = strtol(argc[1], NULL, int_base);
  337.     params.IG = strtol(argc[2], &endptr_ig, int_base);
  338.     params.JG = strtol(argc[3], &endptr_jg, int_base);
  339.     params.IT = strtol(argc[4], &endptr_it, int_base);
  340.     params.JT = strtol(argc[5], &endptr_jt, int_base);
  341.  
  342.     // validate params
  343.     if(params_check(&params, endptr_ig, endptr_jg, endptr_it, endptr_jt)) {
  344.         return 1;
  345.     }
  346.    
  347.     // try to open file proj2.out
  348.     FILE *file = fopen(FILE_NAME, FILE_MODE);
  349.     if (file == NULL) {
  350.         fclose(file);
  351.         fprintf(stderr, "%s\n", "Failed to open file proj2.out");
  352.         return 1;
  353.     }
  354.     setbuf(file, NULL);
  355.  
  356.     // try to load additional resources
  357.     if (load_res()) {
  358.         return 1;
  359.     }
  360.  
  361.     pid_t immigrant;
  362.     pid_t f_immigrant;
  363.     pid_t judge;
  364.  
  365.     // create judge process
  366.     judge = fork();
  367.     if (judge == 0) {
  368.         judge_process(file, &params); // algorithm for judge
  369.     }
  370.     else if (judge > 0) { // if judge was already created
  371.         f_immigrant = fork();
  372.         if (f_immigrant == 0) {
  373.             // sem_post(j_enter_sem); // let judge process start, when first imm entered
  374.             for (int i = 0; i < params.PI; ++i) {
  375.                 immigrant = fork();
  376.                 random_sleep(params.IG);
  377.                 if (immigrant == 0) {
  378.                     immigrant_process(file, &params); // algorithm for immigrant
  379.                 }
  380.             }
  381.             waitpid(immigrant, NULL, 0);
  382.             exit(0);
  383.         }
  384.         else if (f_immigrant < 0){
  385.             fprintf(stderr, "%s\n", "Could not create immigrant child process");
  386.             clean_res();
  387.             return 1;
  388.         }
  389.     }
  390.     else {
  391.         fprintf(stderr, "%s\n", "Could not create judge child process");
  392.         clean_res();
  393.         return 1;
  394.     }
  395.    
  396.     // wait for processes ending end clean initialized resources
  397.     waitpid(f_immigrant, NULL, 0);
  398.     waitpid(judge, NULL, 0);
  399.     if(clean_res()) {
  400.         return 1;
  401.     }
  402.     fclose(file);
  403.     return 0;
  404. }
Advertisement
RAW Paste Data Copied
Advertisement