Today’s agenda Hardware architecture and runtime system Additional slides Module-3 Lab1 will be released soon System calls: create(),resume(),kill(), sleep(), sleepms(), Semaphores: wait(), signal(), semcreate(),semdelete() Read Chapter 2 (2.8, 2.9) Producer-consumer sample in chapter 2 CS354-Fall2018
CS250 in 15 minutes CS354-Fall2018
Processors Each CPU has a specific set of instructions All CPUs contain General registers inside to hold key variables and temporary results Special registers visible to the programmer Program counter contains the memory address of the next instruction to be fetched Stack pointer points to the top of the current stack in memory Control registers (e.g., CR0-CR4 in x86) PSW (Program Status Word) contains the condition code bits which are set by comparison instructions, the CPU priority, the mode (user or kernel) and various other control bits. CS354-Fall2018
How Processors Work Execute instructions CPU cycles Two modes of CPU Fetch (from mem) decode execute Program counter (PC) When is PC changed? Pipeline: fetch n+2 while decode n+1 while execute n Two modes of CPU User mode (a subset of instructions) Privileged mode (all instruction) Trap (special instruction) CS354-Fall2018
Memory Access Memory read: How many mem access for one instruction? Assert address on address lines Wait till data appear on data line Much slower than CPU! How many mem access for one instruction? Fetch instruction Fetch operand (0, 1 or 2) Write results (0 or 1) How to speed up instruction execution? CS354-Fall2018
Memory-Storage Hierarchy - 4MB < -1GB -TB -TB CS354-Fall2018
Memory Registers internal to CPU (as fast as CPU) Storage 32x32 bits on a 32-bit CPU, 64x64 on 64 bit CPU (less than 1KB in both cases) Cache memory controlled by hardware Cache hit and miss RAM (Random Access Memory) Disk (magnetic disk), CD-ROM, DVD,… Cylinder, track, … Non-volatile Memory ROM (Read Only Memory) Programmed at the factory and can’t be changed EEPROM (Electrically Erasable ROM) Flash RAM Can be erased and re-written Volatile Memory CMOS holds current time and date
CPU Cache Cache hit: Cache miss: no need to access memory data obtained from mem, possibly update cache CS354-Fall2018
Memory Management How to protect programs from each other? How to handle relocation ? Base register Limit register Check and Mapping of Addresses Virtual Address - Physical Address Memory Management Unit (MMU – located on CPU chip or close to it Performance effects on memory system Cache Context switch
I/O Devices Controller Device Example: Disk Controller Controllers are complex converting OS request into device parameters Controllers often contain small embedded computers Device Fairly simple interfaces and standardized IDE (Integrated Drive Electronics) – standard disk type on Pentiums and other computers
I/O Devices Device Driver Needed since each type of controller may be different. Software that talks to a controller, giving it comments and accepting responses Each controller manufacturer supplies a driver for each OS it supports (e.g., drivers for Windows XP, Longhorn, UNIX)
Methods for I/O How device driver talks to controller Busy wait Interrupt DMA
Bus Pentium systems have eight buses Cache, local, memory, PCI, SCSI, USB, IDE, ISA PCI (Peripheral Component Interconnect) bus is successor to IBM PC ISA bus Intel bus, 528MB/sec ISA (Industry Standard Architecture) bus 16.67 MB/sec Specialized buses: SCSI (Small Computer System Interface) 160MB/sec – for disks, scanners (popular on Macintosh, UNIX) USB (Universal Serial Bus) 1.5 MB/sec IEEE 1394 – FireWire (Apple) bus 50MB/sec, connectivity for cameras to computer IDE (Integrated Drive Electronics) bus Disk, CD-ROM
Structure of an Intel Pentium System
Switch to Module-3 CS354-Fall2018
Lab 1: Producer-Consumer Read Chapter 2.8, 2.9 ex4.c, ex5.c and ex6.c two concurrent processes that each increments a shared integer n Race condition (how to handle it?) CS354-Fall2018
Start with Lab 0-Task 3 prch(c) char c; { int i; while(1) { for (i=0; i< 20000; i++); kprintf("%c", c); } /* while */ } /* prch */ main () { kprintf("\n\nHello world, Xinu lives!\n\n; resume(create(prch,2000,20,"proc A",1,'A')); resume(create(prch,2000,20,"proc B",1,'B')); resume(create(prch,2000,20,"proc C",1,'C')); } /* main */ CS354-Fall2018
What if producer-consumer? write integer n read consumer CS354-Fall2018
ex4.c /* ex4.c - main, prod2, cons2 */ # include <xinu.h> # include <xinu.h> void produce(void),consume(void); int32 n = 0; /* global variable n shared by all processes */ /* --------------- * main - Example of unsychronized producer and consumer processes * --------------- */ void main(void) { resume (create (consume, 1024, 20, "cons", 0)); resume (create (produce, 1024, 20, "prod", 0)); } CS354-Fall2018
What are outputs? Surprisingly, we do not see all values in [0,2000] /* --------------- * produce - Increment n 2000 times * --------------- */ void produce (void) { int32 i; for (i=1; i<=2000; i++){ n++; } * consume - print n 2000 times and exit void consume(void) printf("n is %d \n", n); What are outputs? Surprisingly, we do not see all values in [0,2000] But, a few 0s at start and most are 2000s. Why not? CS354-Fall2018
How to synchronize independent processes? Semaphore abstraction wait() signal() semcreate() semdelete() Two semaphores: One on which the consumer waits One on which the producer waits CS354-Fall2018
Ex5.c /* ex5.c - main, prod2, cons2 */ # include <xinu.h> void prod2(sid32,sid32), cons2(sid32,sid32); int32 n = 0; /* variable n has initial value zero */ /* --------------- * main - producer and consumer processes sychronizaed with semaphores * --------------- */ void main(void) { sid32 produced, consumed; consumed = semcreate(0); produced = semcreate(1); resume (create (cons2, 1024, 20, "cons", 2, consumed, produced)); resume (create (prod2, 1024, 20, "prod", 2, consumed, produced)); } CS354-Fall2018
* prod2 - Increment n 2000 times, waiting for it to be consumed. /* --------------- * prod2 - Increment n 2000 times, waiting for it to be consumed. * --------------- */ void prod2( sid32 consumed, sid32 produced) { int32 i; for (i=1; i<=2000; i++){ wait(consumed); n++; signal(produced); } /* --------------- * cons2 - print n 2000 times, waiting for it to be produced * --------------- */ void cons2( sid32 consumed, sid32 produced) { int32 i; for (i=1; i<=2000; i++){ wait(produced); printf("n is %d \n", n); signal(consumed); } CS354-Fall2018
Semaphores: Mutual Exclusion Only one of two executing processes obtains access to a shared resource what if not n? but a shared list? Accessed by many processes shared[n++] = item; /* n is updated over time */ CS354-Fall2018
# include <xinu.h> /* ex6.c - additem */ # include <xinu.h> sid32 mutex; /* assume initialized with semcreate */ int32 shared[100]; /* An array shared by many processes */ int32 n = 0; /* Count of items in the array */ int32 n = 0; /* variable n has initial value zero */ /* --------------- * addiotem - obtain exclusive use of array shared and add an item to it. * --------------- */ void additem (int32 item) { wait(mutex); shared[n++] = item; signal(mutex); } CS354-Fall2018