gpt4 book ai didi

c - 我的信号量形式 semaphore.h 仅在 CTRL-Z fg 之后工作

转载 作者:行者123 更新时间:2023-11-30 15:15:44 24 4
gpt4 key购买 nike

我有一个程序,它在一个进程中准备一些配置,然后在父进程中读取这些配置。为了同步它们,我使用 semaphore.h 库中的信号量。但即使在我 sem_post 之后,它似乎也在 sem_wait 中永远等待。不过,在我执行 ctrl-z 和 fg 之后它就可以工作了。这是为什么?谁能告诉我我的代码有什么问题吗?我的操作系统是 Lubuntu

信号量1.h

#ifndef _DNSS_H_
#define _DNSS_H_

#include <pthread.h>
#include <semaphore.h>
#include <time.h>
#include <sys/types.h>

typedef struct configs
{
int i;
sem_t sem;
} CONFIGS;


void init_config(CONFIGS *_configs);
//initiates the threadpool
int init_thread_pool(CONFIGS *configs);

#endif

Semaphore_1.c

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <string.h>
#include <sys/shm.h>
#include <sys/ipc.h>
#include <sys/types.h>
#include <semaphore.h>
#include <pthread.h>
#include "semaphore1.h"



void init_config(CONFIGS *_configs)
{
sem_init(&(_configs->sem),1,0); //Creaates a semaphore that is opened when the configs are read to shared memory

_configs->i=2;

fprintf(stderr, "Result of sem_post:%d\n", sem_post(&(_configs->sem)));
}

Semaphore_2.c

#include"semaphore1.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>


int init_thread_pool( CONFIGS *configs)
{
int aux;

fprintf(stderr, "Value of sem_wait():%d\n", sem_wait(&(configs->sem)));

printf("Threadpool initiated with %d threads!", configs->i);

return 1;
}

Semaphore_main.c

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/types.h>
#include "semaphore1.h"
#include <sys/ipc.h>
#include <sys/shm.h>

int main(int argc, char *argv[])
{
pid_t config_pid; //will hold the configuration process id
int _shmid_configs;
CONFIGS *_configs;

_shmid_configs =shmget( IPC_PRIVATE,
sizeof(CONFIGS), IPC_CREAT|0666); //initializes the shared memory

if( _shmid_configs == -1)
{
perror("Error creating shared memory");
}
_configs=shmat(_shmid_configs, NULL,0); //maps the shared memory created to the processp and the config structure
if( _configs == ( CONFIGS*)-1)
{
perror("Error at shmat");
}

//initialization of the processes
config_pid = fork();
if( config_pid < 0)
{
perror("Failed creating configuration manager process");
}
else if( config_pid == 0)
{
init_config(_configs);
printf("Im config!\n");
return 0;
}

//CODE FOR THE gestor de pedidos
printf("right before the threadpool! Configs has a lmit of %d theads\n", _configs->i);
init_thread_pool(_configs);
printf("im parent and im out\n");
sem_destroy(&_configs->sem);
return 0;
}

编译为gcc -g -pthread Semaphore_2.c Semaphore_main.c Semaphore_1.c -o deb

输出:

./deb
right before the threadpool! Configs has a lmit of 0 theads
Result of sem_post:0
Im config!
^Z
[1]+ Stopped ./deb
fg
./deb
Value of sem_wait():0
Threadpool initiated with 2 threads!im parent and im out

最佳答案

sem_init() 应该 fork() 之前调用。

在您当前的代码中,父线程中的 init_thread_pool(_configs); 可以 init_config() 之前被调用,即您将等待未初始化的信号量。这是未定义的行为

关于c - 我的信号量形式 semaphore.h 仅在 CTRL-Z fg 之后工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33419527/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com