Networking Forums

Networking Forums > Computer Networking > Linux Networking > segmentation fault

Reply
Thread Tools Display Modes

segmentation fault

 
 
Steven Luk
Guest
Posts: n/a

 
      12-09-2003, 04:25 AM
Please I need help fixing this "segmentation fault" problem in my
mulitithreaded echo server code.
-- server code --
static void *doit(void *); /* each thread executes this function */
void str_echo(int);
static int *status;

int
main(int argc, char **argv)
{
int listenfd, rc;
int *iptr;
unsigned short port_num;
int clilen;
struct sockaddr_in cliaddr, servaddr;

if(argc != 2)
{
printf("Usage: %s port\n", argv[0]);
exit(1);
}

port_num = (unsigned short) atoi(argv[1]);

listenfd = socket(PF_INET, SOCK_STREAM, 0);

bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = PF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(port_num);

bind(listenfd, (struct sockaddr *) &servaddr, sizeof(servaddr));

listen(listenfd, 5);

for ( ; ; ) {
//len = addrlen;
iptr = malloc(sizeof(int));
*iptr = accept(listenfd, (struct sockaddr *) &cliaddr, &clilen);

if ((rc = pthread_create(NULL, NULL, &doit, iptr)) > 0)
err_quit("Error Create Thread\n");
}
exit(0);
}

static void *
doit(void *arg)
{
int connfd, rc;

connfd = *((int *) arg);
free(arg);

if ((rc = pthread_detach(pthread_self())) > 0)
err_quit("Error Detach Thread\n");
//str_echo(connfd); /* same function as before */
writen(connfd, "OK!\n", 4);
close(connfd); /* we are done with connected socket */
pthread_exit(&status);
//return(NULL);
}

-- client code --
void str_cli(FILE *, int);
void *copyto(void *);

static FILE *fp;
static int sockfd, done=0, *status;

int
main(int argc, char **argv)
{
int socketfd, ret;
struct sockaddr_in servaddr;
unsigned short port;
if( argc != 3) {
printf("Usage: %s host port\n", argv[0]);
exit(1);
}
port = atoi(argv[2]);
socketfd = socket(PF_INET,SOCK_STREAM, 0);
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = PF_INET;
servaddr.sin_port = htons(port);
servaddr.sin_addr.s_addr = inet_addr(argv[1]);
ret = connect(socketfd, (struct sockaddr *) &servaddr, sizeof(servaddr));
if (ret == 0)
str_cli(stdin, socketfd);
else
{
printf("Error Connect to Server\n");
exit(1);
}

exit(0);
}

void
str_cli(FILE *fp_arg, int sockfd_arg)
{
char recvline[MAXLINE];
int rc;
pthread_t tid;

sockfd = sockfd_arg; /* copy arguments to externals */
fp = fp_arg;

if ((rc = pthread_create(&tid, NULL, copyto, fp)) > 0)
err_quit("Error Create Thread\n");

while (Readline(sockfd, recvline, MAXLINE) > 0)
fputs(recvline, stdout);

if (done == 0)
err_quit("server terminated prematurely");
}

void *
copyto(void *fp)
{
char sendline[MAXLINE];

while (fgets(sendline, MAXLINE, fp) != NULL)
writen(sockfd, sendline, strlen(sendline));

shutdown(sockfd, SHUT_WR); /* EOF on stdin, send FIN */

done = 1;
pthread_exit(&status);
//return(NULL);
}

Right now I just have the server send an "OK" message back to the client.
But I always get the "segmentation fault" every time the client tries to
connect.

[root@linuxgw proj3]# ./tcpserv02 2033
Segmentation fault

[root@linuxgw proj3]# ./tcpcli02 127.0.0.1 2033
OK!
server terminated prematurely

Both programs compiled w/o warnings/errors. Any ideas?


 
Reply With Quote
 
 
 
 
Jim Fischer
Guest
Posts: n/a

 
      12-09-2003, 09:13 PM
Steven Luk wrote:
> Please I need help fixing this "segmentation fault" problem in my
> mulitithreaded echo server code.


Please provide definitions for the following functions:

client code:
err_quit
Readline
writen

server code:
err_quit
writen

--
Jim

To reply by email, remove "link" and change "now.here" to "yahoo"
jfischer_link5809{at}now.here.com


 
Reply With Quote
 
Jim Fischer
Guest
Posts: n/a

 
      12-09-2003, 10:16 PM
Jim Fischer wrote:
> Steven Luk wrote:
>
>> Please I need help fixing this "segmentation fault" problem in my
>> mulitithreaded echo server code.


Also:

* You are not consistently checking for errors after you call a standard
library function (e.g., malloc, bind, listen, connect, etc.).

* I recommend you either: a) use a debugger (e.g., gdb, ddd) to observe
your program as it executes, or b) instrument your program so that it
outputs debug/trace information as it runs. Here's a commonly used
instrumentation technique:

<example>

/* The DEBUG_ENABLED macro:
* 0 : disable debug output
* 1 : enable debug output
*/
#define DEBUG_ENABLED 1
#if DEBUG_ENABLED
# define DEBUG_TRACE(fd_out) \
do { fprintf(fd_out,"%s, %d\n", __FILE__, __LINE__); \
fflush(fd_out); } while(0)
#else
# define DEBUG_TRACE(fd_out)
#endif

....

for ( ; ; ) {
//len = addrlen;
iptr = malloc(sizeof(int));
DEBUG_TRACE(stderr); // assume this is line 100
*iptr = accept(listenfd, (struct sockaddr *) &cliaddr, &clilen);
DEBUG_TRACE(stderr); // assume this is line 102

if ((rc = pthread_create(NULL, NULL, &doit, iptr)) > 0)
err_quit("Error Create Thread\n");
DEBUG_TRACE(stderr); // assume this is line 106
}

</example>

This kind of instrumentation can help you locate the code that's causing
the segfault. For example, if the program reaches line 100 and then it
segfaults (i.e., the program never reaches line 102), then you know the
accept() call is causing the problem (e.g., your program might be
passing an invalid argument into the accept() function).

--
Jim

To reply by email, remove "link" and change "now.here" to "yahoo"
jfischer_link5809{at}now.here.com


 
Reply With Quote
 
Lew Pitcher
Guest
Posts: n/a

 
      12-10-2003, 02:55 AM
Steven Luk wrote:
> Please I need help fixing this "segmentation fault" problem in my
> mulitithreaded echo server code.


I don't claim to be an expert at these things, but I believe I see the problem.

> -- server code --
> static void *doit(void *); /* each thread executes this function */
> void str_echo(int);
> static int *status;
>
> int
> main(int argc, char **argv)
> {
> int listenfd, rc;
> int *iptr;
> unsigned short port_num;
> int clilen;
> struct sockaddr_in cliaddr, servaddr;
>
> if(argc != 2)
> {
> printf("Usage: %s port\n", argv[0]);
> exit(1);
> }
>
> port_num = (unsigned short) atoi(argv[1]);
>
> listenfd = socket(PF_INET, SOCK_STREAM, 0);
>
> bzero(&servaddr, sizeof(servaddr));
> servaddr.sin_family = PF_INET;
> servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
> servaddr.sin_port = htons(port_num);
>
> bind(listenfd, (struct sockaddr *) &servaddr, sizeof(servaddr));
>
> listen(listenfd, 5);
>
> for ( ; ; ) {
> //len = addrlen;
> iptr = malloc(sizeof(int));
> *iptr = accept(listenfd, (struct sockaddr *) &cliaddr, &clilen);


At this point, accept() has blocked until the client attempted it's
connection. The accept() call has now unblocked and processing can continue.

This gives us a synchronization point; your code crashes at some point /after/
the accept() call.

> if ((rc = pthread_create(NULL, NULL, &doit, iptr)) > 0)


I believe that this is your problem.

The manpage for pthread_create(3) says that the first argument is a pointer to
a pthread_t. You have the first argument set to the NULL pointer. This is
important because pthread_create(3) says

"RETURN VALUE
On success, the identifier of the newly created thread is
stored in the location pointed by the |thread| argument,
and a 0 is returned. On error, a non-zero error code is
returned."

It is this |thread| argument that you've set to NULL. pthread_create(3) is
trying to store the thread identifier, and you've directed it to store the
identifier at NULL. This is likely the cause of your segmentation violation;
NULL is not contained within your address space.

Fix this and try again. Let us know what the results are.


> err_quit("Error Create Thread\n");
> }
> exit(0);
> }


[rest of code snipped]

> Right now I just have the server send an "OK" message back to the client.
> But I always get the "segmentation fault" every time the client tries to
> connect.
>
> [root@linuxgw proj3]# ./tcpserv02 2033
> Segmentation fault
>
> [root@linuxgw proj3]# ./tcpcli02 127.0.0.1 2033
> OK!
> server terminated prematurely
>
> Both programs compiled w/o warnings/errors. Any ideas?
>
>



--
Lew Pitcher

Master Codewright and JOAT-in-training
Registered Linux User #112576 (http://counter.li.org/)
Slackware - Because I know what I'm doing.

 
Reply With Quote
 
 
 
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
telnet: "segmentation fault" everytimes antiw Linux Networking 9 10-14-2006 09:22 PM
segmentation fault in tcptraceroute Alex Bransky Linux Networking 2 10-13-2004 04:03 PM
Segmentation fault need help Marcia Hon Linux Networking 4 02-16-2004 07:13 PM
Shell command gets Segmentation Fault Hagit Linux Networking 2 01-02-2004 01:13 PM
Apache2 segmentation fault when using SSLVerifyClient Stephan B. Linux Networking 1 12-15-2003 11:38 AM



1 2 3 4 5 6 7 8 9 10 11