Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
unix
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Analytics
Analytics
Repository
Value Stream
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Commits
Open sidebar
tu-wien
unix
Commits
eb130f4b
Commit
eb130f4b
authored
Dec 15, 2018
by
Ivaylo Ivanov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add string convertion
parent
c70db736
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
46 additions
and
18 deletions
+46
-18
cpair/cpair.c
cpair/cpair.c
+46
-18
No files found.
cpair/cpair.c
View file @
eb130f4b
...
...
@@ -67,7 +67,6 @@ int main(int argc, char *argv[]) {
exit
(
EXIT_FAILURE
);
}
fprintf
(
stderr
,
"Process id: %d
\n
"
,
getpid
());
/**
* Create the pipe file descriptors and inititalize the pipes
*
...
...
@@ -91,14 +90,14 @@ int main(int argc, char *argv[]) {
node_t
*
current
=
head
;
while
(
fgets
(
input
,
__INT8_MAX__
,
stdin
)
!=
NULL
)
{
///< Read line by line
/// Split the input by whitespace as delimiter
char
*
x
=
strtok
(
input
,
" "
);
char
*
y
=
strtok
(
NULL
,
" "
);
char
*
sep
,
*
end
;
float
x
=
strtof
(
input
,
&
sep
);
///< if no characters were converted then input will point to end
float
y
=
strtof
(
sep
,
&
end
);
if
(
x
!=
NULL
&&
y
!=
NULL
)
{
if
(
input
!=
sep
||
sep
!=
end
)
{
/// Convert to float and save to the list
current
->
points
[
0
]
=
strtof
(
x
,
NULL
)
;
current
->
points
[
1
]
=
strtof
(
y
,
NULL
)
;
current
->
points
[
0
]
=
x
;
current
->
points
[
1
]
=
y
;
current
->
next
=
malloc
(
sizeof
(
node_t
));
current
=
current
->
next
;
point_num
++
;
///< Increase the list length
...
...
@@ -131,7 +130,6 @@ int main(int argc, char *argv[]) {
exit
(
EXIT_FAILURE
);
case
0
:
/// Child execution
fprintf
(
stderr
,
"Child 1 id: %d
\n
"
,
getpid
());
close
(
out_pipe_a
[
0
]);
close
(
in_pipe_a
[
1
]);
close
(
STDIN_FILENO
);
...
...
@@ -152,7 +150,6 @@ int main(int argc, char *argv[]) {
exit
(
EXIT_FAILURE
);
case
0
:
/// Child execution
fprintf
(
stderr
,
"Child 2 id: %d
\n
"
,
getpid
());
close
(
out_pipe_b
[
0
]);
close
(
in_pipe_b
[
1
]);
close
(
STDIN_FILENO
);
...
...
@@ -199,27 +196,56 @@ int main(int argc, char *argv[]) {
}
/**
*
Send the correct lists to the children
**/
/// First half
*
* Send first half to first child
*
**/
if
(
dup2
(
in_pipe_a
[
1
],
STDIN_FILENO
)
!=
STDIN_FILENO
)
{
fprintf
(
stderr
,
"ERROR: Failed duplicating STDIN"
);
exit
(
EXIT_FAILURE
);
}
if
(
write
(
in_pipe_a
[
1
],
first_part_buff
,
first_part_len
*
sizeof
(
node_t
))
<
0
)
{
/// Setup string for pipe
current
=
first_part
;
char
first_part_string
[
first_part_len
+
1
][
__UINT8_MAX__
];
int
i
=
0
;
while
(
current
!=
NULL
)
{
sprintf
(
first_part_string
[
i
],
"%f %f
\n
"
,
current
->
points
[
0
],
current
->
points
[
1
]);
i
++
;
current
=
current
->
next
;
}
first_part_string
[
first_part_len
][
0
]
=
EOF
;
/// Write to pipe
if
(
write
(
in_pipe_a
[
1
],
first_part_string
,
first_part_len
*
__UINT8_MAX__
)
<
0
)
{
fprintf
(
stderr
,
"ERROR: Failed writing to child
\n
"
);
exit
(
EXIT_FAILURE
);
}
close
(
STDOUT_FILENO
);
///Second half
/**
*
* Send second half to second child
*
**/
if
(
dup2
(
in_pipe_b
[
1
],
STDIN_FILENO
)
!=
STDIN_FILENO
)
{
fprintf
(
stderr
,
"ERROR: Failed duplicating STDIN"
);
fprintf
(
stderr
,
"ERROR: Failed duplicating STDIN
\n
"
);
exit
(
EXIT_FAILURE
);
}
if
(
write
(
in_pipe_b
[
1
],
first_part_buff
,
second_part_len
*
sizeof
(
node_t
))
<
0
)
{
/// Setup string for pipe
current
=
second_part
;
char
second_part_string
[
second_part_len
+
1
][
__UINT8_MAX__
];
i
=
0
;
while
(
current
!=
NULL
)
{
sprintf
(
second_part_string
[
i
],
"%f %f
\n
"
,
current
->
points
[
0
],
current
->
points
[
1
]);
i
++
;
current
=
current
->
next
;
}
second_part_string
[
second_part_len
][
0
]
=
EOF
;
/// Write to pipe
if
(
write
(
in_pipe_b
[
1
],
second_part_string
,
second_part_len
*
__UINT8_MAX__
)
<
0
)
{
fprintf
(
stderr
,
"ERROR: Failed writing to child
\n
"
);
exit
(
EXIT_FAILURE
);
}
...
...
@@ -227,7 +253,7 @@ int main(int argc, char *argv[]) {
/// Wait for the correct exit of the children
if
(
wait_for_termination
(
child_a
)
!=
EXIT_SUCCESS
||
wait_for_termination
(
child_b
)
!=
EXIT_SUCCESS
)
{
fprintf
(
stderr
,
"ERROR: Children didn't terminate successfully"
);
fprintf
(
stderr
,
"ERROR: Children didn't terminate successfully
\n
"
);
exit
(
EXIT_FAILURE
);
}
...
...
@@ -339,3 +365,5 @@ int wait_for_termination(pid_t child) {
return
WEXITSTATUS
(
exit_stat
);
}
childexec
()
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