Commit 4b709e57 authored by Taddeüs Kroes's avatar Taddeüs Kroes

- Added framework to OS ass3.

parent 52da9aab
PROG = fish
CC = gcc
CFLAGS = -std=c99 -pedantic -Wall -Wextra -O3
OFILES = fishbones.o
RM = rm -f
$(PROG): $(OFILES)
$(CC) $(CFLAGS) -o $@ $(OFILES)
%.o: %.c
$(CC) $(CFLAGS) -o $@ -c $<
clean:
$(RM) $(OFILES) $(PROG)
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
static void gup(FILE * log1, FILE * log2, int pipe_id[2], int myNumber)
{
FILE * log3;
char c;
/* Put any required declarations and initialisations here */
close(pipe_id[1]);
/* Report your existence */
fprintf(log1, "This is child number %d\n", myNumber);
fprintf(log2, "This is child number %d\n", myNumber);
/* just for sure - open one more log file .... */
log3 = fopen("child.log3", "wt+");
setvbuf(log3, NULL, _IOLBF, BUFSIZ);
/* log3 is line buffered - a line of output is written
immediately on encountering an end-of-line */
fprintf(log3, "This is child number %d\n", myNumber);
do
{
/* Read 1 character from the pipe into 'c' here.
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) */
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 * );*/
fprintf(log1, "Child %d normal exit \n", myNumber);
fprintf(log2, "Child %d normal exit \n", myNumber);
fprintf(log3, "Child %d normal exit \n", myNumber);
fclose(log1);
fclose(log2);
fclose(log3);
exit(0);
}
#define STRING_LENGTH (512)
int
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);
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. */
/* create the pipe somewhere around here (why?).
remember to test for success!! */
while( fgets(inString, STRING_LENGTH, stdin) )
{
/* You can now choose to use only the first character in the
string (inString[0]), or the entire string (how would you test
that?).
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 */ )
{
/* write to pipe */
}
else
{
pid_t child_id;
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
{
/* What goes here ? */
}
}
}
/* normal end of program */
/* close all open log files */
return 0;
}
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