0

i want to create a link between two programs throughout the execs functions . my idea is to create function then point on it by a function pointer then send it to the other program to test it . this is my first programenter code here 1- is this possible ? 2- how ?

i get this idea because i find each time to change the function name in the main function but the remainning still as it was but if i send a pointer function as a character pointer then my programm still as it without changing

#include <iostream>
#include <cstdlib>
#include <unistd.h>

using namespace std;

void 
Random(int* ,const int );


int* 
selection_sort(int *arr ,const int length)
{
    int i = 0,minIndex{0},tmp{0},k{0};

    while(i < length-1) // T(n-1) * C1
    {
        
        minIndex = i;   // Tn * C2
        for(int j = i+1 ; j < length ; j++ )    // som(Ti) from i = 0 to i = length-1 )*C3.
        {
            if((arr)[j] < (arr)[minIndex])
                minIndex = j;
        }
        if(minIndex != i)       // Tn * C4
        {
            
            tmp = (arr)[i];
            (arr)[i] = (arr)[minIndex];
            (arr)[minIndex] = tmp;
        }
        i++;
    }
    
    return arr;
}



void 
Random(int* array,const int length)
{
    srand(time(nullptr));
    int i{-1};
    while(i++ < length)
    {
        array[i] = rand()%100;
        sleep(0.2);
    }
}

int main(int argc,char* argv[])
{
    int* (*ptr)(int*,const int ) = selection_sort;
    execl("/home/hellios/Documents/Algorithms/sort_Algorithms/main",(char*)ptr,0); // complete the call
    return EXIT_SUCCESS;
}

sort_Algorithms/main.c

#include <iostream>
#include <unistd.h>
#include <stdlib.h>
#include <ctime>
using namespace std;


void 
Random(int* array,const int length);


int 
main(int argc,char* argv[])
{
    int* (*ptr)(int *,const int ) =(int* (*) (int*,const int)) argv[1];
    int arr1[100],k{0},*arr;
    Random(arr1,100);
    arr = (*ptr)(arr1,100);
    //selection_sort(arr,100);
    cout<<"out of selection_sort"<<endl;
    for(int j = 0 ; j < 100 ; j++ )
    {
        printf("[%d]\t", arr[j]);
        if(!(++k %10))
            cout<<endl;
    }
    printf("\n");
    
    return EXIT_SUCCESS ;
}

void 
Random(int* array,const int length)
{
    srand(time(nullptr));
    int i {-1};
    while(i++ < length)
    {
        array[i] = rand()%100;
        sleep(0.2);
    }
}
anastaciu
  • 23,467
  • 7
  • 28
  • 53
user X
  • 1
  • 3
  • 2
    You can't do that; processes have had separate and isolated memory spaces in most operating system for many years by now. – molbdnilo Aug 17 '20 at 13:18
  • @molbdnilo technically it is not true, this is what shared memory is for. – SergeyA Aug 17 '20 at 13:30
  • @ThomasSablik behold shared memory. – SergeyA Aug 17 '20 at 13:30
  • 1
    it is possible to use os services to map some memory in both processes at the same address. Even function pointers could in theory be made usable. I definitely would not recommend doing that however... We are talking complex and brittle code there. – spectras Aug 17 '20 at 13:33
  • @spectras you can share memory, but I doubt you can get them on the same address with guaranteed result. – Slava Aug 17 '20 at 13:39
  • @Slava you can ask the memory to be mapped at the same address, and it can be even given. – SergeyA Aug 17 '20 at 13:41
  • `execl` *replaces* the current process binary with the new binary, so there will no more be an instance of the old binary`s `selection_sort`. – Werner Henze Aug 17 '20 at 13:43
  • @SergeyA you can ask, sure. But I said **guaranteed** result. And I am not sure that function, even when compiled as relocatebale can be moved to another location after linking. – Slava Aug 17 '20 at 13:45
  • @Slava it has to be, otherwise dynamic linking won't work. (I am not saying any of this is even remotely feasible for OP, just engaging in rather moot technical argument) – SergeyA Aug 17 '20 at 14:01
  • [`Shared memory on linux`](https://stackoverflow.com/questions/5656530/how-to-use-shared-memory-with-linux-in-c), [`Shared memory on windows`](https://stackoverflow.com/questions/1200998/sharing-memory-between-two-processes-c-windows). Please don't say it is impossible, because it's not. Just not sane enough for many people to do it on a daily basis. – Yamirui Aug 17 '20 at 15:05
  • @slava you can request that the memory be mapped at the address of your choice. As any system call, the call may fail so you need to check the returned value for errors before using the mapping. Not sure i see your point. – spectras Aug 17 '20 at 15:28
  • @spectras there is a big difference btw "you can request" and "guaranteed result", especially that you want to make the same request from 2 different applications. Anyway this looks like XY problem and can be quite easily solved using proper mechanisms. – Slava Aug 17 '20 at 15:32
  • This is true for all calls. You can request a file to be opened, the result is not guaranteed. You can request a connection to be established, the result is not guaranteed, you can request memory to be allocated, the result is not guaranteed. For sure you have to check for errors anytime you request sth from the os. "Please map this at that address" is no different. Anyway, as you said it was just about a technical detail: even though it is possible, it is a bad idea and there are plenty better ways to reach whatever goal this tried to achieve. – spectras Aug 17 '20 at 18:19
  • @spectras can i do that using tcp ? And how ? – user X Aug 18 '20 at 02:08
  • By creating an application level protocol. Your program agree on the meaning of exchanged data. For instance 1 is this function, 2 is that one,... Then the server program reads the value and calls the correct function. You can implement this yourself or use one of the many existing protocols &libs that do this. – spectras Aug 18 '20 at 07:43

0 Answers0