【问题标题】:Semaphore error in Linux - :Invalid argument error no :22 (EINVAL)Linux 中的信号量错误 - :Invalid argument error no :22 (EINVAL)
【发布时间】:2015-11-09 18:26:31
【问题描述】:

我正在尝试在 linux 中执行 C 程序,但出现“P_Semaphore :Invalid argument error no :22”错误,有人可以帮忙

Int P_Semaphore(int i_sem_id)
{
 int i_code_returns;
 struct sembuf t_semop;
 #ifdef TRACE
 Debug(N_DEBUG_8, "P_Semaphore", "Entree, i_sem_id=%d<<<<<<\n", i_sem_id);
 #endif
 t_semop.sem_num = 0;
 t_semop.sem_op  = -1;
 t_semop.sem_flg = 0;
 do
 {
   i_code_retour = semop(i_sem_id, &t_semop, 0);

 } while ((i_code_returns == KO) && (errno == EINTR));

 if (i_code_retour == KO)
 {
   printf( "%s(%d):Error semaphore making attempt %d,errno = %d\n", M_HEADER, i_sem_id, errno);
  perror("P_Semaphore");
 }

【问题讨论】:

    标签: linux semaphore


    【解决方案1】:

    根据semop(2)的手册

       EINVAL The semaphore set doesn't exist, or semid is less than zero,
              or nsops has a nonpositive value.
    

    因此,在您的情况下,nsops 为零(非正数)。应该是一个。

    这是调用的第三个参数:

    int semop(int semid, struct sembuf *sops, size_t nsops);
    

    【讨论】:

    • 我查了,信号量集存在,也是正值
    • nsops 是 semop 调用的第三个参数。它是零,应该是 1。
    • 不,这不是我检查的问题,我在 printf 语句中遇到错误 " printf( "%s(%d):Error semaphore making attempt %d,errno = %d\n", M_HEADER, i_sem_id, errno); "
    • 请再看一遍。您的 semop 调用中的第三个参数是错误的。
    • 您应该在正确识别代码中的问题时投票并接受答案。然后其他有相同问题的人也可以从您的问题中受益,并且您可以在此过程中获得声誉积分。
    猜你喜欢
    • 2015-11-22
    • 2023-03-16
    • 2021-08-26
    • 2017-02-04
    • 2020-10-26
    • 1970-01-01
    • 2016-11-03
    • 2010-11-21
    • 2020-04-07
    相关资源
    最近更新 更多