OS & System Programming Flashcards
Revise This Module for Exams
What is Von Neumann Architecture also known as?
Stored-program computer
What is a Stored-program computer
A computer that stores program instructions in memory
Does Reprogramming require any change in hardware?
NO
What are the Architecture 3 main components?
- CPU
- Memory
- I/0 Interface
What is the CPU?
Heart of the Computing System
Includes:
- Control Unit (CU)
- Arithmetic Logic Unit (ALU)
What is Memory?
The computer’s memory stores program data & instructions
What I/O Interfaces?
They are used to receive or send information from the connection.
- Connected devices are called peripheral devices.
How are programs executed on Von Neumann Computers?
- 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
What is ALU?
Union of the circuits for performing Arithmetic & Logical Operations
What is CU role?
Responsible for step-by-step execution of instructions during a program execution.
What are Registers?
- Small storage element, located close to ALU
- Used as temporary storage during computation
- ALU can read & write from Registers very fast
What are the advantages of using Registers?
- 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
Does CPU need Registers?
No, can use memory to store all the data
How long does it take to read/write data?
4ms
How long does Arithmetic take?
1ms
How is Memory Organised?
- Memory consists of small ‘cells’
- Each cell stores a small piece of data
- cells have addresses 0 to n-1
Memory Access?
- Read & Stores operations, CPU generates addresses
- Memory Management unit reads or writes from/to memory location
Decimal Representation :
- Base 10 system
- 0-9 are digits
Binary Representation:
- Base 2 System
- digits are 0&1
- digits called ‘bits’
- 8 ‘bits’ are called a ‘byte’
eg: 1110 = 14
Hexadecimal Representation:
- Base 16 System
- Digits: 0,12,3,4,5,6,7,8, A,B, C,D, E, F
e.g : A3B = 2114
What is the General Structure of a C Program?
- Execution begins at main()
- Header of file contains imports / pre-defined libraries
Examples of Control Flows?
- if statements: conditional computation
- if-else : Multiple Branching
- Switch Statements
- For / while/ do-while loops
Basic if statement implementation?
if (condition) {
statement;
}
Example of if-else
if (condition) {
statement;
}
else if (condition) {
statement
}
else{
statement
}
Example of Switch
switch (condition){
case (A):
statement
break;
case (B):
statement
break;
default:
statement
break;
}
Example of for loop
for (int i; i < MAXRANGE; i++){
statement
}
Example of Conditional While Loop
while(condition){
statement
// Change condition or Not
}
Example of Infinite While Loop
while(1){
statement
// Can break the code
}
Example of Do While Loop
do {
statement
}
while (condition){
statement
}
// RUN THE CODE ONCE AND THEN DOES IT AGAIN IN WHILE LOOP
What does ‘continue;’ do?
- skips the current iteration
- Jumps to the beginning of the next iteration
What does ‘break;’ do?
- Comes out of the loop and the loop gets terminated.
Arrays in C?
int a[4] ; // Uninsialised
int b[3] = {2,3,4}
- Fixed-size
- Sequential collection of elements of the same type
What are errors with Arrays in C?
- Compiler does not check array limit
- Memory Protection was violated and the program crashed due to segmentation fault
How are Undefined Behaviours created?
- Out-of-bound access
- Signed int overflow
- Div by Zero
- UB hit, the program can theoretically do anything
Example of Function?
return_type func_name (arguments ) {
local variables declared
statements
return data of type declared
}
Format of printf() function:
printf(“%format”,variable_name);
format:
The Type for Variable name
What does printf() do?
- Prints values into the standard ouptut
- Defined in stdio.h library
What does scanf() do?
- Receive inputs from keyboards
- Defined in stdio.h library
Format of scanf() function:
scanf(“%format”,&variableName);
- format is placeholder for Data type
- &variableName is a pointer to store input into memory
What is a String?
-String of chars is a 1D array of chars
-Terminated by ‘\0’ == Null Char
- Can be read by scanf
What is strcpy()?
- Copies a string to another memory location / a list of chars
strcpy(newCharLoc, aString);
What is strcat()?
- concatenates 2 strings
strcat(string1, string2);
What is strlen()?
- Gets the length of the string
- strlen(string1);
What is strcmp()?
-Compares 2 Strings
- strcmp(str1,str2);
– str1 == str2 = 0
– str1 /= str2 = 1/-1
What is a String Problem?
- 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
Solutions to String Problems?
- The partial solution is to use the ‘safe’ function
What is the difference between C and Java?
Java:
- Memory allocated by compiler & length of string checked at runtime
- Buffer overflows are impossible
Example of Safe String Functions?
- 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))
Pitfalls of Strings?
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.
What is a Pointer?
- A variable that contains the address of a variable
- They can be passed into a function as an argument
- Only do pass-by-ref
What is & / ‘Address of’?
p = &c
- Assigns Address of c to p
What is * “INDRIECTION / DEREFERENCING”?
c = *p
- *p hold the data c has
How to declare the pointer?
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]
What is scale factor?
No. of Bytes used by the Data type
What does p++ or p+1 do is p is a pointer?
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
What is the array name equal to?
It is an Address
What is Array Name synonymous with?
Location of the Initial Element
int *p = &a[0] <==> int *p = a
Can A pointer with direct Initalization be changed?
No, causes Segmentation fault & program crashes
example:
char *ptr = “Comp Sci”
Format for printing pointers?
print(“%p”p1);
Format for Returning Pointers?
type int *fun() {
Statement;
}
Define scope?
Part of program where variables can be used
What is held in Text or Code Segement?
Contains executable instructions/Programs
What does the Data Segments Contain?
- Initalized and Unintialized Global & Static Variables
What are static variables?
- Stores in data segment NOT STACK
- Preserve their values even after they are out of their scope
What are Global Variables?
- Declared outside all functions
- Can be read & Modified by all functions
- Don’t name Local Vars the same as Global Vars
What Does Stack Segment Store?
All Local or Automatic Variable , Arguments of Functions are also stored in stack.
What does Stack Segment do?
- 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
What are Local Variables?
Scope is within function & allocated in the stack frame of the function
What does the Heap Segment store?
The storage of the dynamic memory allocation
What does .malloc() do?
- 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
What happens if dynamically allocated memory is not freed?
- 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
What can I use to check for Memory Leaks?
Use Valgrind
What happens if you access memory after freeing it?
-Memory is not owned by the program
- Re-accessing the freed memory can cause UNDEFIEND BEHAVIOUR (UB)
DON’Ts of Freeing?
- Don’t free something that didn’t come from Malloc
- Don’t free the same thing twice
Does C have Classes?
No
What does C have instead of Classes?
Structs / Structures
{User-defined Data Types }
What does Struct do?
Groups Items of ‘Possibly’ differnet types into a single type
Format of Struct?
struct tag_name {
T1 member1;
T2 member2;
}tag_name;
// Need a separate function to initalsie the members in the struct
What is a Void Pointer?
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
Format of a Function pointer?
type (*func) (arguments);
// func is a pointer to a function
What does scanf() do?
Takes Inputs until space
What does printf() do?
Outputs values
What does getchar() do?
Gets a Char from a Standard Input
What does putchar() do?
Writes a Char from a Standard Output
What is the getline() format?
getline(input, bufferSize, where its from)
What does getline() do?
- Reads entire Line
- Must be freed at the end
How to open a file in c?
FILE *fp ; // fp is a pointer to the type FILE
fp = fopen(“filename”,”mode”);
// must close it after use
close(fp)
What happens when fp = NULL in fopen?
It was unable to open the specified file
What is the function of File Read (R)?
- File exists, opened as read-only
- sets a pointer, that points to the first char
- Else Errors
What is the function of File Read (R+)?
- Opens file for both reading & writing existing file
- Does not delete the content of the existing file
What is the function of File Write (W)?
- If the file exists it gets overwritten
- Else creates a new one
What is the function of File Write (W+)?
- Opens a reading & writing a new file
- Overwrite if file exists
What is the function of File Appending (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
What is the function of File Appending (A+)?
- Open file for reading/writing from the last char in file
What does getc() do?
Reads a char from a file
What does putc() do?
Write a char to a file
What does fprintf() do?
Writes into file with file pointer
what does fscanf() do?
Reads into file with file pointer
What are possible Errors for Files in C?
- 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
What is Random Access to File?
Can directly jump to a type byte-number in a file without reading previous data using fseek()
What does fseek(fp,-m,1)
Moves back by m bytes from the current byte
What does fseek(fp,m,0)
Moves to the m+1 th byte in the file
What are Command Line Arguments?
Arguments inputted on cmd line that the program has requested or been modified to recieve
What is argc?
- An int
- Stores the number of arguments on cmd line passed by user
What is argv?
- Argument Vector
- Array of all the arguments (Excluding white space)
- argv[0] – Name of program
– argv[1] – 1st Argument
What is a Makefile?
- Compiles Multiple C files, along with additional steps to check for errors
- Can also make warnings appear as errors as C has to be very accurate
- Links all Files for full executable code.
- Don’t need to compile code individually can be done all at once
MakeFile Format?
<target>: <file0> <file1> ... <command></command>
</file1></file0></target>
Typical File Types in MakeFile
- .c (for C)
- .h
- .o (Compiled Machine code)
What does preprocessor do in C?
Runs before compilation & does text situations
- Like pasting file locations of the libraries
What is a Macro?
USEFUL TO AVOID CALL OVERHEAD FOR SMALL FUNCTIONS BUT USE WITH CARE
- If the function is going to be used multiple time
Format of Macro?
define MACRO(x) ((x) = (x) + 5 )
Format of Constant
define CONSTANTS 3.14159