Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
U
uva
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Taddeüs Kroes
uva
Commits
e2b4672d
Commit
e2b4672d
authored
14 years ago
by
Taddeüs Kroes
Browse files
Options
Downloads
Patches
Plain Diff
- Added comment to OS ass3.
parent
cddefcf4
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
os/ass3/fishbones.c
+30
-20
30 additions, 20 deletions
os/ass3/fishbones.c
with
30 additions
and
20 deletions
os/ass3/fishbones.c
+
30
−
20
View file @
e2b4672d
...
...
@@ -29,29 +29,30 @@ static void gup(FILE * log1, FILE * log2, int pipe_id[2], int 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) */
/* Read next character from pipe */
if
(
read
(
pipe_id
[
0
],
&
c
,
1
)
<=
0
)
{
fprintf
(
log1
,
"Child %d exited with error
\n
"
,
myNumber
);
fprintf
(
log2
,
"Child %d exited with error
\n
"
,
myNumber
);
fprintf
(
log3
,
"Child %d exited with error
\n
"
,
myNumber
);
/* Error while reading from the pipe, exit */
fprintf
(
log1
,
"Child %d read from pipe with error "
"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
(
log2
);
fclose
(
log3
);
exit
(
1
);
}
/* Character read successfully, process it */
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
);
if
(
c
==
'P'
)
{
/* Decrease antidote level or exit the process */
if
(
!
antidote
)
{
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)
}
else
if
(
c
==
'A'
)
{
/* Increase antidote level */
antidote
++
;
fprintf
(
log1
,
"Child %d finds antidote (now has %d)
\n
"
,
myNumber
,
antidote
);
...
...
@@ -82,6 +84,7 @@ static void gup(FILE * log1, FILE * log2, int pipe_id[2], int myNumber)
}
}
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
);
...
...
@@ -92,9 +95,9 @@ static void gup(FILE * log1, FILE * log2, int pipe_id[2], int myNumber)
}
void
signal_handler
(
int
action
)
signal_handler
(
int
signum
)
{
switch
(
action
)
switch
(
signum
)
{
case
SIGINT
:
...
...
@@ -134,15 +137,15 @@ main(int argc, char * argv[])
exit
(
1
);
}
//
action.sa_handler = signal_handler;
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);
//
}
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
())
)
{
...
...
@@ -165,16 +168,19 @@ main(int argc, char * argv[])
if
(
c
==
'q'
)
{
/* Exit program */
break
;
}
else
if
(
c
!=
'f'
)
{
/* Write character to pipe for child processes */
if
(
write
(
pipe_id
[
1
],
&
c
,
1
)
!=
1
)
{
perror
(
"Cannot write to pipe
\n
"
);
exit
(
1
);
}
/* Wait for child process to 'eat poison' */
if
(
c
==
'P'
)
wait
(
NULL
);
}
...
...
@@ -190,12 +196,15 @@ main(int argc, char * argv[])
switch
(
(
child_id
=
fork
())
)
{
case
-
1
:
/* An error occured while forking */
perror
(
"Child could not be created
\n
"
);
exit
(
1
);
case
0
:
/* Child process after fork: read from pipe */
gup
(
log1
,
log2
,
pipe_id
,
kiddoCount
);
break
;
default:
/* Parent proces after fork: Print child's process id */
fprintf
(
log1
,
"Started child process %d with id %d
\n
"
,
kiddoCount
,
child_id
);
fprintf
(
log2
,
"Started child process %d with id %d
\n
"
,
...
...
@@ -204,6 +213,7 @@ main(int argc, char * argv[])
}
}
/* Close logs and exit normally */
fprintf
(
log1
,
"Program exited normally
\n
"
);
fprintf
(
log2
,
"Program exited normally
\n
"
);
fclose
(
log1
);
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment