Thread: cyclic buffer

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    1

    cyclic buffer

    i'm trying to produce a program that stays in a endless loop inserting integers into a cyclic array, and i'm stuck on getting integers into the array can anyone point me in the correct direction? my code so far is:

    Code:
    #include <stdio.h>
    #include <sys/types.h>
    #include <sys/ipc.h>
    #include <sys/shm.h>
    #include <string.h>
    #include <stdlib.h>
    #include <sys/sem.h>
    #include <errno.h>
    #include <string.h>
    
    #define KEY     (key_t) 12345
    #define MUTEX  (key_t) 100
    #define PERM 0600
    
    extern int errno;
    extern int showsem(key_t);
    
    int initsem(key_t, int);
    int p(int);
    int v(int);
    int mutex;
    int OFLO;
    int UFLO;
    i
    
    struct shseg
    {   
       int ptr;
       char buffer[100];
    } *ashseg;
    
    
    
    int main() 
    {
       int shmid;
       shmid=shmget(KEY, sizeof(struct shseg), IPC_CREAT|0666);
       ashseg=(struct shseg *) shmat(shmid, 0, 0); //note type cast
       mutex=initsem(MUTEX,1);
       OFLO=initsem(OFLO,3);
       UFLO=initsem(UFLO,0);
       for (ashseg->ptr=0;ashseg->ptr<strlen(ashseg->buffer); ashseg->ptr++) {
       p(OFLO);
       p(mutex);	
       strncpy(ashseg->buffer, "test string\n", 20);
       putchar(ashseg->buffer[getchar()]);
       v(mutex);
       v(UFLO);	
       sleep(1);}
    
       killsem(mutex);
       killsem(OFLO);
       killsem(UFLO);
    }
    
    
    int initsem(key_t semkey,int semval)
     {
        int status=0, semid;
         if ((semid=semget(semkey,1,PERM|IPC_CREAT|IPC_EXCL))==-1) 
                if (errno==EEXIST)
                    semid=semget(semkey,1,0);}
            else
                    status=semctl(semid,0,SETVAL,semval);
           
           if (semid==-1||status==-1) {
               perror("initsem failed");
               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");
           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) failed");
              exit(1);
              }
            else
               return(0);
     }
    
    int killsem(semid)
     {
            if (semctl(semid,0,IPC_RMID,0)==-1)
                    return(-1);
            return(0);}
    Last edited by Fortune; 04-22-2009 at 06:06 PM.

  2. #2
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,226
    First, fix your indentation. Then someone may look at your code.

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,793
    your way to place braces is just purely horrble

    Code:
    int initsem(key_t semkey,int semval)
    {
    	int status=0, semid;
    	if ((semid=semget(semkey,1,PERM|IPC_CREAT|IPC_EXCL))==-1) 
    		if (errno==EEXIST)
    			semid=semget(semkey,1,0);
    }
    		else
    			status=semctl(semid,0,SETVAL,semval);
    
    			if (semid==-1||status==-1) 
    			{
    				perror("initsem failed");
    				return(-1);
    			}
    
    			return(semid);
    }
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 10-29-2006, 05:04 AM
  2. writing a pack-style function, any advices?
    By isaac_s in forum C Programming
    Replies: 10
    Last Post: 07-08-2006, 08:09 PM
  3. buffer contents swapping
    By daluu in forum C++ Programming
    Replies: 7
    Last Post: 10-14-2004, 02:34 PM
  4. Tetris Questions
    By KneeGrow in forum Game Programming
    Replies: 19
    Last Post: 10-28-2003, 11:12 PM
  5. Console Screen Buffer
    By GaPe in forum Windows Programming
    Replies: 0
    Last Post: 02-06-2003, 05:15 AM