I need to pass two addresses (32 bit) to a process spawned by the execl command from C as follows. In the first program:
char buffer[18];
sprintf (&buffer[0],"%x\0 %x\0",lock, count);
arg_list[0]=&(buffer[0]);
arg_list[1]=&(buffer[10]);
execl ("/somedirectory/second_process", arg_list[0], arg_list[1], NULL);
Then in my second program I try and parse the addresses as follows:
if (argc != 2) {
printf ("PROCESS 2: Invalid number of arguments. Terminating %i\n", argc);
return -1;
}
if ( !(sscanf (argv[1],"%x",&lock)) || !(sscanf (argv[2],"%x",&count)) ) {
printf ("PROCESS 2: Problem with parameters passed in");
return -1;
But my program keeps on giving an error saying that the parameters passed in are invalid. When I try and print out the recieved arguments, my program hangs.
printf ("The arguments passsed in are %s %s", argv[1], argv[2]);
FIXED:
I was making the execl call incorrectly. It should have been:
execl ("/somedirectory/second_process", "second_process" arg_list[0], arg_list[1], NULL);
Also in process 2:
if (argc != 3) {
printf ("PROCESS 2: Invalid number of arguments. Terminating %i\n", argc);
return -1;
}
As the first argument passed in is supposed to be that of the process name as Chris below suggested.
Thanks!
Arrays are ALWAYS 0 based in C, so your two arguments will be
argv[0]andargv[1], andargv[2]will be a null pointer, so the sscanf will fail, crash, or hang.Now normally you should always pass the program name as the first (
argv[0]) argument. So you want:that is, you should actually pass THREE arguments (so
argc == 3in the second program).