Ceng204 w5 Systems Programming2024 Spring
Ceng204 w5 Systems Programming2024 Spring
3
Processes and programs
Introduction to Processes
There is another item associated with each process that it would be
appropriate to mention now: an environment. If you type
printenv into a shell, you'll see the environemnt associated with
your shell process.
From within C, an environment is an array of pointers to character
strings, just like the argv vector, except that each string is of the
form VARIABLE=VALUE (operations are case-sensitive).
A program can get to its environment via the global variable
environ, and environments are supported by the calls getenv(),
putenv(), setenv(), and unsetenv(). A particularly important
environment variable is PATH, which provides a list of directories
to search when someone invokes a program.
4
Processes and programs
Introduction to Processes
We think of a process as running the instructions and starting out
with the data in a program, and every operating system
includes some way to run programs.
It would be natural, therefore, to have a system call that takes the
name of a file (or a file descriptor), creates a new process, loads
the program into the address space, and starts it up.
On some operating systems, there is indeed a call to spawn a new
process in this way.
5
Processes and programs
Introduction to Processes
The traditional Unix model is a bit different, relying on two separate
types of system calls that, put together, perform this function.
The fork() call creates a new process
When you fork a new process, the kernel copies your instruction,
data, and stack segments into a new process.
The exec() family of calls replace the program running in a
process with a new program.
No new process is created, the process just runs a different a
program.
6
Processes and programs
Introduction to Processes
Pause and think that through.
The only way to run a program is via exec(), which doesn't create
a new process;
The only way to create a process is fork(), which doesn't run a
new program.
7
Processes and programs
Introduction to Processes
8
Processes and programs
Interprocess Communication
An operating system designed for multitasking or multiprocessing
must provide mechanisms for communicating and sharing data
between applications.
These mechanisms are called interprocess communication
(IPC).
UNIX has several IPC mechanisms that have different
characteristics and which are appropriate for different
situations.
Shared memory, pipes, and message queues are all suitable for
processes running on a single computer.
9
Processes and programs
Interprocess Communication
Shared memory and message queues are suitable for
communicating among unrelated processes.
Pipes are usually chosen for communicating with a child process
through standard input and output.
For communications across the network, sockets are usually the
chosen technique.
10
References
• https://cs.wellesley.edu/~cs249/Lectures/Intro-Processes/intro-processes.html
11