繁体   English   中英

将程序从c转换为bash脚本

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM