1 / 29

CPS4200

CPS4200. Unix Systems Programming Chapter 2. Programs, Processes and Threads. A program is a prepared sequence of instructions to accomplish a defined task. .c and .h files

kay
Download Presentation

CPS4200

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CPS4200 Unix Systems Programming Chapter 2

  2. Programs, Processes and Threads • A program is a prepared sequence of instructions to accomplish a defined task. • .c and .h files • Variables and functions declarations, type and macro definitions (typedef), preprocessor commands (#ifdef, #include, #define) • 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.

  3. Programs, Processes and Threads • A process has an address space/execution state and starts with a single flow of control. • The process must have an ID (PID). • 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.

  4. Programs, Processes and Threads • Context switch: The point at which execution switches from one process to another. • Example 2.2 • 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, ...

  5. Programs, Processes and Threads • There are two threads of execution: • 2451, 2461, 2471, 2451, 2461, 2471 ... and102, 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.

  6. Programs, Processes and Threads • Thread has its own execution stack, program counter value, and state. • A programmer can achieve parallelism with low overhead but may require synchronization. • Process heavyweight • Thread lightweight process

  7. Programs, Processes and Threads • A thread has: • A thread ID • A program counter • A register set • A stack

  8. Layout of Programs Image in Main Memory

  9. Layout of Programs Image in Main Memory • Activation record: • A block of memory allocated on the top of the process stack to hold the execution context of a function during a call. • It contains return address, the parameter, status information and a copy of some of the CPU register values at the time of the call. • Storage allocated on the heap persists until it is freed or until the program exits.

  10. Layout of Programs Image in Main Memory • Activation record: • A block of memory allocated on the top of the process stack to hold the execution context of a function during a call. • It contains return address, the parameter, status information and a copy of some of the CUP register values at the time of the call. • Storage allocated on the heap persists until it is freed or until the program exits.

  11. Layout of Programs Image in Main Memory • Static variables that are not explicitly initialized in their declaration are initialized to 0 at run time. • Initialized static variables  part of the executable module • Un-initialized static variables not part of the executable module • Examples

  12. 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. • For example: The argv array for the call mine -c 10 2.0.

  13. Argument Arrays

  14. Argument Arrays • 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. (double pointers)

  15. Create an Argument Array with makeargv • Makeargv creates an argument array from a string of tokens. • Method 1: One way to write a function to do this is:char **makeargv(char *s) it returns a pointer to an argv array or returns a NULL pointer if the call fails

  16. Create an Argument Array with makeargv • Method 2: 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)

  17. Create an Argument Array with makeargv • Method 3: 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. Note: C language is call-by-value Const means that the function does not modify the memory pointed to by the first two parameters

  18. Create an Argument Array with makeargv • Example: • argtest “This is a test”

  19. #include <stdio.h> #include <stdlib.h> int makeargv(const char *s, const char *delimiters, char ***argvp); int main(int argc, char *argv[]) { char delim[] = " \t"; int i; char **myargv; int numtokens; if (argc != 2) { fprintf(stderr, "Usage: %s string\n", argv[0]); return 1; } if ((numtokens = makeargv(argv[1], delim, &myargv)) == -1) { fprintf(stderr, "Failed to construct an argument array for %s\n", argv[1]); return 1; } printf("The argument array contains:\n"); for (i = 0; i < numtokens; i++) printf("%d:%s\n", i, myargv[i]); return 0; } testarg.c

  20. 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 • restrict qualifier on the two paramters requires that any object referenced by s1 can not also be accessed by s2 • 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.

  21. How to write makeargv • 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.

  22. How to write makeargv • Use malloc to allocate a buffer t for parsing the string in place. • Copy s to t. • Make a pass through the string t, using strtok to conunt the tokens. • Use the count(numtokens) to allocate an argv array • Copy s into t again • Use strtok to obtain pointers to the individual tokens, modifying t and effectively parsing t in place.

  23. How to write makeargv Strspn(char * s1, char * s2) • The strspn() function returns the length of the initial segment of string s1 that consists entirely of characters from string s2.

  24. The makeargv makes a working copy of the string s so that it does not modify that input parameter.

  25. The use of strtok to allocate strings in place for makeargv.

  26. Thread Safe Functions • strtok is not safe to use with threads since it remembers the previous state. • See program: wordaveragebad.c • Another version is available: strtok_r in which you pass another parameter which is used to remember where it is. #include <string.h>char *strtok_r(char *restrict s1, const char *restrict s2, char **restrict lasts); • You must declare a char pointer to hold the current position and pass a pointer to it as the last argument to strtok_r.

  27. Storage and Linkage Classes • 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.

  28. Storage and Linkage Classes • The word static has two meanings is C.One is related to storage class and the other to linkage class. .

  29. Storage and Linkage Classes • 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.

More Related