我可以在以下程序中使用sem_open,但是我看到崩溃了吗?

时间:2014-01-26 05:06:33

标签: c++ linux posix semaphore

我正在尝试为共享内存的学生制作教程。但是,我在等待或发布信号量时看到崩溃。我正在使用sem_open API来初始化信号量。在做sem_open之前我需要分配一些内存吗?它看起来像是一个分段错误。你能帮帮我吗?

#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; 
}

这是输出。

$ ./reader_writer_a.out 
Sem_Post calling
Segmentation fault (core dumped)

在这里分析coredump。

(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

1 个答案:

答案 0 :(得分:2)

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()在失败时返回SEM_FAILED,否则它是一个地址。检查0是没有意义的。 如果失败,则sem_post()调用可能会使程序崩溃