CIS 307: Homework 3: Hints

In this homework we run two or more copies of the same program RAND_PROC.

[RAND_PROC], [STOREPTR], [Asynchronous terminal IO]

RAND_PROC

A RAND_PROC process has the form:
   RAND_PROC:
       Obtain the procid of this process;
       Initialize the random seed;
       Initialization;
       Lock COUNT's lock;
         if segid.dat does not exist then
           create and initialize the segment;
           write 1 to COUNT
           create the segid.dat file and write the segment id to it
         else
           read the segment id from segid.dat
           increment COUNT
       UnLock COUNT's lock;
       Attach to the segmentid and set STOREPTR to value returned by Attach;
       [All access to STORE is done through STOREPTR]
       Display Menu;
       loop
            If there is input from terminal then
               Carry out the user request and, if necessary,
                  Display Menu;
	    Select at random STORE_READ or STORE_UPDATE 
	    and execute the corresponding
            function [in the function you will use locks, sleep, etc.]
            and log the call and result;
	    wait some random time;
	end loop;
        Lock COUNT's lock;
          Detach from shared segment
          Decrement COUNT
          if COUNT becomes 0 then
            Delete the segid.dat file
            Delete the shared segment
        Unlock COUNT's lock;
        Conclusion;

STOREPTR

This code shows how we may use the STORE through a pointer. Note that I have used malloc instead of shmat so as to simplify the example.

  #define STORESIZE ..
  #define STOREIDSIZE ..
  #define STORELEMSIZE ..
  typedef char storeid[STOREIDSIZE];
  typedef char storeelem[STORELEMSIZE];
  typedef struct {
    storeid id;
    storeelem elem;} storepair;
  typedef storepair storetype[STORESIZE];

  storetype * storeptr;

  void storeinit();

  int main () {
    storeptr = (storetype *) malloc(sizeof(storetype));
    storeinit();
    printf ("id of second entry: %s\n", storeptr[1]->id);
    exit(0);
  }

  void storeinit(){
    strcpy(storeptr[0]->id, "......");
    ...........
  }

Non Blocking terminal IO

Here is a small program that shows how to read from the terminal in a non blocking way. This is one of a number of possible ways to do non blocking io from the terminal.
   /* asynctty.c  -- Uses non blocking reads from the terminal.
    */

   #include <stdio.h>
   #include <fcntl.h>
   #define LINESIZE 128

   void menu(void);

   main()
   {
     char line[LINESIZE];
     int  size;
     int fd;

     menu();
     if ((fd = open("/dev/tty", O_RDONLY | O_NONBLOCK, "r")) < 0) {
       perror("open");
       exit(1);
     }
     while(TRUE) {
       sleep(2);
       size = read(fd, line, 1);
       if (size > 0) {
         if (line[0] == '1') {
            /*Take out all that was printed */
            while ((size = read(fd,line,1)) > 0);
            printf("You have asked for statistics\n");
            menu();
         } else {
	   printf("You have asked to terminate\n");
	   break;
         }
       } else
         printf("Spinning\n");
     }
     printf("\nGoodbye\n");
   }

   void menu(void) {
     printf("1. Print Statistics\n");
     printf("2. Terminate\n");
     printf("Enter Choice [1|2] > \n");
   }

ingargiola@cis.temple.edu