信号量和共享内存

时间:2015-11-26 14:20:30

标签: c linux fork shared-memory semaphore

我尝试编写一个程序,其中包含由信号量同步的2个进程。父进程生成一个随机数并将其保存到共享内存。孩子读了这个号码。它只能读取父项是否已完成生成,而父项只有在孩子读取它时才能生成新数字。

我不知道我的程序是否正常运行,因为当我尝试访问data->random_num = random;data->random_num;时出现分段错误。

代码是:

#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <stdio.h>
#include <unistd.h>

#include <time.h>
#include <stdlib.h>

const key_t sem_key = (key_t)0x12345678;
const key_t shm_key = (key_t)0x12339678;

struct shared_data{
  int random_num;
};

union semun{
  int val; //setval value
};

int sem_wait(int semafor_id){
  struct sembuf sb;
  sb.sem_num = 0;
  sb.sem_op = -1;
  sb.sem_flg = SEM_UNDO;

  return semop(semafor_id,&sb,1);
}

int sem_signal(int semafor_id){
  struct sembuf sb;
  sb.sem_num = 0;
  sb.sem_op = 1;
  sb.sem_flg = SEM_UNDO;

  return semop(semafor_id,&sb,1);
}

int main(int argc,char* argv[]){

  int status;

  srand(time(NULL));
  //Create shared memory
  int memory_id = shmget(shm_key,sizeof(struct shared_data),0600|IPC_CREAT|IPC_EXCL);
  if(memory_id < 0){
    printf("Shared memory creating failed\n");
    return 1;
  }

  //Create semafor
  int semafor_id = semget(sem_key,10, 0600|IPC_CREAT|IPC_EXCL);
  if(semafor_id < 0){
    printf("SEMAFOR creating failed\n");
    return 1;
  }
  semctl(semafor_id,0,SETVAL,1); //init semafor

  pid_t pid = fork();
  if(pid < 0){
    printf("FORK FAILED\n");
  }
  else if(pid > 0){

    printf("PARENT\n");
    int i;
    if(i = sem_wait(semafor_id) < 0){
      printf("Failed wait parent\n");
      return 1;
    }else{
      int random = rand() % 80;
      printf("Generate random number: %d\n",random);
      //Try attach memory for writting     
      void* address = shmat(memory_id, NULL ,0);
      if(address == NULL){
        printf("Failed to atach memory\n");
        return 1;
      }

      struct shared_data* data = (struct shared_data*) address;
      data->random_num = random;

      if(shmdt(address) != 0){
        printf("Failed to detach shared memory\n");
      }

      sleep(2); 
    }

    if(i = sem_signal(semafor_id) < 0){
      printf("Failed signal parent\n");
    }else{
      printf("Parent leave generating number\n");
    }

    //wait for child
    wait(&status);
    printf("Destroy semafor\n");
    semctl(semafor_id,0, IPC_RMID,0);
    printf("Destroy shared memory\n");
    shmctl(memory_id,IPC_RMID, 0);

    return 0; 
  }
  else{
    printf("CHILD\n");
    int j;
    //try to get semafor
    if(j = sem_wait(semafor_id) < 0){
      printf("FAILED wait child\n");
    }else{
      printf("Child reading data\n");
      void* address = shmat(memory_id, NULL ,0);
      if(address == NULL){
        printf("Failed to atach memory in child\n");
        return 1;
      }

      struct shared_data* data = (struct shared_data*) address;
      printf("Child read data %d\n",data->random_num);
      sleep(1);

      if(shmdt(address) != 0){
        printf("Failed to detach shared memory in child\n");
      }
    }   

    if(j = sem_signal(semafor_id) < 0){
      printf("Failed signal child\n");
    }else{
      printf("Leave data reading\n");
    }
    return 0;
  }

  return 0;
}

1 个答案:

答案 0 :(得分:0)

void* address = shmat(memory_id, NULL ,0);

我在该行上遇到此错误(以及调用shmat的另一行):

warning: initialization makes pointer from integer without a cast

手册页说shmat返回一个void指针,这就是一个错误的线索。事实证明,添加此行(以获得缺少的原型)可以修复它:

#include <sys/shm.h>

编辑:上面取代之前的答案是红鲱鱼。

P.S。在测试时,它有助于从标志中删除IPC_EXCL,因此 如果你遇到崩溃,你就不必进行ipcs / ipcrm shuffle 不断地,在可以之前删除现有的段/信号量 创造新的。