gpt4 book ai didi

c - 为什么使用共享内存编译 C 代码时会出现错误 "undefined reference to ' sem_init' "

转载 作者:太空宇宙 更新时间:2023-11-04 08:27:40 25 4
gpt4 key购买 nike

我是共享内存的初学者,我实现了一个并行加法器,其中 k 个处理器中的每一个都作为子进程实现。具体来说,给定一组 n 个整数和 k 的值,主程序创建 k 个子进程,分配每个子进程计算其分配的 n/k 个数字上限的总和,等待 k 个中每个子进程的小计子进程,汇总小计,并打印每个小计的结果以及总计。我没有使用线程。

该程序是为学院的作业而创建的,预计可以在任何系的计算机上运行。

此代码在 Kali Linux 上正确编译,但我无法在其他 Linux 版本上编译和运行。当我试图在 Ubuntu 上编译时,它给出了错误信息

undefined reference to 'sem_init'

我在编译行中使用了-lrt。请帮我解决这个问题。

这是我创建的代码。

#include<stdio.h>
#include<sys/ipc.h>
#include<sys/wait.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<fcntl.h>
#include<errno.h>
#include<semaphore.h>
#include<unistd.h>
#include<math.h>
#include<stdlib.h>

#define BUFFER_SIZE 100
#define BUFFER_SUB 2

typedef struct
{
int bufMax;
int datalimit;
int buff[BUFFER_SIZE];
sem_t mutex, empty, full;

} shared_inputs;
typedef struct
{
int sub[BUFFER_SUB];
sem_t mutex,empty,full;
}sub_tot;

int main(int argc, char *argv[])
{
int x = 0;
int data,count,i,j,tot;
int n;
int k =atoi(argv[2]);
int assign_size;
count = tot =0;




int segment_id;
size_t segment_size = sizeof(shared_inputs);
segment_id = shmget(IPC_PRIVATE,segment_size,IPC_CREAT|0666);
shared_inputs *shared_memory = shmat(segment_id,NULL,0);

sem_init(&shared_memory->mutex,1,1);
sem_init(&shared_memory->empty,1,BUFFER_SIZE);
sem_init(&shared_memory->full,1,0);



int segment_idSubs;
size_t segment_size1 = sizeof(sub_tot);
segment_idSubs = shmget(IPC_PRIVATE,segment_size1,IPC_CREAT|0666);
sub_tot *subtotal = shmat(segment_idSubs,NULL,0);

sem_init(&subtotal->mutex,1,1);
sem_init(&subtotal->empty,1,BUFFER_SUB);
sem_init(&subtotal->full,1,0);

FILE *numFile;
numFile = fopen(argv[1], "r");



while(!feof(numFile))
{
fscanf(numFile,"%d",&data);
sem_wait(&shared_memory->empty);
sem_wait(&shared_memory->mutex);
shared_memory->buff[x] = data;
sem_post(&shared_memory->mutex);
sem_post(&shared_memory->full);
printf("%d ", shared_memory->buff[x]);
x++;
n = x;

}
assign_size = ceil((double)n/(double)k);
printf("\n");
shared_memory->datalimit = 0;
shared_memory->bufMax = n-1;
printf("assigned size : %d \n", assign_size);
printf("n : %d , k : %d \n",n,k);


for(i =0; i < k; i++)
{
int id;
int subt = 0;

id = fork();
if(id < 0) // error in fork
{
perror("Error in fork ");
exit(300);
}
else if(id == 0)//the new child process
{


for(j=0;j< assign_size; j++)//getting items from the shared memory
{

sem_wait(&shared_memory->full);
sem_wait(&shared_memory->mutex);
int num = shared_memory->buff[shared_memory->datalimit];
//printf("%d \n",shared_memory->buff[shared_memory->datalimit]);
shared_memory->datalimit++;
sem_post(&shared_memory->mutex);
sem_post(&shared_memory->empty);
subt = subt + num;
if(shared_memory->datalimit == shared_memory->bufMax)
{
break;
}

}

int pid = getpid();
sem_wait(&subtotal->empty);
sem_wait(&subtotal->mutex);
subtotal->sub[0] = pid;
subtotal->sub[1] = subt;
sem_post(&subtotal->mutex);
sem_post(&subtotal->full);
printf("Sub-total produced by Processor with ID %d: %d \n",pid,subt);

exit(0);
}
else//parent process
{
int status;
wait(&status);
sem_wait(&subtotal->full);
sem_wait(&subtotal->mutex);
int sub = subtotal->sub[1];
sem_post(&subtotal->mutex);
sem_post(&subtotal->empty);
tot = tot+sub;
}
}
printf("Total: %d \n",tot);


return 0;

}

最佳答案

编译时需要加上-lpthread,同时使用-lm和-lrt。

关于c - 为什么使用共享内存编译 C 代码时会出现错误 "undefined reference to ' sem_init' ",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29714692/

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