Skip to content
Toggle navigation
Toggle navigation
This project
Loading...
Sign in
Christian Gerdes
/
piloop
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Network
Create a new issue
Builds
Commits
Issue Boards
Files
Commits
Network
Compare
Branches
Tags
Commit
ec89e787
...
ec89e787174ad0acb3a8b59f92613fd4738c1860
authored
2017-08-30 18:33:26 +0200
by
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
Initial commit
0 parents
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
65 additions
and
0 deletions
piloop
piloop.c
piloop
0 → 100755
View file @
ec89e78
No preview for this file type
piloop.c
0 → 100644
View file @
ec89e78
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
#include <errno.h>
int
calcpi
()
{
int
r
[
2800
+
1
];
int
i
,
k
;
int
b
,
d
;
int
c
=
0
;
for
(
i
=
0
;
i
<
2800
;
i
++
)
{
r
[
i
]
=
2000
;
}
for
(
k
=
2800
;
k
>
0
;
k
-=
14
)
{
d
=
0
;
i
=
k
;
for
(;;)
{
d
+=
r
[
i
]
*
10000
;
b
=
2
*
i
-
1
;
r
[
i
]
=
d
%
b
;
d
/=
b
;
i
--
;
if
(
i
==
0
)
break
;
d
*=
i
;
}
c
=
d
%
10000
;
}
return
0
;
}
void
*
thread_main
(
void
*
ptr
)
{
while
(
1
==
1
)
calcpi
();
}
int
main
(
int
argc
,
char
*
*
argv
)
{
if
(
argc
<
2
)
{
printf
(
"
\n
Usage: %s <n> <p>
\n\n
Where
\n
<n> is the number of threads to run in parallell
\n
<p> is the priority (optional)
\n\n
"
,
argv
[
0
]);
return
0
;
}
int
numT
=
atoi
(
argv
[
1
]);
printf
(
"
\n
Starting %d threads calculating PI. Hit Cntrl-C to stop or send a kill signal
\n
"
,
numT
);
pthread_t
lastT
;
int
prio
=
0
;
if
(
argc
==
3
)
{
prio
=
atoi
(
argv
[
2
]);
errno
=
0
;
int
res
=
nice
(
prio
);
if
(
errno
!=
0
)
{
printf
(
"Setting nice %d failed (code %d), are you root?
\n
"
,
prio
,
errno
);
}
else
{
printf
(
"Process nice value set to %d
\n
"
,
res
);
}
}
for
(
int
x
=
0
;
x
<
numT
;
x
++
)
{
pthread_create
(
&
lastT
,
NULL
,
thread_main
,
NULL
);
}
pthread_join
(
lastT
,
NULL
);
exit
(
0
);
}
Please
register
or
sign in
to post a comment