What is meant by subprocesses?

I'm going through my UNIX book and came across a section on Customization and Subprocesses.

Can someone tell me what a subprocess is -- for example, when the book says "Which shell 'thing' are known to subprocesses" what exactly does it mean? The book just talks about it without defining it -- very frustrating, because the following section doesn't make a lot of sense to me.

What is the difference between a bash program and an interactive shell? (I think an interactive shell is one that accepts user input, right?)

A subprocess is a child process, i.e. a process that has been launched by its parent to which it is a subprocess.

An interactive shell is a command line interpreter reading your input line by line and prompting you when a command is done.

A bash program (or more generally a shell script) is reading its commands not from your keyboard but from a text file containing them.

Can you give me an example of a process and a subprocess? I'm still not quite clear -- it's still a little confusing when the word I don't understand the definition of is used in the definition.

Being a subprocess denotes a relationship, not a specific property.
All processes are subprocesses of their parent process, except init which is launched by the kernel itself.

But, with all due respect, that doesn't make it any clearer.

Can you tell me what a process is, and give an example of one, and then explain/give an example, or tell me then what the subprocess would be? I'm just learning about this kind of stuff and it's still confusing. :slight_smile:

Quote from Wikipedia

I can't think of a better explanation, and it would have helped if you stated from the start that you don't know what a process is. We were under the assumption that you're only unclear on the concept of a sub-process.

My apologies -- but your explanation is fine. So I'm guessing that a subprocess consists of other executions required to carry out the primary executions of said instructions?

so when something is 'known' to a subprocess, it means what?

Here are the paragraphs that confused me:

"Some of the variables discussed above are used by commands you may run -- as opposed to the shell itself -- so that they can determine certain aspects of your envrionment. The majority, however, are not even known outside the shell.

This dichotomy begs an important question: which shell "Things" are known outside the shell, and which are only internal? This question is at the heart of many misunderstandings about the shell and shell programming. Before we answer, we'll as it again in a more precise way: which shell "things?" are known to subprocesses? Remember that wenever you enter a command, you are telling the shell to run that commmand in a subprocess; furthermore, some complex programsmay start their own subprocesses. Now for the answer, which (like many UNXI concepts) is unfortunately not as simple as you might like. "A few things are known to subprocesses, but the reverse is not true: subprocesses can never make these things known to the processes that created them.

Which things are know depends on whether the subprocess in question is a bash program or an interactive shell. If the subprocess is a bash program, then it's possible to propagate nearly every type of thing we've seen in this chapter -- options and variables -- plus a few we'll see later."

To which I respond: Huh? :confused:

I ask because the following section i the book concerns what are called environment variables, and if I know what a subprocess is, then I can make sense of the section better.

Maybe I should have just quoted this at the beginning, come to think of it. Apologies again.

Now I'm the one scratching my head, no idea what you're saying here. Maybe first off we should explain what a process is.

A process is an independent virtual memory space that a program can execute inside. It has its own independent set of memory, set of open files, and its own program context.

You get a sub-process when this process makes the fork() system call. It creates a near-perfect copy of the old process all the way down to open files and what instruction its currently running, with one significant difference: the 'child' process will have zero returned to it from the fork() call, while the 'parent' process will have the new process' PID returned to it. Based on that the same program can take different directions. Also, when the 'child' terminates, the 'parent' is told about it via a signal.

So, they're independent, but may share some things like files, and they know which created which.

The only process with no parent at all is process 1, init. All other processes are created by fork(). So a child process isn't some special kind of process apart, it's just a description of which process created which.

Whenever you run a shell script it's probably running in a sub-process. the shell forks off a new process and runs the script inside it.

Maybe although you would need to define what you mean with "executions", "primary executions" and clarify what "said instructions" are referring to.

Known means well, known, the opposite of unknown. In that context, it means the variable names and contents are reachable and usable by the subprocess.

...And the process-fork concept tells you why. The new process gets a copy of the memory of its parent, including environment variables, but after the fork these memory spaces are independent -- changes in either child or parent are not reflected in the other.

In this example script1 invokes script2. Parameters exported from script1 are available to script2.

#!/bin/ksh
# This is script1
#
# Getting environment variables ready for script2
export myparameter1="first_parameter_for_script2"
export myparameter2="second_parameter_for_script2"
# Invoke the subprocess called script2
# script2 will see all exported environment variables
echo "Starting: script1"
echo "myparameter1=${myparameter1}"
echo "myparameter2=${myparameter2}"
./script2
echo "Finishing: script1"


#!/bin/ksh
# This is script2
#
# Indenting the echoes by 5 spaces for visual effect
echo "     Starting: script2"
echo "     ${myparameter1}"
echo "     ${myparameter2}"
echo "     Finishing: script2"



./script1
Starting: script1
myparameter1=first_parameter_for_script2
myparameter2=second_parameter_for_script2
     Starting: script2
     first_parameter_for_script2
     second_parameter_for_script2
     Finishing: script2
Finishing: script1