i can't find why i m getting segmentation error

This is a discussion on i can't find why i m getting segmentation error within the Linux Programming forums, part of the Platform Specific Boards category; hey i want to increment a shared counter using shared memory and semaphores it is actually the code of richard ...

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    14

    i can't find why i m getting segmentation error

    hey i want to increment a shared counter using shared memory and semaphores it is actually the code of richard stevens unp v2
    Code:
    #include <sys/mman.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <stdio.h>
    #include <unistd.h>
    #include <sys/fcntl.h>
    #include <semaphore.h>
    
    #define DEBUG 1
    #define SHM_MODE (S_IRUSR,S_IWUSR,S_IRGRP,S_IROTH)
    #define SHM_FLAGS (O_RDWR,O_CREAT,O_EXCL)
    struct shmstruct
    {
    	int count;
    };//the structure used to share information in the shared memory
    
    sem_t* mutex;
    
    int main(int argc,char** argv)
    {
    	int fd,i,nloop;
    	pid_t pid;
    	struct shmstruct *ptr;
    	
    	if(argc!=4)
    	{
    		fprintf(stderr,"usage: shmsem_read <arg1=shmname> <arg2=semname> <arg3=#loops>");
    		}
    		nloop=atoi(argv[3]);
    	fd=shm_open(argv[1],O_RDWR,SHM_MODE);
    	ptr=mmap(NULL,sizeof(struct shmstruct),PROT_READ|PROT_WRITE,MAP_SHARED,fd,0);
    	
    	close(fd);
    	
    		
    	mutex=sem_open(argv[2],1);
    	if(mutex==-1)
    	{
    		perror("sem_open");
    		}
    	pid=getpid();
    	
    	for(i=0;i<nloop;i++)
    	{
    		sem_wait(mutex);
    		if(DEBUG)
    		{
    			fprintf(stdout,"i m here for the %dth time t\n",i);
    		}
    		fprintf(stdout,"PId 	%ld:%d",(long)pid,ptr->count++);
    		sem_post(mutex);
    		}
    		
    	exit(0);
    	}
    gdb says that i m getting segmentation error from the sem_wait function i can't sort out why
    /*****
    One can't let go and still win
    ******/

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>if (argc != 4)
    You should exit the program if you don't have the correct number of args.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Jun 2003
    Posts
    14
    i forgot the exit statement in the if check for the number of arguments but this doesnot give segmentation error that is for sure
    /*****
    One can't let go and still win
    ******/

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how do you resolve this error?
    By -EquinoX- in forum C Programming
    Replies: 32
    Last Post: 11-05-2008, 03:35 PM
  2. DX - CreateDevice - D3DERR_INVALIDCALL
    By Tonto in forum Game Programming
    Replies: 3
    Last Post: 12-01-2006, 06:17 PM
  3. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 09:33 AM
  4. Why wont my function exit correctly?
    By LightsOut06 in forum C Programming
    Replies: 2
    Last Post: 10-09-2005, 09:23 PM
  5. UNICODE and GET_STATE
    By Registered in forum C++ Programming
    Replies: 1
    Last Post: 07-15-2002, 03:23 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21