Ipc

This is a discussion on Ipc within the C Programming forums, part of the General Programming Boards category; I am having difficulty with my IPC producer consumer program. Code: if ((shmid=shmget(KEY, sizeof(struct shseg), IPC_CREAT|0666|IPC_EXCL ==-1){ if (errno==EEXIST) { ...

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    25

    Ipc

    I am having difficulty with my IPC producer consumer program.

    Code:
    if ((shmid=shmget(KEY, sizeof(struct shseg), IPC_CREAT|0666|IPC_EXCL ==-1){
    	 	if (errno==EEXIST) {
                     }
    }
    This code checks too see if the shared memory segment is in exclusive mode, if it is it should return the EEXIST error. Not sure if my syntax is correct in the if statment as it seems to create error not seen in the program without it.

    Code:
    int simshm(void)        {
            int shmid;
    	int mutex,uflo,oflo;
          	shmid=shmget(KEY, sizeof(struct shseg), IPC_CREAT|0666); 
            ashseg=(struct shseg *) shmat(shmid, 0, 0); // follow initsem logic
    	mutex=initsem(MUTEX,1);
    	oflo=initsem(OFLO, 3);
    	uflo=initsem(UFLO,0);
    	printf("%d %d %d\n", mutex,oflo,uflo);
    	while (1) {
    		p(uflo);
    		p(mutex);
    		printf("con in\n");
    		printf("con: %c\n",ashseg->buffer[ashseg->out]);
    		ashseg->out=(ashseg->out + 1) % BUFSIZE; 
    		v(mutex);
    		v(oflo);
    		printf("con out\n");
    		sleep(1);
    	
    	}
            return 1;
                    }
    Last edited by Rhidian; 03-23-2005 at 08:27 AM.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,498
    It's really hard to tell because
    1. you don't post enough code
    2. the code which you have posted doesn't compile.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    25
    Code:
    #include <sys/types.h>
    #include <sys/ipc.h>
    #include <sys/sem.h>
    
    #include <errno.h>
    extern errno;
    
    #define SEMPERM 0600
    
    
    //initshm();
    
    int initsem(key_t semkey,int semval)
    	{
    	int status=0, semid;
    	if ((semid=semget(semkey,1,SEMPERM|IPC_CREAT|IPC_EXCL))==-1) {
    		if (errno==EEXIST){
    			semid=semget(semkey,1,0);
    				return(semid);}
    		}
    	status=semctl(semid,0,SETVAL,semval);
    	printf("initialising\n");
    	if (semid==-1||status==-1) {
    		perror("initsem failed\n");
    		return(-1);
    		}
    	return(semid);
    	}
    
    int p(int semid)
    	{
    	struct sembuf p_buf;
    	p_buf.sem_num = 0;
    	p_buf.sem_op = -1;
    	p_buf.sem_flg = SEM_UNDO;
    
    	if (semop(semid, &p_buf, 1) == -1) {
    		perror("p(semid) failed\n");
    		exit(1);
    		}
    	else
    		return(0);
    	}
    
    int v(int semid)
    	{
    	struct sembuf v_buf;
    	v_buf.sem_num = 0;
    	v_buf.sem_op = 1;
    	v_buf.sem_flg = SEM_UNDO;
    
    	if (semop(semid, &v_buf,1) == -1) {
    		perror("v(semid) failedi\n");
    		exit(1);
    		}
    	else
    		return(0);
    	}
    
    int killsem(key_t semkey)
    	{
    	if (semctl(semkey,0,IPC_RMID,0)==-1)
    		return(-1);
    	return(0);
    	}
    
    /*test program for semaphore operations*/
    
    
    This should be compiled with intial code i.e. gcc #name #name

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simulating wireless netowork using IPC
    By tenkasian in forum C Programming
    Replies: 3
    Last Post: 03-23-2009, 04:16 PM
  2. Segmentation fault - IPC
    By kbfirebreather in forum C Programming
    Replies: 7
    Last Post: 02-01-2009, 02:17 PM
  3. a question regarding ipc
    By netwizio in forum Linux Programming
    Replies: 6
    Last Post: 01-10-2004, 12:55 AM
  4. IPC Linux to Windows
    By Massive in forum Windows Programming
    Replies: 1
    Last Post: 06-21-2003, 01:13 AM
  5. IPC vs RPC
    By hermit in forum Tech Board
    Replies: 7
    Last Post: 09-09-2002, 09:58 AM

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