/************************************************************************** ** **course: Comp460, Operating Systems, Tuesdays ** **Instructor: Xiuwen Liu **File Name: ~liux/public_html/courses/cop4610/examples/producer.c ** **Description: This program implements a producer process using ** the bounded-buffer aglorithm. Producer process repeat produce an item in nextp wait(empty); wait(mutex); signal(mutex); signal(full); until false; ** ***************************************************************************/ #include #include #include #include #include "bounded-buffer.h" int Delay; /* Delay before exiting */ int Pid; /* Process ID */ int ExitFlag = 0; void catch_sig(int sig); int main(int argc, char *argv[]) { int ii; long now; key_t key, key_1; struct admin *shmaddr; struct buffer_record *buffer; int semid; int shmid, shmid_1; struct buffer_record abuf; struct sembuf sops; int buf_loc; Delay = rand()%5+1; Pid = getpid(); key = getppid(); key_1 = key+1; shmid = shmget(key, sizeof(struct admin), 00600); if (shmid < 0) { fprintf(stderr,"Error: cannot get adminstrative shared memory segment.\n"); exit(-1); } signal (SIGINT, catch_sig); signal (SIGTERM, catch_sig); signal(SIGKILL, catch_sig); shmaddr = (struct admin *)shmat(shmid, (void *)0,0); shmid_1 = shmget(key_1, sizeof(struct buffer_record) * shmaddr->size, 00600); if (shmid_1 < 0) { fprintf(stderr,"Error: cannot get shared circular buffer.\n"); exit(-1); } buffer = (struct buffer_record *)shmat(shmid_1, (void *)0,0); semid = semget(key, 3, 00600); if (semid < 0) { fprintf(stderr,"Error: cannot get semphamores with %d\n", semid); exit(-1); } while(ExitFlag==0) { if (Delay >0) sleep(Delay); abuf.pid = Pid; abuf.tstamp = time((time_t *)NULL); sops.sem_num = MUTEX_SEM; sops.sem_op = -1; semop(semid, &sops, 1); abuf.seqnu = shmaddr->seqnu; shmaddr->seqnu += 1; sops.sem_op = 1; semop(semid, &sops, 1); /* Empty */ sops.sem_num = EMPTY_SEM; sops.sem_op = -1; semop(semid, &sops, 1); /* Mutex */ sops.sem_num = MUTEX_SEM; sops.sem_op = -1; semop(semid, &sops, 1); buffer[shmaddr->nextp] = abuf; buf_loc = shmaddr->nextp; shmaddr->nextp +=1; shmaddr->nextp %= shmaddr->size; /* Mutex */ sops.sem_num = MUTEX_SEM; sops.sem_op = 1; semop(semid, &sops, 1); /* Full */ sops.sem_num = FULL_SEM; sops.sem_op = 1; semop(semid, &sops, 1); printf("P(%d) produced in [%d]: ", Pid, buf_loc); printf("pid = %d seqnu = %d time = %s", abuf.pid, abuf.seqnu, &(ctime(&(abuf.tstamp))[4])); } shmdt((char *)shmaddr); shmdt((char *)buffer); return 0; } void catch_sig(int sig) { ExitFlag = 1; }