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
5b5248e7
Commit
5b5248e7
authored
Nov 07, 2010
by
Taddeüs Kroes
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
- Some final changes to OS ass3.
parent
cfbf4096
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
40 additions
and
41 deletions
+40
-41
os/ass3/fishbones.c
os/ass3/fishbones.c
+40
-41
No files found.
os/ass3/fishbones.c
View file @
5b5248e7
...
@@ -37,17 +37,17 @@ static void gup(FILE * log1, FILE * log2, int pipe_id[2], int myNumber)
...
@@ -37,17 +37,17 @@ 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
);
exit
(
1
);
exit
(
1
);
}
}
/* Character read successfully, process it */
/* 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
);
...
@@ -55,35 +55,35 @@ static void gup(FILE * log1, FILE * log2, int pipe_id[2], int myNumber)
...
@@ -55,35 +55,35 @@ static void gup(FILE * log1, FILE * log2, int pipe_id[2], int myNumber)
if
(
c
==
'P'
)
if
(
c
==
'P'
)
{
{
/* Decrease antidote level or exit the process */
/* 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 "
"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'
);
...
@@ -103,7 +103,7 @@ static void gup(FILE * log1, FILE * log2, int pipe_id[2], int myNumber)
...
@@ -103,7 +103,7 @@ static void gup(FILE * log1, FILE * log2, int pipe_id[2], int myNumber)
void
void
signal_handler
(
int
signum
)
signal_handler
(
int
signum
)
{
{
char
*
sig
=
"TERM"
;
char
*
sig
=
"TERM"
;
pid_t
pid
;
pid_t
pid
;
switch
(
signum
)
switch
(
signum
)
...
@@ -129,8 +129,6 @@ signal_handler(int signum)
...
@@ -129,8 +129,6 @@ signal_handler(int signum)
default:
default:
puts
(
"This handler is unapplicable for this type of signal"
);
puts
(
"This handler is unapplicable for this type of signal"
);
}
}
errno
=
0
;
}
}
/*
/*
...
@@ -147,13 +145,9 @@ main(int argc, char * argv[])
...
@@ -147,13 +145,9 @@ main(int argc, char * argv[])
/* Save parent process id for signal handler */
/* Save parent process id for signal handler */
parent_pid
=
getpid
();
parent_pid
=
getpid
();
/* Open one buffered and one unbuffered log */
printf
(
"argc: %d
\n
"
,
argc
);
log1
=
fopen
(
"child.log1"
,
"wt+"
);
log2
=
fopen
(
"child.log2"
,
"wt+"
);
setvbuf
(
log2
,
NULL
,
_IONBF
,
BUFSIZ
);
printf
(
"argc: %d
\n
"
,
argc
);
/* Create pipe, exit with error on failure */
/* Create pipe, exit with error on failure */
if
(
pipe
(
pipe_id
)
)
if
(
pipe
(
pipe_id
)
)
{
{
...
@@ -164,13 +158,18 @@ main(int argc, char * argv[])
...
@@ -164,13 +158,18 @@ main(int argc, char * argv[])
/* Bind signal handlers */
/* Bind signal handlers */
if
(
sigaction
(
SIGINT
,
&
action
,
NULL
)
if
(
sigaction
(
SIGINT
,
&
action
,
NULL
)
+
sigaction
(
SIGTERM
,
&
action
,
NULL
)
||
sigaction
(
SIGTERM
,
&
action
,
NULL
)
+
sigaction
(
SIGCHLD
,
&
action
,
NULL
)
<
0
)
||
sigaction
(
SIGCHLD
,
&
action
,
NULL
)
)
{
{
perror
(
"An error occured while binding the signal handlers
\n
"
);
perror
(
"An error occured while binding the signal handlers
\n
"
);
exit
(
1
);
exit
(
1
);
}
}
/* Open one buffered and one unbuffered log */
log1
=
fopen
(
"child.log1"
,
"wt+"
);
log2
=
fopen
(
"child.log2"
,
"wt+"
);
setvbuf
(
log2
,
NULL
,
_IONBF
,
BUFSIZ
);
/* Keep reading input */
/* Keep reading input */
while
(
(
c
=
getchar
())
)
while
(
(
c
=
getchar
())
)
{
{
...
...
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