segmentation_fault
i was actually new to this forum,so i had no idea about the rulez of this forum.few minutes ago i had asked the same qestion . i was actually worried that my code get indented when pasting it.i had asked the question which before pasting i think that got deleted in editing my question,as i was editing the question i was to ask so in doing i had perhaps deleted the question.by the way sorry for ur inconvenience .





my question is i am getting segmentation fault while running this code .i dont see any reason for  segmentation fault .can you plz point out any possible reason.
i have already got and intialized semaphores shared memory.thanks!






       #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;
      }
please point out the possible reason for the segmentation fault.thanks!
segmentation_fault
===

i was actually new to this forum,so i had no idea about the rulez of this forum.few minutes ago i had asked the same qestion . i was actually worried that my code get indented when pasting it.i had asked the question which before pasting i think that got deleted in editing my question,as i was editing the question i was to ask so in doing i had perhaps deleted the question.by the way sorry for ur inconvenience .





my question is i am getting segmentation fault while running this code .i dont see any reason for  segmentation fault .can you plz point out any possible reason.
i have already got and intialized semaphores shared memory.thanks!






       #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;
      }
please point out the possible reason for the segmentation fault.thanks!