Newsgroups: comp.parallel.pvm From: sjalali@nortel.ca Subject: Re: pvm_spawn args Organization: Deja News - The Leader in Internet Discussion Date: Wed, 11 Mar 1998 21:27:53 -0600 Message-ID: <6e7kl3$j36$1@nnrp1.dejanews.com> Hello, Thanks, the malloc() solution works, but I really meant to post the version where memory allocation is done via "array" declaration, which still produces the SIGSEGV: char task_arg[3][MAX_RNAME]; // Allocates memory ! strcpy( task_arg[0], "-p "); strcpy( task_arg[1], "\0" ); // or " " as second arg strcpy( task_arg[2], "\0" ); // or " " as second arg .... if( pvm_parent() == PvmNoParent ){ cout << "task_arg is: " ; for( numt=0; numt < 3; numt++ ) cout << task_arg[numt] << "#"; cout << "\n Done. " << endl; /* start up slave tasks */ numt = pvm_spawn( "sjtask", (char **) task_arg, PvmTaskDefault, "", nproc, tids); // If this ^^^ I don't cast then compiler says: // passing `char (*)[20]' as argument 2 cout << "NUMT = " << numt << endl; } .... When I run it, I get: floyd:~/pvm3/YY> sjhj X.IN Y.IN Args are: X.IN Args are: Y.IN task_arg is: -p### Done. Segmentation fault I wonder why the two memory allocations work so differently? Susheel In article <3506489C.167E@aei-potsdam.mpg.de>, Tom Goodale wrote: > > You aren't allocating the memory properly in the second example: > > sjalali@nortel.ca wrote: > > > > Hello, > > > > Could somebody tell me why:- > > > > Following "args" to a spawned task work well: > > > > char *task_arg[] = { "-ivhg0 ", " -pPA", (char *)0 }; > > The compilor allocates memory at compile time for the strings, as it > knows thir lengths. > > > numt = pvm_spawn( "sjtask", (char **) task_arg, > > PvmTaskDefault, "", nproc, tids); > > > > While the following variation does not (causes SIGSEGV): > > > > char *task_arg[3], tmp_str[MAX_RNAME]; > > You are just defining an array of length three holding pointers to > character strings here. The memory for the strings has not yet been > allocated. > > > ostrstream t_arg1( tmp_str, MAX_RNAME); > > strcpy( task_arg[0], "-ivhg0 "); > > Writing to unallocated memory -> seg-fault. > > You need to do task_arg[0] = (char *)malloc(8*sizeof(char)) first. > > > t_arg1 << " -p" << mytid << ends; > > strcat( task_arg[1], t_arg1.str() ); > > task_arg[2][0] = '\0'; > > > > Basically, this a test case for what I want - > > to be able to pass into args a variable > > value character string, so I can spawn a task, > > then have it join a group, then calculate the TID of another > > task and pas that TID as a command line arg to another > > task being spawned here. > > > > Thanks, > > Susheel > > -- > > Susheel Jalali, > > sjalali@nortel.ca > > > > DISCLAIMER: My views are mine; may or may NOT be shared by my employer > > > > -----== Posted via Deja News, The Leader in Internet Discussion ==----- > > http://www.dejanews.com/ Now offering spam-free web-based newsreading > > I hope this helps. > > Tom > -----== Posted via Deja News, The Leader in Internet Discussion ==----- http://www.dejanews.com/ Now offering spam-free web-based newsreading