Advertisement
IvoB1n

ios_2_skoro_hotovy_projekt

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