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.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

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!

7.5

pthreads condition variable problem

Asked by Novice_ in C Programming Language, Linux, Linux Programming

Tags:

I am trying simulate multiple requests that are handled by threads . When a request is created it is pushed in a queue . The main on inserting value into queue signals the condition wait variable for threads to pick up data from queue and execute. The thread in turn prints that data .

Now the code i have written works fine 50% of the time but many time the threads are not picking up task even after signaling the condition varaible and at time two thread are picking up same data from queue .


Desired output
INserting 1
Socket serviced 1
INserting 2
Socket serviced 2
INserting 3
Socket serviced 3
INserting 4
Socket serviced 4
..........

Output that is wrong:
INserting 1
INserting 2
INserting 3
INserting 4
INserting 5

in this case the program exits post insertion and no thread executes inspite of cond var being signaled each time
 or

INserting 1
Socket serviced 1
INserting 2
Socket serviced 2
INserting 3
Socket serviced 3
Socket serviced 3


I am not able to identify what is wrongStart Free Trial
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)	;				
    }   
}
[+][-]09.24.2008 at 03:11PM PDT, ID: 22564538

View this solution now by starting your 14-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: C Programming Language, Linux, Linux Programming
Tags: C
Sign Up Now!
Solution Provided By: sweetfa2
Participating Experts: 3
Solution Grade: B
 
 
[+][-]09.24.2008 at 03:14PM PDT, ID: 22564563

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 14-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]09.24.2008 at 03:18PM PDT, ID: 22564598

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 14-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]09.24.2008 at 08:55PM PDT, ID: 22566148

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 14-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]10.04.2008 at 07:10AM PDT, ID: 22640906

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 14-day free trial to view this Expert Comment or ask the Experts your question.

 
 
Loading Advertisement...
20081112-EE-VQP-43 / EE_QW_2_20070628