[英]Converting a program from c to bash script
I have created a small program in c languge.This program creates some child procceses in with the fork() function.The amount of the procceses that are created is given as the first argument of the console. 我已经在C语言中创建了一个小程序,该程序使用fork()函数创建了一些子过程,所创建的过程的数量作为控制台的第一个参数给出。 I would like someone to help me convert this program from c to bash script.
我希望有人帮助我将此程序从c转换为bash脚本。
/* The first argument is the amount of the procceses to be created*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
main(int argc, char **argv)
{
int pid,i;
int pnumber;
pnumber=atoi(argv[1]);//Converting the first arg to int so i can put in for loop
for(i=1;i<=pnumber;i++){
pid=fork();// Creating the child procceses with fork
if(pid!=0) { //The child procces prints its id and then exit
printf("The id the created proccess is:%d and it is a child proccess \n",pid);
printf("RETURN\n");
exit(1);
}
}
}
#!/bin/bash
if [ -z $1 ]; then
echo "I am child"
exit 0
fi
for i in `seq $1`; do
$0 &
echo "The PID of just spawned child is: $!"
done
fork() is a system call that is used by compiled programs to create another process. fork()是一个系统调用,已编译的程序使用它来创建另一个进程。 There is no need for it in a shell script.
Shell脚本中不需要它。 You can simply use
您可以简单地使用
myscript.sh &
in your script to start a new process. 在您的脚本中开始新的过程。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.