0
我是使用共享內存的初學者,並且實現了一個並行加法器,其中每個k處理器都在其中實現作爲一個孩子的過程。具體來說,給定一組n個整數和k的值,主程序創建k個子進程,爲每個子進程分配計算其分配給n/k個數的上限的總和,等待每個k的小計子流程,總計小計,並打印每個小計的結果以及總計。我沒有使用線程。爲什麼我在共享內存中編譯C代碼時遇到「未定義的引用'sem_init'」錯誤
此程序是爲在大學任命而創建的,他們預計可在任何部門計算機上運行。
此代碼在Kali Linux上正確編譯,但無法在其他Linux版本上編譯和運行。 當我試圖在Ubuntu編譯它給出了錯誤說
未定義參考「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 – amdixon
非常感謝評論.... – Sanke