views:

76

answers:

5

I'm looking to run multiple concurrent processes in a C program. The programs will take arguments from the user and then execute each argument as a child process. I think that means that all I need to do is ensure that the fork() is performed by the original parent process each time, and then each of the resultant child processes will run at the same time, and not sequentially.

Am I correct in thinking this? And can anyone let me know how I might go about doing it?

A: 

Your reasoning seems correct.. The parent process would fork of the same number of childs as there are arguments.

Wouldn't it be more efficient to use threads to do concurrent processing though?

Alexander Sagen
It might be but I have even less experience with threads than I do with fork :)
mispecialist
Fair enough.. Something to have a look at some time then ;)
Alexander Sagen
A: 

I believe that will do what you want. However...

fork() don't really run multiple processes in a single program. It runs multiple copies of the same program in different processes. If there isn't significant overlap between the programs, you might as well just write separate main()s for each.

T.E.D.
A: 

Threads are more efficient to use, and what's more they can access the same global data of the program. So, if you have some computation to perform on common data, threads are the way to go.

You can find a beginner example here.

Kedar Soparkar
Threads may be more efficient, but I don't know about easier to use. A forked child process can access the same global data as its parent as well, only difference is when parent or child tries to *change* that data. In the processes case, copy-on-write will kick in and the change will occur only for the local process and won't be seen by the other one. In the threads case, both threads will see the change, which can be good (if that's what you want) or bad (since it opens the door to race conditions and related problems if you aren't very careful)
Jeremy Friesner
He could use a combination of mmap and fork (and some contract detailing what the mapped memory block contains), to get proper shared memory. A tad dangerous though.
manneorama
Thanks for everyone's input so far, but I'm no closer to a solution as yet. I've updated the code which is closer to what I'm looking for in the end, but when I run it with the following parameters: q5 /bin/ls /bin/ps /bin/datethe program only runs /bin/ps, regardless of what order these are entered in, and if /bin/ps isn't included it just doesn't run any of the processes and the program terminates without doing much.Did I get the logic wrong in the for statement? :S
mispecialist
I suggest that you check the return values from all system calls, including execve(), and print something useful when they fail. Usually they tell you whats wrong.
Per Ekman
+2  A: 

Since you call wait() in the loop you will fork a child process and the forking process will then wait for it to complete before it forks of the next one. You'll need to fork all the child processes before waiting if you want them to execute in parallel,

Edit : You compute length invalidly. sizeof argv returns the size of a pointer to char. This code

#include <stdio.h>
#include <unistd.h>

int main(int argc, char *argv[])
{
    int pid, i, length;

    length = argc;

    for(i = 1; i < length; i++)
    {
        printf("Argv[%d]: %s\n", i, argv[i]);  //error checking
        pid = fork();

        if(pid < 0)
        {
           printf("Fork failed.\n");
        }//end if
        else if(pid == 0)
        {
           execvp(argv[i], 0);
        }//end else if
        else
        {
           printf("Parent process (%d)\n", getpid());
        }//end if-else

    }//end for
    wait();
}//end main

seems to work fine for me :

datan:~/src/c> ./a.out /bin/ps /bin/ps /bin/ps
Argv[1]: /bin/ps
Parent process (12748)
Argv[2]: /bin/ps
Parent process (12748)
Argv[3]: /bin/ps
Parent process (12748)
  PID TTY          TIME CMD
 6615 pts/5    00:00:00 bash
  PID TTY          TIME CMD
 6615 pts/5    00:00:00 bash
12627 pts/5    00:00:01 emacs
12748 pts/5    00:00:00 a.out
12749 pts/5    00:00:00 ps
12750 pts/5    00:00:00 ps
12751 pts/5    00:00:00 ps
datan:~/src/c> 12627 pts/5    00:00:01 emacs
12749 pts/5    00:00:00 ps
12750 pts/5    00:00:00 ps
  PID TTY          TIME CMD
 6615 pts/5    00:00:00 bash
12627 pts/5    00:00:01 emacs
12749 pts/5    00:00:00 ps

(although you should probably wait for all children, not just any as this code does).

Per Ekman
I had the wait outside the loop to begin with, but moving it back out doesn't change anything, except that it then requires user input to bring the prompt back.
mispecialist
Kedar Soparkar
Also, sizeof argv returns the size of a pointer-to-char-pointer not, pointer-to-char.
Kedar Soparkar
A: 

Forgive me for diverting from the matter at hand in my previous answer (by suggesting the use of threads). Since I'm going in a completely new direction here, I feel compelled to add this as a separate answer.

Short version:

Please make the following changes in your program:

1. length = argc;               // in place of length = sizeof(argv);
2. execl(argv[i],argv[i],0);    // in place of execvp(argv[i],0);
3. #include <unistd.h>          // if you haven't already

Long version:

(1) By the variable length, I presume you want to get the total number of arguments. argv is a pointer-to-char-pointer, and as such is simply a memory address. If you print out the length in your program, you will notice it is always 4 (or whatever is the size of a memory address in your system).

So this:

length = sizeof(argv);

Should really be this:

length = argc;

argc holds the total number of arguments passed when executing the process. For example,

./a.out /bin/ps /bin/ls

gives: argc = 3 (and not 2, a very common pitfall)

(2) Another issue with your program, is the execvp call.

The prototpye for the execvp is as follows:

int execvp(const char *file, char *const argv[]);

where, argv is the list of arguments passed to the new command, very similar to the argv in your own program.

What you use in your program is:

execvp(argv[i],0);

Suppose i=1 and argv[1] = "/bin/ls". What this command does is look for the /bin/ls executable & pass a NULL pointer (0) to it. This may lead to the following runtime error:

A NULL argv[0] was passed through an exec system call.

Referring to the exec man page,

The first argument, by convention, should point to the filename associated with the file being executed.

Though it is not mandatory to pass the filename again, you certainly shouldn't pass a NULL pointer. Since you don't want to pass any arguments, I suggest you use the following execl call, instead:

execl(argv[i],argv[i],0);

Remember that all such calls are finally converted to execve() finally & then executed, making them equivalent eventually.

I encourage you to read more about the exec family of functions using man.

Kedar Soparkar
Thanks a lot guys, these are small things I should have picked up myself : \ I have now completed this program and will edit the code above to match the final version. If you can see any other errors feel free to let me know :P
mispecialist