[英]Converting a program from c to bash script
我已經在C語言中創建了一個小程序,該程序使用fork()函數創建了一些子過程,所創建的過程的數量作為控制台的第一個參數給出。 我希望有人幫助我將此程序從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()是一個系統調用,已編譯的程序使用它來創建另一個進程。 Shell腳本中不需要它。 您可以簡單地使用
myscript.sh &
在您的腳本中開始新的過程。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.