int main (int argc, char *argv[]) { int key; /*access key to shared memory and semaphore set */ char *tinp; /* numerical parameters test flag */ unsigned int m; /* craftman identification */ /* validation of comand line parameters */ if (argc != 5) { freopen ("error_GCF", "a", stderr); fprintf (stderr, "Number of parameters is incorrect!\n"); exit (EXIT_FAILURE); } else freopen (argv[4], "w", stderr); m = (unsigned int) strtol (argv[1], &tinp, 0); if ((*tinp != '\0') || (m >= M)) { fprintf (stderr, "Craftman process id is invalid!\n"); exit (EXIT_FAILURE); } nFic = argv[2]; key = (unsigned int) strtol (argv[3], &tinp, 0); if (*tinp != '\0') { fprintf (stderr, "Error on the access key communication!\n"); exit (EXIT_FAILURE); } /* connection to the semaphore set and the shared memory region and mapping the shared region on the process address space */ if ((semgid = semConnect (key)) == -1) { perror ("error on connecting to the semaphore set"); exit (EXIT_FAILURE); } if ((shmid = shmemConnect (key)) == -1) { perror ("error on connecting to the shared memory region"); exit (EXIT_FAILURE); } if (shmemAttach (shmid, (void **) &sh) == -1) { perror ("error on mapping the shared region on the process address space"); exit (EXIT_FAILURE); } /* simulation of the life cycle of the craftsman */ unsigned int np; /* number of products in store */ bool alert; /* low level of prime materials in store */ while (!endOperCraftsman (m)) { alert = collectMaterials (m); /* the craftsman gets the prime materials he needs to manufacture a product */ if (alert) { primeMaterialsNeeded (m); /* the craftsman phones the entrepreneur to let her know the workshop requires more prime materials */ backToWork (m); /* the craftsman returns to his regular duties */ } prepareToProduce (m); /* the craftsman sits down and prepares things for the production of a new piece */ shapingItUp (); /* the craftsman works on a new piece */ np = goToStore (m); /* the craftsman stores the finished product */ if (np >= MAX) /* the craftsman checks if it is transfer time */ batchReadyForTransfer (m); /* the craftsman phones the entrepreneur to let her know she should come and collect a new batch of products */ backToWork (m); /* the craftsman returns to his regular duties */ } /* unmapping the shared region off the process address space */ if (shmemDettach (sh) == -1) { perror ("error on unmapping the shared region off the process address space"); exit (EXIT_FAILURE); } exit (EXIT_SUCCESS); }
int main (int argc, char *argv[]) { int key; /*access key to shared memory and semaphore set */ char *tinp; /* numerical parameters test flag */ unsigned int k; /* flight number */ unsigned int p; /* passenger identification */ unsigned int stat; /* status of operation */ /* validation of command line parameters */ if (argc != 5) { freopen ("error_GPA", "a", stderr); fprintf (stderr, "Number of parameters is incorrect!\n"); return EXIT_FAILURE; } else freopen (argv[4], "w", stderr); p = (unsigned int) strtol (argv[1], &tinp, 0); if ((*tinp != '\0') || (p >= N)) { fprintf (stderr, "Passenger process identification is wrong!\n"); return EXIT_FAILURE; } strcpy (nFic, argv[2]); key = (unsigned int) strtol (argv[3], &tinp, 0); if (*tinp != '\0') { fprintf (stderr, "Error on the access key communication!\n"); return EXIT_FAILURE; } /* connection to the semaphore set and the shared memory region and mapping the shared region onto the process address space */ if ((semgid = semConnect (key)) == -1) { perror ("error on connecting to the semaphore set"); return EXIT_FAILURE; } if ((shmid = shmemConnect (key)) == -1) { perror ("error on connecting to the shared memory region"); return EXIT_FAILURE; } if (shmemAttach (shmid, (void **) &sh) == -1) { perror ("error on mapping the shared region on the process address space"); return EXIT_FAILURE; } /* simulation of the life cycle of the passenger */ for (k = 0; k < K; k++) switch (whatShouldIDo (k, p)) /* the passenger decides on her next move */ { case FDBTC: /* she has arrived to her final destination and has bags to collect */ /* the passenger goes to the luggage collection point to pick up her bags one by one */ while ((stat = goCollectABag (k, p)) == NO); if (stat == MB) /* the passenger checks for missing bags */ reportMissingBags (k, p); /* the passenger go to the baggage reclaim office to fill the form for missing bags */ goHome (k, p); /* the passenger leaves the airport */ break; case FDNBTC: /* she has arrived to her final destination and has no bags to collect */ goHome (k, p); /* the passenger leaves the airport */ break; case INTRAN: /* she is in transit */ takeABus (k, p); /* the passenger goes to the arrival transfer terminal to queue for taking a bus to the departure terminal */ enterTheBus (k, p); /* the passenger goes on board of the bus as it starts its journey */ leaveTheBus (k, p); /* the passenger comes out of the bus as it reaches the departure transfer terminal */ prepareNextLeg (k, p); /* the passenger enters the departure and does the check in for the next leg of the journey */ break; } /* unmapping the shared region off the process address space */ if (shmemDettach (sh) == -1) { perror ("error on unmapping the shared region off the process address space"); return EXIT_FAILURE;; } return EXIT_SUCCESS; }