当前位置: 技术问答>linux和unix
pthread 运行错误
来源: 互联网 发布时间:2016-04-28
本文导语: 如题: 这个是我写的一个server程序: 不过在运行的时候有错误,下面给出了运行的错误,大家帮忙看看 /*echoserver3.c * Creat on: 2008-11-23 * Auther : root * To echo the clients info(ip and hostname) while connect * and...
如题:
这个是我写的一个server程序:
不过在运行的时候有错误,下面给出了运行的错误,大家帮忙看看
错误:(后面是段错误)
[root@localhost network]# ./echoserver3 80
*** glibc detected *** ./echoserver3: double free or corruption (out): 0xbffa16e8 ***
======= Backtrace: =========
/lib/i686/libc.so.6[0xb7e55ff3]
/lib/i686/libc.so.6(cfree+0x90)[0xb7e59af0]
./echoserver3[0x8049272]
/lib/i686/libpthread.so.0[0xb7f364e2]
/lib/i686/libc.so.6(clone+0x5e)[0xb7ebf47e]
======= Memory map: ========
08048000-0804b000 r-xp 00000000 08:0c 16278 /home/data-on-linux/cs/network/echoserver3
0804b000-0804c000 rw-p 00002000 08:0c 16278 /home/data-on-linux/cs/network/echoserver3
0804c000-0806d000 rw-p 0804c000 00:00 0 [heap]
b7400000-b7421000 rw-p b7400000 00:00 0
b7421000-b7500000 ---p b7421000 00:00 0
b75df000-b75ea000 r-xp 00000000 08:0a 383574 /lib/libgcc_s-4.2.3.so.1
b75ea000-b75eb000 rw-p 0000a000 08:0a 383574 /lib/libgcc_s-4.2.3.so.1
b75eb000-b75ec000 ---p b75eb000 00:00 0
b75ec000-b7ded000 rw-p b75ec000 00:00 0
b7ded000-b7f2b000 r-xp 00000000 08:0a 383523 /lib/i686/libc-2.7.so
b7f2b000-b7f2d000 r--p 0013e000 08:0a 383523 /lib/i686/libc-2.7.so
b7f2d000-b7f2e000 rw-p 00140000 08:0a 383523 /lib/i686/libc-2.7.so
b7f2e000-b7f31000 rw-p b7f2e000 00:00 0
b7f31000-b7f44000 r-xp 00000000 08:0a 383527 /lib/i686/libpthread-2.7.so
b7f44000-b7f45000 r--p 00012000 08:0a 383527 /lib/i686/libpthread-2.7.so
b7f45000-b7f46000 rw-p 00013000 08:0a 383527 /lib/i686/libpthread-2.7.so
b7f46000-b7f49000 rw-p b7f46000 00:00 0
b7f64000-b7f7e000 r-xp 00000000 08:0a 383533 /lib/ld-2.7.so
b7f7e000-b7f7f000 r--p 00019000 08:0a 383533 /lib/ld-2.7.so
b7f7f000-b7f80000 rw-p 0001a000 08:0a 383533 /lib/ld-2.7.so
bff8e000-bffa3000 rw-p bffeb000 00:00 0 [stack]
ffffe000-fffff000 r-xp 00000000 00:00 0 [vdso]
已放弃
这个是我写的一个server程序:
不过在运行的时候有错误,下面给出了运行的错误,大家帮忙看看
/*echoserver3.c
* Creat on: 2008-11-23
* Auther : root
* To echo the clients info(ip and hostname) while connect
* and this use the posix thread
*/
#ifndef CSAPP_H_
#define CSAPP_H_
#endif
#ifndef MAXLIEN
#define MAXLIEN 8192
#endif
#include "csapp.h"
#include
void echo (int connfd ) ;
void *thread(void *vargp);
int main ( int argc , char **argv )
{
int listenfd , connfd , port , clientlen,*connfdp ;
struct sockaddr_in clientaddr ;
pthread_t tid ;
clientlen = sizeof ( clientaddr ) ;
if ( argc !=2 )
{
fprintf (stderr, "usage: %s n" , argv[0] ) ;
exit ( 1 ) ;
}
port = atoi ( argv[1] ) ;
listenfd = Open_listenfd ( port ) ;
while ( 1 )
{
connfd = Malloc (sizeof(int)); /*handing the race */
*connfdp = Accept ( listenfd , (SA *) &clientaddr , & clientlen ) ;
Pthread_create(&tid , NULL , thread , connfdp );
}
}
void *thread ( void *vargp )
{
int connfd = *((int *)vargp ) ;
Pthread_detach( pthread_self()) ;
free ( vargp ) ;
echo ( connfd ) ;
close (connfd ) ;
return NULL ;
}
void echo ( int connfd )
{
size_t n ;
char buf[MAXLIEN ] ;
rio_t rio ;
Rio_readinitb(&rio , connfd ) ;
while (( n = Rio_readlineb ( &rio , buf , MAXLIEN )) != 0 )
{
printf ( "server received %d bytes n " , n ) ;
Rio_writen ( connfd ,buf , n ) ;
fputs ( buf , stdout) ;
}
}
错误:(后面是段错误)
[root@localhost network]# ./echoserver3 80
*** glibc detected *** ./echoserver3: double free or corruption (out): 0xbffa16e8 ***
======= Backtrace: =========
/lib/i686/libc.so.6[0xb7e55ff3]
/lib/i686/libc.so.6(cfree+0x90)[0xb7e59af0]
./echoserver3[0x8049272]
/lib/i686/libpthread.so.0[0xb7f364e2]
/lib/i686/libc.so.6(clone+0x5e)[0xb7ebf47e]
======= Memory map: ========
08048000-0804b000 r-xp 00000000 08:0c 16278 /home/data-on-linux/cs/network/echoserver3
0804b000-0804c000 rw-p 00002000 08:0c 16278 /home/data-on-linux/cs/network/echoserver3
0804c000-0806d000 rw-p 0804c000 00:00 0 [heap]
b7400000-b7421000 rw-p b7400000 00:00 0
b7421000-b7500000 ---p b7421000 00:00 0
b75df000-b75ea000 r-xp 00000000 08:0a 383574 /lib/libgcc_s-4.2.3.so.1
b75ea000-b75eb000 rw-p 0000a000 08:0a 383574 /lib/libgcc_s-4.2.3.so.1
b75eb000-b75ec000 ---p b75eb000 00:00 0
b75ec000-b7ded000 rw-p b75ec000 00:00 0
b7ded000-b7f2b000 r-xp 00000000 08:0a 383523 /lib/i686/libc-2.7.so
b7f2b000-b7f2d000 r--p 0013e000 08:0a 383523 /lib/i686/libc-2.7.so
b7f2d000-b7f2e000 rw-p 00140000 08:0a 383523 /lib/i686/libc-2.7.so
b7f2e000-b7f31000 rw-p b7f2e000 00:00 0
b7f31000-b7f44000 r-xp 00000000 08:0a 383527 /lib/i686/libpthread-2.7.so
b7f44000-b7f45000 r--p 00012000 08:0a 383527 /lib/i686/libpthread-2.7.so
b7f45000-b7f46000 rw-p 00013000 08:0a 383527 /lib/i686/libpthread-2.7.so
b7f46000-b7f49000 rw-p b7f46000 00:00 0
b7f64000-b7f7e000 r-xp 00000000 08:0a 383533 /lib/ld-2.7.so
b7f7e000-b7f7f000 r--p 00019000 08:0a 383533 /lib/ld-2.7.so
b7f7f000-b7f80000 rw-p 0001a000 08:0a 383533 /lib/ld-2.7.so
bff8e000-bffa3000 rw-p bffeb000 00:00 0 [stack]
ffffe000-fffff000 r-xp 00000000 00:00 0 [vdso]
已放弃
|
connfd = Malloc (sizeof(int)); /*handing the race */
是不是想写 connfdp ?
是不是想写 connfdp ?
|
...
while ( 1 )
{
connfdp = Malloc (sizeof(int)); /*handing the race */
if(connfdp==NULL)
{
...
continue;//or break;
}
*connfdp = Accept ( listenfd , (SA *) &clientaddr , & clientlen ) ;
Pthread_create(&tid , NULL , thread , connfdp );
}
}
void * thread ( void *vargp )
{
//此处可以增加NULL判断,为了提高效率可以只在while(1)处判断
int connfd = *((int *)vargp ) ;
free ( vargp ) ;//如果下面的函数有异常或者信号中断,可能造成内存泄露,所以,建议放在前面,即用完就释放
Pthread_detach( pthread_self()) ;
echo ( connfd ) ;
close (connfd ) ;
return NULL ;
}
while ( 1 )
{
connfdp = Malloc (sizeof(int)); /*handing the race */
if(connfdp==NULL)
{
...
continue;//or break;
}
*connfdp = Accept ( listenfd , (SA *) &clientaddr , & clientlen ) ;
Pthread_create(&tid , NULL , thread , connfdp );
}
}
void * thread ( void *vargp )
{
//此处可以增加NULL判断,为了提高效率可以只在while(1)处判断
int connfd = *((int *)vargp ) ;
free ( vargp ) ;//如果下面的函数有异常或者信号中断,可能造成内存泄露,所以,建议放在前面,即用完就释放
Pthread_detach( pthread_self()) ;
echo ( connfd ) ;
close (connfd ) ;
return NULL ;
}
|
while ( 1 )
{
connfd = Malloc (sizeof(int)); /*handing the race */
*connfdp = Accept ( listenfd , (SA *) &clientaddr , & clientlen ) ;
Pthread_create(&tid , NULL , thread , connfdp );
}
这段代码,我觉得有问题 Malloc用在了 while ( 1 )里面,程序 不死也难啊
{
connfd = Malloc (sizeof(int)); /*handing the race */
*connfdp = Accept ( listenfd , (SA *) &clientaddr , & clientlen ) ;
Pthread_create(&tid , NULL , thread , connfdp );
}
这段代码,我觉得有问题 Malloc用在了 while ( 1 )里面,程序 不死也难啊