340 likes | 459 Views
Introduction to Operating Systems with Dr. Ramamurthy. Substitution lecture: Project Tips, IPC Scott Settembre, TA September 21, 2010. Project Tips. What does a process look like? Pointers and Arrays? Those are easy! What is a “segmentation fault”? Two main issues that you will have
E N D
Introduction to Operating Systemswith Dr. Ramamurthy Substitution lecture: Project Tips, IPC Scott Settembre, TA September 21, 2010
Project Tips • What does a process look like? • Pointers and Arrays? Those are easy! • What is a “segmentation fault”? • Two main issues that you will have • Debugging a segmentation fault
Visualize a Process space Global variables Data Stack Code Code Program Counter (PC) Line 1 Line 2 Line 3 Line 4 …
Where are variables stored? #include <stdio.h> inta_counter = 1; int main(intargc, char *argv[]) { intb_counter = 10; } Global variables Data Stack Code Code Line 1 Line 2 Line 3 Line 4 …
What is a pointer in C? “a” is an integer int a = 10; … a++; printf(“%i”,a); 11 “b” is a pointer to an integer int * b; b = &a; … *b++; printf(“%i”,a); “b” is now pointing to the location in memory that stores “a” “b” uses the “*” operator to reference the memory it points to 12 printf(“%i”,*b); 12 The address that “b” holds, is incremented by the length of an int b++; printf(“%i”,a); 12 printf(“%i”,*b); Possible segmentation fault
What is an array in C? Note: zero terminated string automatically done for constant assignment char c = ‘a’; a char ca[10] = “hello”; h e l l o null ? ? ? ? char * cb; cb = &ca[0]; “cb” is pointing to the first character of the “ca” char array printf(“%c”,c); a printf(“%c”,ca[0]); h printf(“%s”,ca); hello printf(“%s”,&ca[0]); hello printf(“%s”,cb); hello printf(“%c”,&ca[0]); compiler error
How do pointers and arrays relate? • Easy! In C, a pointer IS AN array and an array IS A pointer! • Differences? • Using the sizeof() operator for an array will give the total size of the array, but for a pointer it will give the size of a pointer • The sizeof() operator is a compiler operator and not a callable function
Code issue : Segmentation Faults • You get a “segmentation fault” when you: • Try to access memory that you are not allowed in • Change a pointer incorrectly • Pass a pointer to a local variable (remember, local variables are on the stack and subject to disappearing when the go out of scope) • “Bus error” is like a segmentation fault, but from misuse of the stack
Example of a “Seg Fault” char * arg[2]; const char * alphabet = ”abcdefghijklmnopqrstuvwxyz”; strcpy(arg[0],alphabet); 4 bytes Visualize arg[2]: char * char * Some other memory… arg[0] arg[1] strcpy Possible segmentation fault 1 byte a b c d e …… x y z null Some other memory… Visualize alphabet:
Indirect example of a “Seg Fault” char * arg[2]; int * fa [100]; // Very important financial data const char * alphabet = ”abcdefghijklmnopqrstuvwxyz”; strcpy(arg[0],alphabet); Visualize our memory layout: int * int * Some other memory… arg[0] arg[1] strcpy int * int * int * int * fa[0] fa[1] fa[2] fa[3] printf(“Scott’s 401K plan value: %i”, *fa[0]); Possible segmentation fault
Advice on debugging faults • For project 1, most faults are due to: • Not using a char array or allocating memory for a char * to use • Passing a local pointer back from a function • Not zero-terminating your strings • Parsing a string, storing the beginning of a “token”, but then using that pointer in a string.h function which expects a NULL terminated string
Inter-process Communication • Process vs. Thread coding issues • Shared memory, shared resources • What is an “atomic” instruction? • How can I use a semaphore properly? • Protect with a semaphore • Signal with a semaphore • What is going on behind the scenes? • Example: Larry, Curly, and Moe IPC problem
Visualize Threads Data Code PC 4 PC 3 PC 2 PC 1 Line 1 Line 2 Line 3 Line 4 … Thread Pool 1 2 3 4
Visualize Multi-processes Data Data Code Code PC 1 PC 1 Line 1 Line 2 Line 3 Line 4 … Line 1 Line 2 Line 3 Line 4 … 1 Thread Pool Thread Pool 1
Atomic instructions // Simple addition int a = 5; a = a + 1; Data expand this into atomic instructions Value of “a” from memory into register Register gets incremented by 1 Register is put back into memory Code a -> r r ++ r -> a a == 5 a == 6 PC
THE Problem that arises For example, let’s say: a == 10 There will be a case where: PC1 runs ‘a->r’ so r==10 PC2 runs ‘a->r’ so r==10 PC1 runs ‘r++’ so r==11 PC2 runs ‘r++’ so r==11 PC1 runs ‘r->a’ so a==11 PC2 runs ‘r->a’ so a==11 But this is BAD, since two additions occurred! The value of ‘a’ should be 12! Data Concurrency Problem Code PC 2 PC 1 while loop { a -> r r ++ r -> a } Thread Pool 1 2
What is a semaphore? • A programming object in a multiple process/threaded environment that can: • Restrict access to a common resource • Help synchronize processes/threads Semaphore Concurrency Problem Thread 1 Wait Uses printer buffer Signal Blocked Thread 2 Wait Uses printer buffer Signal
What is a mutex? • It is a “binary semaphore” • A semaphore with only two states: locked/unlocked • Previous example was a binary semaphore • Short for “Mutual Exclusion” • You can always use a binary semaphore in place of a mutex, however, you may want to use a mutex for other reasons
Uses of a semaphore • Simplistically, you can use a semaphore to achieve two goal perspectives: • Protect a critical resource/critical section so that only N number of processes/threads can access it at a time • Signal between N number of processes/threads when it is time for another process/thread can proceed
Use #1 : Protection Semaphore Blocked Thread 1 Wait Some shared resource or variable Thread 2 Reads or Writes to variable/resource Blocked Thread 3 Wait
Use #2 : Signal Signal Semaphore A Blocked Process 1 Modify Wait Signal Semaphore B Blocked Process 2 Modify Wait Signal Semaphore C Blocked Process 3 Modify Wait
Visualize a semaphore Semaphore Blocked Thread 1 Current value Wait Signal ….. 0 1 2 N Thread modifies the resource or variable Some shared resource or variable Blocked Thread 2 Thread modifies the resource or variable Wait Signal ….. Blocked Thread N Wait
Process “starvation” • Semaphores do not wake blocked processes in any specific order • In other words, it does not use a FIFO queue • This means, starvation of a process can occur
Visualize a “starvation” scenario Semaphore Blocked Thread 1 Wait Some shared resource or variable Thread 2 Thread modifies the resource or variable Wait Signal ….. Blocked Thread N Wait
Process “deadlocks” • Semaphores do not prevent deadlock • They can prevent deadlock, if used cleverly (this will be discussed more in Lecture – “Dining Philosophers”)
Visualize a “deadlock” Some shared resource or variable “A” Deadlock! Modify/Assigned Wait/Request Wait/Request Blocked Blocked Process 1 Process 2 Modify/Assigned Wait/Request Wait/Request Some shared resource or variable “B”
Example: Larry, Curley, Moe (LCM) IPC problem • 3 Farmers named Larry, Curley and Moe Photo order above is: Curley, Moe, and Larry.
LCM Problem • There is one shovel • Larry and Moe need to use the shovel to dig • Larry only digs holes • Curley only plants seeds in open holes • Moe only fills holes up after seed planted • Larry can only dig “N” holes ahead of Moe • (Why? Because Larry is attached by chain to Moe. It is super-comedy-riffic, you see!)
What to understand • There are three farmers (i.e. three processes) • Two farmers (i.e. two processes) require the same shovel (i.e. share a resource) to get the job done. • Farmers can work in parallel, but must also work in sequence • For example: Larry can dig a hole, while Curley plants a different hole, but they cannot dig and plant the same one. • Also, Curley can plant a hole, while Moe fills an already planted hole, but they cannot plant and fill the same one.
Visualize LCM Holes Larry Shovel Or we can say, the shovel is a critical resource (or a critical section). Think “protect” with a semaphore. Since only one process can use at a time, think “binary semaphore” or “mutex”. Also a critical resource, since for any specific hole, only one farmer can work with it. However, think “signal” here instead of “protect”. Have the farmers communicate with each other when done with a hole. Curley Moe Or we can say, 3 threads running 3 different functions.
Let’s build the code Larry (digger) Curley (planter) Moe (filler) wait(Curley2go) wait(Moe2go) wait(Shovel) wait(Shovel) Larry digs hole Curley plants hole Moe fills hole signal(Shovel) signal(Shovel) signal(Curley2go) signal(Moe2go)
Hole digging limitation Let’s pretend Curley and Moe are napping in the field. How many holes would Larry be allowed to dig? Larry (digger) wait(DigHole) According to the problem, he can only dig N holes ahead of Moe. So if we create a semaphore called “DigHole”, have Larry wait on it, and then signal it N times, he would then dig N holes! wait(Shovel) Larry digs hole signal(Shovel) How do we signal the semaphore N times? We just initialize “DigHole” to have an initial value of N! signal(Curley2go)
The Elegant coding solution Have 4 semaphores: DigHole = N, Curley2go = 0, Moe2go = 0, and Shovel = 1. Larry (digger) Curley (planter) Moe (filler) wait(DigHole) wait(Curley2go) wait(Moe2go) wait(Shovel) wait(Shovel) Larry digs hole Curley plants hole Moe fills hole signal(Shovel) signal(Shovel) signal(Curley2go) signal(Moe2go) signal(DigHole)
Test tips • Be sure to understand the LCM IPC problem • There is always some type of IPC problem, but not usually harder than LCM • Difference between Processes and Threads • Be sure to finish project 1 • Review the main system calls • Why you used them