当前位置: 技术问答>linux和unix
帮帮小女子吧T_T 一个编程作业--如何创建几个子进程让他们同时运行??
来源: 互联网 发布时间:2016-05-07
本文导语: The purpose is to write a program which will create N concurrent processes running the specifed command. For example: $ ./PROGRAM -n 3 gcc -c % -- a.c b.c c.c This will create 3 gcc processes each compiling a.c, b.c and c.c concurrently. ...
The purpose is to write a program which will create N concurrent processes running the specifed command.
For example:
$ ./PROGRAM -n 3 gcc -c % -- a.c b.c c.c
This will create 3 gcc processes each compiling a.c, b.c and c.c concurrently.
The syntax of lab4ex3 is where the square brackets denotes an optional part which may
be possibly ommited:
PROGRAM [-n N] command arg1 arg2 : : : argm [-- replace1 replace2 : : : replacel]
The command to be run is command which can be the name of the file, e.g. gcc, or a
pathname to the executable and the remaining arguments if any for the program are given as
arg1 : : : argm. After the --" is an argument list, e.g. a.c b.c c.c". In the i-th command
run, a `%' is replaced with the corresponding replacei argument from the argument list.
For example, using the echo command (which simply prints its arguments) we have the following example and its output:
$ ./PROGRAM -n 3 echo Day % -- 1 2 3
Day 1
Day 2
Day 3
问题就是写一个程序 这个程序会用FORK()创建N个子进程, 但这几个子进程不是一个结束后再创建另一个, 而是不等一个结束就去创建另一个 这样他们就可以"同时"运行. 假设我写的这个程序叫PROGRAM然后我输入以下命令行
$ ./PROGRAM -n 3 gcc -c % -- a.c b.c c.c
这个PROGRAM就会创建3个子进程然后每个都去运行GCC 分别COMPILE a.c b.c c.c
请问能帮我解决让他们同时运行的难题么?
For example:
$ ./PROGRAM -n 3 gcc -c % -- a.c b.c c.c
This will create 3 gcc processes each compiling a.c, b.c and c.c concurrently.
The syntax of lab4ex3 is where the square brackets denotes an optional part which may
be possibly ommited:
PROGRAM [-n N] command arg1 arg2 : : : argm [-- replace1 replace2 : : : replacel]
The command to be run is command which can be the name of the file, e.g. gcc, or a
pathname to the executable and the remaining arguments if any for the program are given as
arg1 : : : argm. After the --" is an argument list, e.g. a.c b.c c.c". In the i-th command
run, a `%' is replaced with the corresponding replacei argument from the argument list.
For example, using the echo command (which simply prints its arguments) we have the following example and its output:
$ ./PROGRAM -n 3 echo Day % -- 1 2 3
Day 1
Day 2
Day 3
问题就是写一个程序 这个程序会用FORK()创建N个子进程, 但这几个子进程不是一个结束后再创建另一个, 而是不等一个结束就去创建另一个 这样他们就可以"同时"运行. 假设我写的这个程序叫PROGRAM然后我输入以下命令行
$ ./PROGRAM -n 3 gcc -c % -- a.c b.c c.c
这个PROGRAM就会创建3个子进程然后每个都去运行GCC 分别COMPILE a.c b.c c.c
请问能帮我解决让他们同时运行的难题么?
|
//定义全局变量
int g_child;
void sig_handler(int signo)
{
if(signo == SIGCHLD)
{
pid_t child_pid = 0;
int status = 0;
while( (child_pid = waitpid(-1,&status,WNOHANG))> 0) //循环回收所有的Zombie进程
{
g_child--;
printf("SIGCHILD --child_pid = %d , status = %dn",child_pid,status);
}
}
}
main(){
...
g_child = 0;
for(int i=0; i
int g_child;
void sig_handler(int signo)
{
if(signo == SIGCHLD)
{
pid_t child_pid = 0;
int status = 0;
while( (child_pid = waitpid(-1,&status,WNOHANG))> 0) //循环回收所有的Zombie进程
{
g_child--;
printf("SIGCHILD --child_pid = %d , status = %dn",child_pid,status);
}
}
}
main(){
...
g_child = 0;
for(int i=0; i