I am trying to make a tutorial for my students on Shared Memory. However, I see a crash while waiting or posting a semaphore. I am using sem_open API to initialize the semaphore. Do I need to allocate some memory before doing sem_open? It looks to me a segmentation fault. Can you please help me on it?
#include <iostream>
#include <sys/ipc.h>
#include <sys/stat.h>
#include <sys/shm.h>
#include <string.h>
/** page size **/
#include <unistd.h>
/** memcpy **/
#include <stdio.h>
#include <semaphore.h>
#include <fcntl.h>
using namespace std;
class READER_WRITER_A
{
private:
string msg;
void *shm_ptr;
int shm_id;
sem_t *sem_rw;
void Init_Shm();
void Init_Sem();
public:
void Read_Msg();
void Write_Msg(string s1);
READER_WRITER_A();
};
void READER_WRITER_A::Init_Shm()
{
key_t key;
key = ftok("/home/joshis1/Downloads/IPC-BLOG/shm",1);
shm_id = shmget(key, getpagesize(), IPC_CREAT| 0660);
shm_ptr = shmat(shm_id, NULL, 0660);
}
void READER_WRITER_A::Init_Sem()
{
sem_rw = sem_open("/home/joshis1/Downloads/IPC-BLOG/rw_sem", O_RDWR | O_CREAT, 0666, 0);
cout<<"Sem_Post calling"<<endl;
if( sem_rw < 0 )
cout<<"Error in opening the sem_rw"<<endl;
sem_post(sem_rw);
cout<<"Sem_Post done"<<endl;
}
void READER_WRITER_A::Read_Msg()
{
//sem_wait(sem_rw);
int *size = (int *)shm_ptr;
void *msg_ptr = shm_ptr;
msg_ptr+=sizeof(int);
cout<<"Message size is = "<<*size<<endl;
memcpy(&msg,(string *)msg_ptr,*size);
cout<<"Reading Message-- "<<msg<<endl;
//sem_post(sem_rw);
}
void READER_WRITER_A::Write_Msg(string s1)
{
//sem_wait(sem_rw);
void *msg_ptr = shm_ptr;
msg = s1;
int *pt = (int*)shm_ptr;
*pt = s1.size();
cout<<"Writing Message size = "<<*pt<<endl;
msg_ptr+=sizeof(int);
cout<<"Writing Message-- "<<s1<<endl;
memcpy((string *)msg_ptr,&msg,s1.size());
//sem_post(sem_rw);
}
READER_WRITER_A::READER_WRITER_A()
{
Init_Shm();
Init_Sem();
}
int main( int argc, char *argv[])
{
READER_WRITER_A val;
val.Write_Msg("I am A-RW");
val.Read_Msg();
return 0;
}
Here is the output.
$ ./reader_writer_a.out
Sem_Post calling
Segmentation fault (core dumped)
Analyzing coredump here.
(gdb) bt
#0 0x0000003fb060d790 in sem_post () from /lib64/libpthread.so.0
#1 0x0000000000400fd0 in READER_WRITER_A::Init_Sem (this=0x7fff50e322a0) at reader_writer_a.cpp:52
#2 0x0000000000401199 in READER_WRITER_A::READER_WRITER_A (this=0x7fff50e322a0) at reader_writer_a.cpp:86
#3 0x00000000004011d8 in main (argc=1, argv=0x7fff50e323c8) at reader_writer_a.cpp:92
void READER_WRITER_A::Init_Sem()
{
sem_rw = sem_open("/home/joshis1/Downloads/IPC-BLOG/rw_sem", O_RDWR | O_CREAT, 0666, 0);
cout<<"Sem_Post calling"<<endl;
if( sem_rw < 0 )
cout<<"Error in opening the sem_rw"<<endl;
sem_post(sem_rw);
cout<<"Sem_Post done"<<endl;
}
sem_open() returns SEM_FAILED on failure and otherwise it's an address. It doesn't make sense to check it against 0. If it failed, your sem_post() call might crash the program
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.