C 从不同大小的整数转换到指针-wint到指针转换
您好,我的代码中有下一个问题: “从不同大小的整数转换为指针-wint转换为指针” 这个问题在这行代码中C 从不同大小的整数转换到指针-wint到指针转换,c,semaphore,C,Semaphore,您好,我的代码中有下一个问题: “从不同大小的整数转换为指针-wint转换为指针” 这个问题在这行代码中 pthread_create(&filos[i], NULL, (void *)filosofos,(void *) i); 特别是在(void*)i中 #include <pthread.h> #include <semaphore.h> #include <stdio.h> #define N 5 //num.
pthread_create(&filos[i], NULL, (void *)filosofos,(void *) i);
特别是在(void*)i中
#include <pthread.h>
#include <semaphore.h>
#include <stdio.h>
#define N 5 //num. de filosofos
#define IZQ (i-1)%N //vecino izquierdo de i
#define DER (i+1)%N //vecino derecho de i
#define PENSANDO 0
#define CON_HAMBRE 1
#define COME 2
pthread_t filos[N]; //hilos que representan a los filósofos
sem_t mutex ; //semáforo para la sección crítica
sem_t s[N]; //semáforos para los filósofos
int estado [N] ; //estado actual de cada filósosfo
/*
el filosofo i va a perder el tiempo... (va a pensar)
*/
void pensar (int i)
{
int t ;
t = rand() % 11;
printf("Filosofo %d pensando \n", i) ;
estado[i] = PENSANDO;
sleep (t) ;
}
/*
El filosofo i, va a comer !!!!!!!!
*/
void comer (int i)
{
printf("Filósofo %d esta comiendo un caballo \n", i);
estado[i] = COME;
sleep (5);
}
/*
Verifica que pueda tomar ambos tenedores
*/
void verifica(int i)
{
if( estado[i]==CON_HAMBRE && estado[IZQ]!=COME && estado[DER]!=COME ){
estado[i] = COME;
printf("Filósofo %d comiendo\n", i) ;
sem_post(&s[i]);
}
}
/*
El filosofo i intenta tomar los tenedores
*/
void toma_tndrs(int i)
{
sem_wait(&mutex); //entra a la sección crítica, hace uso del semaforo
estado[i] = CON_HAMBRE; //dice: tengo mucha hambre!!!!!!!!!!
verifica(i); // verifica que pueda tomar los tenedores
sem_post(&mutex); //sale de la sección crítica y el sem. puede permitir la entrada a alguien más
sem_wait(&s[i]); //se bloquea si no consiguió los tenedores
}
/*
el filosofo i dejará los tenedores
*/
void deja_tndrs(int i)
{
sem_wait(&mutex); // de nuevo entra a la sección critica
estado[i] = PENSANDO; //deja de comer y se pone a pensar
verifica(IZQ);
verifica(DER);
sem_post(&mutex);
}
/*
*/
void * filosofos (int i)
{
int j ;
for (; ; )
{
pensar(i) ;
toma_tndrs(i) ;
comer(i) ;
deja_tndrs(i) ;
}
}
main()
{
int i ;
for(i = 0; i < 5; i++){
sem_init (&s[i], 0, 1);
estado[i] = PENSANDO ;
}
sem_init (&mutex, 0, 1);
//creamos un hilo de ejecucion para cada filosofo, que ejecuta filosofos()
for (i=0; i<N; i++)
pthread_create(&filos[i], NULL, (void *)filosofos,(void *) i);
//cada hilo espera a que terminen los demás y libera los recursos
for (i=0; i<N; i++){
pthread_join(filos[i],NULL);
}
}
#包括
#包括
#包括
#定义N 5//de filosofos的数量
#定义IZQ(i-1)%N//vecino izquierdo de i
#定义顺序(i+1)%N//vecino derecho de i
#定义PENSANDO 0
#定义CON_HAMBRE 1
#定义来2
pthread_t filos[N]//你是一个很有代表性的人吗
sem_t互斥体//塞姆阿弗罗·帕拉塞科恩·克里蒂卡酒店
sem_t s[N]//西蒙弗罗斯·帕拉洛斯菲洛索沃斯酒店
国际社会[N]//埃斯塔多·卡达·菲洛索斯福酒店
/*
菲洛索福酒店。。。(瓦阿彭萨)
*/
void pensar(国际一级)
{
int t;
t=rand()%11;
printf(“Filosofo%d pensando\n”,i);
estado[i]=PENSANDO;
睡眠(t);
}
/*
El filosofo i,弗吉尼亚州一个角落!!!!!!!!
*/
空位角(内部一)
{
printf(“Filósofo%d esta comiendo un caballo”,i);
我来了;
睡眠(5);
}
/*
对ambos tenedores进行验证
*/
无效验证(int i)
{
如果(我要来,我要来,我要来){
我来了;
printf(“Filósofo%d comiendo\n”,i);
sem_post(&s[i]);
}
}
/*
El filosofo i intenta tomar los tenedores酒店
*/
无效toma_tndrs(内部一)
{
sem_wait(&mutex);//在crítica的中心,我们需要一个
estado[i]=CON_HAMBRE;//骰子:tengo mucha HAMBRE!!!!!!!!!!
核实(一);//核实特内多雷斯的财产
sem_post(&mutex);//出售crítica和el-sem.puede permitir la entrada a alguien más
sem_wait(&s[i]);//这是一个没有考虑过的公寓
}
/*
el filosofo i dejarálos tenedores
*/
无效文件(国际一)
{
sem_wait(&mutex);//新的关键时刻
estado[i]=PENSANDO;//deja de comer y se pone a pensar
验证(IZQ);
验证(DER);
sem_post(和互斥);
}
/*
*/
void*filosofos(国际一级)
{
int j;
对于(;;)
{
彭萨(一);
toma_tndrs(一);
角(一);
deja_tndrs(i);
}
}
main()
{
int i;
对于(i=0;i<5;i++){
sem_init(&s[i],0,1);
estado[i]=PENSANDO;
}
sem_init(&mutex,0,1);
//如果你想喝点咖啡,那就喝点咖啡吧()
对于(i=0;i假设是i
变量的强制转换导致了问题,首先将其强制转换为大于int
的整数类型,同时足够大以容纳指针
其中一种类型是intptr\u t
,其设计目的是使其足够大以容纳任何整数或指针,如中所述
演员们看起来就像
(void *) (intptr_t) i
在线程函数中,执行相反的操作,首先转换为intptr\t
,然后转换为int
void * filosofos (void *p)
{
int i = (int) (intptr_t) p;
...
return NULL;
}
请注意,我将线程函数的签名更改为正确的,它使用了一个void*
参数,这在sizeof(int)!=sizeof(void*)的系统上产生了很大的不同
,这在您的情况下似乎是正确的。还要注意,我在函数末尾返回了一个NULL
。这是因为线程函数被声明(并指定)为返回指针。如果不从声明为返回指针的函数返回值,则会导致未定义的行为
对于那些好奇的人来说,虽然我通常不建议像这样对指针强制转换一个整数,但所有情况都有例外,这是少数可以接受这种强制转换的情况之一(如果不是唯一的情况的话)
许多人仍然看不起这样的解决方案,但仍然使用它,因为它比需要的整个malloc
-copy-free
马戏团更简单
为值使用动态分配内存的解决方案如下所示:
- 线程创建
// Allocate memory for the index
int *p = malloc(sizeof i);
if (p == NULL)
{
// TOOD: Handle error! (Left as an exercise to the reader)
}
// Copy the value
*p = i;
// And create the thread
pthread_create(&filos[i], NULL, &filosofos, p); // Note: no cast needed
- 在线程函数中
void * filosofos (void *p)
{
int i = *(int *) p;
free(p);
...
return NULL;
}
虽然此解决方案更“正确”,但它的可读性较差,这意味着它的可维护性较差;它有更多的代码,这意味着出错的可能性更大;如果忘记了free
调用(这迟早会发生),它就有可能发生内存泄漏.假设问题是由i
变量的强制转换引起的,首先将其强制转换为大于int
的整数类型,同时又大到足以容纳指针
其中一种类型是intptr\u t
,其设计目的是使其足够大以容纳任何整数或指针,如中所述
演员们看起来就像
(void *) (intptr_t) i
在线程函数中,执行相反的操作,首先转换为intptr\t
,然后转换为int
void * filosofos (void *p)
{
int i = (int) (intptr_t) p;
...
return NULL;
}
请注意,我将线程函数的签名更改为正确的,它使用了一个void*
参数,这在sizeof(int)!=sizeof(void*)的系统上产生了很大的不同
,这在您的情况下似乎是正确的。还要注意,我在函数末尾返回了一个NULL
。这是因为线程函数被声明(并指定)为返回指针。如果不从声明为返回指针的函数返回值,则会导致未定义的行为
对于那些好奇的人来说,虽然我通常不建议像这样对指针强制转换一个整数,但所有情况都有例外,这是少数可以接受这种强制转换的情况之一(如果不是唯一的情况的话)
许多人仍然看不起这样的解决方案,但仍然使用它,因为它比需要的整个malloc
-copy-free
马戏团更简单
为值使用动态分配内存的解决方案如下所示:
- 线程创建
// Allocate memory for the index
int *p = malloc(sizeof i);
if (p == NULL)
{
// TOOD: Handle error! (Left as an exercise to the reader)
}
// Copy the value
*p = i;
// And create the thread
pthread_create(&filos[i], NULL, &filosofos, p); // Note: no cast needed
- 在线程函数中
void * filosofos (void *p)
{
int i = *(int *) p;
free(p);
...
return NULL;
}
而这个独奏