add labs
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
/*******************************************************************************
|
||||
* File: coffeTeller.c
|
||||
* Purpose: simple sequence with semaphores
|
||||
* Course: bsy
|
||||
* Author: M. Thaler, 2011
|
||||
* Revision: 5/2012, 7/2013
|
||||
* Version: v.fs20
|
||||
*******************************************************************************/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/wait.h>
|
||||
|
||||
#include <sys/mman.h>
|
||||
#include <fcntl.h>
|
||||
#include <semaphore.h>
|
||||
|
||||
#include "commonDefs.h"
|
||||
|
||||
//******************************************************************************
|
||||
|
||||
int main(void) {
|
||||
|
||||
int i;
|
||||
sem_t *coin, *coffee, *ready;
|
||||
|
||||
// set up a semaphore
|
||||
coin = sem_open(COIN_SEMAPHOR, 0);
|
||||
coffee = sem_open(COFFEE_SEMAPHOR, 0);
|
||||
ready = sem_open(READY_SEMAPHOR, 0);
|
||||
|
||||
// start teller machine
|
||||
printf("\nCoffee teller machine starting\n\n");
|
||||
|
||||
i = 0;
|
||||
while (i < ITERS) {
|
||||
printf("teller (%d): waiting for coin\n", i);
|
||||
printf(" (%d): got coin\n", i);
|
||||
printf(" (%d): dispense coffee\n", i);
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
//******************************************************************************
|
||||
@@ -0,0 +1,30 @@
|
||||
#ifndef MY_DEFINITIONS_HEADER
|
||||
#define MY_DEFINITIONS_HEADER
|
||||
|
||||
/*******************************************************************************
|
||||
* File: commonDefs.h
|
||||
* Purpose: common definitions
|
||||
* Course: bsy
|
||||
* Author: M. Thaler, 2011
|
||||
* Revision: 5/2012, 7/2013, 4/2014
|
||||
* Version: v.fs20
|
||||
*******************************************************************************/
|
||||
|
||||
#define MYTURN_SEMAPHOR "/my_semaphor_1_name_simple_seq"
|
||||
#define COIN_SEMAPHOR "/my_semaphor_2_name_simple_seq"
|
||||
#define COFFEE_SEMAPHOR "/my_semaphor_3_name_simple_seq"
|
||||
#define READY_SEMAPHOR "/my_semaphor_4_name_simple_seq"
|
||||
|
||||
#define ITERS (100*1000*1000)
|
||||
#define CUSTOMERS 4
|
||||
|
||||
//******************************************************************************
|
||||
|
||||
#define checkSem(X) {if (X == SEM_FAILED) {perror("sem_open"); exit(-1);}}
|
||||
|
||||
#define drinkingCoffee(X) {usleep((((1+X)*rand())+100000)&0xFFFFF);}
|
||||
|
||||
//******************************************************************************
|
||||
|
||||
#endif
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
/*******************************************************************************
|
||||
* File: customer.c
|
||||
* Purpose: simple sequence with semaphores
|
||||
* Course: bsy
|
||||
* Author: M. Thaler, 2011
|
||||
* Revision: 5/2012, 7/2013
|
||||
* Version: v.fs20
|
||||
*******************************************************************************/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <sys/mman.h>
|
||||
#include <fcntl.h>
|
||||
#include <semaphore.h>
|
||||
|
||||
#include "commonDefs.h"
|
||||
|
||||
//******************************************************************************
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
|
||||
int i, myID;
|
||||
sem_t *myTurn, *coin, *coffee, *ready;
|
||||
|
||||
if (argc > 1)
|
||||
myID = atoi(argv[1]);
|
||||
else
|
||||
myID = 0;
|
||||
|
||||
// set up a semaphore
|
||||
myTurn = sem_open(MYTURN_SEMAPHOR, 0);
|
||||
coin = sem_open(COIN_SEMAPHOR, 0);
|
||||
coffee = sem_open(COFFEE_SEMAPHOR, 0);
|
||||
ready = sem_open(READY_SEMAPHOR, 0);
|
||||
|
||||
// start customer
|
||||
printf("Customer starting (%d)\n", myID);
|
||||
|
||||
// now check the sum
|
||||
for (i = 0; i < ITERS; i++) {
|
||||
printf("\t\t\t\tcustomer(%d) put coin %d\n", myID, i);
|
||||
printf("\t\t\t\tcustomer(%d) waiting for coffee %d\n", myID, i);
|
||||
printf("\t\t\t\tcustomer(%d) got coffee %d\n", myID, i);
|
||||
drinkingCoffee(myID);
|
||||
}
|
||||
}
|
||||
|
||||
//******************************************************************************
|
||||
@@ -0,0 +1,40 @@
|
||||
# Author M. Thaler InIT/ZHAW
|
||||
# Version v.fs20
|
||||
|
||||
CMP= gcc -std=gnu99
|
||||
CMPFLAGS= -Wall -g
|
||||
LIB= -pthread
|
||||
EXENAME0= startApp.e
|
||||
EXENAME1= coffeeTeller.e
|
||||
EXENAME2= customer.e
|
||||
|
||||
doit:
|
||||
@make --no-print-directory clean
|
||||
@make --no-print-directory startApp
|
||||
@make --no-print-directory coffeeTeller
|
||||
@make --no-print-directory customer
|
||||
|
||||
startApp: startApp.o
|
||||
$(CMP) $(CMPFLAGS) startApp.o $(LIB) -o $(EXENAME0)
|
||||
|
||||
coffeeTeller: coffeeTeller.o
|
||||
$(CMP) $(CMPFLAGS) coffeeTeller.o $(LIB) -o $(EXENAME1)
|
||||
|
||||
customer: customer.o
|
||||
$(CMP) $(CMPFLAGS) customer.o $(LIB) -o $(EXENAME2)
|
||||
|
||||
.c.o:
|
||||
$(CMP) -c $(CMPFLAGS) $<
|
||||
|
||||
.cc.o:
|
||||
$(CMP) -c $(CMPFLAGS) $<
|
||||
|
||||
all:
|
||||
@make clean
|
||||
make doit
|
||||
|
||||
clean:
|
||||
@rm -f *.e *.o
|
||||
|
||||
purge:
|
||||
@make clean
|
||||
@@ -0,0 +1,72 @@
|
||||
/*******************************************************************************
|
||||
* File: startApp.c
|
||||
* Purpose: ice cream teller, basic sequence
|
||||
* Course: bsy
|
||||
* Author: M. Thaler, 2011
|
||||
* Revision: 5/2012, 7/2013
|
||||
* Version: v.fs20
|
||||
*******************************************************************************/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/wait.h>
|
||||
|
||||
#include <sys/mman.h>
|
||||
#include <fcntl.h>
|
||||
#include <semaphore.h>
|
||||
|
||||
#include "commonDefs.h"
|
||||
|
||||
//******************************************************************************
|
||||
|
||||
int main(void) {
|
||||
|
||||
int j;
|
||||
char string[8];
|
||||
sem_t *myTurn, *coin, *coffee, *ready;
|
||||
pid_t tellerPID;
|
||||
|
||||
sem_unlink(MYTURN_SEMAPHOR); // delete seamphor if it still exists
|
||||
sem_unlink(COIN_SEMAPHOR); // delete seamphor if it still exists
|
||||
sem_unlink(COFFEE_SEMAPHOR); // delete seamphor if it still exists
|
||||
sem_unlink(READY_SEMAPHOR); // delete seamphor if it still exists
|
||||
|
||||
// set up a semaphore (? -> initial value of semaphor)
|
||||
// checkSem() -> macro defined in commonDefs.h
|
||||
|
||||
/*
|
||||
myTurn = sem_open(MYTURN_SEMAPHOR, O_CREAT, 0700, ?); checkSem(myTurn);
|
||||
coin = sem_open(COIN_SEMAPHOR, O_CREAT, 0700, ?); checkSem(coin);
|
||||
coffee = sem_open(COFFEE_SEMAPHOR, O_CREAT, 0700, ?); checkSem(coffee);
|
||||
ready = sem_open(READY_SEMAPHOR, O_CREAT, 0700, ?); checkSem(ready);
|
||||
*/
|
||||
|
||||
// now that the resources are set up, the supervisor can be started
|
||||
for (j = 1; j <= CUSTOMERS; j++) {
|
||||
if (fork() == 0) {
|
||||
sprintf(string, "%d", j);
|
||||
execl("./customer.e", "customer.e", string, NULL);
|
||||
printf("*** could not start customer.e ***\n");
|
||||
}
|
||||
}
|
||||
|
||||
if ((tellerPID = fork()) == 0) {
|
||||
execl("./coffeeTeller.e", "coffeeTeller.e", "0", NULL);
|
||||
printf("*** could not start coffeTeller ***\n");
|
||||
}
|
||||
|
||||
waitpid(tellerPID, NULL, 0);
|
||||
system("killall coffeeTeller.e");
|
||||
system("killall customer.e"); // kill all customers
|
||||
|
||||
// clean up resources
|
||||
sem_unlink(MYTURN_SEMAPHOR);
|
||||
sem_unlink(COIN_SEMAPHOR);
|
||||
sem_unlink(COFFEE_SEMAPHOR);
|
||||
sem_unlink(READY_SEMAPHOR);
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
//******************************************************************************
|
||||
Reference in New Issue
Block a user