You are on page 1of 23

Unix Systems Programming

(CSE 3041)

Program, Process and Threads

SOA
Deemded to be University

(Ashish Kumar Gupta, Ph.D.) December


Program, Process27, 2021
and Threads December 27, 2021 1 / 27
Program

A program is a prepared sequence of instructions to accomplish a de-


fined task.
The C compiler translates each source file into an object file.
The compiler then links the individual object files with the necessary
libraries to produce an executable module.
When a program is run or executed, the operating system copies the
executable module into a program image in main memory.

(Ashish Kumar Gupta, Ph.D.) Program, Process and Threads December 27, 2021 2 / 27
Process: When does a program become a process?

A process is an instance of a program whose execution has started but


has not yet terminated.
Each instance has its own
� address space and
� execution state.
From the perspective of the compiler writer, the executing target pro-
gram runs in its own logical address space in which each program value
has a location.
The management and organization of this logical address space is
shared between the compiler, operating system, and target machine.
The operating system maps the logical addresses into physical ad-
dresses, which are usually spread throughout memory.

(Ashish Kumar Gupta, Ph.D.) Program, Process and Threads December 27, 2021 3 / 27
Static versus Dynamic Storage Allocation

The layout and allocation of data to memory locations in the run-time


environment are key issues in storage management.
Tricky : the same name in a program text can refer to multiple locations
at run time.
The two adjectives static and dynamic distinguish between compile
time and run time, respectively.
A storage-allocation decision is static, if it can be made by the compiler
looking only at the text of the program, not at what the program does
when it executes.
Conversely, a decision is dynamic if it can be decided only while the
program is running.

(Ashish Kumar Gupta, Ph.D.) Program, Process and Threads December 27, 2021 4 / 27
Many compilers use some combination of the following two strategies for
dynamic storage allocation:
Stack storage. Names local to a functions are allocated space on a
stack. The control stack supports the normal call/return policy for
functions.
Heap storage. Data that may outlive the call to the procedure that
created it is usually allocated on a “heap” of reusable storage.
Heap
The heap is an area of virtual memory that allows objects or other
data elements to obtain storage when they are created and to re-
turn that storage when they are invalidated.

(Ashish Kumar Gupta, Ph.D.) Program, Process and Threads December 27, 2021 5 / 27
Layout of a Program Image
After loading, the program executable appears to occupy a contiguous
block of memory called a program image.
A sample layout of a program image in its logical address space.
The program image has several distinct sections.

(Ashish Kumar Gupta, Ph.D.) Program, Process and Threads December 27, 2021 6 / 27
Stack Allocation

Almost all compilers for languages that use procedures, functions, or


methods as units of user-defined actions manage at least part of their
run-time memory as a stack.
Each time a procedure is called, space for its local variables is pushed
onto a stack, and
when the procedure terminates, that space is popped of the stack.
Stack allocation would not be feasible if procedure calls, or activations
of procedures, did not nest in time.
The activations of procedures during the running of an entire program
can be represented by a tree, called an activation tree.

(Ashish Kumar Gupta, Ph.D.) Program, Process and Threads December 27, 2021 7 / 27
Activation Records

Procedure calls and returns are usually managed by a run-time stack


called the control stack.
An activation record is a block of memory allocated on the top of the
process (control) stack to hold the execution context of a function
during a call.
Each function call creates a new activation record on the stack.
Each live activation has an activation record (sometimes called a frame)
on the control stack,
� with the root of the activation tree at the bottom, and
� the entire sequence of activation records on the stack corresponding to
the path in the activation tree to the activation where control currently
resides.
The latter activation has its record at the top of the stack.
The activation record is removed from the stack when the function
returns.
(Ashish Kumar Gupta, Ph.D.) Program, Process and Threads December 27, 2021 8 / 27
Activation Record

1. Temporary values , such as those arising from the evaluation of expres-


sions, in cases where those temporaries cannot be held in registers.
2. Local data belonging to the procedure whose activation record this is.
3. A saved machine status , with information about the state of the ma-
chine just before the call to the procedure. This information typically
includes the return address (value of the program counter, to which
the called procedure must return) and the contents of registers that
were used by the calling procedure and that must be restored when the
return occurs.
4. An “access link” may be needed to locate data needed by the called
procedure but found elsewhere, e.g., in another activation record.
5. A control link , pointing to the activation record of the caller.

(Ashish Kumar Gupta, Ph.D.) Program, Process and Threads December 27, 2021 9 / 27
6. Space for the return value of the called function, if any. Again, not
all called procedures return a value, and if one does, we may prefer to
place that value in a register for efficiency.
7. The actual parameters used by the calling procedure. Commonly, these
values are not placed in the activation record but rather in registers,
when possible, for greater efficiency. However, we show a space for
them to be completely general

(Ashish Kumar Gupta, Ph.D.) Program, Process and Threads December 27, 2021 10 / 27
Program Image : Heap segment

In addition to the static and automatic variables , the program image


contains space for malloc .
The malloc family of functions allocates storage from a free memory
pool called the heap .
Storage allocated on the heap persists until it is freed or until the
program exits.
If a function calls malloc, the storage remains allocated after the func-
tion returns.
The program cannot access the storage after the return unless it has a
pointer to the storage that is accessible after the function returns.

(Ashish Kumar Gupta, Ph.D.) Program, Process and Threads December 27, 2021 11 / 27
Program Image : space for argc and argv
In addition to the static and automatic variables , the program image
contains space for argc and argv and for environmental variables.

(Ashish Kumar Gupta, Ph.D.) Program, Process and Threads December 27, 2021 12 / 27
Command line arguments and Argument arrays

Token is a string of characters containing no white space unless quo-


tation marks are used to group tokens.
A command line consists of tokens (the arguments) that are separated
by white space : blanks, tabs or a backslash (n) at the end of a line.
When a user enters a command line corresponding to a C executable
program,

the shell parses the command line into tokens and passes the
result to the program in the form of an argument array .

An argument array is an array of pointers to strings. The end of the


array is marked by an entry containing a NULL pointer.

(Ashish Kumar Gupta, Ph.D.) Program, Process and Threads December 27, 2021 13 / 27
Argument array and its structure

(Ashish Kumar Gupta, Ph.D.) Program, Process and Threads December 27, 2021 14 / 27
Printing all command line arguments to standard output

First one : Use value in argc to test loop control variable.

Second one: Use address at argv[i] to test loop control variable.

(Ashish Kumar Gupta, Ph.D.) Program, Process and Threads December 27, 2021 15 / 27
Streams: stdout, stderr, stdin

These are three standard streams that are established when a Linux
command is executed.
In computing, a stream is something that can transfer data.
In the case of these streams, that data is text.
Each stream has a source and an outflow.
� Whichever Linux command you’re using provides one end of each stream.
� The other end is determined by the shell that launched the command.
� That end will be connected to the terminal window, connected to a pipe,
or redirected to a file or other command, according to the command line
that launched the command.

(Ashish Kumar Gupta, Ph.D.) Program, Process and Threads December 27, 2021 16 / 27
Streams

In Linux/Unix,
stdin is the standard input stream. This accepts text as its input.
Text output from the command to the shell is delivered via the stdout
(standard out) stream.
Error messages from the command are sent through the stderr (stan-
dard error) stream.
Because error messages and normal output each have their own conduit
to carry them to the terminal window, they can be handled indepen-
dently of one another.
Streams in Linux—like almost everything else—are treated as though
they were files .
You can read text from a file, and you can write text into a file.
These values are always used for stdin, stdout, and stderr:
0: stdin 1: stdout 2: stderr
(Ashish Kumar Gupta, Ph.D.) Program, Process and Threads December 27, 2021 17 / 27
Advantage of separate error stream

There’s an advantage to having error messages delivered by a dedicated


stream.
It means we can redirect a command’s output (stdout) to a file and
still see any error messages (stderr) in the terminal window.
You can react to the errors if you need to, as they occur.
It also stops the error messages from contaminating the file that stdout
has been redirected into.

(Ashish Kumar Gupta, Ph.D.) Program, Process and Threads December 27, 2021 18 / 27
Library function: strerror()
The strerror function
returns a pointer to the system error message corresponding to the
error code errnum .
If successful, strerror returns a pointer to the error string. No values
are reserved for failure.
Use strerror to produce informative messages.
The strerror function may change errnum . You should save and restore
errnum if you need to use it again.

(Ashish Kumar Gupta, Ph.D.) Program, Process and Threads December 27, 2021 19 / 27
Version-I: Argument array creation

Argument array creation


Develop a function, makeargv, that creates an argument array
from a string of tokens.

Input to makeargv : a string of tokens ; either received from command


line or as a constant string.
Return from makeargv : a pointer to an argument array where
each array element, in turn, is a pointer to a token identified in their sequence
of appearance in the input string and the last array element contains a NULL.
.

# of tokens in the input string to be parsed is a variable quantity .


The original string should not be manipulated.
(Ashish Kumar Gupta, Ph.D.) Program, Process and Threads December 27, 2021 20 / 27
Version-II: Argument array creation

Argument array creation


Develop a function, makeargv, that creates an argument array
from a string of tokens.

Input parameter to makeargv : a string of tokens ; either received


from command line or as a constant string.
Output parameter from makeargv : pointer to a pointer to an ar-
gument array where each array element, in turn, is a pointer to a token
identified in their sequence of appearance in the input string and the last ar-
ray element contains a NULL.
Return from makeargv : Number of tokens in the input string . In this case,
makeargv returns -1 to indicate an error.

(Ashish Kumar Gupta, Ph.D.) Program, Process and Threads December 27, 2021 21 / 27
Version-III: Argument array creation

Argument array creation


Develop a function, makeargv, that creates an argument array
from a string of tokens. The delimiter set is explicitly specified.

Input to makeargv :
� a string of tokens ; either received from command line or as a constant
string.
� delimiter set.
Output parameter from makeargv : pointer to a pointer to an argu-
ment array (SAME AS VERSION-II).
Return from makeargv : Number of tokens in the input string . In this
case, makeargv returns -1 to indicate an error.

(Ashish Kumar Gupta, Ph.D.) Program, Process and Threads December 27, 2021 22 / 27
Summary

(Ashish Kumar Gupta, Ph.D.) Program, Process and Threads December 27, 2021 23 / 27

You might also like