Initial commit
0 parents
Showing
2 changed files
with
65 additions
and
0 deletions
piloop
0 → 100755
No preview for this file type
piloop.c
0 → 100644
| 1 | #include <stdio.h> | ||
| 2 | #include <stdlib.h> | ||
| 3 | #include <pthread.h> | ||
| 4 | #include <unistd.h> | ||
| 5 | #include <errno.h> | ||
| 6 | |||
| 7 | int calcpi() { | ||
| 8 | int r[2800 + 1]; | ||
| 9 | int i, k; | ||
| 10 | int b, d; | ||
| 11 | int c = 0; | ||
| 12 | |||
| 13 | for (i = 0; i < 2800; i++) { | ||
| 14 | r[i] = 2000; | ||
| 15 | } | ||
| 16 | |||
| 17 | for (k = 2800; k > 0; k -= 14) { | ||
| 18 | d = 0; | ||
| 19 | |||
| 20 | i = k; | ||
| 21 | for (;;) { | ||
| 22 | d += r[i] * 10000; | ||
| 23 | b = 2 * i - 1; | ||
| 24 | |||
| 25 | r[i] = d % b; | ||
| 26 | d /= b; | ||
| 27 | i--; | ||
| 28 | if (i == 0) break; | ||
| 29 | d *= i; | ||
| 30 | } | ||
| 31 | c = d % 10000; | ||
| 32 | } | ||
| 33 | |||
| 34 | return 0; | ||
| 35 | } | ||
| 36 | |||
| 37 | void * thread_main(void * ptr) { | ||
| 38 | while (1 == 1) calcpi(); | ||
| 39 | } | ||
| 40 | |||
| 41 | int main(int argc, char * * argv) { | ||
| 42 | if(argc < 2) { | ||
| 43 | printf("\nUsage: %s <n> <p>\n\nWhere\n<n> is the number of threads to run in parallell\n<p> is the priority (optional)\n\n", argv[0]); | ||
| 44 | return 0; | ||
| 45 | } | ||
| 46 | int numT = atoi(argv[1]); | ||
| 47 | printf("\nStarting %d threads calculating PI. Hit Cntrl-C to stop or send a kill signal\n",numT); | ||
| 48 | pthread_t lastT; | ||
| 49 | int prio = 0; | ||
| 50 | if(argc == 3) { | ||
| 51 | prio = atoi(argv[2]); | ||
| 52 | errno = 0; | ||
| 53 | int res = nice(prio); | ||
| 54 | if(errno != 0) { | ||
| 55 | printf("Setting nice %d failed (code %d), are you root?\n", prio, errno); | ||
| 56 | } else { | ||
| 57 | printf("Process nice value set to %d\n", res); | ||
| 58 | } | ||
| 59 | } | ||
| 60 | for(int x=0; x < numT; x++) { | ||
| 61 | pthread_create(&lastT, NULL, thread_main, NULL); | ||
| 62 | } | ||
| 63 | pthread_join(lastT, NULL); | ||
| 64 | exit(0); | ||
| 65 | } |
-
Please register or sign in to post a comment