So I am up to measure time for n processes depending of their nice values. I fork the n processes from the parent process and all they have to do is to count a counter from 0 to X in an endless loop, each time printing the time that they needed for one count from 0 to X.
I guess it is a standard exercise in system programming labs.
I am saving the output of this experiment to a csv file to work out things like the avarege time of a process but.. I have broken output.
My code:
#include <stdio.h>
#include <stdlib.h>
#include <sys/resource.h>
#include <time.h>
#include <unistd.h>
#include <wait.h>
int exitSTATUS=0;
int *t = NULL;
void inputWarning() { printf("Falsche Eingabe"); }
long to_us(const struct timespec *spec) {
// inversion of to_spec from above ^
return spec->tv_sec * 1000000 + spec->tv_nsec / 1000;
}
void handleSIGINT_child(int sig){
exitSTATUS=1;
}
void handleSIGINT_parent(int sig){
if(t!= NULL)
free(t);
}
void testTime(int n, int *t, int X) {
pid_t pid;
int i;
for (i = 0; i < n; i++) {
pid = fork();
if (pid < 0) {
// TODO: Errer handling
} else if (pid == 0) {
signal(SIGINT,handleSIGINT_child);
setpriority(PRIO_PROCESS, 0, t[i]);
while (1) {
if(exitSTATUS>0) exit(0);
struct timespec delta = {0, 0};
long current_us = 0;
// clock for higher precision on difference
clock_gettime(CLOCK_REALTIME, &delta);
current_us = to_us(&delta);
int j;
for (j = 0; j < X; j++) {
}
clock_gettime(CLOCK_REALTIME, &delta);
long diff = to_us(&delta) - current_us;
printf("%i;%lu\n", i, diff);
}
}
}
if (pid > 0) {
signal(SIGINT,handleSIGINT_parent);
wait(NULL);
}
};
int main(int argc, char *argv[]) {
if (argc >= 4) {
int n = strtol((argv)[1], NULL, 10);
t = malloc(n*sizeof(int));
if (n >= 0 && argc >= n + 3) {
int i;
for (i = 0; i < n; i++) {
int niceval =
strtol(argv[i + 2], NULL, 10); // TODO : Auf parseError prüfen
t[i] = niceval;
}
int X = strtol(argv[n + 2], NULL, 10);
testTime(n, t, X);
free(t);
return 0;
}
}
inputWarning();
if (t != NULL) {
free(t);
}
return 0;
}
in the output I get strange things like
1;4287
1;4366
1;4774
1;4243
1;4492
10;8539
0;5856
0;6558
0;6462
0;5886
The 10 in the 6th line is wrong because i have only three processes. I guess the 1 is left from the output of the 1st process and 0 is the output of the 0th process.
I have two ideas concerning the reason for this. 1. The program is terminated by sigint. This causes an abrupt cut in the output. 2. The change between processes causes that cut.
In any case I just want to get my output nice and clean to work with it.
Any help would be appreciated. :)
EDIT:
just for reproducing:
the makefile:
OBJ=main.o
CC=gcc
CFLAGS=-g -Wall -Wextra -std=gnu11
app: $(OBJ)
$(CC) $(CFLAGS) -o $@ $(OBJ)
%.o: %.c
$(CC) $(CFLAGS) -c -o $@ $<
clean:
rm -f *.o app
and the run command
./app 3 19 0 0 2000000 > output.csv