






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!







Arvind Krishnamurthy Spring 2004
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, ...
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
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)
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
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 */
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
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); }
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
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 }
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
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
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 }
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)