producer consumer semaphore

Control two exclusively shared resources(semaphore). The two resources are two files. The producer will write even numbers to one file, and odd numbers to another one. The consumer respectively reads from each file until it gets 5 even numbers and 5 odd numbers.

Can any one help me with the code.

Thanks in advance.

Producer

  #include <stdio.h>
  #include <unistd.h>
  #include <fcntl.h>
  #include <stdlib.h>
  #include <string.h>
  #include <sys/types.h>
  #include <sys/stat.h>
  #include <sys/ipc.h>
  #include <sys/sem.h>
  #define PUB7645 "./PubFile"
  union semun {
     int val;
     struct semid_ds *buf;
     ushort *array;
  };
  int 
  main() {
     int i=0, intValue, semID;
     key_t key;
     FILE *fptr;
      union semun arg;
      static ushort startVal[2] = {1, 0};
      static struct sembuf acquire = {0, -1, SEM_UNDO},
                           release = {1, 1, SEM_UNDO};
     key = ftok(".", 's');
     if ((semID = semget(key, 2, IPC_CREAT | 0666)) == -1) {
                    perror("Sem Creation:");
                exit(2);
     }
     arg.array = startVal;
     if (semctl(semID, 2, SETALL, arg) == -1 ) {
                     perror("semctl: SETALL");
                 exit(3);
     }
  for( ; ; ) {
        srand(getpid()+i);
        intValue =rand()*49/RAND_MAX + 1;
        if (semop(semID, &acquire, 1) == -1) {
                                      perror("Producer waiting");
                          exit(4);
        }
        if ((fptr = fopen(PUB7645, "w")) == NULL) {
                          perror(PUB7645);
                          exit(1);
        }fprintf(fptr, "%d\n", intValue);
        fclose(fptr);
        if (semop(semID, &release, 1) == -1) {
              perror("new");
              exit(5);
        }
        i++;
     }
  }

Consumer

  #include <stdio.h>
  #include <unistd.h>
  #include <fcntl.h>
  #include <stdlib.h>
  #include <string.h>
  #include <sys/types.h>
  #include <sys/stat.h>
  #include <sys/ipc.h>
  #include <sys/sem.h>
  #define PUB7645 "./PubFile"
  union semun {
     int val;
     struct semid_ds *buf;
     ushort *array;
  };
  int  main() {
     int i=0, intValue, semID;
     key_t key;
     FILE *fptr;
                  union semun arg;
                  static struct sembuf acquire = {1, -1, SEM_UNDO},
                           release = {0, 1, SEM_UNDO};
     key = ftok(".", 's');
     if ((semID = semget(key, 2, IPC_CREAT | 0666)) == -1) {
                    perror("Sem Creation:");
               exit(2);
     }
  for(i=1; i<=5 ; i++) {
        if (semop(semID, &acquire, 1) == -1) {
                  perror(�Consumer waiting");
                      exit(4);
        }
        if ((fptr = fopen(PUB7645, "r")) == NULL) {
                          perror(PUB7645);
                          exit(1);
        }
        fscanf(fptr, "%d", &intValue);
        fclose(fptr);
      if (semop(semID, &release, 1) == -1) {
                    perror(�Release");
                    exit(4);
        }
        printf("%d\t", intValue);
     }
     printf("\nI am process %d. I got five numbers ", getpid());
     printf("from a producer as above.\n");
  }

professor : kishore kumar
college: anna university
course: unix programming

So, there is a semaphore to take control of the file, for each file, whether reading or writing. You have to create two semaphores and set them to the available state, two empty files, and then start the participants. Are the numbers in text or raw integer? Is the number count the line count or the file size / 4 ? Where do you see your problem?