Files are available by executing ~classque/usp-02 on the CS network.
Read USP Chapter 2.
Programs, Processes and Threads
A program is a prepared sequence of instructions to accomplish
a defined task.
A compiler translates the instructions to produce an
executable module.
When the program is run, the executable module becomes a
program image in main memory.
A process is an instance of a program that is executing.
A process has an address space and starts with a single flow of control.
The flow of control executes a sequence of instructions called a
thread of execution.
A thread of execution is a logically related sequence of
instruction addresses assigned to the program counter during the
execution of a program's code.
Example (2.2 from USP):
Process 1 executed statements 245, 246 and 247 in a loop and process 2 executes
the statements 10, 11, 12, 13, ... .
If the CPU starts executing process 1, loses the CPU after 5 instructions,
and then executes 4 instructions of process 2 before losing the CPU, the
instructions executed might be:
2451, 2461, 2471, 2451,
2461,
102, 112, 122, 132,
2471, 2451, 2461, ...
There are two threads of execution:
2451, 2461, 2471,
2451, 2461, 2471 ... and
102, 112, 122, 132.
The subscript indicates which process is executing and appears only for clarity.
A thread is an abstract data type that represents a
thread of execution of a process.
It is a basic unit of CPU utilization.
A thread has:
A thread ID
A program counter
A register set
A stack
threads 1
Program Layout
Figure 2.1 (page 24):
Simple layout of a program image in main memory.
Exercise:
The following program prints two numbers.
Describe what these numbers represent and how they would be related according to Figure 2.1.
Argument Arrays
An argument array is an array of pointers terminated by a NULL pointer.
Each element of the array is of type char * and represents a string.
Figure 2.2 (page 32):
The argv array for the call mine -c 10 2.0.
Argument Arrays 1
Sometimes it is necessary to create a structure like this yourself from a
string.
The shell must do this when you execute a command.
argv[] is an array of pointers to chars
In C, this is the same as a pointer to a pointer to a char.
One way to write a function to do this is: char **makeargv(char *s)
If you want to return the number of tokens, you can pass a pointer to the
arg array as in: int makeargv(char *s, char ***argvp)
The version we will use has an additional parameter that specifies a string
of delimiters: int makeargv(const char *s, const char *delimiters, char ***argvp)
The const for the first two parameters indicates that the strings
should not be modified by the function.
makeargv const
Program 2.1 (argtest.c) shows a use of this version.
How to write makeargv
Can use strtok:
#include <string.h>
char *strtok(char *restrict s1, const char *restrict s2);
s1 is the string to parse s2 is a string of delimiters
returns a pointer to the next token or NULL if none left.
First call: s1 points to the string to parse
Additional calls: s1 is NULL.
Note: the string s1 is modified.
We do not want the string passed to makeargv to be modified so we
allocate space for another copy of the string.
Make a pass with strtok to count the tokens.
Use the count to allocate the argv array.
Make a second pass with strtok to set the pointers in the
argv array.
Figure 2.3 (page 36):
The makeargv makes a working copy of the string
s so that it does not modify that input parameter.
Figure 2.4 (page 36):
The use of strtok to allocate strings in place for
makeargv.
Program 2.2 shows an implementation of makeargv.
freemakeargv shows how to free the memory allocated by makeargv.
strtok restrict
Making Functions Safe strtok is not safe to use with threads since it remembers the previous
state.
Another version is available: strtok_r in which you pass
another parameter which is used to remember where it is.
You must declare a char pointer to hold the current position and
pass a pointer to it as the last argument to strtok_r.
Programs that use strtok can fail even if they do not involve signals or
multiple threads.
Suppose you are given the task to write a program that calculates the average
number of words per line in a text file. You write a function: double wordaverage(char *s);
to do this.
You parse the input string into lines using strtok and call a function int wordcount(char *s);
to count the words in each line. You assign the task of implementing
wordcount to another programmer.
If the other programmer also uses strtok in the implementation of
wordcount, your program will fail.
Each of wordcount and wordaverage is correct by itself,
but they do not work together correctly.
You can solve this problem by using strtok_r in either function.
Storage and Linkage Classes
From USP Appendix A.5
Storage classes: static and automatic
static storage class refers to variables that, once allocated, persist
throughout the execution of a program.
automatic storage class refers to variables which come into existence when the
block in which they are declared is entered and are discarded when the defining
block is exited.
Variables declared inside a function have automatic storage class unless
they are declared to be static.
These are usually allocated on the program stack.
Variables defined outside any functions have static storage class.
The word static has two meanings is C.
One is related to storage class and the other to linkage class.
Linkage classes
Linkage class determines whether variables can be accessed in files other than
the one in which they are declared.
Internal linkage class means they can only be accessed in the file in which
they are declared.
External linkage class means they can be accessed in other files.
variables declared outside any function and function name identifiers have
external linkage by default.
They can be given internal linkage with the key word static.
Variables declared inside a function are only known inside that function and
are said to have no linkage.
Variables
Where Declared
static Modifies
static Applied?
Storage Class
Linkage Class
inside a function
storage class
yes
static
none
inside a function
storage class
no
automatic
none
outside any function
linkage class
yes
static
internal
outside any function
linkage class
no
static
external
Functions
static Modifies
static Applied?
Linkage Class
linkage class
yes
internal
linkage class
no
external
Table A.3, page 814 (extended)
Effect of using the static keyword modifier in a C program.
static function
Example: bubblesort, Program 2.5.
Give the storage class and linkage class of each of the
variables and functions in this program.
The function onepass has internal linkage.
The other functions have external linkage.
Functions do not have a storage class.
The count variable has internal linkage and static storage.
All other variables have no linkage and automatic storage.