Commit 0cb7be9e authored by Taddeüs Kroes's avatar Taddeüs Kroes

- Worked on OS ass3.

parent 02d840a8
PROG = fish
CC = gcc
CFLAGS = -std=c99 -pedantic -Wall -Wextra -O3
CFLAGS = -std=gnu99 -pedantic -Wall -Wextra -O3
OFILES = fishbones.o
RM = rm -f
......
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <errno.h>
#include <signal.h>
......@@ -45,13 +46,12 @@ static void gup(FILE * log1, FILE * log2, int pipe_id[2], int myNumber)
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);
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(log3, "Child %d read character %x: '%c'\n", myNumber, c, c);
switch( c )
{
case 'P':
if( c == 'P' )
{
fprintf(log1, "Child %d eats poison...", myNumber);
fprintf(log2, "Child %d eats poison...", myNumber);
fprintf(log3, "Child %d eats poison...", myNumber);
......@@ -71,7 +71,9 @@ static void gup(FILE * log1, FILE * log2, int pipe_id[2], int myNumber)
fprintf(log3, "antidote used \n");
}
break;
case 'A':
}
else if( c == 'A' )
{
antidote++;
fprintf(log1, "Child %d finds antidote (now has %d andidote)\n",
myNumber, antidote);
......@@ -91,7 +93,22 @@ static void gup(FILE * log1, FILE * log2, int pipe_id[2], int myNumber)
exit(0);
}
#define STRING_LENGTH (512)
void
signal_handler(int action)
{
switch( action )
{
case SIGINT:
break;
case SIGTERM:
break;
case SIGCHLD:
break;
}
}
int
main(int argc, char * argv[])
......@@ -100,6 +117,8 @@ main(int argc, char * argv[])
FILE * log2;
char c;
int kiddoCount = 0, pipe_id[2];//, i = 1;
struct sigaction action;
printf("argc: %d\n", argc);
log1 = fopen("child.log1", "wt+");
log2 = fopen("child.log2", "wt+");
......@@ -107,15 +126,25 @@ main(int argc, char * argv[])
/* 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!! */
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!! */
}
//action.sa_handler = signal_handler;
//if( sigaction(SIGINT, &action, NULL) < 0
// || sigaction(SIGTERM, &action, NULL) < 0
// || sigaction(SIGCHLD, &action, NULL) < 0 )
//{
// perror("An error occured while binding the signal handlers \n");
// exit(1);
//}
while( (c = getchar()) )
{
/* You can now choose to use only the first character in the
......@@ -131,8 +160,8 @@ main(int argc, char * argv[])
continue;
}
fprintf(log1, "Character %x read \n", c);
fprintf(log2, "Character %x read \n", c);
fprintf(log1, "Parent read character %x: '%c'\n", c, c);
fprintf(log2, "Parent read character %x: '%c'\n", c, c);
printf("Character '%c' read \n", c);
if( c == 'q' )
......@@ -141,7 +170,7 @@ main(int argc, char * argv[])
}
else if( c == 'P' )
{
wait();
wait(NULL);
}
else if( c != 'f' )
{
......
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