0

There is mostly nothing found through search about calling main - so I am going to guess this question is that fundamentally 'off'.

The suggested questions provides this C# question "Calling Main() from another class" the answer there is that you don't, use a subfunction under Main() and call that; so my assumption is that the same answer applies here with fork().

void somefunction ()
{    
    pid_t  pid; 
    pid = fork();

    if (pid == 0) {

        char *p;
        char *argv[] = { (char*)spawn_count, (char*)gradiant, (char*)i, (char*)(i+spread), p };
        main(5, **argv);
    }
    else if (pid == -1)
       cout << "ERROR: can't fork" << endl;

    else ; //PID == 1,
}

This compiles in g++ with "error: 'main' was not declared in this scope"

Questions:

  • How is main(argc, argv) scoped? I can follow the process in windows with LPTSTR.
  • After fork(), to start the child from main(), how is this done?
  • After fork(), where is the child, in the same function that called child, in main?
Community
  • 1
  • 1
forest.peterson
  • 755
  • 2
  • 13
  • 30
  • 7
    It's illegal to call `main` in C++. – jrok Mar 20 '13 at 13:29
  • 2
    Following jrok's comment --> http://stackoverflow.com/questions/2128321 – Drew Dormann Mar 20 '13 at 13:30
  • if it was not illegal then how is main scoped - I have tried every way but the correct one that compiled without error, is it the NULL pointer at the end? – forest.peterson Mar 20 '13 at 13:31
  • why don't you just `exec` child? – kassak Mar 20 '13 at 13:31
  • @DrewDormann the main() recursion link applies to calling parent main, in this case it is child main. The core issue is I used `CreateProcess` to learn parallel with the VS interface and now I am trying to 'force' the same approach on `fork()`. Somebody else will proabbly have the same problem transitioning and so this question will hopefully help them. – forest.peterson Mar 20 '13 at 13:37

2 Answers2

1

Why do you want to call main? Could you not just fork in main something like below:

int main()
{
    pid_t  pid; 
    pid = fork();

    if(pid < 0 )
    {
        // Error some issue forking
    }
    if (pid == 0) {
        // Child related processing here
    }
    else {
        // parent related processing here
    }
}

Fork returns twice once for parent and once for child and at the same point from where the fork system call was made.

You could refer this link to read more: http://www.yolinux.com/TUTORIALS/ForkExecProcesses.html

Rush
  • 486
  • 2
  • 11
-1

For anyone who falls into the same trap I did - Linux and Windows parallel processing works very differently.

  • In Windows, you call the program again and pass arguments through main()
  • In Linux, you call fork() then call any function you want from within your program, just like with any other function. This approach is super easy compared to the 'crap' you go through to fork windows.

After understanding this distinction, I immediately removed windows from my laptop and installed Linux :)

forest.peterson
  • 755
  • 2
  • 13
  • 30