Commit 7f7b1fe8 authored by Sander van Veen's avatar Sander van Veen

Added 'ass3' for 'Operating systems'.

Also added ignore list.
parent 2dc133fe
report/dot2tex.cache
report/report-dot2tex-*
report/report.pdf
report/report.toc
*.o
child.log*
*.swp
fishbones
PROG=Opgave3_VanVeen_Kroes
MKDIR=mkdir
PACK=tar -czvf
CC=gcc
LFLAGS=-lm
CP=cp
RM=rm -rf
CFLAGS = -std=c99 -pedantic -Wall -Wextra -lm
ifdef DEBUG
CFLAGS += -g -ggdb -DDEBUG
else
CFLAGS += -g0 -O3
endif
ifdef PROFILE
CFLAGS += -pg
endif
fishbones: fishbones.o
$(CC) $(CFLAGS) $(LFLAGS) -o $@ $^
clean:
rm -f fishbones.o fishbones $(PROG).tar.gz
rm -Rf logs/
rm -Rf $(PROG)/
tarball:
$(MAKE) clean
$(MKDIR) $(PROG)
$(CP) * report/report.pdf $(PROG) 2> /dev/null || true
$(PACK) $(PROG).tar.gz $(PROG)/*
$(RM) $(PROG)
%.o: %.c
$(CC) -c $(CFLAGS) -o $@ $<
fishbones.o: fishbones.c
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
static void gup(FILE * log1, FILE * log2, int pipefd[2], int cpid)
{
FILE * log3;
char c;
// Put any required declarations and initialisations here
// Close unused write end
close(pipefd[1]);
// Report your existence
fprintf(log1, "This is child number %d\n", cpid);
fprintf(log2, "This is child number %d\n", cpid);
// just for sure - open one more log file ...
log3 = fopen("child.log3", "wt+");
setvbuf(log3, NULL, _IOLBF, BUFSIZ);
// log3 is line buffered - a line of output is written
// immediately on encountering an end-of-line
fprintf(log3, "This is child number %d\n", cpid);
while( read(pipefd[0], &c, 1) > 0 )
{
// Read 1 character from the pipe into 'c' here.
// Be sure to test that you have indeed read a character.
// If you get an error (read returns a negative value) or
// an end-of-file (read returns zero), end your program
// with an exit(1)
fprintf(log1, "Child %d read character '%x'\n", cpid, c);
fprintf(log2, "Child %d read character '%x'\n", cpid, c);
fprintf(log3, "Child %d read character '%x'\n", cpid, c);
if( write(STDOUT_FILENO, &c, 1) == -1 )
{
fprintf(stderr, "write error: %s\n", strerror(errno));
exit(EXIT_FAILURE);
}
// process the character here
}
char nl = '\n';
if( write(STDOUT_FILENO, &nl, 1) == -1 )
{
fprintf(stderr, "write error: %s\n", strerror(errno));
exit(EXIT_FAILURE);
}
fprintf(log1, "Child %d normal exit \n", cpid);
fprintf(log2, "Child %d normal exit \n", cpid);
fprintf(log3, "Child %d normal exit \n", cpid);
fclose(log1);
fclose(log2);
fclose(log3);
}
#define STRING_LENGTH (512)
int
main(void)
{
FILE * log1;
FILE * log2;
char buf;
int kiddoCount = 0;
int pipefd[2];
log1 = fopen("child.log1", "wt+");
log2 = fopen("child.log2", "wt+");
setvbuf(log2, NULL, _IONBF, BUFSIZ);
// So log1 is buffered; log2 is not buffered, which means that output
// to log2 is written immediately.
// create the pipe somewhere around here (why?).
// remember to test for success!!
printf("while..\n");
if( pipe(pipefd) == -1 )
{
perror("pipe");
exit(EXIT_FAILURE);
}
pid_t cpid;
while( (buf = getchar()) )
{
// You can now choose to use only the first character in the
// string (inString[0]), or the entire string (how would you test
// that?).
// Anyway, read a character, echo it to both log files
// and write it into the pipe (unless you have to call fork)
printf("%c", buf);
switch( buf )
{
case 'q':
goto close_logs;
break;
case 'f':
kiddoCount++;
// When you fork, be sure to print the process id of the child
// (if the fork succeeds) and an error message otherwise
cpid = fork();
if( cpid == -1 )
{
perror("fork");
exit(EXIT_FAILURE);
}
if( cpid == 0 )
{
// Child reads from pipe u1
gup(log1, log2, pipefd, kiddoCount);
_exit(EXIT_SUCCESS);
}
else
{
/* Parent writes argv[1] to pipe */
// Close unused read end
close(pipefd[0]);
//if( write(pipefd[1], argv[1], strlen(argv[1])) == -1 )
//{
// fprintf(stderr, "write error: %s\n", strerror(errno));
// exit(EXIT_FAILURE);
//}
close(pipefd[1]); /* Reader will see EOF */
wait(NULL); /* Wait for child */
exit(EXIT_SUCCESS);
}
default:
printf("buf => default => exit\n");
//exit(EXIT_SUCCESS);
fflush(stdout);
goto close_logs;
}
}
// normal end of program
close_logs:
// close all open log files
fclose(log1);
fclose(log2);
return 0;
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment