Advertisement
Advertisement
| 09.24.2008 at 11:50AM PDT, ID: 23759904 |
|
[x]
Attachment Details
|
||
|
[x]
The Solution Rating System
|
||
With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.
Your Input Matters If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support. Thank you! |
||
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: |
#include <sys/types.h>
#include <string.h>
#include <unistd.h>
#include <stdio.h>
#include<pthread.h>
#include<stdlib.h>
typedef struct queue_struct
{
int head,tail;
int IsEmpty;
int data[30];
}queue;
#define Queue_Size 30
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t condPool = PTHREAD_COND_INITIALIZER;
queue q;
void queue_init(queue *q)
{
q->head=-1;
q->tail=-1;
q->IsEmpty=1;
}
void queue_insert(queue *q,int element)
{
if(q->head==(q->tail+1)%Queue_Size)
printf("Queue size exceeded\n");
else
{
q->tail=(q->tail+1)%Queue_Size;
q->data[q->tail]=element;
if (q->head == -1 )
{
q->head = 0;
q->IsEmpty=0;
}
}
}
int queue_fetch(queue *q)
{
int temp=-1;
if((q->head==q->tail) && (q->tail==-1))
{
printf("Queue empty\n");
}
else
{
temp= q->data[q->head];
if(q->tail==q->head)
{
q->head=q->tail=-1;
q->IsEmpty=1;
}
else
q->head=(q->head+1)%Queue_Size;
}
return temp;
}
void EnterPool()
{
int iData=0;
while(1)
{
pthread_mutex_lock(&mutex);
while(q.IsEmpty==1)
pthread_cond_wait(&condPool,&mutex);
iData=queue_fetch(&q);
pthread_mutex_unlock(&mutex);
printf("Socket serviced %d \n",iData);
}
}
int main(int argc, char* argv[])
{
pthread_t WorkerPool[5];
int i;
//Initialize pool queue
queue_init(&q);
//Create thread pool
for(i=0;i<5;i++)
pthread_create(&WorkerPool[i],NULL,(void *) EnterPool,NULL);
int x=0;
while(1)
{
x++;
if(x>25)
exit(0);
pthread_mutex_lock(&mutex);
printf("INserting %d \n",x);
queue_insert(&q,x);
pthread_cond_signal(&condPool);
pthread_mutex_unlock(&mutex) ;
}
}
|