0

Trying to split a parent process into two children. The first will calculate factorials of a given number. The second will just say I'm child 2! When that's done, the first child will output the time taken to calculate the factorials. Getting the first child to split and do its job is working just fine. However, I can't see to get the second child to do anything. Any idea what I'm doing wrong?

#include <stdio.h>
#include <time.h>
//#include </sts/types.h>
#include <unistd.h>

//prototypes
int rfact(int n);
int temp = 0;

main()
{
    int n = 0;
    long i = 0;
    double result = 0.0;
    clock_t t;
    printf("Enter a value for n: ");
    scanf("%i", &n);

    pid_t pID = fork();
    if (pID ==0)//child
    {
        //get current time
        t = clock();

        //process factorial 2 million times
        for(i=0; i<2000000; i++)
        {
            rfact(n);
        }

        //get total time spent in the loop
        result = ((double)(clock() - t))/CLOCKS_PER_SEC;

        //print result
        printf("runtime=%.2f seconds\n", result);
    }
    else if(pID < 0)
    {
        printf("fork() has failed");
    }
    else //parent
    {
        //second fork for child 2
        pid_t pID2 = fork();
        if (pID2 == 0)
        {
            execl("child2.o","child2", 20, NULL);
        }
        else if (pID2 < 0)
        {
            printf("fork() has failed");
        }
        else
        {
            waitpid(0);
        }
        waitpid(0);
    }
}

//factorial calculation
int rfact(int n)
{
    if (n<=0)
    {
        return 1;
    }
    return n * rfact(n-1);
}

Here's child2.c:

#include <stdio.h>

void main()
{
    printf("I'm child 2!");
}

Alright, so, I was having problems with eclipse. I dropped it and recompiled both .c files. I used execl to point to child2.o, but it's still not doing anything

zakparks31191
  • 919
  • 2
  • 21
  • 42
  • 2
    You're passing "child2.c" rather than the name of an executable to `execl`... – Oliver Charlesworth Feb 26 '13 at 00:21
  • There are two calls to fork. One's 8 lines into main, and the other is in the else of that if statement. I never learned how to properly do two forks, so this is just guessing. Also, Oli, what should I be doing instead? – zakparks31191 Feb 26 '13 at 00:23
  • Compiling the code to an executable, and then passing the name of the executable to `execl`. – Oliver Charlesworth Feb 26 '13 at 00:24
  • Alright, makes sense. And the second fork will work nested like it is? – zakparks31191 Feb 26 '13 at 00:27
  • @OliCharlesworth: I compiled both .c files to an executable and passed the name of the executable to execl (like you'll see above in the updated code. Any idea why nothing different is happening? – zakparks31191 Feb 26 '13 at 00:32
  • You've created an object file, not an executable. I'm not sure what toolchain you're using, so all I can suggest is familiarising yourself with it, in order to be able to create executables. (Hint: if you're successful, you should be able to instantiate "child2" directly from the command line.) – Oliver Charlesworth Feb 26 '13 at 01:02

1 Answers1

2

You cannot execute a .c source file! You need to compile it and execute the resulting binary file.

With scripting languages you can usually add a #!/usr/bin/whatever line at the beginning, and they will be executed using that interpreter, but C needs to be compiled, can't be interpreted.

LtWorf
  • 7,286
  • 6
  • 31
  • 45
  • 1
    I wouldn't go so far as to say it "can't be interpreted". Merely that it never is in any conventional scenario... – Oliver Charlesworth Feb 26 '13 at 00:27
  • Nitpick: [C interpreters do exist!](http://stackoverflow.com/questions/584714) They're just kind of weird and rare. –  Feb 26 '13 at 00:28
  • @OliCharlesworth: I compiled both .c files to an executable and passed the name of the executable to execl (like you'll see above in the updated code. Any idea why nothing different is happening? – zakparks31191 Feb 26 '13 at 00:37
  • 1
    @ZakParks when you say an executable, do you mean the output was a `.o` file? Because `.o` files are object files and are **not** executable. – Nik Bougalis Feb 26 '13 at 01:23
  • Uhm i had no idea there was a C interpreter. But i guess there are countless odd projects around. – LtWorf Feb 26 '13 at 19:20