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

Slides:



Advertisements
Similar presentations
1 Interprocess Communication 1. Ways of passing information 2. Guarded critical activities (e.g. updating shared data) 3. Proper sequencing in case of.
Advertisements

1 Chapter 5 Concurrency: Mutual Exclusion and Synchronization Principals of Concurrency Mutual Exclusion: Hardware Support Semaphores Readers/Writers Problem.
Concurrency: Deadlock and Starvation Chapter 6. Deadlock Permanent blocking of a set of processes that either compete for system resources or communicate.
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
Chapter 6: Process Synchronization
Concurrency in Shared Memory Systems Synchronization and Mutual Exclusion.
1 Pointers A pointer variable holds an address We may add or subtract an integer to get a different address. Adding an integer k to a pointer p with base.
Big Picture Lab 4 Operating Systems Csaba Andras Moritz.
1 Concurrency: Mutual Exclusion and Synchronization Chapter 5.
Avishai Wool lecture Introduction to Systems Programming Lecture 4 Inter-Process / Inter-Thread Communication.
Review: Process Management Objective: –Enable fair multi-user, multiprocess computing on limited physical resources –Security and efficiency Process: running.
5.6 Semaphores Semaphores –Software construct that can be used to enforce mutual exclusion –Contains a protected variable Can be accessed only via wait.
Memory Arrangement Memory is arrange in a sequence of addressable units (usually bytes) –sizeof( ) return the number of units it takes to store a type.
Inter Process Communication:  It is an essential aspect of process management. By allowing processes to communicate with each other: 1.We can synchronize.
Concurrency: Mutual Exclusion, Synchronization, Deadlock, and Starvation in Representative Operating Systems.
Chapter 2.3 : Interprocess Communication
©Brooks/Cole, 2003 Chapter 7 Operating Systems Dr. Barnawi.
1 Concurrency: Deadlock and Starvation Chapter 6.
C pointers (Reek, Ch. 6) 1CS 3090: Safety Critical Programming in C.
Threads© Dr. Ayman Abdel-Hamid, CS4254 Spring CS4254 Computer Network Architecture and Programming Dr. Ayman A. Abdel-Hamid Computer Science Department.
Race Conditions CS550 Operating Systems. Review So far, we have discussed Processes and Threads and talked about multithreading and MPI processes by example.
A. Frank - P. Weisberg Operating Systems Introduction to Cooperating Processes.
Memory Layout C and Data Structures Baojian Hua
CS 241 Section Week #4 (2/19/09). Topics This Section  SMP2 Review  SMP3 Forward  Semaphores  Problems  Recap of Classical Synchronization Problems.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
Object Oriented Analysis & Design SDL Threads. Contents 2  Processes  Thread Concepts  Creating threads  Critical sections  Synchronizing threads.
Concurrency: Mutual Exclusion and Synchronization Chapter 5.
Thread Synchronization with Semaphores
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Introduction to Concurrency.
CS345 Operating Systems Threads Assignment 3. Process vs. Thread process: an address space with 1 or more threads executing within that address space,
1 Announcements The fixing the bug part of Lab 4’s assignment 2 is now considered extra credit. Comments for the code should be on the parts you wrote.
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Mutual Exclusion.
1 Chapter 2.3 : Interprocess Communication Process concept  Process concept  Process scheduling  Process scheduling  Interprocess communication Interprocess.
Concurrency: Mutual Exclusion and Synchronization Chapter 5.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
CPS4200 Unix Systems Programming Chapter 2. Programs, Processes and Threads A program is a prepared sequence of instructions to accomplish a defined task.
Chapter 6 – Process Synchronisation (Pgs 225 – 267)
Computer Architecture and Operating Systems CS 3230: Operating System Section Lecture OS-4 Process Communication Department of Computer Science and Software.
1 Interprocess Communication (IPC) - Outline Problem: Race condition Solution: Mutual exclusion –Disabling interrupts; –Lock variables; –Strict alternation.
Pointers in C Computer Organization I 1 August 2009 © McQuain, Feng & Ribbens Memory and Addresses Memory is just a sequence of byte-sized.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
1 Condition Variables CS 241 Prof. Brighten Godfrey March 16, 2012 University of Illinois.
Operating Systems Lecture Notes Synchronization Matthew Dailey Some material © Silberschatz, Galvin, and Gagne, 2002.
Chapter 7 - Interprocess Communication Patterns
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
Concurrency in Shared Memory Systems Synchronization and Mutual Exclusion.
1 Recall that... char str [ 8 ]; str is the base address of the array. We say str is a pointer because its value is an address. It is a pointer constant.
Operating System Concepts and Techniques Lecture 13 Interprocess communication-2 M. Naghibzadeh Reference M. Naghibzadeh, Operating System Concepts and.
Mutual Exclusion -- Addendum. Mutual Exclusion in Critical Sections.
Big Picture Lab 4 Operating Systems C Andras Moritz
Case Study: Pthread Synchronization Dr. Yingwu Zhu.
Process Tables; Threads
Sarah Diesburg Operating Systems COP 4610
Background on the need for Synchronization
CSE 303 Concepts and Tools for Software Development
HW1 and Synchronization & Queuing
Critical Section and Critical Resources
Critical Section and Critical Resources
Process Tables; Threads
Memory Allocation CS 217.
Threading And Parallel Programming Constructs
Process Synchronization
COMS Prelim 1 Review Session
Pointers The C programming language gives us the ability to directly manipulate the contents of memory addresses via pointers. Unfortunately, this power.
Concurrency: Mutual Exclusion and Process Synchronization
Multithreading Tutorial
Computer Science & Engineering Electrical Engineering
Synchronization Primitives – Semaphore and Mutex
Multithreading Tutorial
Presentation transcript:

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 – Debugging a segmentation fault

Visualize a Process space Code Data Global variables Stack Program Counter (PC) Code Line 1 Line 2 Line 3 Line 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; }

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

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

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); 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

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

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 Code Data Line 1 Line 2 Line 3 Line 4 … Thread Pool PC PC 2PC 3PC 4

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

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

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

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

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: –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

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

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

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

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 Thread 1 Thread 2 Thread N ….. Some shared resource or variable Wait Blocked Thread modifies the resource or variable Signal

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” 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!

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

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

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!

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)

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