Чтобы найти максимальное количество семафоров, которые может открыть один процесс за один раз, я не понял, почему приведенный ниже код _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;
}
Любая помощь будет оценена.