LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-27-2004, 07:33 PM   #1
J-Ral
LQ Newbie
 
Registered: May 2004
Posts: 3

Rep: Reputation: 0
Why do i get segmentation fault upon exiting main()


Hi,

I can't seem to figure out why I am getting the following run-time error when i run my C program.

This is a trace from the debug tool DDD:

Single stepping until exit from function __libc_start_main,
which has no line number information.

Program received signal SIGSEGV, Segmentation fault.
0x4008b27d in _IO_default_xsgetn () from /lib/libc.so.6
(gdb)

This happens at the very end of my main method:
int main(){

getJobNodes();

readJobNodes();

makeTree();
free(nodeProcs);
printf("returning from main...");

return 0;

}

I know the general reason why segementation faults occur - trying to access memory which is outside the scope of the program - common when string handling is done incorrectly.

But this has got me really stumped!!

Any help would be most appreciated! If more information is needed PLEASE say so....i'll do whateva it takes to get some help with this one!

Thank you,

J-ral
 
Old 05-27-2004, 07:53 PM   #2
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
Well, without the code for getJobNodes(), readJobNodes(), and makeTree() I really can't help you.
 
Old 05-27-2004, 08:21 PM   #3
J-Ral
LQ Newbie
 
Registered: May 2004
Posts: 3

Original Poster
Rep: Reputation: 0
Ok, they are big methods....

Should i post them here? Or send them to you?
 
Old 05-27-2004, 08:25 PM   #4
J-Ral
LQ Newbie
 
Registered: May 2004
Posts: 3

Original Poster
Rep: Reputation: 0
Actually, they're not that big...i was thinking of something else.

Here they are:

void getJobNodes(){
int char_count =0;
int maxDigits = 7;
int jobNo;
char ch;
char command[MAXCMD];

printf("Please enter a valid job number: \n");

scanf("%d",&jobNo);
sprintf(command, "getnodes %d > tmp_out.txt",jobNo);
system(command);
}

void addName(char *name, int index){
int length = strlen(name);

strncpy(nodeNames[index],name,length);
//null terminate the name
(nodeNames[index])[length+1] = '\0';
}

void readJobNodes(){

FILE *ifp; //input file pointer
int i,numOfLines,k,j;
int nodeNo = 0;

char *name;
static char *tempString;
int tempLength;
STRING nullTerminator = "\0";

if((ifp=fopen("tmp_out.txt","r"))==NULL){
printf("Unable to open tmp_out.txt.\nFile may not exist.\nExiting...");
exit(1);
}
//read in the user
if((tempString = getString(ifp)) != "EOF"){
tempLength = strlen(tempString);
strncpy(user,tempString,tempLength);
strncat(user,nullTerminator,2);
}
printf("The user is: %s\n",user);
//read in the master node
if((tempString = getString(ifp)) != "EOF"){
tempLength = strlen(tempString);
strncpy(masternode,tempString,tempLength);
strncat(masternode,nullTerminator,2);

numNodes++;
}
printf("The master node is: %s\n",masternode);
printf("Other compute nodes are: \n");
//read in the other nodes if any
for(i = 0; (tempString = getString(ifp)) != "EOF" ; i++){
addName(tempString,i);
printf("%s\n",nodeNames[i]);
numNodes++;
}


}
/* RSH to each node the job is running on and build
the process tree for the user running the job
on that node.
*/
void makeTree()
{
char *command;
char *nodeName;
STRING hostName;
int j,comm;
int hostNameLength = 50;
printf("The master node is: %s\n",masternode);
//rsh to master node
sprintf(command,"rsh %s getproclist %s > %s_proclist.txt",masternode,user,masternode);
system(command);
printf("%s\n",command);

//printf("Commands:\n%s\n",command);
printf("numnodes %d\n",numNodes);

for(j = 1;j<numNodes-1;j++){
printf("in loop");
nodeName = nodeNames[j];
//printf("%s\n",command,nodeNames[j]);
sprintf(command,"rsh %s getproclist %s > %s_proclist.txt",nodeName,user,nodeName);
printf("%s\n",command);

comm = system(command);

if( !( comm==(-1) || comm==127 ) ){
printf("Remote shelled to: %s\n",nodeName);

}

printf("exiting loop...");
}

printf("returning from makeTree...\n");

return;

}
 
Old 05-28-2004, 02:13 AM   #5
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
I think I see one problem right here in readJobNodes():

//read in the user
if((tempString = getString(ifp)) != "EOF"){
tempLength = strlen(tempString);
strncpy(user,tempString,tempLength);
strncat(user,nullTerminator,2);
}

The bolded line should be user[tempLength] = '\0';

strncpy() won't null-terminate user for you since you're only copying strlen(tempString) bytes from tempString, so your strncat() call doesn't actually know where the end of user is. Also, in addName() you should get rid of the +1 for the null-termination. So the line (nodeNames[index])[length+1] = '\0'; should be changed to (nodeNames[index])[length] = '\0';. With the +1 you're leaving room for one byte of garbage before the null-terminator.

I kind of stopped there. Let me know if you're still having problems after fixing all your strncpy() issues.

Here's an exerpt from 'man strncpy':

The strncpy() function is similar, except that not more
than n bytes of src are copied. Thus, if there is no null
byte among the first n bytes of src, the result wil not be
null-terminated.

Last edited by itsme86; 05-28-2004 at 02:17 AM.
 
Old 05-28-2004, 04:27 AM   #6
Ma3oiS
LQ Newbie
 
Registered: May 2004
Posts: 12

Rep: Reputation: 0
Hi,

check if you have allocated memory for variables user and masternode.
And why you free(nodeProcs);? You didn't use nodeProcs at all in your program.

Ma3oiS
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
what does Segmentation Fault mean ? baronlynx Linux - Newbie 10 10-25-2009 04:32 PM
yast segmentation fault, system freezing - nvidia driver at fault? BaltikaTroika SUSE / openSUSE 2 12-02-2005 09:34 AM
segmentation fault libriana Linux - Newbie 2 06-10-2005 05:52 PM
Segmentation Fault monojit_18 Programming 1 12-12-2004 11:22 AM
libxml++: segmentation fault when exiting program. isl01jbe Programming 0 06-20-2004 05:47 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:20 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration