Professional Documents
Culture Documents
USP C2 PBP Partial
USP C2 PBP Partial
(CSE 3041)
SOA
Deemded to be University
(Ashish Kumar Gupta, Ph.D.) Program, Process and Threads December 27, 2021 2 / 27
Process: When does a program become a process?
(Ashish Kumar Gupta, Ph.D.) Program, Process and Threads December 27, 2021 3 / 27
Static versus Dynamic Storage Allocation
(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
(Ashish Kumar Gupta, Ph.D.) Program, Process and Threads December 27, 2021 7 / 27
Activation Records
(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
(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
the shell parses the command line into tokens and passes the
result to the program in the form of an argument array .
(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
(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
(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
(Ashish Kumar Gupta, Ph.D.) Program, Process and Threads December 27, 2021 21 / 27
Version-III: Argument array creation
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