Operating System - HP-UX
1753404 Members
7176 Online
108793 Solutions
New Discussion юеВ

Fork process in Shell scripting

 
Henry Chua
Super Advisor

Fork process in Shell scripting

Hi Guys,

I am just curious, I know that system call "fork" is created whenever a shell command is invoked on the terminal prompt. However, what actually happened when say I ran a script of korn shell, is a single fork created,or is one created for each command it passes thru?

Thank u for your help

regards
Henry
3 REPLIES 3
Biswajit Tripathy
Honored Contributor

Re: Fork process in Shell scripting

When you pass on a shell script to ksh, it executes
fork() system call to create a new shell and passes
on the script to it (along with bunch of env variables).
No more shell is creates (unless the script itself calls
another shell script).

- Biswajit
:-)
Stanimir
Trusted Contributor

Re: Fork process in Shell scripting


Hi!
Really fork is created a child-process.
So you can check the result of your <script> by putting in <script> something like

sleep 1000

then starting it and looking at
running processes using:


#ps -ef | grep <script>

You can see ksh-process like parent and the <script> process like child. That means,
that system call fork is executed.

Regards,Stan

Pete Randall
Outstanding Contributor

Re: Fork process in Shell scripting

Henry,

In fact, in order to avoid spawing a child process, your shell script has to use the ". command" convention. For example, in order to set environment variables in a separate script but have them still valid for the current script, you would need to express it like this:

. myvarscript


Pete

Pete