Code cleanup.

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