Thread: Passing values through execlp()

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    20

    Passing values through execlp()

    Code:
    /************************************************
    **Brandon Mathis
    **Add.cpp v1
    **11 Feb 2007
    ************************************************/
    #include <stdio.h>
    #include <cstdlib>
    #include <unistd.h>
    #include <sys/wait.h>
    #include <sys/types.h>
    #include <iostream>
    #include <stdlib.h>
    
    
    using namespace std;
    
    char itoa(int val, int base)
    {      
            static char buf[32] = {0};
            int i = 30;
            for(; val && i ; --i, val /= base)
            {
                    buf[i] = "0123456789abcdef"[val % base];
            }
            return buf[i+1];
    } 
    
    int controller(int argc, char *argv[]){
    	//cout<<"argc: "<< argc<<endl;
    	pid_t child_pid;
    	pid_t pid;
    	int ANSindex=0;
    	int message;
    	int messageholder;
    	int step=1;
    	char ans[argc];
    	char x, *ptr_x=&x;
    	char y, *ptr_y=&y;
    	char *ptr_null=NULL;
    	
    	while(step<argc){
    		ANSindex++;
    		x=*argv[step++];
    		if(step==argc){
    			y='0';
    		}
    		else
    			y=*argv[step++];
    		cout<<"I will pass "<<*ptr_x<<", "<<*ptr_y<<endl;
    		
    		pid=fork();
    		if(pid==-1){
    			printf("Fork Error\n");
    		}
    		if(pid==0){
    			execlp("./Addworker.o", "./Addworker.o", ptr_x, ptr_y, NULL);
    		}
    		else{
    			child_pid=wait(&message);
    	
    			if(WIFEXITED(message)){
    				messageholder=WEXITSTATUS(message);
    				printf("ANSWER: %d\n", messageholder);
    				ans[ANSindex]=itoa(messageholder, 10);
    				cout<<ans[ANSindex]<<" into spot "<<ANSindex<<endl;				
    			}
    			else
    				printf("Error in subthread\n");
    			
    		}
    	}
    	cout<<"ans: "<<ans[1]<<" "<<ans[2]<<endl;
    	//if(ANSindex>1)
    	//	controller(ANSindex+1, ans);
    	//else return atoi(&ans[1]);
    	return 0;
    }
    int main(int argc, char *argv[]){
    	int index=0;
    	char deref_argv[argc+1];
    	
    //	for(index=1; index<argc; index++){
    //		deref_argv[index]=*argv[index];
    //	}
    	printf("%d\n", controller(argc, argv));
    	return 0;
    	
    }
    Code:
    #include <stdio.h>
    #include <cstdlib>
    #include <iostream>
    
    using namespace std;
    
    int main(int argc, char *argv[]){
    	cout<<"Worker Given: "<<*argv[1]<<" "<<*argv[2]<<endl;
    	int arg1=atoi(argv[1]);
    	int arg2=atoi(argv[2]);
    	cout<<"Adding: "<<arg1<<" "<<arg2<<endl;
    	int x=arg1+arg2;
    	printf("%d\n", x);
    	exit(x);
    }
    ==========================

    Okay, here is my problem. I want to think that Addworker.c is perfect but when I call the process through execlp() something very odd is happening. It is almost impossible to put this into words

    IE:
    ./Add.o 1 2
    will print the debug lines from the Addworker process
    Worker Given: 1 2
    Adding: 1 21
    22
    This means that the worker looks at *argv[0] and *argv[1] and sees 1 and 2 in the cout statement
    However, atoi() will see argv[0] as 1 and argv[1] as 21

    same problem happens when passed two different numbers
    passing 3 and 4 causes atoi to see 3 and 43.

    Please someone help, this code compiles so perhaps it will be best to just compile it and see how it runs.
    Just give Add.c arguments through the command line (End result would be for Add.c to return the sum of all these numbers)
    Last edited by BMathis; 02-15-2009 at 01:07 PM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Because exec functions start at arg0

    As in
    execlp("./Addworker.o", "./Addworker.o", ptr_x, ptr_y, NULL);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    20
    Quote Originally Posted by Salem View Post
    Because exec functions start at arg0

    As in
    execlp("./Addworker.o", "./Addworker.o", ptr_x, ptr_y, NULL);
    Code in initial post changed to reflect suggestion.

    Yea, I also thought that was the problem initially but it still did not fix the bug. If I call Addworker myself from the command line and feed in the variables manually, Addworker seems happy and gives me the solution of those two numbers with no complaints.
    However, when I use execlp and give it ptr_x and ptr_y pointing to identical values held in x and y, Addworker sees them in this strange way.

  4. #4
    Registered User
    Join Date
    Feb 2009
    Posts
    20
    I think the problem lies in my actual use of pointers or some misunderstanding of pointers that I may have. If anyone needs any clarification on the problem please let me know. I have a sinking feeling I am not going to be able to get anything done till I can solve this

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > char x, *ptr_x=&x;
    Maybe allocate enough space for your strings.

    Code:
    char x[100];
    char y[100];
    // some stuff, making sure there's a \0
    // then
    execlp("./Addworker.o", "./Addworker.o", x, y, NULL);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Feb 2009
    Posts
    20
    still got the same error

  7. #7
    Registered User
    Join Date
    Feb 2009
    Posts
    20
    Solved problem, thx guys

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    What, no comments on the answer you found?
    Show us, so others may learn.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing Objects, Constructors, Pointers, References, Values ???
    By BlackSlash12 in forum C++ Programming
    Replies: 24
    Last Post: 12-14-2007, 06:26 PM
  2. Passing integers to escape values
    By AmbliKai in forum C Programming
    Replies: 2
    Last Post: 11-09-2007, 08:35 AM
  3. Passing Values Question...
    By tinkerbell20 in forum C++ Programming
    Replies: 9
    Last Post: 06-27-2005, 11:33 AM
  4. ascii values for keys
    By acid45 in forum C Programming
    Replies: 2
    Last Post: 05-12-2003, 07:13 AM
  5. passing values in array
    By xenodvs1 in forum C Programming
    Replies: 3
    Last Post: 02-12-2003, 01:50 PM