LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
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 04-03-2009, 01:29 PM   #1
Kenhokie
LQ Newbie
 
Registered: Oct 2008
Posts: 1

Rep: Reputation: 0
Help with Spawning a process - passing arguments


I am trying to spawn a new process and pass arguments to that newly spawned process. I am using the call: posix_spawn(..,..,..) I can get the spawned process to run, however I am having trouble passing the arguments correctly. It must be a argument pointer problem. Any ideas. Is there another easier way to spawn a process. (not fork() or threads).

Heres what I have:

Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <sys/types.h>
#include <unistd.h>
#include <pthread.h>
#include <string.h>
#include <unistd.h>
#include <spawn.h>

//
//  Main process
//

int main(void){
 

	pid_t pid;

	char* myArgv[10];

	char arg1[16];
	char arg2[16];
	char arg3[16];
	int retVal;



        //  Create 3 arguments to pass
	sprintf(arg1, "%d", 4);
	sprintf(arg2, "%s", "My string");
	sprintf(arg3, "%d", 350);

 	myArgv[1] = arg1;
 	myArgv[2] = arg2;
 	myArgv[3] = arg3;

        //  Spawn "helloWorld with args"
	retVal = posix_spawn( &pid, "helloWorld",
                              NULL, NULL,
                              myArgv, NULL );

        sleep(10);
	printf("Here is the main process:  Ending\n");


	return 0;
}


//  HelloWorld process


#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <sys/types.h>
#include <unistd.h>
#include <pthread.h>
#include <string.h>
#include <unistd.h>
#include <spawn.h>

int main (int argc, char **argv) {
 
		
    printf(" HelloWorld argc = %d \n", argc);
	
    printf(" argv0 = %s \n", *(argv+0));
    printf(" argv1 = %s \n", *(argv+1));
    printf(" argv2 = %s \n", *(argv+2));
    printf(" argv3 = %s \n", *(argv+3));


    printf(" HelloWorld end. \n");
    return 0;


}  // end main()

Here's what my output looks like:


HelloWorld argc = 0
0 = (null)
1 = (null)
Here is the main process: Ending





Can anybody help me? Thanks in advance...
kenhokie


running on Fedora 9
 
Old 04-03-2009, 01:51 PM   #2
raconteur
Member
 
Registered: Dec 2007
Location: Slightly left of center
Distribution: slackware
Posts: 276
Blog Entries: 2

Rep: Reputation: 44
I suspect your argv array isn't properly terminated. Initialize all of the members to null before assigning values to them and posix_spawn should then pass them correctly to the spawned process (the last entry will be null terminated, followed by another null, which is what argv requires).

edit: in addition, your argv[0] also needs to be initialized. Under normal circumstances this is filled in by the framework but posix_spawn appears to want to be told.
When I did that, the program behaved as expected.

Last edited by raconteur; 04-03-2009 at 02:51 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
bash, passing arguments Four Linux - Newbie 3 02-06-2006 08:24 AM
Passing arguments to a shell script subu_s Programming 3 09-02-2005 05:13 AM
Problem with passing arguments in Perl bahadur Programming 1 05-30-2005 01:47 AM
Passing Arguments into the Thread Function George_gk Programming 2 01-31-2005 05:03 AM
Handline passing arguments in C AMMullan Programming 9 03-22-2004 01:37 AM


All times are GMT -5. The time now is 09:06 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration