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,45 @@
//***************************************************************************
// File: ProcA9_1.c
// Original Author: M. Thaler (Modul BSY)
//***************************************************************************
//***************************************************************************
// system includes
//***************************************************************************
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
//***************************************************************************
// Function: main(), parameter: none
//***************************************************************************
int main(void) {
pid_t pid;
printf("\n");
printf("\nHallo, I am on the way to fork now, ......lo");
pid = fork();
switch (pid) {
case -1:
perror("Could not fork");
break;
case 0:
printf("ok: I am the child\n");
break;
default:
printf("ok: I am the parent\n");
break;
}
printf("\nclear ?\n\n");
exit(0);
}
//***************************************************************************
@@ -0,0 +1,87 @@
//***************************************************************************
// File: ProcA9_2.c
// Original Author: M. Thaler (Modul BSY)
//***************************************************************************
//***************************************************************************
// system includes
//***************************************************************************
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/time.h>
#include <unistd.h>
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#define WAIT_TIME (200*1000) // 0.2 s with usleep
// globaler array
#define ARRAY_SIZE 8
char GArray[ARRAY_SIZE][ARRAY_SIZE];
//***************************************************************************
// Function: main(), parameter: none
//***************************************************************************
int main(void) {
pid_t pid;
int i,j;
// flip coin to select "child first" or "parent first"
struct timeval tv;
gettimeofday(&tv, NULL);
srandom(tv.tv_usec); // evaluate seed
int head = (int)(random()) >> 7; // flip coin
head &= 0x1;
// fill global array with '-' and print array value
for (i = 0; i < ARRAY_SIZE; i++) {
for (j = 0; j < ARRAY_SIZE; j++) {
GArray[i][j] = '-';
printf("%c ", GArray[i][j]);
}
printf("\n");
}
fflush(stdout);
pid = fork();
switch (pid) {
case -1:
perror("Could not fork");
break;
case 0: // --- child fills upper half of array with 'c'
if (head) usleep(WAIT_TIME);
for (i = ARRAY_SIZE / 2; i < ARRAY_SIZE; i++)
for (j = 0; j < ARRAY_SIZE; j++)
GArray[i][j] = 'c';
break;
default: // --- parent fills lower half of array with 'p'
if (! head) usleep(WAIT_TIME);
for (i = 0; i < ARRAY_SIZE / 2; i++)
for (j = 0; j < ARRAY_SIZE; j++)
GArray[i][j] = 'p';
break;
}
if (pid == 0)
printf("\nKinderarray\n\n");
else
printf("\nElternarray\n\n");
for (i = 0; i < ARRAY_SIZE; i++) {
for (j = 0; j < ARRAY_SIZE; j++)
printf("%c ", GArray[i][j]);
printf("\n");
}
fflush(stdout);
if (pid > 0) wait(NULL);
exit(0);
}
//***************************************************************************
@@ -0,0 +1,74 @@
//***************************************************************************
// File: ProcA9_3.c
// Original Author: M. Thaler (Modul BSY)
//***************************************************************************
//***************************************************************************
// system includes
//***************************************************************************
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <sched.h>
#include "workerUtils.h"
#define ANZAHL 15
#define WORK_HARD 1000000
//*****************************************************************************
// Function: main(), parameter: none
//*****************************************************************************
int main(void) {
FILE *fdes;
pid_t pid;
int i;
launchWorkLoad(); // start CPU load to force context switches
fdes = fopen("AnyOutPut.txt", "w");
if (fdes == NULL) perror("Cannot open file");
usleep(500000);
pid = fork();
switch (pid) {
case -1:
perror("Could not fork");
break;
case 0:
for (i = 1; i <= ANZAHL; i++) {
fprintf(fdes, "Fritzli\t%d\n", i);
fflush(fdes); // make sure date is written to file
justWork(WORK_HARD);
}
break;
default:
for (i = 1; i <= ANZAHL; i++) {
fprintf(fdes, "Mami\t%d\n", i);
fflush(fdes); // make sure date is written to file
justWork(WORK_HARD);
}
fflush(stdout);
stopWorkLoad();
break;
}
printf("We are done\n");
if (pid > 0) {
waitpid(pid, NULL, 0);
printf("See file AnyOutPut.txt\n");
}
fflush(stdout);
exit(0);
}
//*****************************************************************************
@@ -0,0 +1,38 @@
# *************************************************************
# Original Autor: M. Thaler (Modul BSY)
# *************************************************************
CMP= gcc -std=gnu99
CMPFLAGS= -Wall
LDFLAGS=
EXENAME1= ProcA8_1.e
FNAM1= ProcA8_1.o
EXENAME2= ProcA8_2.e
FNAM2= ProcA8_2.o
EXENAME3= ProcA8_3.e
FNAM3= ProcA8_3.o workerUtils.o
LIBNAME=
compile: $(EXENAME1) $(EXENAME2) $(EXENAME3)
$(EXENAME1): $(FNAM1)
$(CMP) $(CMPFLAGS) $(FNAM1) $(LIBNAME) $(LDFLAGS) -o $@
$(EXENAME2): $(FNAM2)
$(CMP) $(CMPFLAGS) $(FNAM2) $(LIBNAME) $(LDFLAGS) -o $@
$(EXENAME3): $(FNAM3)
$(CMP) $(CMPFLAGS) $(FNAM3) $(LIBNAME) $(LDFLAGS) -o $@
.c.o:
$(CMP) -c $(CMPFLAGS) $<
.cc.o:
$(CMP) -c $(CMPFLAGS) $<
clean:
rm -f *.o $(EXENAME1) $(EXENAME2) $(EXENAME3)
all:
@make clean
@make
@@ -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,25 @@
//*******************************************************************
// File: workerUtils.h
// Original Author: M. Thaler (Modul BSY)
//*******************************************************************
#ifndef WORKER_UTILS
#define WORKER_UTILS
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);
#endif