English 中文(简体)
并行进程
原标题:Concurrent Processes

我期望在一项C方案中同时开展多个进程。 这些方案将听取使用者的意见,然后作为儿童进程执行每一论点。 我认为,这意味着我需要做的是,确保 for(k)由原来的母体过程进行,然后每个由此产生的儿童过程将同时进行,而不是依次进行。

我在思考这一问题时是否正确? 谁让我知道我怎么做?

最佳回答

Forgive 我想,将我先前回答中手头的问题(建议使用透镜)。 自2006年以来 我在此走了全新的方向,我感到不得不把这个方向作为单独的答案。

www.un.org/Depts/DGACM/index_spanish.htm 短文:

请对您的方案作如下修改:

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

www.un.org/Depts/DGACM/index_spanish.htm 长期版本:

<><>>> 根据变量<代码>length,我假定,你希望获得全部论据。 <代码>argv为<>pointer-to-char-pointer,因此只是一个记忆地址。 如果你在节目中印出篇幅,你就会注意到,节目总长度为4个(或你系统中的记忆地址大小)。

因此:

length = sizeof(argv);

确实应当如此:

length = argc;

argc> 持有执行过程中通过的论点总数。 例如,

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

提供:argc = 3 (而不是2,这是一个非常常见的陷阱)

<><>>>>>> 贵方案的另一个问题是execvp

The prototpye for the execvp:

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

在那些地方,向新指挥部提出的论点清单非常相似,与你自己的方案一样。

贵方方案中使用的是:

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.

参看前男子页,

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

虽然不必再次通过档案名称,但你肯定会通过全国人民力量联盟的协调人。 由于你不想通过任何论点,我建议你使用以下<代码>execl。 而是:

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

认为所有这类电话最终都转换为execve(> final &然后予以执行,使之最终达到等值。

我鼓励大家阅读更多有关exec,使用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
", i, argv[i]);  //error checking
        pid = fork();

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

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

似乎为我工作罚款:

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

(尽管你可能要等所有儿童,而不是像本法典那样。)

你的推理似乎正确。 家长程序将预示着与提出论点相同数目的儿童。

是否更高效地利用read子同时处理?

我认为,你将做些什么。 然而......

。 该节目在不同过程中使用同一节目的多份。 如果方案之间没有重大重叠,那么,你可以单独写“<>条码>()。





相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签