Commit 5b5248e7 authored by Taddeüs Kroes's avatar Taddeüs Kroes

- Some final changes to OS ass3.

parent cfbf4096
...@@ -37,17 +37,17 @@ static void gup(FILE * log1, FILE * log2, int pipe_id[2], int myNumber) ...@@ -37,17 +37,17 @@ static void gup(FILE * log1, FILE * log2, int pipe_id[2], int myNumber)
{ {
/* Error while reading from the pipe, exit */ /* Error while reading from the pipe, exit */
fprintf(log1, "Child %d read from pipe with error " fprintf(log1, "Child %d read from pipe with error "
"and exited \n", myNumber); "and exited \n", myNumber);
fprintf(log2, "Child %d read from pipe with error " fprintf(log2, "Child %d read from pipe with error "
"and exited \n", myNumber); "and exited \n", myNumber);
fprintf(log3, "Child %d read from pipe with error " fprintf(log3, "Child %d read from pipe with error "
"and exited \n", myNumber); "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 */ /* 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);
...@@ -55,35 +55,35 @@ static void gup(FILE * log1, FILE * log2, int pipe_id[2], int myNumber) ...@@ -55,35 +55,35 @@ static void gup(FILE * log1, FILE * log2, int pipe_id[2], int myNumber)
if( c == 'P' ) if( c == 'P' )
{ {
/* Decrease antidote level or exit the process */ /* 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 "
"and will exit \n", myNumber); "and will exit \n", myNumber);
fprintf(log2, "Child %d has no antidote for poison " fprintf(log2, "Child %d has no antidote for poison "
"and will exit \n", myNumber); "and will exit \n", myNumber);
fprintf(log3, "Child %d has no antidote for poison " fprintf(log3, "Child %d has no antidote for poison "
"and will exit \n", myNumber); "and will exit \n", myNumber);
break; break;
} }
else else
{ {
antidote--; antidote--;
fprintf(log1, "antidote used (%d left) \n", antidote); fprintf(log1, "antidote used (%d left) \n", antidote);
fprintf(log2, "antidote used (%d left) \n", antidote); fprintf(log2, "antidote used (%d left) \n", antidote);
fprintf(log3, "antidote used (%d left) \n", antidote); fprintf(log3, "antidote used (%d left) \n", antidote);
} }
} }
else if( c == 'A' ) else if( c == 'A' )
{ {
/* Increase antidote level */ /* 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);
fprintf(log2, "Child %d finds antidote (now has %d) \n", fprintf(log2, "Child %d finds antidote (now has %d) \n",
myNumber, antidote); myNumber, antidote);
fprintf(log3, "Child %d finds antidote (now has %d) \n", fprintf(log3, "Child %d finds antidote (now has %d) \n",
myNumber, antidote); myNumber, antidote);
} }
} while( c != '\0' ); } while( c != '\0' );
...@@ -103,7 +103,7 @@ static void gup(FILE * log1, FILE * log2, int pipe_id[2], int myNumber) ...@@ -103,7 +103,7 @@ static void gup(FILE * log1, FILE * log2, int pipe_id[2], int myNumber)
void void
signal_handler(int signum) signal_handler(int signum)
{ {
char *sig = "TERM"; char * sig = "TERM";
pid_t pid; pid_t pid;
switch( signum ) switch( signum )
...@@ -129,8 +129,6 @@ signal_handler(int signum) ...@@ -129,8 +129,6 @@ signal_handler(int signum)
default: default:
puts("This handler is unapplicable for this type of signal"); puts("This handler is unapplicable for this type of signal");
} }
errno = 0;
} }
/* /*
...@@ -147,13 +145,9 @@ main(int argc, char * argv[]) ...@@ -147,13 +145,9 @@ main(int argc, char * argv[])
/* Save parent process id for signal handler */ /* Save parent process id for signal handler */
parent_pid = getpid(); parent_pid = getpid();
/* Open one buffered and one unbuffered log */
printf("argc: %d\n", argc);
log1 = fopen("child.log1", "wt+");
log2 = fopen("child.log2", "wt+");
setvbuf(log2, NULL, _IONBF, BUFSIZ);
printf("argc: %d\n", argc);
/* Create pipe, exit with error on failure */ /* Create pipe, exit with error on failure */
if( pipe(pipe_id) ) if( pipe(pipe_id) )
{ {
...@@ -164,13 +158,18 @@ main(int argc, char * argv[]) ...@@ -164,13 +158,18 @@ main(int argc, char * argv[])
/* Bind signal handlers */ /* Bind signal handlers */
if( sigaction(SIGINT, &action, NULL) if( sigaction(SIGINT, &action, NULL)
+ sigaction(SIGTERM, &action, NULL) || sigaction(SIGTERM, &action, NULL)
+ sigaction(SIGCHLD, &action, NULL) < 0 ) || sigaction(SIGCHLD, &action, NULL) )
{ {
perror("An error occured while binding the signal handlers \n"); perror("An error occured while binding the signal handlers \n");
exit(1); exit(1);
} }
/* Open one buffered and one unbuffered log */
log1 = fopen("child.log1", "wt+");
log2 = fopen("child.log2", "wt+");
setvbuf(log2, NULL, _IONBF, BUFSIZ);
/* Keep reading input */ /* Keep reading input */
while( (c = getchar()) ) while( (c = getchar()) )
{ {
......
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