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
563cced7
Commit
563cced7
authored
Dec 12, 2018
by
Ivaylo Ivanov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refactor cpair and add initial forking
parent
ccfbf9e1
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
84 additions
and
20 deletions
+84
-20
cpair/Makefile
cpair/Makefile
+1
-1
cpair/cpair.c
cpair/cpair.c
+83
-19
No files found.
cpair/Makefile
View file @
563cced7
...
@@ -4,7 +4,7 @@ TARGET = cpair
...
@@ -4,7 +4,7 @@ TARGET = cpair
all
:
$(TARGET).c
all
:
$(TARGET).c
$(CC)
$(CFLAGS)
$(TARGET)
.c
$(CC)
$(CFLAGS)
$(TARGET)
.c
$(CC)
$(TARGET)
.o
-o
$(TARGET)
$(CC)
$(TARGET)
.o
-o
$(TARGET)
-lm
install
:
install
:
cp
$(TARGET)
/usr/local/bin/
$(TARGET)
cp
$(TARGET)
/usr/local/bin/
$(TARGET)
...
...
cpair/cpair.c
View file @
563cced7
...
@@ -38,13 +38,28 @@
...
@@ -38,13 +38,28 @@
#include <stdlib.h>
#include <stdlib.h>
#include <string.h>
#include <string.h>
#include <math.h>
#include <math.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
float
get_x_mean
(
float
*
points
[
2
],
int
arr_len
);
/// Node struct for linked list
typedef
struct
node
{
float
points
[
2
];
struct
node
*
next
;
}
node_t
;
int
main
()
{
float
get_x_mean
(
node_t
*
head
);
float
get_distance
(
float
x1
,
float
x2
,
float
y1
,
float
y2
);
int
main
(
void
)
{
char
input
[
__UINT8_MAX__
]
=
""
;
///< An array to save the input to
char
input
[
__UINT8_MAX__
]
=
""
;
///< An array to save the input to
float
*
points
[
2
];
///< A pointer to a two-valued array to save the points to
int
point_num
=
0
;
///< Save the number of points
int
point_num
=
0
;
///< Save the number of points
node_t
*
head
=
NULL
;
pid_t
child_a
,
child_b
;
head
=
malloc
(
sizeof
(
node_t
));
node_t
*
current
=
head
;
while
(
fgets
(
input
,
__INT8_MAX__
,
stdin
)
!=
NULL
)
{
///< Read line by line
while
(
fgets
(
input
,
__INT8_MAX__
,
stdin
)
!=
NULL
)
{
///< Read line by line
/// Split the input by whitespace as delimiter
/// Split the input by whitespace as delimiter
...
@@ -52,10 +67,12 @@ int main () {
...
@@ -52,10 +67,12 @@ int main () {
char
*
y
=
strtok
(
NULL
,
" "
);
char
*
y
=
strtok
(
NULL
,
" "
);
if
(
x
!=
NULL
&&
y
!=
NULL
)
{
if
(
x
!=
NULL
&&
y
!=
NULL
)
{
/// Convert to float and save to the array
/// Convert to float and save to the list
points
[
point_num
][
0
]
=
strtof
(
x
,
NULL
);
current
->
points
[
0
]
=
strtof
(
x
,
NULL
);
points
[
point_num
][
1
]
=
strtof
(
y
,
NULL
);
current
->
points
[
1
]
=
strtof
(
y
,
NULL
);
point_num
++
;
///< Increase the array length
current
->
next
=
malloc
(
sizeof
(
node_t
));
current
=
current
->
next
;
point_num
++
;
///< Increase the list length
}
else
{
}
else
{
puts
(
"ERROR: Ill-formed line found"
);
puts
(
"ERROR: Ill-formed line found"
);
exit
(
EXIT_FAILURE
);
exit
(
EXIT_FAILURE
);
...
@@ -71,28 +88,75 @@ int main () {
...
@@ -71,28 +88,75 @@ int main () {
exit
(
EXIT_SUCCESS
);
exit
(
EXIT_SUCCESS
);
if
(
point_num
==
2
)
{
if
(
point_num
==
2
)
{
printf
(
"%f %f
\n
"
,
points
[
0
][
0
],
points
[
0
]
[
1
]);
printf
(
"%f %f
\n
"
,
head
->
points
[
0
],
head
->
points
[
1
]);
printf
(
"%f %f
\n
"
,
points
[
1
][
0
],
points
[
1
]
[
1
]);
printf
(
"%f %f
\n
"
,
head
->
next
->
points
[
0
],
head
->
next
->
points
[
1
]);
exit
(
EXIT_SUCCESS
);
exit
(
EXIT_SUCCESS
);
}
}
float
x_mean
=
get_x_mean
(
points
,
point_num
);
float
x_mean
=
get_x_mean
(
head
);
child_a
=
fork
();
child_b
=
fork
();
switch
(
child_a
){
case
-
1
:
puts
(
"ERROR: Coudn't fork child"
);
exit
(
EXIT_FAILURE
);
case
0
:
puts
(
"Child"
);
break
;
default:
break
;
}
switch
(
child_b
){
case
-
1
:
puts
(
"ERROR: Coudn't fork child"
);
exit
(
EXIT_FAILURE
);
case
0
:
puts
(
"Child"
);
break
;
default:
break
;
}
int
status
;
waitpid
(
child_a
,
&
status
,
0
);
waitpid
(
child_b
,
&
status
,
0
);
exit
(
EXIT_SUCCESS
);
exit
(
EXIT_SUCCESS
);
}
}
/**
/**
* @brief Function to calculate the mean of the X coordinates from a points array
* @brief Function to calculate the mean of the X coordinates from a points list
* @details Loops through the points array, gets the sum of X coordinates and returns the mean
* @details Loops through the points list, gets the sum of X coordinates and returns the mean
* @param points - a pointer to an array with the points
* @param head - a pointer to a list with the points
* point_num - number of points in the array
* @return mean of all X coordinates
* @return mean of all X coordinates
*
*
**/
**/
float
get_x_mean
(
float
*
points
[
2
],
int
point_num
)
{
float
get_x_mean
(
node_t
*
head
)
{
node_t
*
current
=
head
;
float
x_sum
=
0
;
float
x_sum
=
0
;
for
(
int
i
=
0
;
i
<
point_num
;
i
++
)
{
int
i
=
0
;
x_sum
+=
points
[
i
][
0
];
while
(
current
->
next
!=
NULL
)
{
x_sum
+=
current
->
points
[
0
];
current
=
current
->
next
;
i
++
;
}
}
return
x_sum
/
point_num
;
return
x_sum
/
i
;
}
/**
* @brief Function to calculate the distance between 2 points
* @details The function calculates the distance between 2 points
* in a Cartesian coordinate system, given their coordinates
* @param x1, y1 - pair of coordinates for the first point
* x2, y2 - pair of coordinates for the second point
* @return distance between the two points
*
**/
float
get_distance
(
float
x1
,
float
x2
,
float
y1
,
float
y2
)
{
return
sqrt
(
pow
((
x2
-
x1
),
2
)
+
pow
((
y2
-
y1
),
2
));
}
}
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