Docsity

Understanding Concurrency: Processes, Threads, and Address Spaces, Slides of Computer Numerical Control

Toggle sidebar
bg1
1
Concurrency
Arvind Krishnamurthy
Spring 2004
Today’s lecture
nSemester roughly divided into:
nThreads, synchronization, scheduling
nVirtual memory
nFile systems
nNetworking
nNext 2.5 weeks: threads and synchronization
nToday: thread basics
nFriday: C++ tutorial
nWednesday: thread implementation
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Understanding Concurrency: Processes, Threads, and Address Spaces and more Slides Computer Numerical Control in PDF only on Docsity!

Concurrency

Arvind Krishnamurthy Spring 2004

Today’s lecture

n Semester roughly divided into: n Threads, synchronization, scheduling n Virtual memory n File systems n Networking

n Next 2.5 weeks: threads and synchronization

n Today: thread basics

n Friday: C++ tutorial

n Wednesday: thread implementation

n Uniprogramming: 1 process at a time n “Cooperative timesharing”: mostly PCs, vintage OSes n Easy for OS, hard for user (generally) n Does not deal with concurrency (by defining it away) n Violates isolation: Infinite loops

n Multiprogramming: > 1 process at a time n Time-sharing: CTSS, Multics, Unix, VMS, NT, ...

OS

OS

Process Environments

Why concurrency?

n Operating systems have two general functions:

A. Standard services: Provide standard facilities that everyone needs Examples: standard libraries, file systems, windowing systems

B. Coordinator: allow several things to work together Examples: memory protection, virtual memory

n OS has to coordinate all the activity on a machine n Requests from multiple users, I/O interrupts

n Decompose hard problems into simpler ones n Instead of dealing with everything at once, deal with one at a time

Program vs. process (cont’d)

n Process > program n Program is just part of process state n Example: many users can run the same program (creating different processes)

n Process < program n A program can invoke more than one process n Example: cc starts up cpp, cc1, cc2, as, ld (each are programs themselves)

Process creation in Unix

n Each process has its own state

n Even if two processes are created from the same executable, they will still have different state associated with them

n When a process clones itself using “fork”, a separate copy is created

int x = 1; int pid; main() { pid = fork(); x = x + 1; }

Updates made on different variables

Process creation in Unix (contd.)

n How to make processes: n fork clones a process n exec overlays the current process

n Question: Is this a good interface?

if((pid = fork()) == 0) { /* child process / exec(“foo”); / exec does not return / else / parent / wait(pid); / wait for child to finish */

Announcements

n Zheng Ma is the TA for the class n Email: [email protected] n Please put “cs422” in subject line

n Assignment 0 is on the class website n Each person needs to this separately n Due: Jan 26th

Explicit Yields

void SimpleThread (int which) { int num; for (num=0; num<5; num++) { printf(“*** thread %d looped %d times\n”, which, num); currentThread->Yield(); } }

void ThreadTest() { Thread *t = new Thread(“forked thread”); t->Fork(SimpleThread, 1); SimpleThread(0); }

Thread State

n Can be classified into two types: n Private n Shared

n Shared state n Contents of memory (global variables, heap) n File system

n Private state n Program counter n Registers n Stack

Threads Share Memory

int done[2] = {0, 0}; void SimpleThread (int which) { int num; for (num=0; num<5; num++) printf(“*** thread %d looped %d times\n”, which, num); done[which] = 1; }

void ThreadTest() { Thread *t = new Thread(“forked thread”); t->Fork(SimpleThread, 1); SimpleThread(0); while (!done[1]); // Perform work that incorporates results from two threads }

Classifying State

int x;

void foo() { int y; x = 1; y = 1; }

main() { int *p; p = (int *)malloc(sizeof(int)); *p = 1; }

stack variable

heap access

global variable

Thread State

n Shared state: n Global variables n Heap variables

n Private state: n Stack variables n Registers

n When thread switch occurs, OS needs to save and restore private state

Thread State

int done[2] = {0, 0}; void SimpleThread (int which) { int num; for (num=0; num<5; num++) printf(“*** thread %d looped %d times\n”, which, num); done[which] = 1; printf(“%x %x\n”, done, &num); // diff. Addresses for num } void ThreadTest() { Thread *t = new Thread(“forked thread”); t->Fork(SimpleThread, 1); SimpleThread(0); while (!done[1]); // Perform work that incorporates results from two threads }

Wrap Up

n Threads encapsulate concurrency

n Address spaces encapsulate protection

n Thread is active, address space is passive

n Examples: n MS/DOS --- one thread, one address space n Old Unix --- one thread per address space, many address spaces n New Unix (Linux, Solaris), Win NT, Mach --- many threads per address space, many address spaces n Embedded systems (VxWorks, JavaOS) --- many threads, one address space (either no need for protection or it is achieved via other means)