My basic problem is that I'm having a hard time implementing a Pthread that consists of a round robin dequeuing 4 arrays consisting of ints. Followed by a Pthread that creates random numbers with which to enqueue them with. I've written most of the code for the queue itself and I've read up a lot on Pthreads, but I haven't found anything that explains it clearly. Here is my code for reference.
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <pthread.h>
#define MaxQueueSize 10
#define FALSE 0
#define TRUE 1
typedef struct {
int content[MaxQueueSize];
int head;
int tail;
int count;
int dropped;
}queue;
void initializeQ(queue *content);
void enqueue(queue *qPtr,int i);
int dequeue(queue *qPtr);
int randomLimit(int low, int high);
int randomLimit(int low, int high);
int isEmpty(queue *qPtr);
int isFull(queue *qPtr);
void initializeQ(queue *content){
content->head = 0;
content->tail = MaxQueueSize - 1;
content->count = 0;
content->dropped = 0;
}
void enqueue(queue *qPtr,int i){
//has a 50 percent chance of dropping i instead of queuing it
if(qPtr->count > (MaxQueueSize*.9)){
if(randomLimit(0,100)<50){
qPtr->dropped++;
qPtr->count--;
}
}
else{
qPtr->tail = (qPtr->tail + 1) % MaxQueueSize;
qPtr->content[qPtr->tail] = i;
qPtr->count = qPtr->count++;
}
}
int dequeue(queue *qPtr){
int n = qPtr->content[qPtr->head];
qPtr->head = (qPtr->head + 1) % MaxQueueSize;
qPtr->count = qPtr->count - 1;
return n;
}
int isEmpty(queue *qPtr){
if(qPtr->count == 0)
return TRUE;
return FALSE;
}
int isFull(queue *qPtr){
if(qPtr->count == MaxQueueSize)
return TRUE;
return FALSE;
}
int randomLimit(int low, int high){
int k;
double d;
d = (double) rand() / ((double) RAND_MAX + 1);
k = (int) (d* (high - low + 1));
return (low + k);
}
int main(int argc, char * argv[]) {
void *exit_status;
int i;
int n;
pthread_t rRobin;
queue dog[4];
initializeQ(&dog[1]);
printf("The head of dog is %d and the tail is %d\n",dog[1].head,dog[1].tail);
for(i = 0; i<=MaxQueueSize; i++){
if(!isFull(&dog[1]))
enqueue(&dog[1],i);
printf("The number %d has been enqueued\n",dog[1].tail);
}
printf("The number of dropped packets is %d\n",dog[1].dropped);
printf("The head of dog is %d and the tail is %d\n",dog[1].head,dog[1].tail);
printf("The size is %d\n",dog[1].count);
for(i = 0; i<=MaxQueueSize; i++){
if(!isEmpty(&dog[1]))
printf("The number %d has been dequeued\n",dequeue(&dog[1]));
}
printf("The head of dog is %d and the tail is %d\n",dog[1].head,dog[1].tail);
printf("The size is %d\n",dog[1].count);
}
If you could show me some clear and simple examples of how to make a Pthread using code like mine as an example I could finally make some more progress. If I'm not clear enough with what I need help with I'll be happy to answer any questions about that. Thanks for giving any input on this.