You are on page 1of 2

Binary Hex Binary Hex

8
0000 0 STOP 1000
AND

9
0001 1 1001
OR

0010 2 1010 A

0011 3 1011 B

0100 4 1100 C LDWA

0101 5 1101 D LDBA

6
0110 1110 E STWA
ADDA

7
0111 1111 F STBA
SUBA

0 0000 accumulator immediate


9 1001 Index register direct
1 0001 accumulator direct
8 1000 index immediate
Contents on index register in the direct memory location. Load exact imm value into accum/index register. Executing a CALL 1. Set the Stack Pointer (SP) to 2 less than its current value. 2. Set the memory
location at the location of the SP to the current value of the Program Counter (PC). 3. Set the PC to be the operand of the CALL instruction. This moves the SP up, stores the return address, and moves
to the location of the subprogram. Executing a RET 1. Set the PC to be the value stored in the location pointed to by the SP. 2. Set the SP to be 2 larger than its current value. This loads the return address
back into the PC, then drops the SP to remove the return address from the stack. Params: The calling code adds the parameters above the current Stack Pointer (SP), then pushes them onto the stack
executes SUBSP). The calling code pushes the return address (executes CALL). The called subprogram allocates local variables (executes SUBSP). The called subprogram executes its code. The called
subprogram deallocates local variables (executes ADDSP). The called subprogram pops the return address (executes RET). The calling code pops the actual parameters (executes ADDSP). Value param:
copied. Ref- linked, changing one changes another. Global array indexed addressing. Aquire -1 release +1. Dining phil sol: Only allow four philosophers to attempt to eat at once Allow a philosopher to
eat only if both chopsticks are available Number the chopsticks, and require philosophers to request the chopsticks in order by number Use tools to request both chopsticks atomically, Read-write sol: lock
object, 1 P access at a time. Readers: unnecessarily locked. Readers can lock, other readers join (writers may starve). Writer lock: no one joins. Deadlock Recovery: preemption- take R away to give a P
that can finish, rollback to safe state, killing until gone. Avoidance: check, don’t grant. Prevention: construct system imp deadlock. Abstraction of address space. Goals of Memory Virtualization There are
goals for the operating system to not only virtualize memory, but to do so with style. Transparency Programs should not be aware that they are using virtualized memory. Efficiency Virtualizing memory
should not cost much time or space to use. Protection Processes should not be able to access any memory outside of its address space. Isolation Each process is in its space and cannot be affected by other
processes. pros to this memory abstraction? Allows multiple processes in memory at once. Allows relocation if processes is restarted. Cons Does not provide protection or security. Cannot relocate while
running.execution time address binding: special hardware, most flexi, modern OS. physical address = virtual address + base. Base-bound procon: efficient, internal fragment. Segment-base-size: Code-
32K-2K.Heap-34-2.Stack-28-2. 1KB=1024B. Address space heap virtual 4096-6143. Before heap: code. In range: legal. Segment: 00 - code 01 - heap 10 - not used 11 - stack. Ex frag is the wasted space
between memory allocated to processes/users. In frag is within. Vir mem: base bound: both. Segmentation: ex. Paging: no ex.Paging Pro: Flexibility, simplicity - just 4 free pg. No exfrag. Con: slow, lot
space in mem. Vir ad- 101011, page- 10 (2), offset - 1011. pageT is DS to map vir add to phy add. Translation Lookaside Buffer (TLB) cpu-part storing hardware cache PT data. TLB_Lookup, TLP_Insert.
Caching: temporal: soon re-access, spatial: nearer inst/data val will re-access. TLB Miss: CISC Complex instruction set computers. The hardware handles the miss. All the information is stored in
hardware. RISC Reduced instruction. software. hardware raises an exception and the OS handles it. TLB Entries: VPN, PFN, Valid bit: data in the entry valid. On boot invalid. How to Manage TLB Contents
on a Context Switch?When context-switching between processes, the translations in the TLB for the last process are not meaningful in the about-to-be-run process. Flush the TLB:set every valid bit to 0
in the TLB, essentially removing all information from the TLB after the context is switched, better Address Space Identifiers (ASID): identify which process the entry applies to, so we only use the entries
for that process. TLB replace policy: minmiss rate. MIPS TLBP - Probe TLBR - Read into registersTLBWI - Write (replace) a specific TLB entry TLBWR - Write (replace) a random TLB entry. If the process
accesses a number of pages in a short period of time that is larger than the number of spots in the TLB, we will get a lot of misses. This is known as exceeding the TLB coverage. Make page table small
(linear array gets big): Bigger pages? More infrag. smaller pages better. LRU go back frame times, skip recently used, not recently added.
#include <iostream> #include <vector> #include <thread>#include <algorithm>#include <cstdlib>#include <ctime>#include <limits>
// Function to find the maximum element in a specific range of the array int findMaxInRange(const std::vector<int>& array, size_t start, size_t end) {
return *std::max_element(array.begin() + start, array.begin() + end); } // Thread function to find the maximum in a quarter of the array void findMaxInQuarter(const std::vector<int>&
array, size_t start, size_t end, int& result) { result = findMaxInRange(array, start, end);} int main() { // Seed for random number generation std::srand(static_cast<unsigned
int>(std::time(nullptr))); // Size of the array const size_t arraySize = 1000; // Create and fill the array with random integers std::vector<int> myArray(arraySize); for (int& value : myArray)
{ value = std::rand() % 1000; // Random integers between 0 and 999 }//numThread const size_t numThreads = 4; // Container for thread objects and results std::vector<std::thread>
threads; std::vector<int> results(numThreads, std::numeric_limits<int>::min()); // Divide the array into quarters and assign each quarter to a thread size_t quarterSize = arraySize /
numThreads; for (size_t i = 0; i < numThreads; ++i) { size_t start = i * quarterSize; size_t end = (i == numThreads - 1) ? arraySize : (i + 1) * quarterSize;
threads.emplace_back(findMaxInQuarter, std::cref(myArray), start, end, std::ref(results[i])); }// Wait for all threads to finish for (auto& thread : threads) {thread.join();} // Find the maximum
value from the results obtained by each thread int finalMax = std::max_element (results.begin(), results.end()); // Output the result std::cout << "The largest item in the array is: " <<
finalMax << std::endl; return 0;}
/* Prime Number counting * Asks user to enter a low and a high, then * tells how many prime numbers are in that * range. Print them for now. */
#include <iostream>#include <thread>#include <chrono>#include <math.h>using namespace std;using namespace std::chrono;// Returns true if n is prime and false otherwise.
bool is_prime( int n ) { if ( n == 1 ) return false; for ( int i = 2; i <= sqrt(n); i++ ) { if ( ( n % i ) == 0 ) return false; } return true;}int number_of_primes( int low, int high ) { int count = 0; for ( int
i = low; i <= high; i++ ) { if ( is_prime( i ) ) { count++; //cout << i << " "; } }cout << "Done calculating primes between " << low << " and " << high << endl; return count;}
int number_of_primes_parallel( int low, int high ) { int mid = 2 * ( low + high ) / 3; int low_count, high_count; thread t1( [&] { low_count = number_of_primes( low, mid ); } ); thread t2( [&]
{ high_count = number_of_primes( mid + 1, high ); } ); t1.join(); t2.join(); return low_count + high_count;}int main( ) { int lower, higher; int number1, number2; cout << "Enter a
low value: "; cin >> lower; cout << "Enter a high value: "; cin >> higher; high_resolution_clock::time_point t1 = high_resolution_clock::now( ); number1 = number_of_primes( lower,
higher );
high_resolution_clock::time_point t2 = high_resolution_clock::now( );number2 = number_of_primes_parallel( lower, higher ); high_resolution_clock::time_point t3 =
high_resolution_clock::now( ); auto duration_sequential = duration_cast<microseconds>( t2 - t1 ).count();auto duration_parallel = duration_cast<microseconds>( t3 - t2 ).count();cout <<
"There are " << number1
<< " prime numbers between " << lower<< " and " << higher << "." << endl; cout << "This took " << duration_sequential << " microseconds sequentially." << endl; cout << "There are "
<< number2 << " prime numbers between " << lower<< " and " << higher << "." << endl; cout << "This took " << duration_parallel << " microseconds in parallel." << endl;}

import java.util.concurrent.Semaphore; import import java.util.concurrent.Semaphore;


java.util.concurrent.Semaphore; import java.util.Random;
public class Main { public class Main { public class Philosopher extends Thread
static public int counter; static int MAX = 10; {public static int NUM_PHIL = 5; public static Semaphore[] chopsticks = new
static public Semaphore lock; static int PROD = 100; Semaphore[NUM_PHIL]; public int number;public int left;public int right;public static
int buffer[] = new int[MAX]; Random r; public Philosopher( int n ){number = n;left = number;
public static void main( String[] args) Semaphore empty = new right = ( number + 1 ) % NUM_PHIL;
throws InterruptedException { Semaphore( MAX ); for ( int i = 0; i < NUM_PHIL; i++ ){ chopsticks[i] = new Semaphore( 1 );}r = new Random(); }
counter = 0; Semaphore full = new public void get_chopsticks( ){try{ chopsticks[left].acquire();
Semaphore( 0 ); System.out.println( "Philosopher " + number + " got chopstick " + left );
Thread myThread = new Thread( ) { int fill = 0; chopsticks[right].acquire();
public void run( ) { int use = 0; System.out.println( "Philosopher " + number + " got chopstick " + right ); }
for ( int i = 0; i < 1000000; i++ ) { void put( int n ) { catch( InterruptedException e ){
counter++; buffer[fill] = n; System.out.println( "Problem in thread." );}}
} fill = ( fill + 1 ) % MAX; public void drop_chopsticks( ) {
} } chopsticks[left].release();
}; int get( ) { chopsticks[right].release(); }
int temp = buffer[use]; public void eat( ){
Thread myThread2 = new Thread( ) { use = ( use + 1 ) % MAX; System.out.println( "Philosopher " + number + " is eating." );
public void run( ) { return temp; try{Thread.sleep( r.nextInt(100) ); }catch( InterruptedException e )
for ( int i = 0; i < 1000000; i++ ) { } {System.out.println( "Something wrong!" ); }System.out.println( "Philosopher " + number + " is
counter++; public class ProdThread extends done eating." );}public void think( ){
} Thread { System.out.println( "Philosopher " + number + " is thinking." );try{Thread.sleep( r.nextInt(200) );
} public void run( ) { } catch( InterruptedException e ){
}; for ( int i = 0; i < PROD; i++ ) { System.out.println( "Something wrong!" );} System.out.println( "Philosopher " +
try { number + " is done thinking." ); } public void run( ) {
myThread.start( ); empty.acquire( ); while( true) { think();get_chopsticks();eat();drop_chopsticks();}}}
myThread2.start(); System.out.println( "Adde
myThread.join( ); d item " + i );
myThread2.join( ); put( i );
System.out.println( "Counter = " + full.release();
counter ); }
} catch( InterruptedException e ) {
} System.out.println( e ); } }
System.out.println( "The final value of }}
counter is " + counter + "." ); public class ConsThread extends
System.out.println( "It should be " + Thread {
( NUM_THREADS * NUM_INC ) + "." ); public void run( ) {
System.out.println( "Thank you." ); int temp;
} for ( int i = 0; i < PROD; i++ ) {
} try {
full.acquire( );
temp = get( );
//java thread System.out.println( "Rem
public class HelloWorld { oved item " + temp );
public static void main( String[] args) empty.release( );
throws InterruptedException { }
Thread myThread = new Thread( ) { catch( InterruptedException e ) {
public void run( ) { System.out.println( e ); } }
System.out.println( "Hello from new }}
thread." ); public void begin( ) throws
} InterruptedException {
}; ProdThread p = new
myThread.start( ); ProdThread();
Thread.yield( ); ConsThread c = new
ConsThread();
System.out.println( "Hello from main
p.start();
thread." );
c.start();
myThread.join( ); p.join();
} c.join();
}

System.out.println( "Finished!" );
}

public static void main( String[] args


) throws InterruptedException {
Main m = new Main();
m.begin();
}
}

You might also like