Like Tree1Likes
  • 1 Post By Bruins77

Help Finding Error in Thread Prime Number

This is a discussion on Help Finding Error in Thread Prime Number within the C Programming forums, part of the General Programming Boards category; My assignment is to write a program that finds the prime numbers using threads for number filters. My program compiles, ...

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

    Question Help Finding Error in Thread Prime Number

    My assignment is to write a program that finds the prime numbers using threads for number filters. My program compiles, but it does not even reach the test messages I'm printing in the very beginning of my main. It just sits there when I run the a.out file. Can anyone pinpoint what I've done wrong?

    Code:
    // Homework 8
    // 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 filter {
        struct queue *input;
        struct queue *output;
        int p;
    };
    
    void *numberFilter(void *filterStruct) {  // check #s
        struct filter *temp = (struct filter *)filterStruct;
        int number = temp->p;
        struct queue *inputQ = temp->input;
        struct queue *outputQ = temp->output;
        printf(" %d",88);
        while(1) {
            int nextNumber = dequeue(inputQ);
            printf(" %d ", nextNumber);
            if(nextNumber == -1) {
                enqueue(number, outputQ);
                return;  // stop executing. poison pill received.
            }
            if(nextNumber == number) enqueue(number, outputQ);
            if(nextNumber%number != 0) enqueue(nextNumber, outputQ);
        }
    }
    
    void *generator(struct queue *q) {      // generate #'s 2 thru 1000
        struct queue *queue1 = q;
        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 #
        struct queue *temp = (struct queue *)inputQ;
        int nextNumber;
        while(1) {
            nextNumber = dequeue(temp);
            if(nextNumber == -1) {
                return;  // stop executing. poison pill received.
            }
            int prime = 1; int i;
            for(i = 13; i<=sqrt(nextNumber); i+=2) {
                if(nextNumber%i == 0) prime = 0;
            }
            if(prime == 1) printf("%d ", nextNumber);
        }
    }
    
    void *initQueue(struct queue *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);
        }
        sem_unlink("QueueMutex");
        sem_unlink("QueueSlots");
        sem_unlink("QueueItems");
    }
    
    
    void enqueue(int newElement, struct queue *q) {
        sem_wait(q->slots); // P(slots) operation.
        sem_wait(q->mutex); // P(mutex) operation.
        q->queue[q->back] = newElement;
        q->back = (q->back+1) % Q_SIZE;
        sem_post(q->mutex); // V(mutex) operation.
        sem_post(q->items); // V(items) operation.
    }
    
    int dequeue(struct queue *q) {
        sem_wait(q->items); // P(items) operation.
        sem_wait(q->mutex); // P(mutex) operation.
        int value = q->queue[q->front];
        q->front = (q->front+1) % Q_SIZE;
        sem_post(q->mutex); // V(mutex) operation.
        sem_post(q->slots); // V(slots) operation.
        return value;
    }
    
    int main() {
        printf("anything???");
        struct queue *queue1, *queue2, *queue3, *queue4, *queue5, *queue6;
        printf("justbeforemalloc");
        queue1 = (struct queue *) malloc(sizeof(struct queue));
        printf("we did one");
        queue2 = (struct queue *) malloc(sizeof(struct queue));
        queue3 = (struct queue *) malloc(sizeof(struct queue));
        queue4 = (struct queue *) malloc(sizeof(struct queue));
        queue5 = (struct queue *) malloc(sizeof(struct queue));
        queue6 = (struct queue *) malloc(sizeof(struct queue));
        
        printf("We got to here.");
        
        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(queue1);
        pthread_t thread2;
        pthread_create(&thread2, NULL, numberFilter, &filter2);
        printf("Checkpoint");
        // creates thread for sorting through 2's.
        pthread_t thread3;
        pthread_create(&thread3, NULL, numberFilter, &filter3);
        printf("Checkpoint2");
        // creates thread for sorting through 3's.
        pthread_t thread5;
        pthread_create(&thread5, NULL, numberFilter, &filter5);
        printf("Checkpoint3");
        // creates thread for sorting through 5's.
        pthread_t thread7;
        pthread_create(&thread7, NULL, numberFilter, &filter7);
        printf("Checkpoint4");
        // creates thread for sorting through 7's.
        pthread_t thread11;
        pthread_create(&thread11, NULL, numberFilter, &filter11);
        printf("Checkpoint5");
        // creates thread for sorting through 11's.
        pthread_t threadFinal;
        pthread_create(&threadFinal, NULL, finalCheck, &queue6);
        printf("Done");
        // creates thread for final check.
        
        free(queue1);
        free(queue2);
        free(queue3);
        free(queue4);
        free(queue5);
        free(queue6);
    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,558
    Well you won't see anything unless you put some \n at the end of your printf statements, or call fflush(stdout) after every printf call.

    stdout is buffered you see.
    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.

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,305
    So, your main does some initialisation, creates a bunch of threads, then does some cleanup, and then immediately terminates.
    When exactly were you planning on having it wait for the threads to do their work?
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    Registered User
    Join Date
    Jan 2012
    Posts
    14
    I assume you mean that I need to have some pthread_join calls in the main, but as I mentioned, I'm a beginner, still trying to figure out how to place those and which threads to wait for which.
    iMalc likes this.

  5. #5
    Registered User
    Join Date
    Jan 2012
    Posts
    14
    Solved it, thanks for the heads up iMalc that was the problem, code was fine otherwise aside from minor tweaks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Finding prime numbers
    By stdq in forum C Programming
    Replies: 3
    Last Post: 01-28-2012, 11:51 AM
  2. Finding Prime Numbers
    By alireza beygi in forum C Programming
    Replies: 2
    Last Post: 01-13-2012, 03:06 AM
  3. Replies: 2
    Last Post: 12-24-2009, 01:41 AM
  4. Replies: 15
    Last Post: 11-25-2007, 02:47 PM
  5. Replies: 3
    Last Post: 03-29-2005, 03:24 PM

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