Code cleanup.

parent cfbf4096
...@@ -37,11 +37,11 @@ static void gup(FILE * log1, FILE * log2, int pipe_id[2], int myNumber) ...@@ -37,11 +37,11 @@ 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);
...@@ -59,38 +59,38 @@ static void gup(FILE * log1, FILE * log2, int pipe_id[2], int myNumber) ...@@ -59,38 +59,38 @@ static void gup(FILE * log1, FILE * log2, int pipe_id[2], int myNumber)
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' );
/* Close logs and exit normally */ /* 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);
fclose(log1); fclose(log1);
fclose(log2); fclose(log2);
fclose(log3); fclose(log3);
...@@ -113,18 +113,18 @@ signal_handler(int signum) ...@@ -113,18 +113,18 @@ signal_handler(int signum)
case SIGTERM: case SIGTERM:
if( (pid = getpid()) == parent_pid ) if( (pid = getpid()) == parent_pid )
{ {
printf("Parent process received SIG%s and will exit \n", sig); printf("Parent process received SIG%s and will exit\n", sig);
} }
else else
{ {
printf("Process with id %d received SIG%s and will exit \n", printf("Process with id %d received SIG%s and will exit\n",
pid, sig); pid, sig);
} }
exit(1); exit(1);
case SIGCHLD: case SIGCHLD:
puts("Parent process received SIGCHLD"); puts("Parent process received SIGCHLD");
pid = wait(NULL); pid = wait(NULL);
printf("Child with id %d exited \n", pid); printf("Child with id %d exited\n", pid);
break; break;
default: default:
puts("This handler is unapplicable for this type of signal"); puts("This handler is unapplicable for this type of signal");
...@@ -157,7 +157,7 @@ main(int argc, char * argv[]) ...@@ -157,7 +157,7 @@ main(int argc, char * argv[])
/* Create pipe, exit with error on failure */ /* Create pipe, exit with error on failure */
if( pipe(pipe_id) ) if( pipe(pipe_id) )
{ {
fprintf(stderr, "Error %d occured while creating the pipe \n", fprintf(stderr, "Error %d occured while creating the pipe\n",
errno); errno);
exit(1); exit(1);
} }
...@@ -167,7 +167,7 @@ main(int argc, char * argv[]) ...@@ -167,7 +167,7 @@ main(int argc, char * argv[])
+ sigaction(SIGTERM, &action, NULL) + sigaction(SIGTERM, &action, NULL)
+ 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);
} }
...@@ -198,7 +198,7 @@ main(int argc, char * argv[]) ...@@ -198,7 +198,7 @@ main(int argc, char * argv[])
fprintf(log1, "Parent read character %x: '%c'\n", c, c); fprintf(log1, "Parent read character %x: '%c'\n", c, c);
fprintf(log2, "Parent read character %x: '%c'\n", c, c); fprintf(log2, "Parent read character %x: '%c'\n", c, c);
printf("Character '%c' read \n", c); printf("Character '%c' read\n", c);
if( c == 'q' ) if( c == 'q' )
{ {
...@@ -210,7 +210,7 @@ main(int argc, char * argv[]) ...@@ -210,7 +210,7 @@ main(int argc, char * argv[])
/* Write character to pipe for child processes */ /* Write character to pipe for child processes */
if( write(pipe_id[1], &c, 1) <= 0 ) if( write(pipe_id[1], &c, 1) <= 0 )
{ {
perror("Cannot write to pipe \n"); perror("Cannot write to pipe\n");
exit(1); exit(1);
} }
...@@ -231,7 +231,7 @@ main(int argc, char * argv[]) ...@@ -231,7 +231,7 @@ main(int argc, char * argv[])
{ {
case -1: case -1:
/* An error occured while forking */ /* 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: Start reading from pipe */ /* Child process after fork: Start reading from pipe */
...@@ -239,19 +239,19 @@ main(int argc, char * argv[]) ...@@ -239,19 +239,19 @@ main(int argc, char * argv[])
break; break;
default: default:
/* Parent proces after fork: Print child's process id */ /* 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",
kiddoCount, child_id); kiddoCount, child_id);
printf("Started child process %d with id %d \n", printf("Started child process %d with id %d\n",
kiddoCount, child_id); kiddoCount, child_id);
} }
} }
} }
/* Close logs and exit normally */ /* 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);
fclose(log2); fclose(log2);
......
...@@ -165,7 +165,7 @@ over, maar krijgen de standaard signal handlers toegewezen. ...@@ -165,7 +165,7 @@ over, maar krijgen de standaard signal handlers toegewezen.
\begin{table}[H] \begin{table}[H]
\begin{tabular}{|l|l|} \begin{tabular}{|l|l|}
\hline \hline
SIGINT & Interrupt van toetsenbord (toets wordt ingedrukt) \\ SIGINT & Interrupt van toetsenbord (CTRL + C wordt ingedrukt) \\
SIGKILL & Kill signal (kill -9 proces\_id) \\ SIGKILL & Kill signal (kill -9 proces\_id) \\
SIGSEGV & Invalid memory reference (resulteert in een segfault) \\ SIGSEGV & Invalid memory reference (resulteert in een segfault) \\
SIGPIPE & Broken pipe: write to pipe, no readers (verbroken ssh connectie) \\ SIGPIPE & Broken pipe: write to pipe, no readers (verbroken ssh connectie) \\
...@@ -177,17 +177,17 @@ SIGTERM & Termination signal (kill proces\_id) \\ ...@@ -177,17 +177,17 @@ SIGTERM & Termination signal (kill proces\_id) \\
Het is mogelijk om in de taal C met de functie \texttt{kill()} een signal te Het is mogelijk om in de taal C met de functie \texttt{kill()} een signal te
sturen naar een proces. Dit kan ook vanuit de shell met het gelijknamige sturen naar een proces. Dit kan ook vanuit de shell met het gelijknamige
commando \texttt{kill}, eventueel gevolgd door het nummer van de signal die commando \texttt{kill}, eventueel gevolgd door \texttt{-s} en het nummer van de
dient te worden verstuurt. Het sturen van signals in de taal C kwam niet verder signal die dient te worden verstuurt. Het sturen van signals in de taal C kwam
aan bod in dit practicum en wordt daarom niet verder behandeld. niet verder aan bod in dit practicum en wordt daarom niet verder behandeld.
\pagebreak \pagebreak
\section{De opdracht} \section{De opdracht}
\subsection{Keyboard verwerken}
\subsection{Keyboard verwerken}
\subsection{Fork/pipe/wait implementatie} \subsection{Fork/pipe/wait implementatie}
......
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