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 @@ ...@@ -324,20 +324,20 @@
} }
\section{Toekomst} \section{Toekomst}
\frame \frame
{ {
\frametitle{Toekomstbeeld} \frametitle{Toekomstbeeld}
\begin{itemize} \begin{itemize}
\item Verschillende vakken \item Verschillende vakken
\item Nakijkmodule \item Nakijkmodule
\item Automatisch nakijken \item Automatisch nakijken
\begin{itemize} \begin{itemize}
\item Mini unit tests \item Mini unit tests
\item Fuzzy matching van code \item Fuzzy matching van code
\end{itemize} \end{itemize}
\item Met meerdere gebruikers aan dezelfde code werken \item Met meerdere gebruikers aan dezelfde code werken
\end{itemize} \end{itemize}
} }
......
...@@ -2,11 +2,14 @@ ...@@ -2,11 +2,14 @@
#include <stdlib.h> #include <stdlib.h>
#include <sys/types.h> #include <sys/types.h>
#include <unistd.h> #include <unistd.h>
#include <errno.h>
#include <signal.h>
static void gup(FILE * log1, FILE * log2, int pipe_id[2], int myNumber) static void gup(FILE * log1, FILE * log2, int pipe_id[2], int myNumber)
{ {
FILE * log3; FILE * log3;
char c; char c;
int antidote = 0;
/* Put any required declarations and initialisations here */ /* Put any required declarations and initialisations here */
close(pipe_id[1]); close(pipe_id[1]);
...@@ -31,12 +34,53 @@ static void gup(FILE * log1, FILE * log2, int pipe_id[2], int myNumber) ...@@ -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 an end-of-file (read returns zero), end your program
with an exit(1) */ 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(log1, "Child %d read character '%x'\n", myNumber, c);
fprintf(log2, "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); fprintf(log3, "Child %d read character '%x'\n", myNumber, c);
/* process the character here */ switch( c )
} while ( 0);/* Put a suitable condition here * );*/ {
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(log1, "Child %d normal exit \n", myNumber);
fprintf(log2, "Child %d normal exit \n", myNumber); fprintf(log2, "Child %d normal exit \n", myNumber);
...@@ -54,19 +98,25 @@ main(int argc, char * argv[]) ...@@ -54,19 +98,25 @@ main(int argc, char * argv[])
{ {
FILE * log1; FILE * log1;
FILE * log2; FILE * log2;
char inString[STRING_LENGTH]; char c;
int kiddoCount = 0, pipe_id[2], i = 1; int kiddoCount = 0, pipe_id[2];//, i = 1;
printf("argc: %d\n",argc); printf("argc: %d\n", argc);
log1 = fopen("child.log1", "wt+"); log1 = fopen("child.log1", "wt+");
log2 = fopen("child.log2", "wt+"); log2 = fopen("child.log2", "wt+");
setvbuf(log2, NULL, _IONBF, BUFSIZ); setvbuf(log2, NULL, _IONBF, BUFSIZ);
/* So log1 is buffered; log2 is not buffered, which means that output /* So log1 is buffered; log2 is not buffered, which means that output
to log2 is written immediately. */ 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?). /* create the pipe somewhere around here (why?).
remember to test for success!! */ 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 /* You can now choose to use only the first character in the
string (inString[0]), or the entire string (how would you test string (inString[0]), or the entire string (how would you test
...@@ -74,33 +124,60 @@ main(int argc, char * argv[]) ...@@ -74,33 +124,60 @@ main(int argc, char * argv[])
Anyway, read a character, echo it to both log files Anyway, read a character, echo it to both log files
and write it into the pipe (unless you have to call fork) 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 else
{ {
pid_t child_id; pid_t child_id;
kiddoCount ++; kiddoCount++;
/* When you fork, be sure to print the process id of the child /* When you fork, be sure to print the process id of the child
(if the fork succeeds) and an error message otherwise */ (if the fork succeeds) and an error message otherwise */
if( child_id = fork() ) switch( (child_id = fork()) )
{
/* What code should go here ? */
}
else
{ {
/* 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 */ fprintf(log1, "Program exited normally \n");
/* close all open log files */ fprintf(log2, "Program exited normally \n");
fclose(log1);
fclose(log2);
return 0; return 0;
} }
\documentclass[twocolumn]{article} TT\documentclass[twocolumn]{article}
\usepackage{url} \usepackage{url}
\title{Web-based digital examination for specific learning areas} \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