Example #1
0
int oprpc_getPktPointer(void *ap, void **p, int len)
{
    RPCHandle *hp = ap;
    char f;
#ifdef ENABLE_SHMEM
    int id;
#endif

    if (oprpc_getPkt(ap,&f,1) < 0) {
	return -1;
    }
    switch (f) {
    case 0:
	hp->inBuf.pktIndex = ((hp->inBuf.pktIndex+3)/4)*4;
	if (len >= 0 && hp->inBuf.wIndex - hp->inBuf.pktIndex < len) {
	    return -1;
	}
	*p = hp->inBuf.body+hp->inBuf.pktIndex;
	if (len >= 0) hp->inBuf.pktIndex += len;
	break;
    case 1:
#ifdef ENABLE_SHMEM
	if (oprpc_getPkt(ap,(char *)&id,sizeof(id)) < 0) {
	    return -1;
	}
	if ((*p = shmemAttach(hp,id)) == NULL) {
	    return -1;
	}
#else
	return -1;
#endif
	break;
    case 2:
	*p = NULL;
	break;
    default:
	return -1;
	break;
    }

    return f;
}
Example #2
0
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;
}