OS & System Programming Flashcards

Revise This Module for Exams

1
Q

What is Von Neumann Architecture also known as?

A

Stored-program computer

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

What is a Stored-program computer

A

A computer that stores program instructions in memory

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

Does Reprogramming require any change in hardware?

A

NO

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

What are the Architecture 3 main components?

A
  • CPU
  • Memory
  • I/0 Interface
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is the CPU?

A

Heart of the Computing System

Includes:
- Control Unit (CU)
- Arithmetic Logic Unit (ALU)

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

What is Memory?

A

The computer’s memory stores program data & instructions

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

What I/O Interfaces?

A

They are used to receive or send information from the connection.

  • Connected devices are called peripheral devices.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How are programs executed on Von Neumann Computers?

A
  • Program is stored in memory
  • Translated in Machine code

1) CU understands that data needs to be provided by the user.

2) Data is Read from input device & bought to the CPU

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

What is ALU?

A

Union of the circuits for performing Arithmetic & Logical Operations

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

What is CU role?

A

Responsible for step-by-step execution of instructions during a program execution.

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

What are Registers?

A
  • Small storage element, located close to ALU
  • Used as temporary storage during computation
  • ALU can read & write from Registers very fast
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What are the advantages of using Registers?

A
  • Fast, does computation in Registers without constantly storing new values
  • Improves processing speed
  • Allows us to read data from memory once(Registers Store Immediate Data)
  • Reading Registers tend to have zero-time overhead
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Does CPU need Registers?

A

No, can use memory to store all the data

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

How long does it take to read/write data?

A

4ms

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

How long does Arithmetic take?

A

1ms

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

How is Memory Organised?

A
  • Memory consists of small ‘cells’
  • Each cell stores a small piece of data
  • cells have addresses 0 to n-1
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

Memory Access?

A
  • Read & Stores operations, CPU generates addresses
  • Memory Management unit reads or writes from/to memory location
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

Decimal Representation :

A
  • Base 10 system
  • 0-9 are digits
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

Binary Representation:

A
  • Base 2 System
  • digits are 0&1
  • digits called ‘bits’
  • 8 ‘bits’ are called a ‘byte’

eg: 1110 = 14

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

Hexadecimal Representation:

A
  • Base 16 System
  • Digits: 0,12,3,4,5,6,7,8, A,B, C,D, E, F

e.g : A3B = 2114

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

What is the General Structure of a C Program?

A
  • Execution begins at main()
  • Header of file contains imports / pre-defined libraries
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

Examples of Control Flows?

A
  • if statements: conditional computation
  • if-else : Multiple Branching
  • Switch Statements
  • For / while/ do-while loops
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

Basic if statement implementation?

A

if (condition) {
statement;
}

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

Example of if-else

A

if (condition) {
statement;
}
else if (condition) {
statement
}
else{
statement
}

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

Example of Switch

A

switch (condition){
case (A):
statement
break;
case (B):
statement
break;
default:
statement
break;
}

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

Example of for loop

A

for (int i; i < MAXRANGE; i++){
statement
}

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

Example of Conditional While Loop

A

while(condition){
statement
// Change condition or Not
}

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

Example of Infinite While Loop

A

while(1){
statement
// Can break the code
}

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

Example of Do While Loop

A

do {
statement
}
while (condition){
statement
}

// RUN THE CODE ONCE AND THEN DOES IT AGAIN IN WHILE LOOP

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

What does ‘continue;’ do?

A
  • skips the current iteration
  • Jumps to the beginning of the next iteration
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
31
Q

What does ‘break;’ do?

A
  • Comes out of the loop and the loop gets terminated.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
32
Q

Arrays in C?

A

int a[4] ; // Uninsialised

int b[3] = {2,3,4}

  • Fixed-size
  • Sequential collection of elements of the same type
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
33
Q

What are errors with Arrays in C?

A
  • Compiler does not check array limit
  • Memory Protection was violated and the program crashed due to segmentation fault
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
34
Q

How are Undefined Behaviours created?

A
  • Out-of-bound access
  • Signed int overflow
  • Div by Zero
  • UB hit, the program can theoretically do anything
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
35
Q

Example of Function?

A

return_type func_name (arguments ) {
local variables declared
statements
return data of type declared
}

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

Format of printf() function:

A

printf(“%format”,variable_name);

format:
The Type for Variable name

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

What does printf() do?

A
  • Prints values into the standard ouptut
  • Defined in stdio.h library
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
38
Q

What does scanf() do?

A
  • Receive inputs from keyboards
  • Defined in stdio.h library
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
39
Q

Format of scanf() function:

A

scanf(“%format”,&variableName);

  • format is placeholder for Data type
  • &variableName is a pointer to store input into memory
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
40
Q

What is a String?

A

-String of chars is a 1D array of chars
-Terminated by ‘\0’ == Null Char
- Can be read by scanf

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

What is strcpy()?

A
  • Copies a string to another memory location / a list of chars

strcpy(newCharLoc, aString);

42
Q

What is strcat()?

A
  • concatenates 2 strings

strcat(string1, string2);

43
Q

What is strlen()?

A
  • Gets the length of the string
  • strlen(string1);
44
Q

What is strcmp()?

A

-Compares 2 Strings

  • strcmp(str1,str2);
    – str1 == str2 = 0
    – str1 /= str2 = 1/-1
45
Q

What is a String Problem?

A
  • Ensure that sufficient memory is allocated at runtime (Not done in the compiler)
  • No checks at run-time, hence common source of errors
  • ‘Buffer Overflow’ has been exploited to achieve execution code supplied by the attacker
46
Q

Solutions to String Problems?

A
  • The partial solution is to use the ‘safe’ function
47
Q

What is the difference between C and Java?

A

Java:
- Memory allocated by compiler & length of string checked at runtime

  • Buffer overflows are impossible
48
Q

Example of Safe String Functions?

A
  • Checks length so buffer doesn’t overflow
  • strncmp:
    • strncmp(str1,str2, sizeOf(str1));
  • strncpy:
    • strncpy(str1,str2, sizeOf(str1));
  • strncat:
    • strncat(str1,str2, sizeOf(str1))
49
Q

Pitfalls of Strings?

A

The string is length 10, will need a list of size 11 for the null char

If string is length 11 but the new list of char has space 8 because it does not have enough space for remaining char.

50
Q

What is a Pointer?

A
  • A variable that contains the address of a variable
  • They can be passed into a function as an argument
  • Only do pass-by-ref
51
Q

What is & / ‘Address of’?

A

p = &c
- Assigns Address of c to p

52
Q

What is * “INDRIECTION / DEREFERENCING”?

A

c = *p
- *p hold the data c has

53
Q

How to declare the pointer?

A

Type *p; // Declares pointer
int c = 5 , d , e[3];
int *p;

p = c; // p points to c
d = *p; // d has value 5
p = &e[0] // p points to e[0]

54
Q

What is scale factor?

A

No. of Bytes used by the Data type

55
Q

What does p++ or p+1 do is p is a pointer?

A

Points to the next object of the same type:

increments by:
- 4 when p is a float/int pointer
- 1 when p is a char pointer

56
Q

What is the array name equal to?

A

It is an Address

57
Q

What is Array Name synonymous with?

A

Location of the Initial Element

int *p = &a[0] <==> int *p = a

58
Q

Can A pointer with direct Initalization be changed?

A

No, causes Segmentation fault & program crashes

example:
char *ptr = “Comp Sci”

59
Q

Format for printing pointers?

A

print(“%p”p1);

60
Q

Format for Returning Pointers?

A

type int *fun() {
Statement;
}

61
Q

Define scope?

A

Part of program where variables can be used

62
Q

What is held in Text or Code Segement?

A

Contains executable instructions/Programs

63
Q

What does the Data Segments Contain?

A
  • Initalized and Unintialized Global & Static Variables
64
Q

What are static variables?

A
  • Stores in data segment NOT STACK
  • Preserve their values even after they are out of their scope
65
Q

What are Global Variables?

A
  • Declared outside all functions
  • Can be read & Modified by all functions
  • Don’t name Local Vars the same as Global Vars
66
Q

What Does Stack Segment Store?

A

All Local or Automatic Variable , Arguments of Functions are also stored in stack.

67
Q

What does Stack Segment do?

A
  • Gives each function call, its own stack frame
  • Stack grows from High to Low Address
  • After function returns values, the stack frame is released and all the local variables die
68
Q

What are Local Variables?

A

Scope is within function & allocated in the stack frame of the function

69
Q

What does the Heap Segment store?

A

The storage of the dynamic memory allocation

70
Q

What does .malloc() do?

A
  • Allocates requested number of bytes of contiguious memory from the heap
  • Returns a pointer of the first byte of allocated space
  • Memory Allocation fails, will return NULL POINTER
71
Q

What happens if dynamically allocated memory is not freed?

A
  • Memory will Leak
  • Memory not returned to Heap, then the program grow larger
  • Large programs cause slowdowns in system performance by reducing amount of avaliable memory
72
Q

What can I use to check for Memory Leaks?

A

Use Valgrind

73
Q

What happens if you access memory after freeing it?

A

-Memory is not owned by the program

  • Re-accessing the freed memory can cause UNDEFIEND BEHAVIOUR (UB)
74
Q

DON’Ts of Freeing?

A
  • Don’t free something that didn’t come from Malloc
  • Don’t free the same thing twice
75
Q

Does C have Classes?

A

No

76
Q

What does C have instead of Classes?

A

Structs / Structures
{User-defined Data Types }

77
Q

What does Struct do?

A

Groups Items of ‘Possibly’ differnet types into a single type

78
Q

Format of Struct?

A

struct tag_name {
T1 member1;
T2 member2;
}tag_name;

// Need a separate function to initalsie the members in the struct

79
Q

What is a Void Pointer?

A

Don’t know the type you want to point to

  • You have to cast the pointer to the correct type before you will use it
80
Q

Format of a Function pointer?

A

type (*func) (arguments);

// func is a pointer to a function

81
Q

What does scanf() do?

A

Takes Inputs until space

82
Q

What does printf() do?

A

Outputs values

83
Q

What does getchar() do?

A

Gets a Char from a Standard Input

84
Q

What does putchar() do?

A

Writes a Char from a Standard Output

85
Q

What is the getline() format?

A

getline(input, bufferSize, where its from)

86
Q

What does getline() do?

A
  • Reads entire Line
  • Must be freed at the end
87
Q

How to open a file in c?

A

FILE *fp ; // fp is a pointer to the type FILE

fp = fopen(“filename”,”mode”);

// must close it after use
close(fp)

88
Q

What happens when fp = NULL in fopen?

A

It was unable to open the specified file

89
Q

What is the function of File Read (R)?

A
  • File exists, opened as read-only
  • sets a pointer, that points to the first char
  • Else Errors
90
Q

What is the function of File Read (R+)?

A
  • Opens file for both reading & writing existing file
  • Does not delete the content of the existing file
91
Q

What is the function of File Write (W)?

A
  • If the file exists it gets overwritten
  • Else creates a new one
92
Q

What is the function of File Write (W+)?

A
  • Opens a reading & writing a new file
  • Overwrite if file exists
93
Q

What is the function of File Appending (A)?

A
  • Opens file if it exits
  • Otherwise create a new one
  • Sets a pointer to the last char
  • New content appears after the old one
94
Q

What is the function of File Appending (A+)?

A
  • Open file for reading/writing from the last char in file
95
Q

What does getc() do?

A

Reads a char from a file

96
Q

What does putc() do?

A

Write a char to a file

97
Q

What does fprintf() do?

A

Writes into file with file pointer

98
Q

what does fscanf() do?

A

Reads into file with file pointer

99
Q

What are possible Errors for Files in C?

A
  • Reaching beyond End-of-file
  • Uses a file that has not been opened
  • Opens a file with an invalid file name
  • Opens a file not permitted by fopen
  • Write to write-protected file
100
Q
A