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. I would like someone to help me convert this program from c to bash script.
/* 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. There is no need for it in a shell script. You can simply use
myscript.sh &
in your script to start a new process.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.