P09 lab code added

This commit is contained in:
Andreas Gieriet
2020-04-26 15:02:50 +02:00
parent 94945069e3
commit f4c374887c
45 changed files with 2116 additions and 0 deletions
@@ -0,0 +1,61 @@
//***************************************************************************
// File: ProcA5.c
// Original Author: M. Thaler (Modul BSY)
//***************************************************************************
//***************************************************************************
// system includes
//***************************************************************************
#include <sys/types.h>
#include <sys/wait.h>
#include <signal.h>
#include <unistd.h>
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include "workerUtils.h"
#include "selectCPU.h"
#define ITERATIONS 20
#define WORK_HARD 2000000
//***************************************************************************
// Function: main(), parameter: none
//***************************************************************************
int main(void) {
pid_t pid;
int i;
pid = fork();
selectCPU(0); // select CPU 0
switch (pid) {
case -1:
perror("Could not fork");
break;
case 0:
for (i = 0; i < ITERATIONS; i++) {
justWork(WORK_HARD);
printf("%d \t\tChild\n", i);
fflush(stdout); // force output
}
break;
default:
for (i = 0; i < ITERATIONS; i++) {;
justWork(WORK_HARD);
printf("%d \tMother\n", i);
fflush(stdout); // force output
}
}
printf("I go it ...\n");
if (pid > 0) // wait for child to terminate
waitpid(pid, NULL, 0);
exit(0);
}
//***************************************************************************
@@ -0,0 +1,2 @@
setCPU() does not work on OSX
@@ -0,0 +1,27 @@
# *************************************************************
# Original Autor: M. Thaler (Modul BSY)
# *************************************************************
CMP= gcc
CMPFLAGS= -Wall
LDFLAGS=
EXENAM1= ProcA4.e
FNAM1= ProcA4.o workerUtils.o selectCPU.o
LIBNAME=
$(EXENAM1): $(FNAM1)
$(CMP) $(FNAM1) $(LIBNAME) $(LDFLAGS) -o $@
.c.o:
$(CMP) -c $(CMPFLAGS) $<
.cc.o:
$(CMP) -c $(CMPFLAGS) $<
clean:
rm -f *.o $(EXENAM1)
all:
@make clean
@make
@@ -0,0 +1,41 @@
//***************************************************************************
// File: setCPU.c
// Original Author: M. Thaler (Modul BSY)
//***************************************************************************
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#ifdef __linux
#include <sched.h>
#endif
//******************************************************************************
#ifdef __linux
void selectCPU(unsigned int n) {
cpu_set_t cpuset;
if (n >= sysconf(_SC_NPROCESSORS_ONLN)) {
printf("CPU %d not availble\n", n);
exit(0);
}
sched_getaffinity(0, sizeof(cpu_set_t), &cpuset);
CPU_ZERO(&cpuset);
CPU_SET(n, &cpuset);
sched_setaffinity(0, sizeof(cpu_set_t), &cpuset);
}
#endif
#ifdef __APPLE__
void selectCPU(unsigned int n) {
printf("Cannot set single CPU on OSX\n ... continue anyway");
}
#endif
//******************************************************************************
@@ -0,0 +1,15 @@
//*******************************************************************
// File: selectCPU.h
// Original Author: M. Thaler (Modul BSY)
//*******************************************************************
#ifndef SET_CPUS_HEADER_FILE
#define SET_CPUS_HEADER_FILE
//*******************************************************************
void selectCPU(unsigned int n);
//*******************************************************************
#endif
@@ -0,0 +1,90 @@
//*******************************************************************
// File: workerUtils.c
// Original Author: M. Thaler (Modul BSY)
// Purpose: helper applications to consume cpu time
//*******************************************************************
#include <unistd.h>
#include <stdlib.h>
#include <time.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/time.h>
#include <stdio.h>
#include "workerUtils.h"
//------------------------------------------------------------------------------
#define MAX_CPUS 16
//------------------------------------------------------------------------------
pid_t worker[MAX_CPUS];
int nCPUS;
//------------------------------------------------------------------------------
void launchWorkLoad() {
int i;
nCPUS = sysconf(_SC_NPROCESSORS_ONLN); // get number of available CPUs
nCPUS = (nCPUS > MAX_CPUS) ? MAX_CPUS : nCPUS;
for (i = 0; i < nCPUS; i++) // start workers -> random load
worker[i] = startWorker();
}
void stopWorkLoad() {
int i;
for (i = 0; i < nCPUS; i++) // stop worker
stopWorker(worker[i]);
}
void setRandom(void) {
srandom((unsigned int)time(NULL)); // new random sequence
}
void justWork(unsigned int load) {
unsigned int j;
for (j = 0; j < load; j++) {}; // just work
}
void workHard(unsigned int low, unsigned int high) {
double rv;
unsigned int us, j;
high = high - low;
rv = ((double)random())/RAND_MAX; // make random value (0..1)
us = low + (unsigned int)(rv * high); // between lower & higher limit
for (j = 0; j < us; j++) {}; // just work
setRandom(); // reseed random generator
}
void randomSleep(unsigned int low, unsigned int high) {
double rv;
unsigned int us;
high = high - low;
rv = ((double)random())/RAND_MAX; // make random value (0..1)
us = low + (unsigned int)(rv * high); // between lower & higher limit
usleep(us*1000);
}
pid_t startWorker(void) { // make a hard working process
struct timeval tv; // limit run time to 60 secs
time_t st;
pid_t pid = fork();
if (pid == 0) { // child: pid = 0 -> ifinite loop
gettimeofday(&tv, NULL); // take start time
st = tv.tv_sec;
while ((tv.tv_sec-st) < 60) {
workHard(50, 800); // work hard (random interval
gettimeofday(&tv, NULL); // between 50us and 800us)
}
}
if (pid < 0) { // exit fork failed
printf("forking worker failed\n");
exit(0);
}
return pid; // return pid if parent
}
void stopWorker(pid_t worker) {
kill(worker, SIGKILL); // terminate worker process
}
@@ -0,0 +1,33 @@
//*******************************************************************
// File: workerUtils.h
// Original Author: M. Thaler (Modul BSY)
// Purpose: helper applications to consume cpu time
//*******************************************************************
#include <unistd.h>
#include <stdlib.h>
#include <time.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/time.h>
#include <stdio.h>
//------------------------------------------------------------------------------
#define MAX_CPUS 16
//------------------------------------------------------------------------------
void launchWorkLoad();
void stopWorkLoad();
void setRandom(void);
void justWork(unsigned int load);
void workHard(unsigned int low, unsigned int high);
void randomSleep(unsigned int low, unsigned int high);
pid_t startWorker(void);
void stopWorker(pid_t worker);