H. Semaphore in Unix Using C Programming

SEMAPHORE

producer.c

#include<stdio.h>
#include<sys/types.h>
#include<sys/sem.h>
#include<unistd.h>
#include<sys/ipc.h>
#include<fcntl.h>
#define KEY 1010
 union semun {
               int              val;
               struct semid_ds *buf;
               unsigned short  *array;
               struct seminfo  *__buf;
           };
void main()
{
int semid,count=0,fd;
struct sembuf put,get;
union semun u;
unsigned a[2]={1,0};
semid=semget(KEY,2,IPC_CREAT|IPC_EXCL|0666);
u.array=a;
semctl(semid,0,SETALL,u);
fd=open("buff",O_CREAT|O_RDWR,0666);
write(fd,&count,sizeof(int));
while(1)
{
put.sem_num=0;
put.sem_op=-1;
put.sem_flg=0;
semop(semid,&put,1);
lseek(fd,0,0);
read(fd,&count,sizeof(int));
count++;
lseek(fd,0,0);
write(fd,&count,sizeof(int));
printf("Produced item: %d\n",count);
get.sem_num=1;
get.sem_op=1;
get.sem_flg=0;
semop(semid,&get,1);
sleep(5);
}
close(fd);
}

consumer.c

#include<stdio.h>
#include<sys/types.h>
#include<sys/sem.h>
#include<unistd.h>
#include<sys/ipc.h>
#include<fcntl.h>
#define KEY 1010
 union semun {
               int              val;
               struct semid_ds *buf;
               unsigned short  *array;
               struct seminfo  *__buf;
           };

void main()
{
int semid,count,fd;
struct sembuf put,get;
semid=semget(KEY,0,0);
fd=open("buff",O_RDONLY);
while(1)
{
get.sem_num=1;
get.sem_op=-1;
get.sem_flg=0;
semop(semid,&put,1);
lseek(fd,0,0);
read(fd,&count,sizeof(int));
printf("Consumed item: %d\n",count);
put.sem_num=0;
put.sem_op=1;
put.sem_flg=0;
semop(semid,&put,1);
sleep(5);
}
close(fd);
}

OUTPUT:

[09mcr110@localhost semaphore]$ ./prod
Produced item: 1
Produced item: 2
Produced item: 3
Produced item: 4
Produced item: 5
Produced item: 6
Produced item: 7
Produced item: 8
Produced item: 9
Produced item: 10
Produced item: 11
Produced item: 12
[09mcr110@localhost semaphore]$ ./cons
Consumed item: 1
Consumed item: 2
Consumed item: 3
Consumed item: 4
Consumed item: 5
Consumed item: 6
Consumed item: 7
Consumed item: 8
Consumed item: 9
Consumed item: 10
Consumed item: 11