在我的项目中遇到信号量问题

时间:2017-03-15 03:58:21

标签: c unix semaphore shared-memory critical-section

我有一个项目,我有5个进程在运行,用fork()创建。一个父母和四个孩子。其中3个过程是游泳者。其中2个过程是锅炉工。该男爵和锅炉工人都进入洗浴区,等待一段时间,离开洗浴区,等待一段时间。我必须使用信号量来确保2名锅炉工人不能同时进入洗浴区域,而且一名牧师和锅炉工人不能同时进入洗浴区域。

我无法使用我需要用来完成这个项目的信号量,任何帮助告诉我哪里出错都会有所帮助。我在这个项目中也使用共享内存。我将包括bather和boilerman的两个关键部分,如果你想让我再添加代码,请告诉我。任何帮助将不胜感激。

BATHERMAN CODE:

 <input type="checkbox" class="checkbox" data-bind="iCheck: StaffIsHeadOffice" />Head Office

BOILERMAN CODE:

    create5();
   // The child process ----------------------------- //
   if (process_id == 0 && p_shm->programCounter < 3)
   {
       p_shm->programCounter++;
        printf( "[BA] pid %d from pid %d\n", getpid(), getppid() );

        //CRITICAL SECTION
        if(process_id == 0 && p_shm->ba_num == 0){
            my_TID = 1;
            p_shm->ba_num++;
        }else if(process_id == 0 && p_shm->ba_num == 1){
            my_TID = 2;
            p_shm->ba_num++;
        }else if(process_id == 0 && p_shm->ba_num == 2){
            my_TID = 3;
            p_shm->ba_num++;
        }

         while (p_shm->boiler_done_flag == 0)
       {
              // WAIT on the semaphore M2

            operations[0].sem_num = 1;
            operations[0].sem_op = -1; // "wait" on the semaphore   
            operations[0].sem_flg = 0;  
            ret_val = semop(sem_id, operations, 1);         
            if (ret_val != 0)  
            {   
                fprintf(stderr, "P-OP(wait) failed (child) ....\a\n");   
            }  

                     sleep_time = RAND(BATHER_TIME_01);
                     usleep(sleep_time);

           p_shm->bather_count++;
           if(p_shm->bather_count == 1){

               // WAIT on the semaphore M1
            operations[0].sem_num = 0;
            operations[0].sem_op = -1; // "wait" on the semaphore   
            operations[0].sem_flg = 0;  
            ret_val = semop(sem_id, operations, 1);         
            if (ret_val != 0)  
            {   
                fprintf(stderr, "P-OP(wait) failed (child) ....\a\n");   
            }

           }

              // SIGNAL on the semaphore M2
            operations[0].sem_num = 1;  
            operations[0].sem_op  = 1;  
            operations[0].sem_flg = 0;  
            ret_val = semop(sem_id, operations, 1);   
            if (ret_val != 0)
            {
                fprintf(stderr, "V-OP (wait) failed (child) ....\a\n");    
            }  

                     // the critical section starts here -------------------
                     printf("T%d is entering the bathing area ..\n", my_TID);

                         // do nothing .. it’s time to relax!!
                     sleep_time = BATHER_TIME_02;
                     usleep(sleep_time); 
                     printf ("T%d is leaving the bathing area ..\n", my_TID);
                     // the critical section ends here --------------------

            // WAIT on the semaphore M2
            operations[0].sem_num = 1;
            operations[0].sem_op = -1; // "wait" on the semaphore   
            operations[0].sem_flg = 0;  
            ret_val = semop(sem_id, operations, 1);         
            if (ret_val != 0)  
            {   
                fprintf(stderr, "P-OP(wait) failed (child) ....\a\n");   
            }

            p_shm->bather_count--;
            if(p_shm->bather_count == 0){

                // SIGNAL on the semaphore M2
            operations[0].sem_num = 1;  
            operations[0].sem_op  = 1;  
            operations[0].sem_flg = 0;  
            ret_val = semop(sem_id, operations, 1);   
            if (ret_val != 0)
            {
                fprintf(stderr, "V-OP (wait) failed (child) ....\a\n");    
            }  

            }

            // SIGNAL on the semaphore M1
            operations[0].sem_num = ;  
            operations[0].sem_op  = 1;  
            operations[0].sem_flg = 0;  
            ret_val = semop(sem_id, operations, 1);   
            if (ret_val != 0)
            {
                fprintf(stderr, "V-OP (wait) failed (child) ....\a\n");    
            }  
      }

        //CRITICAL SECTION END



       exit(0);
   }

0 个答案:

没有答案