Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Operating Systems with Dr. Ramamurthy Substitution lecture: Project Tips, IPC Scott Settembre, TA September 21, 2010.

Similar presentations


Presentation on theme: "Introduction to Operating Systems with Dr. Ramamurthy Substitution lecture: Project Tips, IPC Scott Settembre, TA September 21, 2010."— Presentation transcript:

1 Introduction to Operating Systems with Dr. Ramamurthy Substitution lecture: Project Tips, IPC Scott Settembre, TA September 21, 2010

2 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

3 Visualize a Process space Code Data Global variables Stack Program Counter (PC) Code Line 1 Line 2 Line 3 Line 4 …

4 Code Data Global variables Stack Code Line 1 Line 2 Line 3 Line 4 … Where are variables stored? #include int a_counter = 1; int main(int argc, char *argv[]) { int b_counter = 10; }

5 What is a pointer in C? int a = 10; … a++; printf(“%i”,a); 11 int * b; b = &a; … *b++; printf(“%i”,a); 12 printf(“%i”,*b); 12 b++; printf(“%i”,a); printf(“%i”,*b);Possible segmentation fault “a” is an integer “b” is a pointer to an integer “b” is now pointing to the location in memory that stores “a” “b” uses the “*” operator to reference the memory it points to The address that “b” holds, is incremented by the length of an int

6 What is an array in C? char c = ‘a’; char ca[10] = “hello”; a hellonull???? 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”,cb);hello printf(“%s”,&ca[0]);hello printf(“%c”,&ca[0]);compiler error Note: zero terminated string automatically done for constant assignment

7 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

8 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

9 Example of a “Seg Fault” char * arg[2]; const char * alphabet = ”abcdefghijklmnopqrstuvwxyz”; strcpy(arg[0],alphabet); Visualize arg[2]: char * arg[0]arg[1] 4 bytes Some other memory… Visualize alphabet: a 1 byte bcdexyz null Some other memory… …… strcpy Possible segmentation fault

10 Some other memory… 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 * arg[0]arg[1] int * fa[0]fa[1]fa[2]fa[3] strcpy printf(“Scott’s 401K plan value: %i”, *fa[0]);Possible segmentation fault

11 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

12 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

13 Visualize Threads Code Data Line 1 Line 2 Line 3 Line 4 … Thread Pool PC PC 2PC 3PC 4

14 Visualize Multi-processes Code Data Line 1 Line 2 Line 3 Line 4 … Thread Pool PC 1 1 Code Data Line 1 Line 2 Line 3 Line 4 … Thread Pool PC 1 1

15 Atomic instructions // Simple addition int a = 5; a = a + 1; expand this into atomic instructions 1.Value of “a” from memory into register 2.Register gets incremented by 1 3.Register is put back into memory Code Data a -> r r ++ r -> a a == 5 PC a == 6

16 THE Problem that arises Code Data while loop { a -> r r ++ r -> a } Thread Pool PC 1 12 PC 2 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! Concurrency Problem Concurrency Problem

17 Semaphore 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 Thread 1 Thread 2 Uses printer buffer Concurrency Problem Concurrency Problem Wait Signal Blocked

18 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

19 Uses of a semaphore Simplistically, you can use a semaphore to achieve two goal perspectives: –P–Protect a critical resource/critical section so that only N number of processes/threads can access it at a time –S–Signal between N number of processes/threads when it is time for another process/thread can proceed

20 Use #1 : Protection Thread 1 Thread 2 Thread 3 Some shared resource or variable Semaphore Wait Blocked Reads or Writes to variable/resource

21 Process 1 Process 2 Process 3 Semaphore A Semaphore B Semaphore C Use #2 : Signal Wait Blocked Signal Modify Signal Modify Signal

22 Visualize a semaphore Semaphore Thread 1 Thread 2 Thread N ….. Some shared resource or variable Current value 012N ….. Wait Blocked Thread modifies the resource or variable Signal Thread modifies the resource or variable Blocked Signal

23 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

24 Visualize a “starvation” scenario Semaphore Thread 1 Thread 2 Thread N ….. Some shared resource or variable Wait Blocked Thread modifies the resource or variable Signal

25 Process “deadlocks” Semaphores do not prevent deadlock – They can prevent deadlock, if used cleverly (this will be discussed more in Lecture – “Dining Philosophers”)

26 Visualize a “deadlock” Process 1 Process 2 Some shared resource or variable “A” Some shared resource or variable “B” Wait/Request Modify/Assigned Wait/Request Modify/Assigned Blocked Deadlock!

27 Example: Larry, Curley, Moe (LCM) IPC problem 3 Farmers named Larry, Curley and Moe Photo order above is: Curley, Moe, and Larry.

28 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!)

29 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.

30 Visualize LCM Larry Curley Moe Or we can say, 3 threads running 3 different functions. 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”. Holes 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.

31 Let’s build the code Larry (digger)Curley (planter)Moe (filler) Larry digs hole wait(Shovel) signal(Shovel) Moe fills hole wait(Shovel) signal(Shovel) signal(Curley2go) wait(Curley2go) signal(Moe2go) wait(Moe2go) Curley plants hole

32 Hole digging limitation Larry (digger) Larry digs hole wait(Shovel) signal(Shovel) signal(Curley2go) Let’s pretend Curley and Moe are napping in the field. How many holes would Larry be allowed to dig? 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(DigHole) How do we signal the semaphore N times? We just initialize “DigHole” to have an initial value of N!

33 The Elegant coding solution Have 4 semaphores: DigHole = N, Curley2go = 0, Moe2go = 0, and Shovel = 1. Larry (digger)Curley (planter)Moe (filler) Larry digs hole wait(Shovel) signal(Shovel) Moe fills hole wait(Shovel) signal(Shovel) signal(Curley2go) wait(Curley2go) signal(Moe2go) wait(Moe2go) Curley plants hole wait(DigHole) signal(DigHole)

34 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


Download ppt "Introduction to Operating Systems with Dr. Ramamurthy Substitution lecture: Project Tips, IPC Scott Settembre, TA September 21, 2010."

Similar presentations


Ads by Google