Results 1 to 4 of 4

Thread: Clever use of fork and exec?

  1. #1
    Join Date
    Oct 2013
    Beans
    16

    Clever use of fork and exec?

    Hello friends,
    Before starting off, apologies for the misguided title, but i really dont know what to write in title for this programming doubt.


    compute_credit(param1, param2 , param3){
    // How to replace this computation logic with a executable C file, to which i will pass these 3 parameters. Ideally, fork replicates the process. I was thinking of calling the fork here and then exec. But then everytime this function is called, there would be a fork call and soon I will have numerous processes in my system? Any ideas?
    }

    I hope I was clear, If there is any doubt, please let me know. Thank you for your time!

  2. #2
    Join Date
    Nov 2005
    Location
    Bordeaux, France
    Beans
    11,297
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Clever use of fork and exec?

    Well, if you want to run a program, you must have a process for it, I don't see any way around it. Why is it a problem?
    「明後日の夕方には帰ってるからね。」


  3. #3
    Join Date
    Oct 2013
    Beans
    16

    Re: Clever use of fork and exec?

    Hello, Thank you for your reply. That is my exact question, Inside the loop, how can i call another executable, as far as i know it can be achieved with exec...

  4. #4
    Join Date
    Nov 2005
    Location
    Bordeaux, France
    Beans
    11,297
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Clever use of fork and exec?

    You want something like this (I wrap it in a function just for demonstation):

    Code:
    int compute(char *program, char *arg1, char *arg2, char *arg3)
    {
        pid_t p = fork();
        if (p == -1) {
            perror("Error forking");
            return -1;
        }
    
        /* Child */
        if (p == 0) {
            int exec = execl(program, program, arg1, arg2, arg3, (char*)NULL);
            if (exec == -1) {
                perror("Error executing");
                exit(EXIT_FAILURE);
            }
        }
    
        /* Parent */
        int status;
        wait(&status);
        return WEXITSTATUS(status);
    }
    「明後日の夕方には帰ってるからね。」


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •