【问题标题】:Use variable from shared memory in function在函数中使用共享内存中的变量
【发布时间】:2022-10-13 15:15:17
【问题描述】:

我正在尝试在我的 main 之外的函数中访问我的结构共享内存,但我不断得到 : use of undeclared identifier 'shr' in sem_wait(&shr->empty); 编译时出现错误。我想,因为我的结构在一个全局变量中,并且也在使用 POSIX 的共享内存中,所以我可以在主函数之外访问它。有人可以帮我理解为什么它不起作用。谢谢!


#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/wait.h>
#include <sys/mman.h>
#include <semaphore.h>
#include <sys/stat.h>
#include <pthread.h> 
#include <stdbool.h>

//clang -Wall  -lpthread -lrt 


/* The program should parse three integer arguments from the command line 

# buffer size 20,  avg producer sleep 400 ms, avg consumer sleep 1200 ms */

//function declarations
void* producer(void*);
void* consumer(void*);

typedef struct {
    sem_t mtx;
    sem_t empty;
    sem_t full;
    int in;
    int out;
    int psTime;
    int csTime;
    int buff[];
    
} shared_info;
 
int main (int argc, char*argv[]) {
    shared_info *shr;
    //initializing threads
    pthread_t pro, con;

    int shmId;
    char shmName[50];
    sprintf(shmName, "swap-%d", getuid());
    shmId = shm_open (shmName, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR);
    ftruncate(shmId, sizeof(shared_info));
    shr = mmap(NULL, sizeof(shared_info), PROT_READ|PROT_WRITE, MAP_SHARED, shmId, 0);

    if( argc != 4 ){
        fprintf(stderr, "incorrect amount of arguments\n"); //It prints an error message (to stderr) when it's the incorrect number of arguments.
        exit(1);
    } 

    //initializing producer and consumer input output count;
    shr->in = 0;
    shr->out = 0;

    //grabbing input from command line arguments
    int buffSize = strtol(argv[1], NULL, 10);
    shr->psTime = strtol(argv[2], NULL, 10);
    shr->csTime = strtol(argv[3], NULL, 10);
    shr->buff[buffSize];
    printf("buff: %d psTime:%d csTime: %d", buffSize,shr->psTime,shr->csTime);

    sem_init(&shr->mtx, 0, 1);
    sem_init(&shr->full, 0, 0);
    sem_init(&shr->empty, 0, buffSize);

    pthread_create(&pro, NULL, producer, (void *)&pro);
    sleep(1);
    pthread_create(&con, NULL, consumer, (void *)&con);

    pthread_join(pro, NULL);
    pthread_join(con, NULL);

    printf("Main thread done.\n");
    sem_destroy(&shr->mtx);
    sem_destroy(&shr->empty);
    sem_destroy(&shr->full);

    munmap (shr->buff, sizeof(shared_info));
    shm_unlink(shmName);
    return 0;
}

//producer thread 
void *producer(void * arg){
    int i;
    while( true){
     
        sem_wait(&shr->empty);
        sem_wait(&shr->mtx);
        //produces" random integers between 1000-900
        int rand1 = rand()%101+900;
        printf("%d\n", rand1);
  }
  
}

//Consumer thread
void *consumer(void * arg){
 
} 

【问题讨论】:

  • 它在 producer() 函数范围内不可见。
  • shared_info *shr; 移动到全局范围:static shared_info *shr;main 之前,或将其传递给函数(producerconsumer)。另外,你想在这里做什么shr-&gt;buff[buffSize];?编译时出现警告:gcc -Wall -Wextra -Wpedantic -o program program.c -pthread
  • 我知道,但我不知道如何解决它
  • @DavidRanieri 谢谢!我试图让用户输入缓冲区大小作为命令行参数,然后它将结构中的缓冲区数组设置为用户输入的大小
  • @DavidRanieri 我使用 clang -Wall -lpthread -lrt filename.c 编译

标签: c multithreading posix producer-consumer


【解决方案1】:

只需将 shr 作为参数传递给生产者线程。

pthread_create(&pro, NULL, producer, shr);

// in producer
sem_wait(&arg->mtx);

【讨论】:

  • 等待如果使用 &arg-> 更改变量,它也会在共享内存中更改它吗?就像它可以更改结构中的变量还是仅更改函数中的变量?我希望它也改变共享内存结构中的内存
猜你喜欢
  • 1970-01-01
  • 2014-04-20
  • 1970-01-01
  • 2013-01-30
  • 1970-01-01
  • 1970-01-01
  • 2014-07-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多