C 查找一个进程一次可以打开的最大信号量数
为了找到一个进程一次可以打开的信号量的最大数量,我不明白为什么下面的代码C 查找一个进程一次可以打开的最大信号量数,c,linux,posix,semaphore,C,Linux,Posix,Semaphore,为了找到一个进程一次可以打开的信号量的最大数量,我不明白为什么下面的代码\u SC\u SEM\u NSEMS\u 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);
\u SC\u SEM\u NSEMS\u 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;
}
结构计数{
sem_t sem_addr;
整数计数;
};
内部主(空){
int fd=0,0=0;
结构计数*共享;
fd=shm_open(“/my_信号量”,O_RDWR | O_CREAT,0777);
如果(fd==-1){
佩罗尔(“shm_open”);
出口(0);
}
//ftruncate(fd,4096);
写入(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();
如果(pid>0){
//printf(“父进程:%d\n”,getpid());
sem_wait(&shared->sem_addr);
对于(int i=0;i<50;i++){
printf(“父项=%d\n”,共享->计数+);
}
sem\u post(&shared->sem\u addr);
}
否则如果(pid==0){
//printf(“子进程:%d\n”,getpid());
sem_wait(&shared->sem_addr);
对于(int i=0;i<50;i++){
printf(“child=%d\n”,shared->count++);
}
sem\u post(&shared->sem\u addr);
}
sem_销毁(共享->sem_添加);
返回0;
}
任何帮助都将不胜感激。也许你应该检查一下错误号
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.
也许你应该查一查,不
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.
从手册页面:
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可能表示您的系统不知道\u SC\u SEM\u NSEMS\u MAX
,也可能表示没有限制。无论哪种方式,我都将其解释为,开放信号量的最大数量不受系统的任意限制(当然,它可能受到内存约束等的限制)。来自手册页面:
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可能表示您的系统不知道\u SC\u SEM\u NSEMS\u MAX
,也可能表示没有限制。不管怎样,我将其解释为,开放信号量的最大数量不受系统的任意限制(当然,它可能受到内存约束等的限制)
返回由名称指定的限制值,
或–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;
}
#包括
#包括
#包括
#包括
艾尔诺;
int main()
{
errno=0;
最大长极限=0;
max\u limit=sysconf(\u SC\u SEM\u NSEMS\u max);
printf(“%ld\n”,最大限制);
printf(“错误消息:%m\n”);
返回0;
}
输出:-1
错误消息:成功 返回由名称指定的限制值, 或–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;
}
#包括
#包括
#包括
#包括
艾尔诺;
int main()
{
errno=0;
最大长极限=0;
max\u limit=sysconf(\u SC\u SEM\u NSEMS\u max);
printf(“%ld\n”,最大限制);
printf(“错误消息:%m\n”);
返回0;
}
输出:-1
err msg:Success在调用
sysconf
之前,将errno
设置为0,然后检查。@MatteoItalia是否有任何最大限制?我在调用sysconf
之前检查了ipcs-l
它没有说明一个进程在时间集errno
上可以拥有的最大信号量为0,之后检查它。@MatteoItalia有任何最大限制吗?我检查了ipcs-l
它没有说任何关于一个进程一次可以拥有的最大信号量的事情,谢谢你的解释-1可能表示您的系统不知道_SC\u SEM\u NSEMS\u MAX?你是说POSIX不支持它。谢谢你的解释-1可能表示您的系统不知道_SC\u SEM\u NSEMS\u MAX?你是说POSIX不支持它吗。