All pastes #422467 Raw Edit

micolous

public c v1 · immutable
·published 2007-04-03 14:05 UTC
rendered paste body
#include <stdio.h>#include <stdlib.h>#ifndef MAX#define MAX 10000#endif#define uint64 unsigned long longuint64 calc_primes(uint64 max) {//      unsigned char f = 0; // found        uint64 n;            // number to test        uint64 m;            // n/2        uint64 i;            // iterator        uint64 j;            // last prime found        for (n=1; n<=max; n++) {                if (n <= 3) {                        goto isprime;                }                if (n == 4 || n == 5) {                        goto next;                }                m = n/2;                for (i=2; i<=m; i++) {  // iterate through possible multiples.                        if (n % i == 0) {                                goto next;                        }                }isprime:                // mark last found prime in memory.                j = n;next:                0; // nop        }        return j;}int main(int argc, char *argv[]) {        int pid, status;        char c[] = "CHILD:  ";        char ch[] = "child";        char p[] = "PARENT: ";        char comp[] = "Completed, highest value was: ";        pid = fork();        if (pid == 0) {                // we are a child process, run the benchmark.                printf("%sStarted!\n", c);                fflush(NULL);                uint64 r = calc_primes(MAX);                printf("%s%s%u\n", c, comp, r);        } else {                // we are the parent process, return the pid of the child and do calculations.                printf("%sForked child: %d\n", p, pid);                fflush(NULL);                uint64 q = calc_primes(MAX);                printf("%s%s%u\n%sWaiting for child...\n", p, comp, q, p);                while (wait(&status) != pid);                printf("%sFinished.\n", p);        }        return 0;}