为什么我的这段代码会出现分段错误?

发布于 2024-07-17 09:12:20 字数 1792 浏览 7 评论 0原文

我在运行此代码时遇到分段错误。 我不明白为什么会发生这种情况 - 有人能看出可能的原因吗? (我已经获取并初始化了信号量的共享内存。)

My code:
   #include<stdlib.h>
   #include<sys/types.h>
   #include<sys/shm.h>
   #include<sys/ipc.h>
   #include<stdio.h>
   #include<sys/sem.h>

   union semun 
   {
   int val;
   struct semid_ds *buf;
   unsigned short *array;
   } arg;



  int main()
  {
  key_t semkey;
  int shmid,semid,nsem,sops;
  struct sembuf buf[1];

  char *ptrr,*shm,c,*s;

  semkey=ftok("/home/mawia/abc.c",'a');

  printf("entered into main of producer\n");
  if(semkey<0)
  {
   perror("ftok");
   exit(1);
  }

  shmid=shmget(semkey,30,0777);

  if(shmid<0)
  {
   printf("error");
   perror("shmget");
   exit(1);
  }

  shm=shmat(shmid,0,0);
  if(shm==(char *) -1)
  {
  perror("shm");
  exit(1);
  } 

  s=shm;
  semid=semget(semkey,1,0777);
 if(semid<0)
 {
  printf("error");
  perror("semget");
  exit(0);
 }

 ptrr=shm+1;
 *s='w';
 printf("going to check the value 0th semaphores\n");
 buf[0].sem_num=0;
 buf[0].sem_op=0;
  buf[0].sem_flg=0;
 buf[1].sem_num=0;
 buf[1].sem_op=1;
 buf[1].sem_flg=0;
  printf("entered the critical region\n");
 //printf("waiting to enter the buffer zone...");
 semop(semid,buf,2);

 printf("entered the critical region\v");
 if(*s!='r')
 {
  printf("\nPRODUCER IS PRODUCING\n\n\n");

  printf("ENTER DATA\n");

  while((c=getchar())!='\n')
  {
    *ptrr++=c;
  }
  *ptrr='\0';
  *s='r';
 } 

  else 
  printf("RESOURCE IS FULL:CAN'T PRODUCE");

 //printf("produced enough for  the consumer \nexiting from the buffer area now...");
 buf[0].sem_num=0;
 buf[0].sem_op=-1;
 buf[0].sem_flg=0;

 semop(semid,buf,1);

 ptrr=shm+1;

  if(!strcmp(ptrr,"exit"))
  {
  printf("exiting...");
  exit(0);
  }
  sleep(1);

 return 0;
  }

I am getting a segmentation fault while running this code. I can't work out why this is happening - can anyone see a possible reason? (I have already got and initialized the semaphore's shared memory.)

My code:

   #include<stdlib.h>
   #include<sys/types.h>
   #include<sys/shm.h>
   #include<sys/ipc.h>
   #include<stdio.h>
   #include<sys/sem.h>

   union semun 
   {
   int val;
   struct semid_ds *buf;
   unsigned short *array;
   } arg;



  int main()
  {
  key_t semkey;
  int shmid,semid,nsem,sops;
  struct sembuf buf[1];

  char *ptrr,*shm,c,*s;

  semkey=ftok("/home/mawia/abc.c",'a');

  printf("entered into main of producer\n");
  if(semkey<0)
  {
   perror("ftok");
   exit(1);
  }

  shmid=shmget(semkey,30,0777);

  if(shmid<0)
  {
   printf("error");
   perror("shmget");
   exit(1);
  }

  shm=shmat(shmid,0,0);
  if(shm==(char *) -1)
  {
  perror("shm");
  exit(1);
  } 

  s=shm;
  semid=semget(semkey,1,0777);
 if(semid<0)
 {
  printf("error");
  perror("semget");
  exit(0);
 }

 ptrr=shm+1;
 *s='w';
 printf("going to check the value 0th semaphores\n");
 buf[0].sem_num=0;
 buf[0].sem_op=0;
  buf[0].sem_flg=0;
 buf[1].sem_num=0;
 buf[1].sem_op=1;
 buf[1].sem_flg=0;
  printf("entered the critical region\n");
 //printf("waiting to enter the buffer zone...");
 semop(semid,buf,2);

 printf("entered the critical region\v");
 if(*s!='r')
 {
  printf("\nPRODUCER IS PRODUCING\n\n\n");

  printf("ENTER DATA\n");

  while((c=getchar())!='\n')
  {
    *ptrr++=c;
  }
  *ptrr='\0';
  *s='r';
 } 

  else 
  printf("RESOURCE IS FULL:CAN'T PRODUCE");

 //printf("produced enough for  the consumer \nexiting from the buffer area now...");
 buf[0].sem_num=0;
 buf[0].sem_op=-1;
 buf[0].sem_flg=0;

 semop(semid,buf,1);

 ptrr=shm+1;

  if(!strcmp(ptrr,"exit"))
  {
  printf("exiting...");
  exit(0);
  }
  sleep(1);

 return 0;
  }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

落日海湾 2024-07-24 09:12:20

快速浏览(非常快)后,我想说这可能是由于

struct sembuf buf[1];

/*some other code*/

buf[1].sem_num=0;
buf[1].sem_op=1;
buf[1].sem_flg=0;

您正在访问缓冲区之外的内存引起的。 buf[1] 在堆栈中仅为一个 struct sembuf 保留内存,您尝试使用 2。在这种情况下,您应该使用

 struct sembuf buf[2]

After a quick glance (very quick), i would say that it MAY be caused by

struct sembuf buf[1];

/*some other code*/

buf[1].sem_num=0;
buf[1].sem_op=1;
buf[1].sem_flg=0;

You are accessing memory outside of the buffer. buf[1] reserves memory in the stack for only one struct sembuf, you are trying to use 2. In that case, you should use

 struct sembuf buf[2]
爱给你人给你 2024-07-24 09:12:20

啊...当你声明时显然有一些非常错误的东西

struct sembuf buf[1];

,但几行之后就出现了

buf[1].sem_num=0;
buf[1].sem_op=1;
buf[1].sem_flg=0;

Ah... there is obviously something very wrong when you declare

struct sembuf buf[1];

but a few lines later do

buf[1].sem_num=0;
buf[1].sem_op=1;
buf[1].sem_flg=0;
誰認得朕 2024-07-24 09:12:20

数组分配太小。

这个例子通常太长,不能被认为是一个好的例子; 尝试找到一个更小的(最小是理想的)情况来复制错误,特别是依赖尽可能少的外部库的情况。 另外,请尝试在调试器中运行并在询问之前单步执行代码。

Array allocation too small.

This example is generally too long to be considered a good example; try to find a smaller (minimal is ideal) case which replicates the error, particularly one which depends on as few external libraries as possible. Also, try running in the debugger and stepping through the code before asking.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文