Like Tree1Likes
  • 1 Post By Salem

Help with Threaded Prime Number Finder Assignment

This is a discussion on Help with Threaded Prime Number Finder Assignment within the C Programming forums, part of the General Programming Boards category; Hey guys. I'm a beginner in C and I have a college assignment here that I could use some help ...

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    14

    Question Help with Threaded Prime Number Finder Assignment

    Hey guys. I'm a beginner in C and I have a college assignment here that I could use some help solving. Let me explain the basic assignment.

    The program should find all the primes between 2-1000, inclusive. There will be threads to filter out multiples of 2, 3, 5, 7 and 11 before the final thread checks the rest (checks if the number is divisible by any odd numbers from 13 to the sqrt of the number).

    Here is my code, there are obviously errors in it and I can't compile it. I could use some pointers, no pun intended. Thanks.

    Code:
    // Threaded Primes
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <pthread.h>
    #include <math.h>
    #include <semaphore.h>
    #include <fcntl.h>
    
    #define Q_SIZE 10
    
    struct queue {
        int queue[Q_SIZE];
        int front; int back;
        sem_t *mutex;
        sem_t *slots;
        sem_t *items;
    };
    
    struct queue queue1;
    struct queue queue2;
    struct queue queue3;
    struct queue queue4;
    struct queue queue5;
    struct queue queue6;
    
    struct filter {
        void *input;
        void *output;
        int p;
    }
    
    void numberFilter(void *filterStruct) {  // check #s
        int nextNumber;
        int number = filterStruct->number;
        void *inputQ = filterStruct->inputQ;
        void *outputQ = filterStruct->outputQ;
        
        while(1) {
            nextNumber = dequeue(inputQ);
            if(nextNumber == -1) {
                enqueue(number, outputQ);
                pthread_exit(0);  // stop executing. poison pill received.
            }
            if(nextNumber == number) enqueue(number, outputQ);
            if(nextNumber%number != 0) enqueue(nextNumber, outputQ);
        }
            
    }
    
    void generator() {      // generate #'s 2 thru 1000
        int counter = 2;
        while(counter<1001) {
            enqueue(counter, queue1);
            counter++;
        }
        enqueue(-1, queue1); // send poison pill
    }
    
    void finalCheck(void *inputQ) {     // final checker, 13 thru sqrt of #
        int nextNumber;
        while(1) {
            nextNumber = dequeue(inputQ);
            if(nextNumber == -1) {
                pthread_exit(0);  // stop executing. poison pill received.
            }
            int prime = 1;
            for(int i = 13; i<=sqrt(nextNumber); i+=2) {
                if(nextNumber%i == 0) prime = 0;
            }
            if(prime == 1) printf("%d ", nextNumber);
        }
    }
    
    void initQueue(void *q) {
        q.queue[0] = 0;
        q.front = 0; q.back = 0;
        q.mutex = sem_open("QueueMutex", O_CREAT, 0700, 1); // init mutex sem
        if (!q.mutex) {
            perror("semaphore create error");
            exit(4);
        }
        q.slots = sem_open("QueueSlots", O_CREAT, 0700, 10); // init slots sem
        if (!q.slots) {
            perror("semaphore create error");
            exit(4);
        }
        q.items = sem_open("QueueItems", O_CREAT, 0700, 0); // init items sem
        if (!q.items) {
            perror("semaphore create error");
            exit(4);
        }
    }
    
    
    void enqueue(int newElement, void *q) {
        sem_wait(q.slots); // P(slots) operation.
        sem_wait(q.mutex); // P(mutex) operation.
        q.queue[back] = newElement;
        back = (back+1) % Q_SIZE;
        sem_post(q.mutex); // V(mutex) operation.
        sem_post(q.items); // V(items) operation.
    }
    
    int dequeue(void *q) {
        sem_wait(q.items); // P(items) operation.
        sem_wait(q.mutex); // P(mutex) operation.
        int value = q.queue[q.front];
        front = (front+1) % Q_SIZE;
        sem_post(q.mutex); // V(mutex) operation.
        sem_post(q.slots); // V(slots) operation.
        return value;
    }
    
    int main() {
        initQueue(*queue1);
        initQueue(*queue2);
        initQueue(*queue3);
        initQueue(*queue4);
        initQueue(*queue5);
        initQueue(*queue6);
        
        struct filter filter2 = {&queue1, &queue2, 2};
        struct filter filter3 = {&queue2, &queue3, 3};
        struct filter filter5 = {&queue3, &queue4, 5};
        struct filter filter7 = {&queue4, &queue5, 7};
        struct filter filter11 = {&queue5, &queue6, 11};
        
        generator();
        pthread_t thread2;
        int k = pthread_create(&thread2, NULL, numberFilter, &filter2);
        // creates thread for sorting through 2's.
        pthread_t thread3;
        int k = pthread_create(&thread3, NULL, numberFilter, &filter3);
        // creates thread for sorting through 3's.
        pthread_t thread5;
        int k = pthread_create(&thread5, NULL, numberFilter, &filter5);
        // creates thread for sorting through 5's.
        pthread_t thread7;
        int k = pthread_create(&thread7, NULL, numberFilter, &filter7);
        // creates thread for sorting through 7's.
        pthread_t thread11;
        int k = pthread_create(&thread11, NULL, numberFilter, &filter11);
        // creates thread for sorting through 11's.
        pthread_t threadFinal;
        int k = pthread_create(&threadFinal, NULL, finalCheck, &queue6);
        // creates thread for final check.
        
    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,485
    > Here is my code, there are obviously errors in it and I can't compile it. I could use some pointers, no pun intended. Thanks.
    So what error messages do you get?

    Code:
    $ gcc bar.c
    bar.c:33: error: two or more data types in declaration specifiers
    bar.c: In function ‘numberFilter’:
    bar.c:35: warning: dereferencing ‘void *’ pointer
    bar.c:35: error: request for member ‘number’ in something not a structure or union
    bar.c:36: warning: dereferencing ‘void *’ pointer
    bar.c:36: error: request for member ‘inputQ’ in something not a structure or union
    bar.c:37: warning: dereferencing ‘void *’ pointer
    bar.c:37: error: request for member ‘outputQ’ in something not a structure or union
    bar.c: In function ‘finalCheck’:
    bar.c:68: error: ‘for’ loop initial declarations are only allowed in C99 mode
    bar.c:68: note: use option -std=c99 or -std=gnu99 to compile your code
    bar.c: In function ‘initQueue’:
    bar.c:76: error: request for member ‘queue’ in something not a structure or union
    bar.c:77: error: request for member ‘front’ in something not a structure or union
    bar.c:77: error: request for member ‘back’ in something not a structure or union
    bar.c:78: error: request for member ‘mutex’ in something not a structure or union
    bar.c:79: error: request for member ‘mutex’ in something not a structure or union
    bar.c:83: error: request for member ‘slots’ in something not a structure or union
    bar.c:84: error: request for member ‘slots’ in something not a structure or union
    bar.c:88: error: request for member ‘items’ in something not a structure or union
    bar.c:89: error: request for member ‘items’ in something not a structure or union
    bar.c: At top level:
    bar.c:96: warning: conflicting types for ‘enqueue’
    bar.c:42: note: previous implicit declaration of ‘enqueue’ was here
    bar.c: In function ‘enqueue’:
    bar.c:97: error: request for member ‘slots’ in something not a structure or union
    bar.c:98: error: request for member ‘mutex’ in something not a structure or union
    bar.c:99: error: request for member ‘queue’ in something not a structure or union
    bar.c:99: error: ‘back’ undeclared (first use in this function)
    bar.c:99: error: (Each undeclared identifier is reported only once
    bar.c:99: error: for each function it appears in.)
    bar.c:101: error: request for member ‘mutex’ in something not a structure or union
    bar.c:102: error: request for member ‘items’ in something not a structure or union
    bar.c: In function ‘dequeue’:
    bar.c:106: error: request for member ‘items’ in something not a structure or union
    bar.c:107: error: request for member ‘mutex’ in something not a structure or union
    bar.c:108: error: request for member ‘queue’ in something not a structure or union
    bar.c:108: error: request for member ‘front’ in something not a structure or union
    bar.c:109: error: ‘front’ undeclared (first use in this function)
    bar.c:110: error: request for member ‘mutex’ in something not a structure or union
    bar.c:111: error: request for member ‘slots’ in something not a structure or union
    bar.c: In function ‘main’:
    bar.c:116: error: invalid type argument of ‘unary *’ (have ‘struct queue’)
    bar.c:117: error: invalid type argument of ‘unary *’ (have ‘struct queue’)
    bar.c:118: error: invalid type argument of ‘unary *’ (have ‘struct queue’)
    bar.c:119: error: invalid type argument of ‘unary *’ (have ‘struct queue’)
    bar.c:120: error: invalid type argument of ‘unary *’ (have ‘struct queue’)
    bar.c:121: error: invalid type argument of ‘unary *’ (have ‘struct queue’)
    bar.c:131: warning: passing argument 3 of ‘pthread_create’ from incompatible pointer type
    /usr/include/pthread.h:227: note: expected ‘void * (*)(void *)’ but argument is of type ‘struct filter (*)(void *)’
    bar.c:134: error: redefinition of ‘k’
    bar.c:131: note: previous definition of ‘k’ was here
    bar.c:134: warning: passing argument 3 of ‘pthread_create’ from incompatible pointer type
    /usr/include/pthread.h:227: note: expected ‘void * (*)(void *)’ but argument is of type ‘struct filter (*)(void *)’
    bar.c:137: error: redefinition of ‘k’
    bar.c:134: note: previous definition of ‘k’ was here
    bar.c:137: warning: passing argument 3 of ‘pthread_create’ from incompatible pointer type
    /usr/include/pthread.h:227: note: expected ‘void * (*)(void *)’ but argument is of type ‘struct filter (*)(void *)’
    bar.c:140: error: redefinition of ‘k’
    bar.c:137: note: previous definition of ‘k’ was here
    bar.c:140: warning: passing argument 3 of ‘pthread_create’ from incompatible pointer type
    /usr/include/pthread.h:227: note: expected ‘void * (*)(void *)’ but argument is of type ‘struct filter (*)(void *)’
    bar.c:143: error: redefinition of ‘k’
    bar.c:140: note: previous definition of ‘k’ was here
    bar.c:143: warning: passing argument 3 of ‘pthread_create’ from incompatible pointer type
    /usr/include/pthread.h:227: note: expected ‘void * (*)(void *)’ but argument is of type ‘struct filter (*)(void *)’
    bar.c:146: error: redefinition of ‘k’
    bar.c:143: note: previous definition of ‘k’ was here
    bar.c:146: warning: passing argument 3 of ‘pthread_create’ from incompatible pointer type
    /usr/include/pthread.h:227: note: expected ‘void * (*)(void *)’ but argument is of type ‘void (*)(void *)’
    A couple of free hints, but you need to do better yourself.
    1. line 31 is missing a ;
    2. line 35, you need to cast your void* pointer back into whatever pointer type it is supposed to be
    3. all your initQueue(*queue1); should use & rather than *

    More to the point, did you write 150 lines and then press "compile" for the first (and only) time, only for you to discover that there are a sack-full of issues, and your easy way out is to dump the whole mess on a forum for someone else to fix?

    Here's how you're supposed to code - you write 5 to 10 lines of what seems to be syntactically valid code and you press compile. If it compiles, fine - carry on.

    If it doesn't compile, then you FIX it so it does compile.
    Fixing errors in the code you just added is easy to do, and above all easy to find (you just added them).
    Also, you're only going to get a couple of errors (not a couple of pages of errors), so you won't be demotivated.
    dmh2000 likes this.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Undefined Behaviour (Palindromic Number Finder)
    By pobri19 in forum C++ Programming
    Replies: 12
    Last Post: 09-28-2008, 04:54 AM
  2. largest number prime number that can be produced...
    By ElemenT.usha in forum C Programming
    Replies: 8
    Last Post: 02-17-2008, 12:44 AM
  3. Replies: 3
    Last Post: 03-29-2005, 03:24 PM
  4. prime number finder.
    By Aalmaron in forum C++ Programming
    Replies: 2
    Last Post: 03-05-2004, 03:56 PM
  5. IDEA: Perfect number finder
    By ygfperson in forum Contests Board
    Replies: 2
    Last Post: 08-16-2002, 08:45 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21