开发者

Concurrent Processes

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 t开发者_C百科ime, and not sequentially.

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


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.


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).


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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜