Reputation: 3
I have assingment problem which states :
Servers can be designed to limit the number of open connections. For example, a server may wish to have only N socket connections at any point in time. As soon as N connections are made, the server will not accept another incoming connection until an existing connection is released. Write a program using semaphores to synchronize server activity to limit the number of concurrent connections.
To solve it ,, I have created 2 semaphore :
I have to create in such a way that if all N connection is made, client must wait till release is made.
I have tried to code it, but code is not working properly :
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<pthread.h>
#include<semaphore.h>
#include<stdlib.h>
#include<string.h>
sem_t allow;
sem_t mutex;
void *acquire(void * arg)
{
int tid = *(int *)arg;
tid+=1;
sem_wait(&mutex);
sem_wait(&allow);
printf("\n Client %d is connected to the server....",tid);
sem_post(&mutex);
sleep(rand()%10);
release(tid);
}
void release(int num)
{
printf("\n Client %d releases the Server ....",num);
sem_post(&allow);
}
void main(int argc,char ** argv)
{
int n;
int i;
printf("\n Enter the number of client :");
scanf("%d", &n);
int maxi=3; // maximum no. of connection available
sem_init(&allow,0,maxi); // semaphore that is initialised to maxi.
//no. of connection
sem_init(&mutex,0,1); // for mutual exclusion
pthread_t thread[n];
for(i=0;i<n;i++) {
pthread_create(&thread[i],NULL,acquire,&(i)); // Clients are
// acquiring ..
}
for(i=0;i<n;i++)
pthread_join(thread[i],NULL);
sem_destroy(&allow);
sem_destroy(&mutex);
}
Its giving different order of execution like , even before acquiring connection (client "x") its releasing the connection like ..
Output :
Enter the number of client :6
Client 3 is connected to the server....
Client 3 is connected to the server....
Client 4 is connected to the server....
Client 3 releases the Server ....
Client 6 is connected to the server....
Client 3 releases the Server ....
Client 6 is connected to the server....
Client 4 releases the Server ....
Client 1 is connected to the server....
Client 6 releases the Server ....
Client 6 releases the Server ....
Client 1 releases the Server ....
Please help me to correct the code !!
And Sorry for bad title !!!
Upvotes: 0
Views: 1463
Reputation: 409136
The most likely problem is how you create your thread:
pthread_create(&thread[i],NULL,acquire,&(i));
As argument for the threads you pass a pointer to the local variable i
. The problem is that all threads get the same pointer, and that can mean multiple threads could dereference it and get the same value!
Normally one should not really cast normal values as pointers, but in a case such as this it's usually accepted. However you should not cast directly to a pointer, but use the standard fixed width type intptr_t
and then cast that to a pointer:
pthread_create(&thread[i],NULL,acquire,(void *) (intptr_t) i);
Then you do the opposite cast in the thread functions:
void *acquire(void * arg)
{
int tid = (int) (intptr_t) arg;
...
}
That way each thread will have their own "id".
Upvotes: 1