查找一个进程一次可以打开的最大信号量数

时间:2018-04-30 18:41:41

标签: c linux posix semaphore

要查找一个进程一次可以打开的最大信号量数量,我无法理解为什么下面的代码_SC_SEM_NSEMS_MAX会返回-1

int main(void) {
        long max_limit = 0;
        errno = EINVAL;
        max_limit = sysconf(_SC_SEM_NSEMS_MAX);
        printf("max_limit : %ld error_no : %d\n",max_limit,errno);

        return 0;
} 

编辑: - 以下是我尝试手动获取最大限额的内容。

struct count {
        sem_t sem_addr;
        int count;
};
int main(void) {
        int fd = 0,zero = 0;
        struct count *shared;
        fd = shm_open("/my_semaphore",O_RDWR|O_CREAT,0777);
        if(fd == -1){
                perror("shm_open");
                exit(0);
        }
        //ftruncate(fd,4096);
        write(fd,&zero,4);
        shared = mmap(NULL,4096,PROT_READ|PROT_WRITE,MAP_SHARED,fd,(off_t)0);
        sem_init(&shared->sem_addr,1,1);

        pid_t pid = fork();
        if(pid > 0) {
                //printf("parent process: %d \n",getpid());
                sem_wait(&shared->sem_addr);
                for(int i = 0;i < 50 ;i++) {
                        printf("parent = %d \n",shared->count++);
                }
                sem_post(&shared->sem_addr);
        }
        else if (pid == 0) {
                //printf("child process: %d \n",getpid());
                sem_wait(&shared->sem_addr);
                for(int i = 0;i < 50 ;i++) {
                        printf("child = %d \n",shared->count++);
                }
                sem_post(&shared->sem_addr);
        }
        sem_destroy(&shared->sem_addr);
        return 0;
}

任何帮助将不胜感激。

3 个答案:

答案 0 :(得分:2)

从手册页:

RETURN VALUE
       If name is invalid, -1 is returned, and errno is set to EINVAL.  Other‐
       wise, the value returned is the value of the system resource and errno
       is  not  changed.  In the case of options, a positive value is returned
       if a queried option is available, and -1 if it is not.  In the case  of
       limits, -1 means that there is no definite limit.

特别注意最后一句。因此,-1可能意味着您的系统不知道_SC_SEM_NSEMS_MAX或者没有限制。无论哪种方式,我都在解释这意味着系统不会任意限制开放信号量的最大数量(当然它可能受到内存限制等的限制)。

答案 1 :(得分:1)

long sysconf(int name );

返回name指定的限制值, 如果limit是不确定的或发生错误,则返回-1。

如果无法确定限制,sysconf()将返回-1。如果发生错误,它也可能返回-1。 (唯一指定的错误是EINVAL,表示名称无效。)为了区分不确定限制和错误的情况,我们必须在调用之前将errno设置为零;如果调用返回-1并且在调用后设置了errno,则发生错误,否则限制是不确定的。

在您的情况下,不是错误,而是无法确定限制。

#include <stdio.h>
#include <errno.h>
#include <limits.h>
#include <unistd.h>
extern errno ;
int main()
{
    errno = 0;
    long max_limit = 0;
    max_limit = sysconf(_SC_SEM_NSEMS_MAX);
    printf("%ld\n", max_limit);
    printf("err msg: %m\n");
    return 0;
}  

<强>输出:
    -1
    错误消息:成功

答案 2 :(得分:0)

也许你应该检查errno?

   The return value of sysconf() is one of the following:

   *  On error, -1 is returned and errno is set to indicate the cause of
      the error (for example, EINVAL, indicating that name is invalid).

   *  If name corresponds to a maximum or minimum limit, and that limit
      is indeterminate, -1 is returned and errno is not changed.  (To
      distinguish an indeterminate limit from an error, set errno to
      zero before the call, and then check whether errno is nonzero when
      -1 is returned.)

   *  If name corresponds to an option, a positive value is returned if
      the option is supported, and -1 is returned if the option is not
      supported.
相关问题