我在一个进程中有两个线程。这两个线程与试图通过信号量同步的共享内存竞争。但是当一个线程在另一个线程旁边调用semop函数时,我随机得到了一个错误4。我做了一些调查,发现似乎是系统调用中断了调用。
当在这个系统调用中被阻塞时,进程捕获到一个信号;参见信号(7)。呃4号是这个吗?
请注意583和601行。
哪个系统调用中断了它?函数semop()本身?是否可以忽略中断或恢复/重新启动此功能的系统调用?
semop可以在多线程环境中使用吗?

[Switching to Thread -1208269120 (LWP 4501)]
GetMyQue2Wait (MyModule=RM, wait_shm_ptr=0xbf8a5cf4) at tdm_ipc.c:247
247                                             TDM_SEM_P( MyModule );
(gdb) s
tdm_sem_p (mid=RM) at tdm_ipc.c:579
579             sem_b.sem_num = 0;
(gdb) s
580             sem_b.sem_op = -1;
(gdb) s
581             sem_b.sem_flg = SEM_UNDO;
(gdb) s
583             if (semop(TDM_M[mid].semid, &sem_b, 1) == -1)
(gdb) s
[Switching to Thread -1208480880 (LWP 4506)]

GetMyQue2Send (MyModule=RM, send_shm_ptr=0xb7f7ff54) at tdm_ipc.c:180
180             DMINT           TryTimes = SEND_TIMES;
(gdb) s
353             TDM_SEM_V( DstModule );
(gdb) s
tdm_sem_v (mid=RM) at tdm_ipc.c:597
597             sem_b.sem_num = 0;
(gdb) s
598             sem_b.sem_op = 1;
(gdb) s
599             sem_b.sem_flg = SEM_UNDO;
(gdb) s
601             if (semop(TDM_M[mid].semid, &sem_b, 1) == -1)
(gdb) s
606             return SUCC;
(gdb) s
607     }


(gdb) s
RM:4501: V operation on Semaphore .
SEND_MSG (SrcModule=51, DstModule=RM, msg_ptr=0xb7f7ff94, MsgLength=28) at tdm_ipc.c:368
368             printf("%s:%d: SEND_MSG: succeeded.\n",
(gdb) s
RM:4501: SEND_MSG: succeeded.
[Switching to Thread -1208269120 (LWP 4501)]
tdm_sem_p (mid=RM) at tdm_ipc.c:585
585                     printf("thread %u: errno = %d\n", (unsigned int)pthread_self(),errno);
(gdb) s
thread 3086698176: errno = 4
[Switching to Thread -1208480880 (LWP 4506)]



 main thread:

        ...
         while(1)
           {
                 if ((RetVal = WAIT_MSG( p1, &Msg )) !=SUCC)
                 {
                     switch ( RetVal )
                     {
                     ...
                      }
                  }
            }
     ------------------------------------
        thread1:
        ...
        send(src, dst, &msg, lenght);

        /* both SEND_MSG() and WAIT_MSG() have an operation P and V on semid by calling the following */

        DMINT tdm_sem_p( key_t semid )
        {
           struct sembuf sem_b;

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

           if (semop(semid, &sem_b, 1) == -1)
           {
              printf("thread %u: errno = %d\n", (unsigned int)pthread_self(),errno);

              return S_PFAIL;
           }

           return SUCC;
        }

        DMINT tdm_sem_v( key_t semid )
        {
           struct sembuf sem_b;

           sem_b.sem_num = 0;
           sem_b.sem_op = 1;
           sem_b.sem_flg = SEM_UNDO;

           if (semop(semid, &sem_b, 1) == -1)
           {
              return S_VFAIL;
           }

           return SUCC;
        }

        /* semid is init by the following */
        DMINT tdm_set_sem(key_t semid)
        {
           union semun sem_union;
           sem_union.val = 1;

           if (semctl(semid, 0, SETVAL, sem_union) == -1)
           {
              return FAILURE;
           }
           return SUCC;
        }

此问题有另一个链接,该链接可能有错误的问题描述。
P semaphore failed
谢谢。

最佳答案

errno 4确实是EINTR。当您得到那个错误时,这意味着您正在运行的系统调用(在本例中为semop)被一个信号中断。
在这种情况下,您负责重新启动系统调用。只有有限的一组系统调用会自动重新启动,并且只有在使用SA_RESTART标志设置信号处理程序时才会重新启动。请参阅signal(7)以了解有关“信号处理程序中断系统调用和库函数”部分的详细信息。您会注意到semop在从未重新启动的系统调用列表中,而不管信号处理程序的配置如何。
如何重新启动呼叫取决于您。其中一种方法是:

int rc;

while ((rc = semop(...)) == -1) {
  if (errno != EINTR) {
    break;
  } else {
    // decide whether to restart the call after interruption
    // or not
  }
}
// here, if rc == 0, semop worked, otherwise an error different from
// EINTR happened (or you decided not to restart)

你不知道是什么信号中断了一个给定的系统调用,除非你有一个信号处理程序。不过,gdb确实有options for signal handling所以你可以试着用它来找出答案。试着从“也许”开始。

关于c - semop()因errno 4而失败。semop()是否支持进程内线程的竞争?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9579158/

10-09 01:56