Thread: function calling problems

  1. #1
    Registered User
    Join Date
    Jun 2006
    Posts
    28

    function calling problems

    I'm writing code for my degree essay based on column generation. I'm having a problem running the code i have written so far. the code is split up into 2 routines:

    lin_call() calls a lin keringham algorithm which is already installed on my linux system and asks from the user to input the lin executable location and the input data file formated in .tsp format.

    lin_call.c:
    Code:
    #include<stdio.h>
    
    /******* Lin-Keringham function call **************/
    void lin_call(char *exec_path,char *exec_file,char *in_file){
    	int ret;
    	ret=execl(exec_path,exec_file,"-o","lin_out",in_file,"..",NULL);
    	if(ret=-1) perror("execl");
    }
    lin_to_col() takes the results file from lin_col() and creates a route column using quick sort. it also calculates the final tour cost.

    lin_to_col.c:
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    int comp(const void *i,const void *j){
    	return *(int *)i-*(int *)j;
    }
    
    void lin_to_col(char *in_file){
    
    	FILE *fp;
    	int i,dum,col_size;
    	float weight,sum_weight=0;
    	struct prevnext{
    		int prev;
    		int next;
    	}*col;
    
    	fp=fopen(in_file,"r");
    	fscanf(fp,"%d %d",&col_size,&dum);
    	col=(int *)calloc(col_size,sizeof(struct prevnext));
    	
    	for(i=0;i<col_size;++i){
    		fscanf(fp,"%d %d %f",&col[i].prev,&col[i].next,&weight);
    		sum_weight+=weight;
    	}
    
    	qsort(col,col_size,sizeof(struct prevnext),comp);
    
    	for(i=0;i<col_size;++i){
    		printf("%d\t%d\n",col[i].prev,col[i].next);		
    	}
    	printf("\n\n%.2f\n",sum_weight);
    	fclose(fp);
    }
    Now note that both procedures run flawlessly when called independentely and with the right order from the main file. the syntax in linux shell is:

    suppressing the lin_to_col call:
    Code:
    gcc main.c lin_call.c -o lin_call
    ./lin_call (executable path) (input file path)
    suppressing the lin_cal call:
    Code:
    gcc main.c lin_to_col.c -o lin_to_col
    ./lin_to_call
    the code for calling both procedures is shown below:
    main.c:
    Code:
    #include<stdio.h>
    
    int main(char *argc,char *argv[]){
    	
    	lin_call(argv[1],"linkern",argv[2]);
    	
    	lin_to_col("lin_out");
    	
    return 0;
    }
    compiling and running the whole program:
    Code:
    gcc main.c lin_call.c lin_to_col.c -o main
    ./main (executable path) (input file path)
    The problem is when trying to call both procedures in the same time as shown above. this is when main() runs only the first call producing right results but not running the lin_to_col(). is anything wrong to what i'm doing.

    any input would be usefull

    thanks in advance

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,662
    exec() functions replace the current running program with a new program - that is, they do NOT return if they're successful.

    To get round this, call fork() to create another process (a child), in which the execl takes place and have a wait() call in the parent.
    Code:
    void lin_call(char *exec_path,char *exec_file,char *in_file){
    	int ret;
    	pid_t pid = fork();
    	if ( pid == 0 ) {
    		ret=execl(exec_path,exec_file,"-o","lin_out",in_file,"..",NULL);
    		if(ret=-1) perror("execl");
    	} else {
    		int status;
    		wait(&status);
    	}
    }
    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
    Jun 2006
    Posts
    28
    Quote Originally Posted by Salem
    exec() functions replace the current running program with a new program - that is, they do NOT return if they're successful.

    To get round this, call fork() to create another process (a child), in which the execl takes place and have a wait() call in the parent.
    Code:
    void lin_call(char *exec_path,char *exec_file,char *in_file){
    	int ret;
    	pid_t pid = fork();
    	if ( pid == 0 ) {
    		ret=execl(exec_path,exec_file,"-o","lin_out",in_file,"..",NULL);
    		if(ret=-1) perror("execl");
    	} else {
    		int status;
    		wait(&status);
    	}
    }
    since i don't have much knowledge in unix programming i'll have to read a bit more for fork() and wait(). i'll return to this topic for any question on those two.

    supposed you know any online documentation for fork,wait plz post any link...

    thanks a lot for your help...

  4. #4
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Are you using a UNIX type machine? Type in man fork or man wait at your shell command prompt. When you are done looking at the manual page for the function, you can press <SHIFT + Q> to exit back to the shell prompt.

    Alternatively, you could do a search on google with the same terms.

  5. #5
    Registered User
    Join Date
    Jun 2006
    Posts
    28
    Quote Originally Posted by kermit
    Are you using a UNIX type machine? Type in man fork or man wait at your shell command prompt. When you are done looking at the manual page for the function, you can press <SHIFT + Q> to exit back to the shell prompt.

    Alternatively, you could do a search on google with the same terms.
    Ubuntu 5.10

    man fork,wait did nothing, but got some nice results from google...

    thanks alot

  6. #6
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    hmmm - well glad that worked for you. Sounds to me like Ubuntu maybe does not come with the man pages installed default. What kind of a Linux system is that?!

  7. #7
    Registered User
    Join Date
    Jun 2006
    Posts
    28
    Quote Originally Posted by kermit
    hmmm - well glad that worked for you. Sounds to me like Ubuntu maybe does not come with the man pages installed default. What kind of a Linux system is that?!
    as if it would be the first thing they have ommited...

    ubuntu doesn't even have the basic gcc packages for gcc on first install.

  8. #8
    Registered User
    Join Date
    Jun 2006
    Posts
    28
    another problem too. when i compile the above source i get the following messages:

    when compiling lin_call.c :
    Code:
    lin_call.c: In function ‘lin_call’:
    lin_call.c:6: warning: incompatible implicit declaration of built-in function ‘execl’
    when compiling lin_to_call.c:
    Code:
    lin_to_col.c: In function ‘lin_to_col’:
    lin_to_col.c:20: warning: assignment from incompatible pointer type
    what does these mean?

    i have only one clue for the second warning. it means i have assigned a pointer to a struct of certain size and then i allocate much more memory for this pointer. however it works just fine . correct me if i'm wrong...

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,662
    > lin_to_col.c:20: warning: assignment from incompatible pointer type
    Remove the cast of the result of calloc
    It's not needed at all in C

    > lin_call.c:6: warning: incompatible implicit declaration of built-in function ‘execl’
    execl is in unistd.h
    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.

  10. #10
    Registered User
    Join Date
    Jun 2006
    Posts
    28
    Quote Originally Posted by Salem
    > lin_to_col.c:20: warning: assignment from incompatible pointer type
    Remove the cast of the result of calloc
    It's not needed at all in C

    > lin_call.c:6: warning: incompatible implicit declaration of built-in function ‘execl’
    execl is in unistd.h
    thanks a lot!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM