week2_synch_all - CSCC 69H3 Operating Systems Winter 2010...

Info iconThis preview shows pages 1–11. Sign up to view the full content.

View Full Document Right Arrow Icon
CSCC 69H3 Operating Systems Winter 2010 Professor Bianca Schroeder U of T
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Process Creation: Unix In Unix, processes are created using fork() int fork() fork() Creates a new address space Initializes the address space with a copy of the entire contents of the address space of the parent Initializes the kernel resources to point to the resources used by parent (e.g., open files)
Background image of page 2
Process Creation: Unix (2) Wait a sec . .. How do we actually start a new program? int exec(char *prog, char *argv[]) exec() Stops the current process Loads the program “prog” into the process’ address space Initializes hardware context and args for the new program Places the PCB onto the ready queue Note: It does not create a new process What does it mean for exec to return?
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Unix Shells while (1) { char *cmd = read_command(); int child_pid = fork(); if (child_pid == 0) { exec(cmd); panic(“exec failed”); } else { wait(child_pid); } }
Background image of page 4
TODAY: Synchronization
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Motivating Example Suppose we write functions to handle withdrawals and deposits to bank account: Withdraw(acct, amt) { balance = get_balance(acct); balance = balance - amt; put_balance(acct,balance); return balance; } Deposit(account, amount) { balance = get_balance(acct); balance = balance + amt; put_balance(acct,balance); return balance; } Idea: Create separate threads for each action, which may run at the bank’s central server • What’s wrong with this implementation? • Think about potential schedules for these two threads
Background image of page 6
Suppose we write functions to handle withdrawals and deposits to bank account: Withdraw(acct, amt) { balance = get_balance(acct); balance = balance - amt; put_balance(acct,balance); return balance; } Deposit(account, amount) { balance = get_balance(acct); balance = balance + amt; put_balance(acct,balance); return balance; } • Suppose you share this account with someone and the balance is $1000 • You each go to separate ATM machines - you withdraw $100 and your friend deposits $100 Motivating Example
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Interleaved Schedules The problem is that the execution of the two processes can be interleaved: What is the account balance now? Is the bank happy with our implementation? • Are you? balance = get_balance(acct); balance = balance - amt; balance = get_balance(acct); balance = balance + amt; put_balance(acct, balance); put_balance(acct, balance); balance = get_balance(acct); balance = balance - amt; balance = get_balance(acct); balance = balance + amt; put_balance(acct, balance); put_balance(acct, balance); Context switch Schedule A Schedule B
Background image of page 8
Interleaved Schedules The problem is that the execution of the two processes can be interleaved: What is the account balance now? Is the bank happy with our implementation? • Are you? balance = get_balance(acct); balance = balance - amt; balance = get_balance(acct); balance = balance + amt; put_balance(acct, balance); put_balance(acct, balance); balance = get_balance(acct); balance = balance - amt; balance = get_balance(acct); balance = balance + amt; put_balance(acct, balance); put_balance(acct, balance); Context switch Schedule A Schedule B
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
What Went Wrong Two concurrent threads manipulated a
Background image of page 10
Image of page 11
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 05/22/2010 for the course CS CSCC69 taught by Professor Bianca during the Spring '10 term at University of Toronto- Toronto.

Page1 / 56

week2_synch_all - CSCC 69H3 Operating Systems Winter 2010...

This preview shows document pages 1 - 11. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online