信号量不会锁定进程,而是继续执行关键部分

时间:2017-03-15 12:52:48

标签: c semaphore race-condition critical-section

我有2个进程,其中进程将进程的日志写入文件。为了保持同步,我计划使用信号量来完成这个过程,直到一个人在关键部分执行。以下是我使用的函数的代码片段。

static int createAndInitializeSemphore()
{
       int                        rv                                = SUCCESS;
       FILE*                fptr                       = NULL;
#ifdef DEBUG
       char          szDbgMsg[256] = "";
#endif

       printf( "%s: --- enter ---", __FUNCTION__);


       while(1)
       {
              if(doesFileExist(MESSAGE_KEY_FILE) != SUCCESS)
              {
                     printf(  "%s: %s File doesnt exist Creating It", __FUNCTION__, MESSAGE_KEY_FILE);


                     fptr = fopen(MESSAGE_KEY_FILE, "w");
                     if(fptr == NULL )
                     {
                           printf(  "%s: Could Not Open the File", __FUNCTION__);


                           return FAILURE;
                     }

                     fprintf(fptr, "SCA APP LOG APP\n");
                     fclose(fptr);
              }

              if( (semKey = ftok(MESSAGE_KEY_FILE, 'B')) == -1 )
              {
                     printf( "%s: FAILED to generate Semaphore key", __FUNCTION__);

                     printf( "%s: Something went wrong ftok(): %s, errno=%d", __FUNCTION__, strerror(errno), errno);

                     rv = FAILURE;
                     break;
              }

              printf( "%s: Semaphore key[%d]", __FUNCTION__, semKey);


              semID = semget(semKey, 0, 0);

              if(semID == -1)
              {
                     printf( "%s: semget returned Error: %s, errno=%d", __FUNCTION__, strerror(errno), errno);


                     if ((semID = semget(semKey, 1, IPC_CREAT | 0666)) != -1)
                     {
                           printf( "%s: Semaphore ID Created", __FUNCTION__);


                           arg.val = 1;

                           if(semctl(semID, 0, SETVAL, arg) == -1)
                           {
                                  printf( "%s: semctl returned Error: %s, errno=%d", __FUNCTION__, strerror(errno), errno);

                                  //What to do now?
                           }
                     }
                     else if(errno == EEXIST)
                     {
                           printf( "%s: semID already exists", __FUNCTION__);

                     }
              }

              printf( "%s: Successfully Created Application Log Message Queue",__FUNCTION__);

              break;
       }

       printf( "%s: Returning[%d]", __FUNCTION__, rv);


       return rv;
}

static void lockSemaphore()
{
#ifdef DEBUG
       char   szDbgMsg[128] = "";
#endif

       struct sembuf sbuf;

       sbuf.sem_flg = SEM_UNDO;
       sbuf.sem_num = 0;
       sbuf.sem_op = -1;

       if(semop(semID, &sbuf, 1) == -1)
       {
              printf( "%s: Failed to Lock Semaphore Id[%d]: %s, errno=%d", __FUNCTION__, semID, strerror(errno), errno);

       }

       return;
}

static void unlockSemaphore()
{
#ifdef DEBUG
       char   szDbgMsg[128] = "";
#endif

       struct sembuf sbuf ;

       sbuf.sem_flg = 0;
       sbuf.sem_num = 0;
       sbuf.sem_op = 1;

       if(semop(semID, &sbuf, 1) == -1)
       {
              printf( "%s: Failed to Un-Lock Semaphore Id[%d]: %s, errno=%d", __FUNCTION__, semID, strerror(errno), errno);

       }

       return;
}

createAndInitializeSemaphore将在启动时仅在每个进程中调用一次。假设我们有两个进程P1和P2,它们都将日志写入名为log.txt的文件中.Below是将日志写入文件时两个进程的代码:

lockSemaphore();

/* code for opening the log file writing the logs and the close the file*/

unlockSemaphore();

但它没有按预期工作。即使P1获得锁并执行临界区P2也能够获取并执行临界区并且文件被破坏。你能帮忙我做错了吗?

0 个答案:

没有答案