日期:2014-05-16  浏览次数:20748 次

我的ubuntu 11.04 是不是不支持共享内存?
今天做共享内存实验,编译不通过,检查无果,求大侠解救。谢谢!!
源程序:
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "shm_com.h"
#include <sys/shm.h>

#include <sys/types.h>
#include <sys/ipc.h>
//#define BUF 1024;

int main()
{
  int running=1;
  void *shared_memory=(void *)0;//??
  struct shared_use_st *shared_stuff;
  int shmid;
  char buffer[BUFSIZ];//这个BUFSIZ 是多少???注:BUFSIZ宏在stdio.h中定义
 /*创建共享内存*/
 shmid=shmget((key_t)1234,sizeof(struct shared_use_st),0666|IPC_CREAT);
 if(shmid==-1)
  {
  fprintf(stderr,"shmget failed \n");
  exit(EXIT_FAILURE);
  }
  /*映射共享内存*/
 shared_memory=shmat(shmid,(void *)0,0);
 if(shared_memory==(void *)-1)
  {
  fprintf(stderr,"shmat failed \n");
  exit(EXIT_FAILURE);
  }
  printf("memory attached at %X \n",(int)shared_memory);
  /*让结构体指针指向这块内存*/
 shared_stuff=(struct shared_use_st *)shared_memory;
 /*控制读写顺序*/
 shared_stuff->written_by_you=0;
 /*循环从共享内存中读数据,直到读到“end"为止*/
 while(running)
{  
  if(shared_stuff->written_by_you==1)
  {
   
  sleep(1);//??读进程读了之后再写
  printf("waiting for client...\n");
   
  } 
  printf("enter some text:");
  fgets(buffer,BUFSIZ,stdin);
  strncpy(shared_stuff->some_text,buffer,TEXT_SZ);
  shared_stuff->written_by_you=0;
  if(strncmp(shared_stuff->some_text,"end",3)==0)  
  {
  running=0;//结束循环
  }
}
 
  /*删除共享内存*/
  if(shmdt(shared_memory)==-1)
  {
  fprintf(stderr,"shmdt failed \n");
  exit(EXIT_FAILURE);
  }
  exit(EXIT_SUCCESS);
}
编译:
qust@qust-K42JZ:~/test$ gcc shm2.c -o shm2
In file included from /usr/include/i386-linux-gnu/sys/ipc.h:29:0,
  from /usr/include/i386-linux-gnu/sys/shm.h:28,
  from shm2.c:6:
/usr/include/i386-linux-gnu/bits/ipctypes.h:31:9: 错误: expected ‘;’, identifier or ‘(’ before ‘unsigned’
这难道是系统里地文件出错了??/?我没动里面的内容啊!

------解决方案--------------------
9成的可能是#include "shm_com.h"这个文件有问题。查查。