我正在尝试通过共享内存将文本行从编写器程序发送到读取器程序。作家作品完美。但是,在执行阅读器程序的过程中,我在以下一行遇到了分段错误
cout<<buffer->str[initial]<<endl;
当我在注释上述行的同时与读取器同时运行读取器时,共享结构的计数值的更改是可见的并正确增加了。因此,根据我的诊断,问题在于访问共享结构的字符串数组。为什么会这样?
代码由writer.cpp和reader.cpp组成,下面提供了这些代码以供参考。
writer.cpp的目的是从输入文件中读取行,然后将其写入共享内存中。
Writer.cpp
// for c++ related functions
#include<iostream>
#include<fstream>
#include<string>
// for c related functions
#include<cstdio>
#include<cstdlib>
#include<unistd.h>
// for system calls
#include<sys/types.h>
#include<sys/ipc.h>
// for shared memory
#include<sys/shm.h>
using namespace std;
// store the key for accessing the shared memory
key_t shm_key = ftok("practical",65);
// the structure for shared memory
struct shared{
bool the_end = false;
int count = 0;
string str[6];
};
int main(){
// create the shared memory
int shmid = shmget(shm_key,sizeof(shared),0666|IPC_CREAT);
if(shmid == -1){
perror("Error in creating shared memory");
return 1;
}
// attach server to the shared memory
struct shared *buffer = (shared*)shmat(shmid,NULL,0);
if(buffer == (void *) -1){
perror("Unable to attach to shared memory");
return 1;
}
// open the file for reading the input
ifstream fin("input.txt");
// check for errors in opening the file
if(!fin){
cerr<<"Error in opening the file"<<endl;
exit(1);
}
// to store the read line
string inptxt;
cout<<"Reading contents from file\n"<<endl;
while(fin){
// read a line
getline(fin,inptxt);
// exit the loop if string is empty
if(inptxt == "\0") break;
// store the line in the buffer
buffer->str[buffer->count++] = inptxt;
cout<<"This was written : "<<buffer->str[buffer->count-1]<<endl;
sleep(3);
}
cout<<"Closing the file"<<endl;
fin.close();
// marks the end of writing into shared memory
buffer->the_end = true;
if(shmdt(buffer) == -1){
perror("error in deatching from shared memory");
return 1;
}
if(shmctl(shmid,IPC_RMID,0) == -1){
perror("error in destroying the shared memory");
return 1;
}
cout<<"Writing Completed"<<endl;
return 0;
}
读取器程序的目的是从共享内存中读取行,然后在控制台上显示它们。
Reader.cpp
// for c++ related functions
#include<iostream>
#include<fstream>
#include<string>
// for c related functions
#include<cstdio>
#include<cstdlib>
#include<unistd.h>
// for system calls
#include<sys/types.h>
#include<sys/ipc.h>
// for shared memory
#include<sys/shm.h>
using namespace std;
// store the key for accessing the shared memory
key_t shm_key = ftok("practical",65);
// the structure for shared memory
struct shared{
bool the_end = false;
int count = 0;
string str[6];
};
int main(){
// create the shared memory
int shmid = shmget(shm_key,sizeof(shared),0666|IPC_CREAT);
if(shmid == -1){
perror("Error in creating shared memory");
return 1;
}
// attach server to the shared memory
struct shared *buffer = (shared*)shmat(shmid,NULL,0);
if(buffer == (void *) -1){
perror("Unable to attach to shared memory");
return 1;
}
cout<<"Reading contents from shared memory\n"<<endl;
int initial = 0;
while(!buffer->the_end){
if(buffer->count > initial){
cout<<"initial : "<<initial<<" buffer count : "<<buffer->count<<"\n";
// the following line gives segmentation fault
// cout<<buffer->str[initial]<<endl;
initial++;
}
}
if(shmdt(buffer) == -1){
perror("error in deatching from shared memory");
return 1;
}
cout<<"Reading Completed"<<endl;
return 0;
}