SER334

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

ready queue

processes ready for execution on cpu

device queue

processes waiting to use a device/resource

two types of threads and what is the process of connecting them called

user and kernel Thread mapping

blocking or synchronous

waits for process to receive before continueing

asynchronous threading

when the parent creates a child and both execute concurrently. Little to no data sharing so all threads are independant.

symmetric communication

when the process needs to know who it is sending the message to.

mutex use

while (condition) pthread_mutex_lock(&lock) ///crit section pthread_mutex_unlock(&lock)

in java how do you provide data to read and write to threads?

wrapper class (nested)

How does OpenMP library support threading?

It examines hints left by the programmer and determine if the code segment can be parallelizable.

What is benefit economy?

Less resource overhead since you are not cloning.

what are two types of pipes

Ordinary pipes - read end and write end Named pipes - a named resource that can be accessed by multiple processes and is persistent (similar to file)

what is a pipe?

Unix concept of passing information

non-blocking and asynchronous

does not wait for process to receive before moving on

instead of doing deep copy with fork() what can we do?

on fork() make pcb2 point to the state of pcb1 to avoid having to pay resource cost of copying.

OpenMP directives and description

parallel - spawns threads based on processors for - equally distributes for loop operation over threads sections - indicates independance and may run parallel

how do you navigate the shared memory to print lines?

print line the more the pointer by the length of the string printed

compare_and_swap

compares value in variable to what is expected, if true then sets to new value. int lock = 0; do while (compare_and_swap (&lock, 0, 1) != 0); // critical section lock = 0; // remainder section } while true

Data Parallelism and Task parallelism

data is computations on subset of data task is different computations done simultaneously over entire data set

from ready queue to cpu is called?

dispatched

Consider the problem of finding the maximum element in an array of values, which is solved by the following program: #inc lude <s t d i o . h> #d e f i n e COUNT 1024 i n t data [COUNT] ; i n t findMaximum( i n t data [ ] , i n t s i z e ) { i n t max = data [ 0 ] ; f o r ( i n t i = 1 ; i < s i z e ; i++) i f ( data [ i ] > max) max = data [ i ] ; r e turn max ; { void main ( void ) { // assume that data i s populated he r e p r i n t f ("max i s %d" , findMaximum( data , COUNT) ; } Using pthreads, rewrite this program to perform this same work but split across two threads.

#inc lude <pthread . h> #inc lude <s t d i o . h> i n t data [ 1 0 2 4 ] ; s t r u c t i n f o { // fancy way i n t numbers ; i n t s t a r t ; i n t end ; i n t r e s u l t ; } ; void findMaximum( void param) { s t r u c t i n f o in = ( s t r u c t i n f o ) param; i n t max = data [ in>s t a r t ] ; f o r ( i n t i = in>s t a r t +1; i < in>end ; i++) i f ( data [ i ] > max) max = data [ i ] ; in>r e s u l t = max ; pthread_exi t ( 0 ) ; } void main ( void ) { i n f o t i [ 2 ] ; pthread_t t id1 , t id2 ; pthread_attr_t a t t r ; // assume that data i s populated he r e t i [ 0 ] . numbers = data ; t i [ 0 ] . s t a r t = 0 ; t i [ 0 ] . end = 1024/2; t i [ 1 ] . numbers = data ; t i [ 1 ] . s t a r t = 1024/2; t i [ 0 ] . end = 1024; pthr ead_at t r_ini t (&a t t r ) ; pthread_create (&t id1 , &at t r , findMaximum , &t i [ 0 ] ) ; pthread_create (&t id2 , &at t r , findMaximum , &t i [ 1 ] ) ; pthread_join ( t id1 , NULL) ; pthread_join ( t id2 , NULL) ; i n t f i n a l = t i [ 1 ] . r e s u l t > t i [ 0 ] . r e s u l t ? t i [ 1 ] . r e s u l t : t i [ 0 ] . r e s u l t ; p r i n t f ("max i s %d" , f i n a l ) ; }

OpenMP critical

#pragma omp critical

Consider each of the following resource scenarios: (a) Recording audio though a computer's audio-in port. (b) Managing a pair of systems set up in a producer-consumer relationship. (c) Applying a blur to an image in an image editor. (d) Saving a large file (1+ GB) to a hard drive. For each of these, select and justify the appropriate synchronization tool: mutex, semaphore, or none.

(a) None. Any number of programs may record input since they do not impact it. (b) A mutex. (c) None. Pixels are independent data. (d) A mutex. Only one application should be allowed to read/write the file.

Trace the program below, identify the values of the pids at lines A, B, C, D, E, and F. (Assume that the actual pid of Process 1 is 1502, Process 2 is 1505, and Process 3 is 1507. Also assume that fork will always succeed.) [4 points] #include <sys / types . h> #include <s t d i o . h> #include <uni s td . h> int main ( ) { pid_t temp1 , temp2 , temp3 ; temp1 = f o r k ( ) ; i f ( temp1 < 0) { / Error occurred / f p r i n t f ( s tde r r , " Fork Fa i l ed " ) ; return 1 ; } else i f ( temp1 == 0) { / Process 2 / temp2 = f o r k ( ) ; i f ( temp2 < 0) { / Error occurred / f p r i n t f ( s tde r r , " Fork Fa i l ed " ) ; return 1 ; } else i f ( temp2 == 0) { / Process 3 / temp3 = g e tpid ( ) ; p r i n t f ( " temp2 = %d" , temp2 ) ; / A / p r i n t f ( " temp3 = %d" , temp3 ) ; / B / } else { / Process 2 / temp3 = g e tpid ( ) ; p r i n t f ( " temp2 = %d" , temp2 ) ; / C / p r i n t f ( " temp3 = %d" , temp3 ) ; / D / wait (NULL) ; } } else { / Process 1 / temp2 = g e tpid ( ) ; p r i n t f ( " temp1 = %d" , temp1 ) ; / E / p r i n t f ( " temp2 = %d" , temp2 ) ; / F / wait (NULL) ; }r eturn 0 ; }

A-0, B-1507, C-1507, D-1505, E-1505, F-1502

Explain why the queue diagram in slide 9 has a continuous cycle that flows between the listed queues and resources? Is a cycle necessary? (scheduling queues of ready->cpu->resources

All processes start in the ready queue, and then move to the CPU for computation before moving into another queue that requests resources such as I/O, forking of processes, and listening for interrupts to invoke other actions. Each of these resources are limited and are made available via a queue. Once the resource has been used, the process returns to the ready queue to determine if they can be terminated, or need another round of resource allocation. The ready queue is continuously looping through processes, and feeding it into the CPU to ensure that each process gets resources for as long as it needs while no particular one prevents others from getting CPU/IO time.

Why would it be appropriate to say that an assignment operation (a = 5;) is an atomic operation while an increment operation (a++;) is not? Will your answer be the same for all possible hardware?

An assignment operation has only an value on the right side, so it will compile down to just an assignment without any preconditions. Since it's just a single operation, it's naturally atomic. For the increment, we need to emit code that loads, adds, and stores the value. Since we have multiple operations, it cannot be atomic.

Consider the algorithmic task of compiling a set of source files in a programming project. For each source file, the compiler needs to emit an object file containing byte-code that represents the original source code at a lower level. Then, each file is linked together (with standard libraries) to generate a final executable file. Say we want to design a compiler to compile a project as quickly as possible. Of the five issues in multi-core programming, which is the most problematic for multi-threading this system?

Balance. Source files can be of vastly different sizes (think 1 method vs 100). Since the length of the files is proportional to how long they take to compile, we might end up with cases where compilation of one file takes so long that other the threads compiling other files end up idling. (A runner up would be Data Dependency, for cases where files dependent on one another. Like inheritance.)

A PCB typically contains a list of the files that the process has open. Explain how the PCB concept could be used to avoid concurrent IO issues?

Before the operating system allows a process to open a file for writing, it can check through the other PCBs to make sure that another process is not already writing to that file. The operating system could also prevent reads from files that are currently being written. (It should be noted that operating systems do not commonly prevent concurrent file access issues).

Consider the algorithmic task of compressing a folder. The typical threaded implementation usually divides up files in a folder being compressed, and then sends out the work of compressing some sub-set of them to different things. Each thread will produce compressed versions of each file it is given, which are then combined into a single compression archive. This gives a significant speedup. Another compression scheme uses a so-called a "dictionary". In this "dictionary", chunks of uncompressed data are mapped to indices with compressed data. The dictionary will end up stored in the compressed file so it can be used to reconstruct the compressed data. The idea is that if data repeats (e.g., same file twice), both compressed instances of the file can share the same compressed data. This can give a significant improvement in file size. Say we want to design a dictionary based compression tool. Of the five issues in multi-core programming, which is the most problematic for multi-threading this system?

Data Dependency. The issue is that we are limited to a single dictionary of pieces of compressed file. If two threads try to compress files and decide that they need to add an entry to the dictionary, then they will compute it in parallel, but only one result will actually get to be stored. Since the purpose of the dictionary is to support reuse, the slower thread will have to discard its result (the dictionary already has an entry), and instead reference the entry produced by the first.

why most of the time writing data (in parallel processing) is done within a critical section.

In parallel programming, writing data can become problematic when two or more executing threads attempt to write data to the same spot at the same time. This can very quickly lead to corrupted data, as each thread is not aware of the changes being made by other threads. By limiting these operations to exist only within the critical section only one thread can write to a file at a time, thus guaranteeing mutual exclusion.

Would priority queues make sense for scheduling?

No because a low priority process may never get to execute

how it would be possible to have a situation where programs are making progress but do not have bounded waiting time.

Progress only means that one program is able to do something (and will) when no process is in the critical section. This does not mean there could be another program running which never gets to the critical section, or does not enter it enough times to complete. (If a program doesn't complete, then its waiting time is unbounded.) This can also be caused by the addition of many new processes which have a higher "priority" to enter the critical section.

There are four general regions to a process in memory: stack, heap, data, and text. List and explain which of these must be separated for each program running and which might be combined globally.

Stack: This must be kept separate since programs must track their own execution. Heap: This can safely be combined - all programs are simply linking to allocations at different addresses. Data: This can safely be combined - variables will get stored in different places. Text: This can be problematic - code will get stored in different places but we will need some way to distinguish between different entry points (main functions).

process control block (pcb)

The data structure used by the operating system to capture the entire state of a process

The second parameter for sem_init() is used to determine if the thread is to be shared between processes or not. What would be the benefit of enabling the shared parameter for initializing a semaphore? Explain.

The shared parameter would be useful if we want the semaphore to have access to some dataset/property, and we want this semaphore to be shared between threads/processes. This benefit allows us to have synchronization with the external, semaphored dataset, so that whenever accessing it, it is ensured that we don't cause problems affiliated to the Critical Section Problem. Thus, our benefit is that we have the chance to further synchronize our processes for segments of code we want executed in concurrency for processes and threads.

How does supporting mutual exclusion, progress, and bounded wait time determine the effectiveness of a solution for the critical section problem?

The three properties are all effective ways to measure the quality of control for processes and threads. Mutual Exclusion is a factor to verify no deadlocks or anything of the liking. Progress ensures we handle global starvation with processes by allocating time whenever available. Bounded Wait Time is to ensure priority is equivalent for all processes, and that no single thread starves. During critical execution of a process we must ensure that other processes are receiving justified wait times in relationship to the pool of processes.

Consider the following program written with the pthreads library: #inc lude <pthread . h> #inc lude <s t d i o . h> i n t t1 , t2 , input ; void runner ( void param) { i n t r e s u l t , upper = ( i n t ) param; i n t a = 1 , b = 1 , c = 1 ; f o r ( i n t i = 2 ; i < inputupper ; i++) { c = a + b ; a = b ; b = c ; } i f ( upper == 1) t1 = c ; e l s e t2 = c ; pthread_exi t ( 0 ) ; } i n t main ( i n t argc , char argv [ ] ) { pthread_t t id1 , t id2 ; pthread_attr_t a t t r ; input = 1 0 ; pthr ead_at t r_ini t (&a t t r ) ; pthread_create (&t id1 , &at t r , runner , 1 ) ; pthread_create (&t id2 , &at t r , runner , 2 ) ; pthread_join ( t id1 , NULL) ; pthread_join ( t id2 , NULL) ; p r i n t f (" r e s u l t = %d\n" , t1+t2 ) ; } What is this programming trying to compute and how does it uses threads?

This program is computing the 10th Fibonacci number. It uses one thread to compute fib(n-1) and store it in t1, and another to compute fib(n-2) and store it into t2.

What IPC model does threads use and why?

Threads use shared memory because they do not have individual memory allocations

How to speed up memory management of task structs?

Through the use of a task struct pool of unused task structs for reuse.

what does wait() do? exit()?

Waits for the child to finish. terminate process

Think about why the atomic functions test_and_set() and compare_and_swap()would have been developed. Why are these functions useful for process synchronization?

We need these functions for higher level synchronization operations that aren't sensitive to race conditions (due to their atomicity), and to avoid the need for many spinlocks. We don't want wasted time for processing. The concepts come from the scenarios where threads are trying to acquire a lock, change the state of the object, and perform an operation on top of it in real-time. There is a common issue where multiple threads have to the potential to write over each other's output and generate inconsistent state. Or perhaps we want some process to execute its critical section code, and have the other process execute its non-critical section of code in parallel.

synchronous threading

When the parent thread must wait for all child threats to terminate before resuming (fork-join). Has significant data sharing were the parent may combine results calculated from several child threads.

Consider a program which creates three threads and uses one of them to display "A", another for "B", and the last for "C" in the same console window. No synchronization occurs. Does this program contain a race condition?

Yes. If each thread prints a different string at the same time, there is no guarantee what the final ordering is. Since we cannot guarantee the final state of the program, there is a race condition.

Consider using an image editor that uses a background thread to regularly save the the file being edited to ensure the user doesn't lose data. This image editor contains open, save, close, and edit functionality. Does this program contain any possible race condition?

Yes. There is a chance the image could auto-save, then since images are large and take time to save, the user could make a small change and click save manually. This would result in two threads saving the image at once, with the result that the contents of the file might be corrupted.

Consider the following code fragment and answer the following questions: pid_t pid ; temp = f o r k ( ) ; i f ( temp == 0) { f o r k ( ) ; } thread_create ( . . . ) ; (a) Using "lifeline notation", draw the creation of processes and threads during execution. (b) How many unique processes are created? (Do not include the initial process.) (c) How many unique threads are created? (Hint: processes don't count!)

a) p-> c1-> c2 b) 2 c) 3

What is benefit responsiveness? and example

allow a program to continue running even if part of it is blocked or is performing a lengthy operation, thereby increasing responsiveness to the user. GUI that uses separate thread to handle operations while remaining responsive to user.

What operation prevents concurrency issues

atomic

how are variables shared to threads

global is available to all threads whereas local variables exist in a stack and all threads have their own stack so local variables sharing will need to be arranged.

Scheduling queues

handled by a process scheduler to divide up resources for current processes.

How to use threads?

https://www.youtube.com/watch?v=uA8X5zNOGw8&list=PL9IEJIKnBJjFZxuqyJ9JqVYmuFZHr7CFM

Five challenges for multicore system programming?

identifying tasks (independant functionality) balance (each thread should do = work) data splitting (memory if finite, only copy needed) data dependency (some need to be done serial) testing and debugging

Why are signals discussed as thread issues?

if a process has many threads, how do you determine which thread handles the signal?

priority inversion

if low priority process is being starved, switch priority with high priority process

what are reasons for cooperative processes?

info sharing computation speedup modularity convenience

how does pcb allow the cpu to run programs concurrently?

it saves the state of currently running process. reloads the state of "to be ran" process executes until interrupt or system call saves the state reloads previous process

semaphore

like mutex but for a set of resources. uses int instead of boolean. uses wait() signal()

test_and_set two initial ideas

locks and atomic boolean lock = false; do while (test_and_set (&lock)); // critical section lock = false // remainder section } while true

how to rpc's find specific port?

matchmaker

3 properties of solving critical section

mutual exclusion - no two processes in crit region progress - if no process in crit region, then one should enter bounded waiting time - no process should starve

What states can a process be in?

new (brought into memory) ready (ready queue, waiting to run on cpu) running (process is doing work on cpu) waiting (I/O call or event wait) termination (exit process)

What problem does a shared memory system have?

producer-consumer, needs to use mutex or semaphore.

thread funcitons

pthread_t thread; pthread_attr_t attr; pthread_attr_init(&attr); pthread_create(&thread, &attr, function_to_run, void* arg) pthread_join(thread, NULL);

java threads need to implement

runnable

semaphore functions

sem_t sem; sem_init(&sem, 0, 1) - create and set to 1 sem_wait(&sem) - block sem_post(&sem) - release

how to enforce order with semaphore

set sem variable = 0 execute f1 then signal increments variable to 1 to allow f2 to execute

four key functions of shared memory

shm_open - open new/create virtual file ftruncate - load file and set size mmap - creates pointer from RAM to memory location shm_unlink - unlink from memory

what are the 3 types of schedulers?

short-term (RAM) Medium (swap) Long (Disk)

Remote Procedure Call (RPC) vs socket

socket is low level (stream of characters) rpc is high level (well-structured packet)

Peterson's Solution

solves critical section problems if load/stores are atomic where you have variable turn and array (flag) to signal it is ready to enter. The processes take turns entering //shared memory int turn = 0; bool flag[2] = { false , false} //for some process i do { flag[i] = true turn = j; while (flag[j] && turn == j) //critical section flag[i] = false //remainder section }while true

how to avoid busy waiting with semaphore?

use wait semaphore to store processes in a struct list and then block process until the signal semaphore is used to increment and wakeup the process

What to do instead of busy waiting?

use wait() to halt thread until true

Given that you have test_and_set support, implement a mutex (you may use the following struct and method headers in your implementation): struct mutex_lock struct mutex_lock create_mutex() void acquire(mutex_lock* m) void release(mutex_lock* m)

struct mutex_lock { boolean lock;}; struct mutex_lock create_mutex() { struct mutex_lock m; m.lock = false; r return m;} void acquire(mutex_lock* m) { while (test_and_set(&m->lock));} void release(mutex_lock* m) { m->lock = false;}

mutex lock

term mutex is short for mutual exclusion.) We use the mutex lock to protect critical regions and thus prevent race conditions. That is, a process must acquire the lock before entering a critical section; it releases the lock when it exits the critical section. uses: aquire() - blocks further access release() - releases the lock

which of these can run in parallel? for (int i = 0; i < n; i++) sum += d[i]; for (int i = 0; i < n; i++) c[i ] = a[i] + b[i]

the first cannot since the for loop depends on sum the second one can be ran in parallel

thread pooling is?

the method of encapsulating the creation and destruction of threads. Maintain a pool of thread not running but existing.

asymmetric communication

the receiver will accept from anyone and record where it came from (pid)


Ensembles d'études connexes

Business Communications- Chapter 6

View Set

Chapter 3: Organizational Climate and Culture

View Set

Chapter 4: Credit Risk Analysis and Interpretation

View Set

WEEK 3:: PYTHON CRASH COURSE : LOOPS, WHILE LOOPS

View Set

Chapter 8 - Management Structures & Organization - Complete

View Set

05.F BIO, HN The Calvin Cycle (PART F)

View Set

Lesson 9/Chapter 21: The Respiratory System

View Set