shmc - s sell_seats(); s ret = shmdt(memptr); exit(0); }...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
/* shmc.c */ #include "registration.h" #include <sys/types.h> #include <sys/ipc.h> #include <sys/sem.h> #include <sys/shm.h> #include <sys/wait.h> #include <unistd.h> #include <stdlib.h> #include <iostream.h> #include <stdio.h> #include <memory.h> # CLASS *class_ptr; void *memptr; char *pname; int semid, shmid, ret; static struct sembuf mylock = { 0, -1, SEM_UNDO }; static struct sembuf myunlock = { 0, 1, SEM_UNDO }; void rpterror(char *), srand(), perror(), sleep(); void sell_seats(); v main(int argc, char* argv[]) { if (argc < 3) { fprintf (stderr, "Usage:, %s shmid semid\n", argv[0]); exit(1); } pname = argv[0]; sscanf (argv[1], "%d", &shmid); memptr = shmat (shmid, (void *)0, 0); if (memptr == (char *)-1 ) { rpterror ("shmat failed"); exit(2); } class_ptr = (struct CLASS *)memptr; sscanf (argv[2], "%d", &semid);
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: s sell_seats(); s ret = shmdt(memptr); exit(0); } void sell_seats() { int all_out = 0; i srand ( (unsigned) getpid() ); while ( !all_out) { /* loop to sell all seats */ if ( semop(semid, &mylock, 1) == -1) { rpterror ("semop lock failed"); exit(4); } if (class_ptr->seats_left > 0) { class_ptr->seats_left--; cout << pname << " SOLD SEAT -- " << class_ptr->seats_left << " left" << endl; } else { all_out++; cout << pname << " sees no seats left" << endl; } ret = semop(semid, &myunlock, 1); if (ret == -1) { rpterror ("semop unlock failed"); exit(4); } sleep ( (unsigned)rand()%10 + 1); } } void rpterror(char* string) { char errline[50]; c sprintf (errline, "%s %s", string, pname); perror (errline); }...
View Full Document

This note was uploaded on 04/11/2010 for the course CECS 326 taught by Professor Staff during the Spring '08 term at CSU Long Beach.

Page1 / 2

shmc - s sell_seats(); s ret = shmdt(memptr); exit(0); }...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online