Commit e2b4672d authored by Taddeüs Kroes's avatar Taddeüs Kroes

- Added comment to OS ass3.

parent cddefcf4
...@@ -29,29 +29,30 @@ static void gup(FILE * log1, FILE * log2, int pipe_id[2], int myNumber) ...@@ -29,29 +29,30 @@ static void gup(FILE * log1, FILE * log2, int pipe_id[2], int myNumber)
do do
{ {
/* Read 1 character from the pipe into 'c' here. /* Read next character from pipe */
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) */
if( read(pipe_id[0], &c, 1) <= 0 ) if( read(pipe_id[0], &c, 1) <= 0 )
{ {
fprintf(log1, "Child %d exited with error \n", myNumber); /* Error while reading from the pipe, exit */
fprintf(log2, "Child %d exited with error \n", myNumber); fprintf(log1, "Child %d read from pipe with error "
fprintf(log3, "Child %d exited with error \n", myNumber); "and exited \n", myNumber);
fprintf(log2, "Child %d read from pipe with error "
"and exited \n", myNumber);
fprintf(log3, "Child %d read from pipe with error "
"and exited \n", myNumber);
fclose(log1); fclose(log1);
fclose(log2); fclose(log2);
fclose(log3); fclose(log3);
exit(1); exit(1);
} }
/* Character read successfully, process it */
fprintf(log1, "Child %d read character %x: '%c'\n", myNumber, c, c); fprintf(log1, "Child %d read character %x: '%c'\n", myNumber, c, c);
fprintf(log2, "Child %d read character %x: '%c'\n", myNumber, c, c); fprintf(log2, "Child %d read character %x: '%c'\n", myNumber, c, c);
fprintf(log3, "Child %d read character %x: '%c'\n", myNumber, c, c); fprintf(log3, "Child %d read character %x: '%c'\n", myNumber, c, c);
if( c == 'P' ) if( c == 'P' )
{ {
/* Decrease antidote level or exit the process */
if( !antidote ) if( !antidote )
{ {
fprintf(log1, "Child %d has no antidote for poison " fprintf(log1, "Child %d has no antidote for poison "
...@@ -72,6 +73,7 @@ static void gup(FILE * log1, FILE * log2, int pipe_id[2], int myNumber) ...@@ -72,6 +73,7 @@ static void gup(FILE * log1, FILE * log2, int pipe_id[2], int myNumber)
} }
else if( c == 'A' ) else if( c == 'A' )
{ {
/* Increase antidote level */
antidote++; antidote++;
fprintf(log1, "Child %d finds antidote (now has %d) \n", fprintf(log1, "Child %d finds antidote (now has %d) \n",
myNumber, antidote); myNumber, antidote);
...@@ -82,6 +84,7 @@ static void gup(FILE * log1, FILE * log2, int pipe_id[2], int myNumber) ...@@ -82,6 +84,7 @@ static void gup(FILE * log1, FILE * log2, int pipe_id[2], int myNumber)
} }
} while( c != '\0' ); } while( c != '\0' );
/* Close logs and exit normally */
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);
fprintf(log3, "Child %d normal exit \n", myNumber); fprintf(log3, "Child %d normal exit \n", myNumber);
...@@ -92,9 +95,9 @@ static void gup(FILE * log1, FILE * log2, int pipe_id[2], int myNumber) ...@@ -92,9 +95,9 @@ static void gup(FILE * log1, FILE * log2, int pipe_id[2], int myNumber)
} }
void void
signal_handler(int action) signal_handler(int signum)
{ {
switch( action ) switch( signum )
{ {
case SIGINT: case SIGINT:
...@@ -134,15 +137,15 @@ main(int argc, char * argv[]) ...@@ -134,15 +137,15 @@ main(int argc, char * argv[])
exit(1); exit(1);
} }
//action.sa_handler = signal_handler; action.sa_handler = signal_handler;
//if( sigaction(SIGINT, &action, NULL) < 0 if( sigaction(SIGINT, &action, NULL) < 0
// || sigaction(SIGTERM, &action, NULL) < 0 || sigaction(SIGTERM, &action, NULL) < 0
// || sigaction(SIGCHLD, &action, NULL) < 0 ) || sigaction(SIGCHLD, &action, NULL) < 0 )
//{ {
// perror("An error occured while binding the signal handlers \n"); perror("An error occured while binding the signal handlers \n");
// exit(1); exit(1);
//} }
while( (c = getchar()) ) while( (c = getchar()) )
{ {
...@@ -165,16 +168,19 @@ main(int argc, char * argv[]) ...@@ -165,16 +168,19 @@ main(int argc, char * argv[])
if( c == 'q' ) if( c == 'q' )
{ {
/* Exit program */
break; break;
} }
else if( c != 'f' ) else if( c != 'f' )
{ {
/* Write character to pipe for child processes */
if( write(pipe_id[1], &c, 1) != 1 ) if( write(pipe_id[1], &c, 1) != 1 )
{ {
perror("Cannot write to pipe \n"); perror("Cannot write to pipe \n");
exit(1); exit(1);
} }
/* Wait for child process to 'eat poison' */
if( c == 'P' ) if( c == 'P' )
wait(NULL); wait(NULL);
} }
...@@ -190,12 +196,15 @@ main(int argc, char * argv[]) ...@@ -190,12 +196,15 @@ main(int argc, char * argv[])
switch( (child_id = fork()) ) switch( (child_id = fork()) )
{ {
case -1: case -1:
/* An error occured while forking */
perror("Child could not be created \n"); perror("Child could not be created \n");
exit(1); exit(1);
case 0: case 0:
/* Child process after fork: read from pipe */
gup(log1, log2, pipe_id, kiddoCount); gup(log1, log2, pipe_id, kiddoCount);
break; break;
default: default:
/* Parent proces after fork: Print child's process id */
fprintf(log1, "Started child process %d with id %d \n", fprintf(log1, "Started child process %d with id %d \n",
kiddoCount, child_id); kiddoCount, child_id);
fprintf(log2, "Started child process %d with id %d \n", fprintf(log2, "Started child process %d with id %d \n",
...@@ -204,6 +213,7 @@ main(int argc, char * argv[]) ...@@ -204,6 +213,7 @@ main(int argc, char * argv[])
} }
} }
/* Close logs and exit normally */
fprintf(log1, "Program exited normally \n"); fprintf(log1, "Program exited normally \n");
fprintf(log2, "Program exited normally \n"); fprintf(log2, "Program exited normally \n");
fclose(log1); fclose(log1);
......
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