为什么一创建线程就提示段错误呢?

lyleliu 2004-09-06 05:08:26

就提示"段错误"呢, 都要郁闷死了, 基本上是按教科书来写的啊?会是哪方面的原因呢?刚在linux下编程, 就碰见这个问题, 该改的地方都该了, 还是不行.相关代码如下:
typedef _param{
int fd;
char* addBuf;
}Param;

pthread_t th;
pthread_attr_t ta;
pthread_attr_init(&ta);

Param.fd = 3;
Param.char[3] = 4;

pthread_create(&th, &ta, ((void*(*)(void*))robothandle, (void*)¶m);
robothandle是一个空函数
void robothandle(Param* para)
{
}
现在程序一执行到pthread_create那一步就不动了, 提示段错误.
...全文
546 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
slone 2005-04-12
  • 打赏
  • 举报
回复
up
fengge8ylf 2005-04-11
  • 打赏
  • 举报
回复
我的程序也出现这个问题了 一个很简单的线程程序
zly1980 2004-12-16
  • 打赏
  • 举报
回复
一个进程的线程数=(2*1024*1024)/8192
8192是ulimit -a看到的那个栈大小。你可以用ulimit -s 4096修改,这样你的程序可以生成512个线程了。
lyleliu 2004-09-06
  • 打赏
  • 举报
回复
下面是我的调试信息, 哪位帮忙给看看, 谢谢了
63 if(pthread_create(&th, &ta, robothandle, (void*)¶m) < 0)
(gdb)

Program received signal SIGSEGV, Segmentation fault.
0x00000000 in ?? ()
(gdb) bt
#0 0x00000000 in ?? ()
#1 0x08048948 in main () at MyServer.cpp:63
#2 0x42015574 in __libc_start_main () from /lib/tls/libc.so.6
(gdb) frame 0
#0 0x00000000 in ?? ()
(gdb) info local
No symbol table info available.
(gdb) list if
Function "if" not defined.
(gdb) frame 1
#1 0x08048948 in main () at MyServer.cpp:63
63 if(pthread_create(&th, &ta, robothandle, (void*)¶m) < 0)
(gdb) info local
th = 1073829932
ta = {__detachstate = 1108545304, __schedpolicy = 1108544020, __schedparam = {
__sched_priority = -1073745112}, __inheritsched = 134514245, __scope = 1108544020,
__guardsize = 1073792608, __stackaddr_set = -1073745096, __stackaddr = 0x8048b5e,
__stacksize = 1108544020}
param = {fd = 1074517909, addBuf = 0x80487b6 "[\201肻202\026"}
fsin = {sin_family = 62272, sin_port = 49151, sin_addr = {s_addr = 1073790467},
sin_zero = "訹\001@(l\001@"}
alen = 1073830456
msock = 456
ssock = 1073829932
lyleliu 2004-09-06
  • 打赏
  • 举报
回复
我都不行了, 还是没找到什么原因, 简直就是怪死了.这个段错误太可爱了!!!!!!!!!
yanghuajia 2004-09-06
  • 打赏
  • 举报
回复
似乎是因为exit ,改为pthread_exit()
exit 是退出程序, pthread_exit()结束线程

write 的返回值,失败是-1,成功返回写入的字符数

if(write(para->addBuf[i],buf,cc))
{
printf("The message sent to other sockets %d is failed",para->addBuf[i]);
exit(-1);
}


int
errexit(const char* format, ...)
{
va_list args;
va_start(args,format);
vfprintf(stderr, format, args);
va_end(args);
exit(1);
}
lyleliu 2004-09-06
  • 打赏
  • 举报
回复
不好意思, 写错了, 那两句可以去掉, 我只是想给Param定义的变量param初始化.
lynux 2004-09-06
  • 打赏
  • 举报
回复
Param.char[3] = 4;
这一句是什么意思啊?Param是一个结构类型,不是一个变量,并且也没有char这个成员啊?
lyleliu 2004-09-06
  • 打赏
  • 举报
回复
和线程有关的代码就这些了, 莫非还牵涉到别的东西.
#include "../include/MyServer.h"

#define BUFSIZE 4096

//#define INterval 5
#define PORTNUM 6000
#define QLEN 12
struct {
pthread_mutex_t st_mutex;
unsigned int st_concount;
unsigned int st_contotal;
unsigned long st_contime;
unsigned long st_bytecount;
}stats;

typedef struct _param{
int fd;
char* addBuf;
}Param;

void robothandle(Param* para);
int errexit(const char* format, ...);
int passiveTCP(int qlen);

/*---------------------------------------------------------------
* main - The concurrent UDP server for robot handle service
*---------------------------------------------------------------
*/

int main()
{
pthread_t th;
pthread_attr_t ta;
Param param;
struct sockaddr_in fsin;//the address of a client
unsigned int alen; //length of client's address
int msock;//master server socket
int ssock;//slave server socket;
msock = passiveTCP(QLEN);
(void) pthread_attr_init(&ta);
(void) pthread_attr_setdetachstate(&ta, PTHREAD_CREATE_DETACHED);//the thread resource are immediately freed when it terminate;
(void) pthread_mutex_init(&stats.st_mutex, 0);
while(1){
alen = sizeof(fsin);
memset(&fsin,0, alen);
stats.st_concount = 0;
ssock = accept(msock, (struct sockaddr*) &fsin, &alen);
//string str = ntohs(fsin.sin_addr.s_addr);

// unsigned long addNum;
//addNum = ntohl(fsin.sin_addr.s_addr);
param.addBuf[stats.st_concount] = ssock;
param.fd = ssock;

if(ssock < 0){
printf("accept:%s\n", strerror(errno));
exit(-1);
}
else
{
if(pthread_create(&th, &ta, (void* (*)(void *))robothandle, (void*)¶m) < 0)
{
errexit("pthread_create:%s\n", strerror(errno));
}

}

}
return 1;
}

void robothandle(Param* para)
{
int cc;
char buf[BUFSIZE];
(void) pthread_mutex_lock(&stats.st_mutex);
stats.st_concount++;
(void) pthread_mutex_unlock(&stats.st_mutex);
while (cc = read(para->fd, buf, sizeof(buf)))
{
for(int i=0; i<stats.st_concount;i++)
{
if(para->fd != para->addBuf[i]&&(cc > 0))
if(write(para->addBuf[i],buf,cc))
{
printf("The message sent to other sockets %d is failed",para->addBuf[i]);
exit(-1);
}
}
}



}
int passiveTCP(int qlen)
{
struct sockaddr_in sin;//an INternet ednpoint address
int s, type;//socket descriptor and socket type

memset(&sin, 0, sizeof(sin));
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = INADDR_ANY;
sin.sin_port = PORTNUM;
s = socket(PF_INET, SOCK_STREAM,0);
if(s<0)
printf("Can not create socket");
//bind the socket
if(bind(s, (struct sockaddr*)&sin, sizeof(sin)) < 0)
printf("Can not print bind to s");
if(listen(s, qlen) < 0)
{
printf("Can not listen");
exit(-1);
}
return s;

}

MyServer.h代码如下:
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <pthread.h>

#include </usr/include/kde/arts/type.h>
#include <sys/signal.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <sys/wait.h>
#include <errno.h>
#include <netinet/in.h>
#include <netdb.h>

#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>

int
errexit(const char* format, ...)
{
va_list args;
va_start(args,format);
vfprintf(stderr, format, args);
va_end(args);
exit(1);
}

blh 2004-09-06
  • 打赏
  • 举报
回复
paster your full source codes.

2,161

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 UNIX文化
社区管理员
  • UNIX文化社区
  • 文天大人
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧