Commit 02d840a8 authored by Taddeüs Kroes's avatar Taddeüs Kroes

- Implemented step 1-6 of OS assignment 3.

parent 4b709e57
......@@ -324,20 +324,20 @@
}
\section{Toekomst}
\frame
{
\frametitle{Toekomstbeeld}
\begin{itemize}
\item Verschillende vakken
\item Nakijkmodule
\item Automatisch nakijken
\item Verschillende vakken
\item Nakijkmodule
\item Automatisch nakijken
\begin{itemize}
\item Mini unit tests
\item Fuzzy matching van code
\end{itemize}
\item Met meerdere gebruikers aan dezelfde code werken
\item Met meerdere gebruikers aan dezelfde code werken
\end{itemize}
}
......
......@@ -2,11 +2,14 @@
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <errno.h>
#include <signal.h>
static void gup(FILE * log1, FILE * log2, int pipe_id[2], int myNumber)
{
FILE * log3;
char c;
int antidote = 0;
/* Put any required declarations and initialisations here */
close(pipe_id[1]);
......@@ -31,12 +34,53 @@ static void gup(FILE * log1, FILE * log2, int pipe_id[2], int myNumber)
an end-of-file (read returns zero), end your program
with an exit(1) */
if( read(pipe_id[0], &c, 1) <= 0 )
{
fprintf(log1, "Child %d exited with error \n", myNumber);
fprintf(log2, "Child %d exited with error \n", myNumber);
fprintf(log3, "Child %d exited with error \n", myNumber);
fclose(log1);
fclose(log2);
fclose(log3);
exit(1);
}
fprintf(log1, "Child %d read character '%x'\n", myNumber, c);
fprintf(log2, "Child %d read character '%x'\n", myNumber, c);
fprintf(log3, "Child %d read character '%x'\n", myNumber, c);
/* process the character here */
} while ( 0);/* Put a suitable condition here * );*/
switch( c )
{
case 'P':
fprintf(log1, "Child %d eats poison...", myNumber);
fprintf(log2, "Child %d eats poison...", myNumber);
fprintf(log3, "Child %d eats poison...", myNumber);
if( !antidote )
{
fprintf(log1, "no antidote, child will exit\n");
fprintf(log2, "no antidote, child will exit\n");
fprintf(log3, "no antidote, child will exit\n");
break;
}
else
{
antidote--;
fprintf(log1, "antidote used \n");
fprintf(log2, "antidote used \n");
fprintf(log3, "antidote used \n");
}
break;
case 'A':
antidote++;
fprintf(log1, "Child %d finds antidote (now has %d andidote)\n",
myNumber, antidote);
fprintf(log2, "Child %d finds antidote (now has %d andidote)\n",
myNumber, antidote);
fprintf(log3, "Child %d finds antidote (now has %d andidote)\n",
myNumber, antidote);
}
} while( c != '\0' );
fprintf(log1, "Child %d normal exit \n", myNumber);
fprintf(log2, "Child %d normal exit \n", myNumber);
......@@ -54,19 +98,25 @@ main(int argc, char * argv[])
{
FILE * log1;
FILE * log2;
char inString[STRING_LENGTH];
int kiddoCount = 0, pipe_id[2], i = 1;
printf("argc: %d\n",argc);
char c;
int kiddoCount = 0, pipe_id[2];//, i = 1;
printf("argc: %d\n", argc);
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. */
if( pipe(pipe_id) )
{
fprintf(stderr, "Error %d occured while createing the pipe \n", errno);
exit(1);
}
/* create the pipe somewhere around here (why?).
remember to test for success!! */
while( fgets(inString, STRING_LENGTH, stdin) )
while( (c = getchar()) )
{
/* You can now choose to use only the first character in the
string (inString[0]), or the entire string (how would you test
......@@ -74,33 +124,60 @@ main(int argc, char * argv[])
Anyway, read a character, echo it to both log files
and write it into the pipe (unless you have to call fork)
*/
if( /* suitable condition */ )
/* Ignore newlines and spaces */
if( c == '\n' || (int)c == 32 )
{
/* write to pipe */
continue;
}
fprintf(log1, "Character %x read \n", c);
fprintf(log2, "Character %x read \n", c);
printf("Character '%c' read \n", c);
if( c == 'q' )
{
break;
}
else if( c == 'P' )
{
wait();
}
else if( c != 'f' )
{
if( write(pipe_id[1], &c, 1) != 1 )
{
perror("Cannot write to pipe \n");
exit(1);
}
}
else
{
pid_t child_id;
kiddoCount ++;
kiddoCount++;
/* When you fork, be sure to print the process id of the child
(if the fork succeeds) and an error message otherwise */
if( child_id = fork() )
{
/* What code should go here ? */
}
else
switch( (child_id = fork()) )
{
/* What goes here ? */
case -1:
perror("Child could not be created \n");
exit(1);
case 0:
gup(log1, log2, pipe_id, child_id);
break;
default:
puts("Parent process continuing...");
}
}
}
/* normal end of program */
/* close all open log files */
fprintf(log1, "Program exited normally \n");
fprintf(log2, "Program exited normally \n");
fclose(log1);
fclose(log2);
return 0;
}
\documentclass[twocolumn]{article}
TT\documentclass[twocolumn]{article}
\usepackage{url}
\title{Web-based digital examination for specific learning areas}
......
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