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
cfbf4096
Commit
cfbf4096
authored
Nov 07, 2010
by
Taddeüs Kroes
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
- Implemented signals in OS ass3.
parent
ea3add61
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
102 additions
and
68 deletions
+102
-68
os/ass3/fishbones.c
os/ass3/fishbones.c
+102
-68
No files found.
os/ass3/fishbones.c
View file @
cfbf4096
...
...
@@ -6,6 +6,8 @@
#include <errno.h>
#include <signal.h>
static
pid_t
parent_pid
;
/*
* Read from pipe (child processes only).
*/
...
...
@@ -22,12 +24,10 @@ static void gup(FILE * log1, FILE * log2, int pipe_id[2], int myNumber)
fprintf
(
log1
,
"This is child number %d
\n
"
,
myNumber
);
fprintf
(
log2
,
"This is child number %d
\n
"
,
myNumber
);
/* just for sure - open one more log file .... */
/* Open a line buffered log. */
log3
=
fopen
(
"child.log3"
,
"wt+"
);
setvbuf
(
log3
,
NULL
,
_IOLBF
,
BUFSIZ
);
/* log3 is line buffered - a line of output is written
immediately on encountering an end-of-line */
fprintf
(
log3
,
"This is child number %d
\n
"
,
myNumber
);
do
...
...
@@ -103,18 +103,34 @@ static void gup(FILE * log1, FILE * log2, int pipe_id[2], int myNumber)
void
signal_handler
(
int
signum
)
{
char
*
sig
=
"TERM"
;
pid_t
pid
;
switch
(
signum
)
{
case
SIGINT
:
break
;
sig
=
"INT"
;
case
SIGTERM
:
break
;
if
(
(
pid
=
getpid
())
==
parent_pid
)
{
printf
(
"Parent process received SIG%s and will exit
\n
"
,
sig
);
}
else
{
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
);
break
;
default:
puts
(
"This handler is unapplicable for this type of signal"
);
}
errno
=
0
;
}
/*
...
...
@@ -126,15 +142,17 @@ main(int argc, char * argv[])
FILE
*
log1
;
FILE
*
log2
;
char
c
;
int
kiddoCount
=
0
,
pipe_id
[
2
];
//, i = 1;
struct
sigaction
action
;
int
kiddoCount
=
0
,
pipe_id
[
2
];
struct
sigaction
action
=
{.
sa_handler
=
signal_handler
}
;
/* Save parent process id for signal handler */
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
);
/* So log1 is buffered; log2 is not buffered, which means that output
* to log2 is written immediately. */
/* Create pipe, exit with error on failure */
if
(
pipe
(
pipe_id
)
)
...
...
@@ -145,11 +163,9 @@ main(int argc, char * argv[])
}
/* Bind signal handlers */
action
.
sa_handler
=
signal_handler
;
if
(
sigaction
(
SIGINT
,
&
action
,
NULL
)
<
0
||
sigaction
(
SIGTERM
,
&
action
,
NULL
)
<
0
||
sigaction
(
SIGCHLD
,
&
action
,
NULL
)
<
0
)
if
(
sigaction
(
SIGINT
,
&
action
,
NULL
)
+
sigaction
(
SIGTERM
,
&
action
,
NULL
)
+
sigaction
(
SIGCHLD
,
&
action
,
NULL
)
<
0
)
{
perror
(
"An error occured while binding the signal handlers
\n
"
);
exit
(
1
);
...
...
@@ -158,6 +174,22 @@ main(int argc, char * argv[])
/* Keep reading input */
while
(
(
c
=
getchar
())
)
{
if
(
c
==
EOF
)
{
if
(
errno
==
EINTR
)
{
/* Wait for signal handler to end */
continue
;
}
else
{
puts
(
"Error while reading input from stdin"
);
fclose
(
log1
);
fclose
(
log2
);
exit
(
1
);
}
}
/* Ignore newlines and spaces */
if
(
c
==
'\n'
||
c
==
32
)
{
...
...
@@ -176,15 +208,15 @@ main(int argc, char * argv[])
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
)
<=
0
)
{
perror
(
"Cannot write to pipe
\n
"
);
exit
(
1
);
}
/* Wait for child process to 'eat the poison' */
if
(
c
==
'P'
)
wait
(
NULL
);
//
if( c == 'P' )
//
wait(NULL);
}
else
{
...
...
@@ -202,7 +234,7 @@ main(int argc, char * argv[])
perror
(
"Child could not be created
\n
"
);
exit
(
1
);
case
0
:
/* Child process after fork: read
from pipe */
/* Child process after fork: Start reading
from pipe */
gup
(
log1
,
log2
,
pipe_id
,
kiddoCount
);
break
;
default:
...
...
@@ -211,6 +243,8 @@ main(int argc, char * argv[])
kiddoCount
,
child_id
);
fprintf
(
log2
,
"Started child process %d with id %d
\n
"
,
kiddoCount
,
child_id
);
printf
(
"Started child process %d with id %d
\n
"
,
kiddoCount
,
child_id
);
}
}
}
...
...
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