wssoh85
December 18th, 2008, 06:39 PM
Hi, everyone. I'm learning thread now and I face the problem of segmentation fault. I know that the segmentation fault is most probably occur when pthread_cancel() function was called. But I can't fix it. Please help me.
Here is my code. Sorry for such a long code
/* threadclient.c */
#include <pthread.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
void *cthr_do();
void *cthr_send();
void *cthr_recv();
struct package {pthread_t send_thr, recv_thr;
int sock;} ;
int main()
{
int k;
pthread_t new_thr;
int check = pthread_create(&new_thr, NULL, cthr_do, NULL );
if(check!=0)
printf("The thread cannot be created!");
pthread_join (new_thr, NULL);
printf("The whole process terminated\n");
return 0;
}
void *cthr_do()
{
printf("thread run sucessful\n");
pthread_t send_thr, recv_thr;
int sock, bytes_received, connected, s, r;
char send_data[1024],recv_data[1024];
struct hostent *host;
struct sockaddr_in6 server_addr;
struct package pack, *p_pack;
host = gethostbyname("::1");
if ((sock = socket(AF_INET6, SOCK_STREAM, 0)) == -1)
{
perror("Socket");
exit(1);
}
p_pack=&pack;
pack.sock=sock;
server_addr.sin6_family = AF_INET6;
server_addr.sin6_port = htons(5000);
server_addr.sin6_addr= in6addr_any;
connected = connect(sock, (struct sockaddr *)&server_addr, sizeof(server_addr));
if (connected== -1)
{
perror("Connect");
exit(1);
}
printf("The connection is established.\n");
int chk_send = pthread_create(&send_thr, NULL, cthr_send, (void *)p_pack);
if(chk_send!=0)
printf("The send message thread cannot be created!");
int chk_recv = pthread_create(&recv_thr, NULL, cthr_recv, p_pack);
if(chk_recv!=0)
printf("The receive message thread cannot be created!");
pack.send_thr = send_thr;
pack.recv_thr = recv_thr;
while (1)
{
s = pthread_join (pack.send_thr, NULL);
r = pthread_join (pack.recv_thr, NULL);
if (s && r)
break;
}
close(sock);
printf("closed socket\n");
}
void *cthr_send(void *arg)
{
int sock, test;
char send_data [1024], recv_data[1024];
struct package *point;
point = arg;
pthread_t recv_thr = (*point).recv_thr;
while (1)
{
printf("\n SEND (q or Q to quit) : ");
gets(send_data)/*,1024,stdin)*/;
if (strcmp(send_data , "q") == 0 || strcmp(send_data , "Q") == 0)
{
send((*point).sock, send_data,strlen(send_data), 0);
/*close ((*point).sock);*//**/
break;
}
else
send((*point).sock, send_data,strlen(send_data), 0);
}
puts("position of segmentation fault");
if ( pthread_cancel(/*(*point).*//**/recv_thr != 0) )
{
perror("pthread_cancel failed");
exit(3);
}
}
void *cthr_recv(void *arg)
{
char send_data [1024] , recv_data[1024];
int bytes_received, sock;
struct package *point;
point = arg;
if ((sock = socket(AF_INET6, SOCK_STREAM, 0)) == -1)
{
perror("Socket");
exit(1);
}
while (1)
{
bytes_received = recvfrom((*point).sock, recv_data, 1024-1, 0, NULL, NULL);
/*recv((int) arg,recv_data,1024,0);*/
if (bytes_received == -1) //Check the received message error status
{
perror ("Client recv()");
exit(1);
}
printf("\nThe number of digit received: %d\n", bytes_received);
recv_data[bytes_received] = '\0';
if (strcmp(recv_data, "q") == 0 || strcmp(recv_data , "Q") == 0)
{
send(sock, "q", strlen("q"), 0);
// close((int) arg);
break;
}
else
printf("\n RECEIVED DATA = %s \n" , recv_data);
fflush(stdout);
}
if ( pthread_cancel((*point).send_thr != 0) )
{
perror("pthread_cancel failed");
exit(1);
}
}
The purpose of this program is simple TCP client to have continuous conversation with server. Therefore, I create 2 thread which is to send message and receive message from server.
compile had no problem, but when run and this client type a "q" to quit the program, the error occur.
error is shown here
vince@vince-laptop:~/Desktop$ ./tethreadclient
thread run sucessful
The connection is established.
SEND (q or Q to quit) : q
position of segmentation fault
Segmentation fault (core dumped)
vince@vince-laptop:~/Desktop$
Anyone please give me some direction how to solve this.
Here is my code. Sorry for such a long code
/* threadclient.c */
#include <pthread.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
void *cthr_do();
void *cthr_send();
void *cthr_recv();
struct package {pthread_t send_thr, recv_thr;
int sock;} ;
int main()
{
int k;
pthread_t new_thr;
int check = pthread_create(&new_thr, NULL, cthr_do, NULL );
if(check!=0)
printf("The thread cannot be created!");
pthread_join (new_thr, NULL);
printf("The whole process terminated\n");
return 0;
}
void *cthr_do()
{
printf("thread run sucessful\n");
pthread_t send_thr, recv_thr;
int sock, bytes_received, connected, s, r;
char send_data[1024],recv_data[1024];
struct hostent *host;
struct sockaddr_in6 server_addr;
struct package pack, *p_pack;
host = gethostbyname("::1");
if ((sock = socket(AF_INET6, SOCK_STREAM, 0)) == -1)
{
perror("Socket");
exit(1);
}
p_pack=&pack;
pack.sock=sock;
server_addr.sin6_family = AF_INET6;
server_addr.sin6_port = htons(5000);
server_addr.sin6_addr= in6addr_any;
connected = connect(sock, (struct sockaddr *)&server_addr, sizeof(server_addr));
if (connected== -1)
{
perror("Connect");
exit(1);
}
printf("The connection is established.\n");
int chk_send = pthread_create(&send_thr, NULL, cthr_send, (void *)p_pack);
if(chk_send!=0)
printf("The send message thread cannot be created!");
int chk_recv = pthread_create(&recv_thr, NULL, cthr_recv, p_pack);
if(chk_recv!=0)
printf("The receive message thread cannot be created!");
pack.send_thr = send_thr;
pack.recv_thr = recv_thr;
while (1)
{
s = pthread_join (pack.send_thr, NULL);
r = pthread_join (pack.recv_thr, NULL);
if (s && r)
break;
}
close(sock);
printf("closed socket\n");
}
void *cthr_send(void *arg)
{
int sock, test;
char send_data [1024], recv_data[1024];
struct package *point;
point = arg;
pthread_t recv_thr = (*point).recv_thr;
while (1)
{
printf("\n SEND (q or Q to quit) : ");
gets(send_data)/*,1024,stdin)*/;
if (strcmp(send_data , "q") == 0 || strcmp(send_data , "Q") == 0)
{
send((*point).sock, send_data,strlen(send_data), 0);
/*close ((*point).sock);*//**/
break;
}
else
send((*point).sock, send_data,strlen(send_data), 0);
}
puts("position of segmentation fault");
if ( pthread_cancel(/*(*point).*//**/recv_thr != 0) )
{
perror("pthread_cancel failed");
exit(3);
}
}
void *cthr_recv(void *arg)
{
char send_data [1024] , recv_data[1024];
int bytes_received, sock;
struct package *point;
point = arg;
if ((sock = socket(AF_INET6, SOCK_STREAM, 0)) == -1)
{
perror("Socket");
exit(1);
}
while (1)
{
bytes_received = recvfrom((*point).sock, recv_data, 1024-1, 0, NULL, NULL);
/*recv((int) arg,recv_data,1024,0);*/
if (bytes_received == -1) //Check the received message error status
{
perror ("Client recv()");
exit(1);
}
printf("\nThe number of digit received: %d\n", bytes_received);
recv_data[bytes_received] = '\0';
if (strcmp(recv_data, "q") == 0 || strcmp(recv_data , "Q") == 0)
{
send(sock, "q", strlen("q"), 0);
// close((int) arg);
break;
}
else
printf("\n RECEIVED DATA = %s \n" , recv_data);
fflush(stdout);
}
if ( pthread_cancel((*point).send_thr != 0) )
{
perror("pthread_cancel failed");
exit(1);
}
}
The purpose of this program is simple TCP client to have continuous conversation with server. Therefore, I create 2 thread which is to send message and receive message from server.
compile had no problem, but when run and this client type a "q" to quit the program, the error occur.
error is shown here
vince@vince-laptop:~/Desktop$ ./tethreadclient
thread run sucessful
The connection is established.
SEND (q or Q to quit) : q
position of segmentation fault
Segmentation fault (core dumped)
vince@vince-laptop:~/Desktop$
Anyone please give me some direction how to solve this.