2018-12-12 Semaphore Assignment

posted Dec 12, 2018, 10:14 AM by Konstantinovich Samuel   [ updated Dec 13, 2018, 12:02 PM ]
including This will be due Friday, 12/14

With your table buddy, write a telephone style game! It will use a semaphore to control access to a file and shared memory. 

The main program should work as follows:
  1. Print a message this it will check if resources are available.
  2. When they are, display the last line added to the story (or none, if it is new).
  3. Prompt the user to enter the next line
  4. Update the story
  5. quit

This game will require 2 files 
  1. A control program
    • Take a command line argument to determine if it is creating or removing the resources or viewing the story.
    • If creating (command line argument: -c):
      • Should make the shared memory, semaphore and file (open the file with the truncate flag). 
      • Set any values that are needed.
    • If removing (command line argument: -r)
      • Remove the shared memory, the semaphore and the story.
      • Display the full contents of the story.
      • This should wait until the semaphore is available.
    • If viewing (command line argument: -v)
      • Output the contents of the story file. 
      • This mode does not need to interact with the semaphore
  2. A main writing program
    • Will attempt to access the resources using the semaphore
      1. Once in, it should display the last line added to the file (use shared memory to contain the size of the last line).
      2. Prompt the use for the next line.
      3. Once a new line is read, write that to the file, update the shared memory and then release the semaphore
  3. Your run target in your makefile should be the main program
  4.  You can also have a setup target that is the control program (including the args)
e.g.
#compile everything
make

#execute setup (including an argument)
make setup args="-c"

#execute main program:
make run

github:
MKS65C-semaphone




Sample Control Program:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <sys/types.h>
#include <errno.h>

#define KEY 0xDEADBEEF

 union semun {
   int              val;    /* Value for SETVAL */
   struct semid_ds *buf;    /* Buffer for IPC_STAT, IPC_SET */
   unsigned short  *array;  /* Array for GETALL, SETALL */
   struct seminfo  *__buf;  /* Buffer for IPC_INFO */
 };

int main() {

  int semd;
  int r;
  int v;
  semd = semget(KEY, 1, IPC_CREAT | IPC_EXCL | 0644);
  if (semd == -1) {
    printf("error %d: %s\n", errno, strerror(errno));
    semd = semget(KEY, 1, 0);
    v = semctl(semd, 0, GETVAL, 0);
    printf("semctl returned: %d\n", v);
  }
  else {
    union semun us;
    us.val = 3;
    r = semctl(semd, 0, SETVAL, us);
    printf("semctl returned: %d\n", r);
  }

  return 0;
}


Sample Semaphore Use:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <sys/types.h>
#include <errno.h>

#define KEY 0xDEADBEEF


int main() {
  int semd;
  semd = semget(KEY, 1, 0);


  //DOWNING
  struct sembuf sb;
  sb.sem_num = 0;

  sb.sem_flg = SEM_UNDO;
  sb.sem_op = -1;
  semop(semd, &sb, 1);

  printf("got the semaphore!\n");
  sleep(1);

  //UPPING
  sb.sem_op = 1;
  semop(semd, &sb, 1);

  return 0;
}



Comments