当前位置: 技术问答>linux和unix
高手帮下忙 运行时段错误怎么回事
来源: 互联网 发布时间:2016-04-03
本文导语: 以下是客户端 #include #include #include #include #include #include #include #include #include #include #include #include #define BUFFER_SIZE 1000 #define SER_PORT 3999 struct send_data_buffer { char buffer[BUFFER_SIZE]; pthread_mutex_t lock; pthread_cond_t send_dat...
以下是客户端
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define BUFFER_SIZE 1000
#define SER_PORT 3999
struct send_data_buffer
{
char buffer[BUFFER_SIZE];
pthread_mutex_t lock;
pthread_cond_t send_data_able;
pthread_cond_t write_data_able;
int buffer_full;
};
struct receive_data_buffer
{
char buffer[BUFFER_SIZE];
pthread_mutex_t lock;
pthread_cond_t receive_data_able;
pthread_cond_t read_data_able;
int buffer_empty;
};
char ser_IP[]={"192.168.0.115"};
struct send_data_buffer *send_buffer;
struct receive_data_buffer *receive_buffer;
void send_buffer_init(struct send_data_buffer *b)
{
pthread_mutex_init(&b->lock,NULL);
pthread_cond_init(&b->send_data_able,NULL);
pthread_cond_init(&b->write_data_able,NULL);
b->buffer_full=0;
}
void receive_buffer_init(struct receive_data_buffer *b)
{
pthread_mutex_init(&b->lock,NULL);
pthread_cond_init(&b->receive_data_able,NULL);
pthread_cond_init(&b->read_data_able,NULL);
b->buffer_empty=1;
}
void *transmission(void *data)
{
pid_t child1,child2;
int sockfd, numbytes,status1,status2;
char buf[BUFFER_SIZE];
struct hostent *he;
struct sockaddr_in ser_addr;
if((he=gethostbyname(ser_IP))==NULL)
{
herror("gethostbyname error");
exit(1);
}
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
perror("socket create error");
return;
}
ser_addr.sin_family = AF_INET;
ser_addr.sin_port = htons(SER_PORT);
ser_addr.sin_addr = *((struct in_addr *)he->h_addr);
bzero(&(ser_addr.sin_zero),8);
if (connect(sockfd, (struct sockaddr *)&ser_addr, sizeof(struct sockaddr)) == -1)
{
perror("connect error");
return;
}
if(!(child1=fork()))
{
int len;
while(1)
{
pthread_mutex_lock(&send_buffer->lock);
while(send_buffer->buffer_full==0)
{
pthread_cond_wait(&send_buffer->send_data_able,&send_buffer->lock);
}
len = strlen(send_buffer->buffer);
if (send(sockfd,send_buffer->buffer,len, 0) == -1)
{
perror("send error");
return;
}
send_buffer->buffer_full=0;
pthread_cond_signal(&send_buffer->write_data_able);
pthread_mutex_unlock(&send_buffer->lock);
}
exit(0);
}
if(!(child2=fork()))
{
char buf[BUFFER_SIZE];
while(1)
{
if (( numbytes = recv(sockfd, buf, BUFFER_SIZE, 0)) == -1||numbytes==0)
{
perror("recv error");
}
buf[numbytes] = '';
pthread_mutex_lock(&receive_buffer->lock);
while(receive_buffer->buffer_empty==0)
{
pthread_cond_wait(&receive_buffer->receive_data_able,&receive_buffer->lock);
}
strcpy(receive_buffer->buffer,buf);
receive_buffer->buffer_empty=0;
pthread_cond_signal(&receive_buffer->read_data_able);
pthread_mutex_unlock(&receive_buffer->lock);
}
}
waitpid(child1,&status1,0);
waitpid(child2,&status2,0);
close(sockfd);
return;
}
void *infor_format(void *data)
{
pid_t child1,child2;
int status;
if(!(child1=fork()))
{
char buf[BUFFER_SIZE];
while(1)
{
gets(buf);
//buf=strcat("client send:",buf);
//buf=strcat(buf,'n');
pthread_mutex_lock(&send_buffer->lock);
while(send_buffer->buffer_full==1)
{
pthread_cond_wait(&send_buffer->write_data_able,&send_buffer->lock);
}
strcpy(send_buffer->buffer,buf);
send_buffer->buffer_full=1;
pthread_cond_signal(&send_buffer->send_data_able);
pthread_mutex_unlock(&send_buffer->lock);
}
}
if(!(child2=fork()))
{
char buf[BUFFER_SIZE];
while(1)
{
pthread_mutex_lock(&receive_buffer->lock);
while(receive_buffer->buffer_empty==1)
{
pthread_cond_wait(&receive_buffer->read_data_able,&receive_buffer->lock);
}
strcpy(buf,receive_buffer->buffer);
// buf=strcat("client receive:",buf);
// buf=strcat(buf,'n');
puts(buf);
receive_buffer->buffer_empty=1;
pthread_cond_signal(&receive_buffer->read_data_able);
pthread_mutex_unlock(&receive_buffer->lock);
}
}
waitpid(child1,&status,0);
waitpid(child2,&status,0);
close(sockfd);
}
int main(void)
{
pthread_t th_a,th_b;
void *retval;
send_buffer_init(send_buffer);
receive_buffer_init(receive_buffer);
pthread_create(&th_a,NULL,transmission,0);
pthread_create(&th_b,NULL,infor_format,0);
pthread_join(th_a,&retval);
pthread_join(th_b,&retval);
return 0;
}
以上我写的SOCKET通信的客户端 能编译通过
但是在运行的时候出现了“段错误”的提示
高手帮下忙 这是怎么回事
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define BUFFER_SIZE 1000
#define SER_PORT 3999
struct send_data_buffer
{
char buffer[BUFFER_SIZE];
pthread_mutex_t lock;
pthread_cond_t send_data_able;
pthread_cond_t write_data_able;
int buffer_full;
};
struct receive_data_buffer
{
char buffer[BUFFER_SIZE];
pthread_mutex_t lock;
pthread_cond_t receive_data_able;
pthread_cond_t read_data_able;
int buffer_empty;
};
char ser_IP[]={"192.168.0.115"};
struct send_data_buffer *send_buffer;
struct receive_data_buffer *receive_buffer;
void send_buffer_init(struct send_data_buffer *b)
{
pthread_mutex_init(&b->lock,NULL);
pthread_cond_init(&b->send_data_able,NULL);
pthread_cond_init(&b->write_data_able,NULL);
b->buffer_full=0;
}
void receive_buffer_init(struct receive_data_buffer *b)
{
pthread_mutex_init(&b->lock,NULL);
pthread_cond_init(&b->receive_data_able,NULL);
pthread_cond_init(&b->read_data_able,NULL);
b->buffer_empty=1;
}
void *transmission(void *data)
{
pid_t child1,child2;
int sockfd, numbytes,status1,status2;
char buf[BUFFER_SIZE];
struct hostent *he;
struct sockaddr_in ser_addr;
if((he=gethostbyname(ser_IP))==NULL)
{
herror("gethostbyname error");
exit(1);
}
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
perror("socket create error");
return;
}
ser_addr.sin_family = AF_INET;
ser_addr.sin_port = htons(SER_PORT);
ser_addr.sin_addr = *((struct in_addr *)he->h_addr);
bzero(&(ser_addr.sin_zero),8);
if (connect(sockfd, (struct sockaddr *)&ser_addr, sizeof(struct sockaddr)) == -1)
{
perror("connect error");
return;
}
if(!(child1=fork()))
{
int len;
while(1)
{
pthread_mutex_lock(&send_buffer->lock);
while(send_buffer->buffer_full==0)
{
pthread_cond_wait(&send_buffer->send_data_able,&send_buffer->lock);
}
len = strlen(send_buffer->buffer);
if (send(sockfd,send_buffer->buffer,len, 0) == -1)
{
perror("send error");
return;
}
send_buffer->buffer_full=0;
pthread_cond_signal(&send_buffer->write_data_able);
pthread_mutex_unlock(&send_buffer->lock);
}
exit(0);
}
if(!(child2=fork()))
{
char buf[BUFFER_SIZE];
while(1)
{
if (( numbytes = recv(sockfd, buf, BUFFER_SIZE, 0)) == -1||numbytes==0)
{
perror("recv error");
}
buf[numbytes] = '';
pthread_mutex_lock(&receive_buffer->lock);
while(receive_buffer->buffer_empty==0)
{
pthread_cond_wait(&receive_buffer->receive_data_able,&receive_buffer->lock);
}
strcpy(receive_buffer->buffer,buf);
receive_buffer->buffer_empty=0;
pthread_cond_signal(&receive_buffer->read_data_able);
pthread_mutex_unlock(&receive_buffer->lock);
}
}
waitpid(child1,&status1,0);
waitpid(child2,&status2,0);
close(sockfd);
return;
}
void *infor_format(void *data)
{
pid_t child1,child2;
int status;
if(!(child1=fork()))
{
char buf[BUFFER_SIZE];
while(1)
{
gets(buf);
//buf=strcat("client send:",buf);
//buf=strcat(buf,'n');
pthread_mutex_lock(&send_buffer->lock);
while(send_buffer->buffer_full==1)
{
pthread_cond_wait(&send_buffer->write_data_able,&send_buffer->lock);
}
strcpy(send_buffer->buffer,buf);
send_buffer->buffer_full=1;
pthread_cond_signal(&send_buffer->send_data_able);
pthread_mutex_unlock(&send_buffer->lock);
}
}
if(!(child2=fork()))
{
char buf[BUFFER_SIZE];
while(1)
{
pthread_mutex_lock(&receive_buffer->lock);
while(receive_buffer->buffer_empty==1)
{
pthread_cond_wait(&receive_buffer->read_data_able,&receive_buffer->lock);
}
strcpy(buf,receive_buffer->buffer);
// buf=strcat("client receive:",buf);
// buf=strcat(buf,'n');
puts(buf);
receive_buffer->buffer_empty=1;
pthread_cond_signal(&receive_buffer->read_data_able);
pthread_mutex_unlock(&receive_buffer->lock);
}
}
waitpid(child1,&status,0);
waitpid(child2,&status,0);
close(sockfd);
}
int main(void)
{
pthread_t th_a,th_b;
void *retval;
send_buffer_init(send_buffer);
receive_buffer_init(receive_buffer);
pthread_create(&th_a,NULL,transmission,0);
pthread_create(&th_b,NULL,infor_format,0);
pthread_join(th_a,&retval);
pthread_join(th_b,&retval);
return 0;
}
以上我写的SOCKET通信的客户端 能编译通过
但是在运行的时候出现了“段错误”的提示
高手帮下忙 这是怎么回事
|
没有给
struct send_data_buffer *send_buffer;
struct receive_data_buffer *receive_buffer;
这两个指针分配内存
struct send_data_buffer *send_buffer;
struct receive_data_buffer *receive_buffer;
这两个指针分配内存
|
struct send_data_buffer *send_buffer;
struct receive_data_buffer *receive_buffer;
这两个指针没有进行初始化,你后面的操作是对其进行空指针操作,所以会报段错误。
struct receive_data_buffer *receive_buffer;
这两个指针没有进行初始化,你后面的操作是对其进行空指针操作,所以会报段错误。
|
pintf打印输出,或者gdb调试,段错误一般是你内存操作溢出。很容易找到
|
一般来说段错误是指针问题造成的