Multithreading and Object Orientation Flashcards

1
Q

What is Multithreading?

A

Multithreading means several programs are running simulataneously. These programs are called threads.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How does an operating system handle multiple processes on computers with single and multi-core processors?

A

If the computer has multiple processors for processor, then multiple processes can be executed simultaneously. With a single processor it simulates parallel processing by switching so quickly between processes that the user thinks it is parallel computing.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What does the scheduler of Linux OS do?

A

It assigns CPU time to processes, and puts them to sleep if they are not ready to be executed, and coordiantes multithreading.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

When is multithreading useful?

A
  1. Implementing the UI and backend of a program as separate threads, the ui can be reactive to user input while the backend can be busy performing calculations.
  2. Shorter execution time via distributing long calculation over multiple threads using multi-core processors.
  3. Threads can wait during certain events.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

When to not use multithreading?

A

As multithreading is very difficult, avoid it when it’s not necessary.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What does the Global Interpreter Lock (GIL) do in Python, and how does it affect multithreaded Python programs?

A

A Python script runs in a Python interpreter rather than directly on the CPU or being managed by the operating system. The most common Python interpreter is single-threaded, meaning that even in multithreaded programs, only one CPU core is used. This limitation is due to the Global Interpreter Lock (GIL), which simplifies the interpreter’s implementation but restricts true parallel execution of threads.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is an issue that arises with multithreading.

A

Issues arise mostly when multiple threads need access to a single resource that cannot be shared.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What are some programming techniques for multithreading.

A

Coordinate when threads can access a resource, so they don’t access the resource at the same time

Make sure that the system operates smoothly so by making sure that access is managed in a fair way so threads can all gain access to the resource at some time. Also that the system does not hang up. This is acheived through synchronisation.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What are Locks?

A

Locks are a mechanism for threads to reserve a resource exclusively. While the resource is locked. no other thread can use the resource.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What are the advantages and disadvantages of Locks?

A

The advantage that lock gives is that it ensures that only one thread can access a critical section of code at a time which prevents race conditions and maintains data integrity.

The disadvantages of locks are that it adds overhead for each access to a resource even when collisions are very rare.

Also that some threads have to wait until a lock is released. If one of the threads holding a lock dies, stalls, blocks or enters an infinite loop, other threads waiting for the lock have to wait indefinitely until the computer is restarted.

Debugging, due to bugs associated with locks being time dependant and can be very subtle and hard to replicate such as deadlocks

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How does a sephamore work?

A

A private counter variable tracks the number of available resources.

A function “p” checks if a resource is available. If so, it decrements the counter and the program proceeds; otherwise, it waits until a resource is free.

A function “v” releases the resource and increments the counter when the program is done using it.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How does Interthread communication with conditions work?

A

Condition variables enable wait and notify operations, representing an event or resource. A process can use the wait operation to block or sleep until the event occurs or the resource becomes available. While one process waits, another can release the resource and use the notify function to wake up the waiting processes. Internally, condition variables rely on locking mechanisms, offering stronger encapsulation and abstraction compared to semaphores, with the notify function simplifying inter-process communication.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How does Inter-thread communication queues with work?

A

Inter-thread communication with conditions does not allow information to pass through. Queues is an effecient and easy way to pass information between threads and alos provide synchronisation. A queue is a data type that allows one process to add data into the queue, whereas another process can retrieve data from the queue. In case the buffer associated with the queue is full, a process adding data would wait. In case the buffer is empty, a process trying to retrieve data would wait as well.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What are the different types of data retrieval methods in queues, and how do they work?

A

FIFO: Data retrieved in the same order as it has been added to the queue.

LIFO: Data is retrieved in reverse order compared to how it has been added to the queue.

Priority Queue: Data stored in the queue is sorted by some key value. It is then retrieved in ascending or descending order, depending on how it is configured.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Multithreading with a timer

A

Timers allow for a thread or function to be called at refular time intervals. With multithreading, one thread could be created to accomplish a short piece of work. When the work is finish, after a set amount of time e.g 1s, the process is sent to sleep. When the thread wakes up again, it repeats the work. A second thread wakes up the first thread at regular intervals say 2s.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is Object Orientation and what is it’s purpose.

A

Object orientation solves the problem of understanding large programs. It is based on certain principles that support good software design and its main terms are class and object.

16
Q

What is the problem with large programs.

A
  1. Control flow is harder to understand so higher chance of programming errors
  2. Access of global variables and functions from many places so errors happen in one place and can have effects in distant place as well as being hard to track.
  3. Duplication of code with similar functionality so any change to the functionality requires changes across entire program.
  4. Without a good startegy to organise the code projects exceed planned budgets, milestones, they fail and if software is ineffecient, full of errors, difficult to change.
17
Q

How are functions and data organized in large programs, particularly in object-oriented programming?

A

Functions and data in large programs are not distributed randomly they are usually groups of functions that work with related data. In object-oriented programming functions and data can be grouped together in the source code to solve a common task these group of functions is a class and in a program there can be many classes.

18
Q

What is a benefit of OOP?

A

It adds another level of abstraction for designing software, it allows for writing bigger programs without getting lost.

19
Q

What is the common terminology in OOP.

A

The functions and variables of a class are called member. So member functions and member variables.

Class - A class contains the names of variables to store dta, but it does not store the values.

Object - An object stores the values for the variables in a class.

20
Q

What are the things to learn about OO Design

A

Ability to create classes and objects in a programming language.

Ability to structure an application problem into a simple set of classes and objects.

Principles and concepts being OO

21
Q

What are the main principles of OO

A

The main principles are

  • Encapsulation
  • Abstraction
  • Inheritance
  • Polymorphism
22
Q

What is Encapsulation and its advantages?

A

Encapsulation is a language mechanism to achieve the grouping of parts of the program and restricted access across parts.

The advantages are:

It reduces the complexity of possible interconnections of pieces of code.

Simplifies finding and correcting programming errors.

23
Q

What is Abstraction and its advantages?

A

Abstraction denotes the separation of the specification of the functionality from the algorithm used to implement it.

The programmer writes an ‘interface’ that describes the input and output of a part of the program.

  • The interface is an outside view of the functionality of a part of the program
  • Knowledge of the interface is sufficient to work with part of the program.
  • The implementation of the interface is of no concern to developers of other parts of the program.

The advantages of Abstraction are :

  • The implementation can be exchanged without changing the interface
  • Errors don’t spread easily across interfaces
  • Code is easier to understand and manage
24
Q

What is inheritance it’s stages and its advantage?

A

Inheritance is a language mechanism to reuse pieces of code for new purposes

Step 1: Create a piece of code with some useful functions

Step 2: Create a new piece of code and declare that it also contains all functions from step 1

Step 3: Add more functions to the second piece of code.

Result: Two pieces of code with some shared functionalityand some add-ons in the second piece.

Advantage of Inheritance:

Easy modelling of systems where one thing is a special type of another thing.

25
Q

What is polymorphism and its advantage.

A

Polymorphism means that the same names can be used to denote a particular, irrespective of differences in type.

Advantage: Functions can have the same name if they do the same thing.