Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7590599
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T20:26:28+00:00 2026-05-30T20:26:28+00:00

I have two threads in a process. These two threads have a race to

  • 0

I have two threads in a process. These two threads have a race to a share memory which is attempting to be synchronized by semaphore. But I randomly got a failure with errno 4 when one threads is next to the other to call semop function. I did a little digging and found it looks to seem that calling was interrupted by system call.

EINTR While blocked in this system call, the process caught a signal; see signal(7). errno 4 is this one?

Please note lines 583 and 601.

which system call interrupted it? the function semop() itself? Any way to ignore this system call interrupting or recover/restart this function?

semop can be used in a multi-thread environment?

[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;
        }

this problem have a another link which may have a bad problem description.
P semaphore failed

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T20:26:29+00:00Added an answer on May 30, 2026 at 8:26 pm

    Errno 4 is indeed EINTR. When you get that error, it means the system call you were running (semop in this case) was interrupted by a signal.

    You’re responsible for restarting the system call in that case. Only a limited set of system calls restart automatically, and then only if the signal handler was set up using the SA_RESTART flag. See signal(7) for the details on that, “Interruption of System Calls and Library Functions by Signal Handlers” section. You’ll notice semop is in the list of system calls that is never restarted, regardless of the disposition of the signal handler.

    How you restart the call is up to you. One of the ways is to do something like:

    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)
    

    You don’t know what signal interrupted a given system call unless you have a handler for that signal. gdb does have options for signal handling though, so you could try and find out with that. Try handle all print to start with maybe.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a program (process) which needs to listen on 3 ports... Two are
I have the following situation: I have two threads thread1 , which is a
I have two methods, ProcessQueue and AddToQueue , which happen on different threads. Sometimes
I have a process where two threads are continuously executing sql queries on the
I have two threads, one needs to poll a bunch of separate static resources
I have two threads, one updating an int and one reading it. This is
I have two threads in an Android application, one is the view thread, and
I have two threads, a producer thread that places objects into a generic List
I have two threads referencing the same variable -- the UI thread and a
I have two threads, one thread processes a queue and the other thread adds

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.