信号量锁在问题中无法正常工作
Semaphore lock is not working properly in Problem
我有一个评估问题:
服务器可以设计限制开启次数
连接。例如,服务器可能希望在任何时候只有 N 个套接字连接
及时。一旦建立了 N 个连接,服务器将不再接受另一个传入
连接,直到释放现有连接。编写一个使用信号量的程序
同步服务器 activity 以限制并发连接数。
为了解决这个问题,我创建了 2 个信号量:
- 初始化为N的信号量
- 提供互斥的信号量。
我必须以这样的方式创建,如果建立了所有 N 个连接,客户端必须等到释放。
我尝试编写代码,但代码无法正常工作:
#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);
}
它给出了不同的执行顺序,例如,甚至在获取连接(客户端“x”)之前,它释放了连接,例如..
输出:
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 ....
请帮我更正代码!!
抱歉标题不好!!!
最有可能的问题是您创建线程的方式:
pthread_create(&thread[i],NULL,acquire,&(i));
作为线程的参数,您传递一个指向局部变量的指针 i
。问题是所有线程 获得相同的指针 ,这可能意味着多个线程可以取消引用它并获得相同的值!
通常不应该将普通值转换为指针,但在这种情况下通常可以接受。但是,您不应该直接转换为指针,而是使用 standard fixed width type intptr_t
然后将其转换为指针:
pthread_create(&thread[i],NULL,acquire,(void *) (intptr_t) i);
然后在线程函数中做相反的转换:
void *acquire(void * arg)
{
int tid = (int) (intptr_t) arg;
...
}
这样每个线程都会有自己的"id"。
我有一个评估问题:
服务器可以设计限制开启次数 连接。例如,服务器可能希望在任何时候只有 N 个套接字连接 及时。一旦建立了 N 个连接,服务器将不再接受另一个传入 连接,直到释放现有连接。编写一个使用信号量的程序 同步服务器 activity 以限制并发连接数。
为了解决这个问题,我创建了 2 个信号量:
- 初始化为N的信号量
- 提供互斥的信号量。
我必须以这样的方式创建,如果建立了所有 N 个连接,客户端必须等到释放。
我尝试编写代码,但代码无法正常工作:
#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);
}
它给出了不同的执行顺序,例如,甚至在获取连接(客户端“x”)之前,它释放了连接,例如..
输出:
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 ....
请帮我更正代码!!
抱歉标题不好!!!
最有可能的问题是您创建线程的方式:
pthread_create(&thread[i],NULL,acquire,&(i));
作为线程的参数,您传递一个指向局部变量的指针 i
。问题是所有线程 获得相同的指针 ,这可能意味着多个线程可以取消引用它并获得相同的值!
通常不应该将普通值转换为指针,但在这种情况下通常可以接受。但是,您不应该直接转换为指针,而是使用 standard fixed width type intptr_t
然后将其转换为指针:
pthread_create(&thread[i],NULL,acquire,(void *) (intptr_t) i);
然后在线程函数中做相反的转换:
void *acquire(void * arg)
{
int tid = (int) (intptr_t) arg;
...
}
这样每个线程都会有自己的"id"。