Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

create a child process by fork() a parent process in shell script

Tags:

linux

bash

shell

It seems some answers on the site but those seem not to resolve my question.

Say,

#/bin/sh
fpfunction(){
n=1
while (($n<20))
do

        echo "Hello World-- $n times"
        sleep 2
        echo "Hello World2-- $n times"
        n=$(( n+1 ))
done
}

fork(){
    count=0
    while (($count<=10))
    do
      fpfunction &
      count=$(( count+1 ))
    done
}

fork

I am expecting to "fork" this parent process to the number of 10 child processes.

The way I check that those child processes are actually created is to type "ps -l" command.

I am a newbie to the shell world and please let me know how to archive this.

Thanks in advance.

like image 846
Sam Avatar asked Jun 25 '13 06:06

Sam


People also ask

How do you make a child process from the parent process?

Child Process: A child process is created by a parent process in an operating system using a fork() system call. A child process may also be known as subprocess or a subtask. A child process is created as a copy of its parent process. The child process inherits most of its attributes.

How do you create a fork child process?

fork() in C. Fork system call is used for creating a new process, which is called child process, which runs concurrently with the process that makes the fork() call (parent process). After a new child process is created, both processes will execute the next instruction following the fork() system call.

What does the fork () return to the child process?

Upon successful completion, fork() returns 0 to the child process and returns the process ID of the child process to the parent process. Otherwise, -1 is returned to the parent process, no child process is created, and errno is set to indicate the error.

What is fork in shell script?

fork() creates a child process that differs from the parent process only in its PID and PPID, and in the fact that resource utilizations are set to 0. File locks and pending signals are not inherited.


1 Answers

The ampersand (&) after a command will run it in a forked subshell.

fpfunction &
like image 187
Ignacio Vazquez-Abrams Avatar answered Oct 22 '22 12:10

Ignacio Vazquez-Abrams