Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
U
uva
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Taddeüs Kroes
uva
Commits
e2b4672d
Commit
e2b4672d
authored
Nov 07, 2010
by
Taddeüs Kroes
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
- Added comment to OS ass3.
parent
cddefcf4
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
30 additions
and
20 deletions
+30
-20
os/ass3/fishbones.c
os/ass3/fishbones.c
+30
-20
No files found.
os/ass3/fishbones.c
View file @
e2b4672d
...
@@ -29,29 +29,30 @@ static void gup(FILE * log1, FILE * log2, int pipe_id[2], int myNumber)
...
@@ -29,29 +29,30 @@ static void gup(FILE * log1, FILE * log2, int pipe_id[2], int myNumber)
do
do
{
{
/* Read 1 character from the pipe into 'c' here.
/* Read next character from pipe */
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) */
if
(
read
(
pipe_id
[
0
],
&
c
,
1
)
<=
0
)
if
(
read
(
pipe_id
[
0
],
&
c
,
1
)
<=
0
)
{
{
fprintf
(
log1
,
"Child %d exited with error
\n
"
,
myNumber
);
/* Error while reading from the pipe, exit */
fprintf
(
log2
,
"Child %d exited with error
\n
"
,
myNumber
);
fprintf
(
log1
,
"Child %d read from pipe with error "
fprintf
(
log3
,
"Child %d exited with error
\n
"
,
myNumber
);
"and exited
\n
"
,
myNumber
);
fprintf
(
log2
,
"Child %d read from pipe with error "
"and exited
\n
"
,
myNumber
);
fprintf
(
log3
,
"Child %d read from pipe with error "
"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 */
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
);
fprintf
(
log3
,
"Child %d read character %x: '%c'
\n
"
,
myNumber
,
c
,
c
);
fprintf
(
log3
,
"Child %d read character %x: '%c'
\n
"
,
myNumber
,
c
,
c
);
if
(
c
==
'P'
)
if
(
c
==
'P'
)
{
{
/* 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 "
...
@@ -72,6 +73,7 @@ static void gup(FILE * log1, FILE * log2, int pipe_id[2], int myNumber)
...
@@ -72,6 +73,7 @@ static void gup(FILE * log1, FILE * log2, int pipe_id[2], int myNumber)
}
}
else
if
(
c
==
'A'
)
else
if
(
c
==
'A'
)
{
{
/* 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
);
...
@@ -82,6 +84,7 @@ static void gup(FILE * log1, FILE * log2, int pipe_id[2], int myNumber)
...
@@ -82,6 +84,7 @@ static void gup(FILE * log1, FILE * log2, int pipe_id[2], int myNumber)
}
}
}
while
(
c
!=
'\0'
);
}
while
(
c
!=
'\0'
);
/* 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
);
...
@@ -92,9 +95,9 @@ static void gup(FILE * log1, FILE * log2, int pipe_id[2], int myNumber)
...
@@ -92,9 +95,9 @@ static void gup(FILE * log1, FILE * log2, int pipe_id[2], int myNumber)
}
}
void
void
signal_handler
(
int
action
)
signal_handler
(
int
signum
)
{
{
switch
(
action
)
switch
(
signum
)
{
{
case
SIGINT
:
case
SIGINT
:
...
@@ -134,15 +137,15 @@ main(int argc, char * argv[])
...
@@ -134,15 +137,15 @@ main(int argc, char * argv[])
exit
(
1
);
exit
(
1
);
}
}
//
action.sa_handler = signal_handler;
action
.
sa_handler
=
signal_handler
;
//
if( sigaction(SIGINT, &action, NULL) < 0
if
(
sigaction
(
SIGINT
,
&
action
,
NULL
)
<
0
//
|| sigaction(SIGTERM, &action, NULL) < 0
||
sigaction
(
SIGTERM
,
&
action
,
NULL
)
<
0
//
|| 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
);
//
}
}
while
(
(
c
=
getchar
())
)
while
(
(
c
=
getchar
())
)
{
{
...
@@ -165,16 +168,19 @@ main(int argc, char * argv[])
...
@@ -165,16 +168,19 @@ main(int argc, char * argv[])
if
(
c
==
'q'
)
if
(
c
==
'q'
)
{
{
/* Exit program */
break
;
break
;
}
}
else
if
(
c
!=
'f'
)
else
if
(
c
!=
'f'
)
{
{
/* Write character to pipe for child processes */
if
(
write
(
pipe_id
[
1
],
&
c
,
1
)
!=
1
)
if
(
write
(
pipe_id
[
1
],
&
c
,
1
)
!=
1
)
{
{
perror
(
"Cannot write to pipe
\n
"
);
perror
(
"Cannot write to pipe
\n
"
);
exit
(
1
);
exit
(
1
);
}
}
/* Wait for child process to 'eat poison' */
if
(
c
==
'P'
)
if
(
c
==
'P'
)
wait
(
NULL
);
wait
(
NULL
);
}
}
...
@@ -190,12 +196,15 @@ main(int argc, char * argv[])
...
@@ -190,12 +196,15 @@ main(int argc, char * argv[])
switch
(
(
child_id
=
fork
())
)
switch
(
(
child_id
=
fork
())
)
{
{
case
-
1
:
case
-
1
:
/* 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: read from pipe */
gup
(
log1
,
log2
,
pipe_id
,
kiddoCount
);
gup
(
log1
,
log2
,
pipe_id
,
kiddoCount
);
break
;
break
;
default:
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
);
kiddoCount
,
child_id
);
fprintf
(
log2
,
"Started child process %d with id %d
\n
"
,
fprintf
(
log2
,
"Started child process %d with id %d
\n
"
,
...
@@ -204,6 +213,7 @@ main(int argc, char * argv[])
...
@@ -204,6 +213,7 @@ main(int argc, char * argv[])
}
}
}
}
/* 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
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment