热血高校漫画哪里有:【讨论】malloc的可重入性和线程安全性

来源:百度文库 编辑:中财网 时间:2024/05/09 20:09:12

#include
#include
#include
#include
#include
#include
#include

#define TRUE 1
#define FALSE 0

int GP[10];

int num = 10;

pthread_mutex_t  _mutex = PTHREAD_MUTEX_INITIALIZER;

#define VMallocLightType( type, amount ) ( (type *)malloc( sizeof(type)*(amount) ) )
#define VFreeLightType( pointer ) ( free( pointer ) )   //统一起见
#define SleepMs( ms ) ( usleep( (ms)*1000 ) )

void doing( int ii )
{
 int *pp[100][100];
 for( int i = 0; i < 100; ++i )
 for( int j = 0; j < 100; ++j )
 {
  pthread_mutex_lock( &_mutex );
  pp[i][j] = VMallocLightType( int, num );
  pthread_mutex_unlock( &_mutex );
  
  pp[i][j][0] = GP[0];
 }

 for( int i = 0; i < 100; ++i )
 for( int j = 0; j < 100; ++j )
 {
  pthread_mutex_lock( &_mutex );
  VFreeLightType( pp[i][j] ); 
  pthread_mutex_unlock( &_mutex );
 }
 
// ++num;

 printf( "[%d]:%p\n", ii, pp );

 SleepMs( 1 );
}

void *tFun0( void * )

 int a[1];
 while(1)
 {
  doing(0);
 }
}

void *tFun1( void * )
{
 int a[1];
 while(1)
 {
  doing(1);
 }
}

void *tFun2( void * )
{
 int a[1];
 while(1)
 {
  doing(2);
 }
}

void *tFun3( void * )
{
 int a[1];
 while(1)
 {
  doing(3);
 }
}

void *tFun4( void * )
{
 int a[1];
 while(1)
 {
  doing(4);
 }
}

void *tFun5( void * )
{
 int a[1];
 while(1)
 {
  doing(5);
 }
}

int  createThread( pthread_t  *thread, pthread_attr_t * attr, void *(*start_routine)(void *), void * arg )
{
 if ( pthread_create( thread, attr, start_routine, arg ) )  //成功则返回0
 {


 }

 return TRUE;  
}

void  timeSignalFunc( int )
{
 int a[1];
 doing(9);
}

void initSigaction()
{
   struct sigaction act;
   act.sa_handler = timeSignalFunc;
   act.sa_flags = SA_RESTART;
   sigemptyset( &act.sa_mask );
   sigaction( SIGALRM, &act, NULL );
}

void initTime()
{
   struct itimerval value;
   value.it_value.tv_sec = 0;
   value.it_value.tv_usec = 100 * 1000;
   value.it_interval = value.it_value;
   setitimer( ITIMER_REAL, &value, NULL );
}

int main(int argc, char* argv[])
{
 scanf( "%d", &num );

 printf( "glibc1:%p,glibc2:%p,glibc3:%p,heap1:%p,heap2:%p,heap3:%p,stack:%p,data:%p,code0:%p,code1:%p\n",
   printf, sigaction, pthread_mutex_lock, malloc(16), malloc(128), malloc(1024*1024),&argc, GP, main, initTime );
 
//glibc1:0x8654,glibc2:0x86a8,glibc3:0x86cc,heap1:0x111a0,heap2:0x111b8,heap3:0x40135008,stack:0xbffffd4c,
//data:0x11060,code0:0x8cd0,code1:0x8c78

// initSigaction();  
// initTime(); 
 pthread_t touch_thread[6];  


 createThread( &touch_thread[0], NULL, tFun0, NULL );
 createThread( &touch_thread[1], NULL, tFun1, NULL );
 createThread( &touch_thread[2], NULL, tFun2, NULL );
 createThread( &touch_thread[3], NULL, tFun3, NULL );
 createThread( &touch_thread[4], NULL, tFun4, NULL );
 createThread( &touch_thread[5], NULL, tFun5, NULL ); 
 
 while( 1 )
 {
 // num = num % 121;
   
  doing( 8 );

 // printf( "main:%p\n", pp );
 }    
 return 0;
}

//注1:定时函数与主线程函数共用堆栈
//注2:普通线程栈区的默认大小是2M,主线程栈区是8M
//注3:

//经测试:malloc非线程安全(在malloc,free操作很频繁时,会出错),加互斥量后,OK

/*
未加互斥量:
[5]:0xbedf6104
[0]:0xbf7f6104
[8]:0xbfff60f0
[4]:0xbeff6104
[2]:0xbf3f6104
[1]:0xbf5f6104
[3]:0xbf1f6104
[3]:0xbf1f6104
[5]:0xbedf6104
[0]:0xbf7f6104
[8]:0xbfff60f0
[2]:0xbf3f6104
[4]:0xbeff6104
[1]:0xbf5f6104
[3]:0xbf1f6104
[2]:0xbf3f6104
[8]:0xbfff60f0
[0]:0xbf7f6104
[1]:0xbf5f6104
[8]:0xbfff60f0
[4]:0xbeff6104
[2]:0xbf3f6104
[1]:0xbf5f6104
[0]:0xbf7f6104
[5]:0xbedf6104
Out of Memory: Killed process 185 (mmmm).
Out of Memory: Killed process 186 (mmmm).
Out of Memory: Killed process 187 (mmmm).
Out of Memory: Killed process 188 (mmmm).
Out of Memory: Killed process 189 (mmmm).
Out of Memory: Killed process 190 (mmmm).
Out of Memory: Killed process 191 (mmmm).
Out of Memory: Killed process 192 (mmmm).
Terminated
*/

//经测试:malloc也不可重入,定时函数重入主线程时,都会死掉。

//注:以上测试平台PXA255_LINUX。