Podcast
Questions and Answers
Which of the following best describes the role of the kernel in a computer system?
Which of the following best describes the role of the kernel in a computer system?
- It is a high-level programming language used for application development.
- It acts as an intermediary between hardware and software, managing system resources. (correct)
- It is the central processing unit (CPU) responsible for all calculations.
- It is an external device that manages peripheral connections.
Which of the following is NOT a typical function performed by a kernel?
Which of the following is NOT a typical function performed by a kernel?
- Process management
- Directly executing user applications (correct)
- File system management
- Memory management
In a Linux filesystem, what does the root directory signify and how is it represented?
In a Linux filesystem, what does the root directory signify and how is it represented?
- The base of the filesystem's tree structure, represented by `/`. (correct)
- The location for temporary files, represented by `.`.
- The user's home directory, represented by `~`.
- The starting point of the directory hierarchy, represented by `\`.
Which command is used in Linux to list the contents of a directory?
Which command is used in Linux to list the contents of a directory?
What is the primary purpose of the sudo
command?
What is the primary purpose of the sudo
command?
In the context of compiling C code with gcc
, what is the function of the -o
option?
In the context of compiling C code with gcc
, what is the function of the -o
option?
How does a program typically view memory?
How does a program typically view memory?
Which memory section is responsible for storing initialized global variables?
Which memory section is responsible for storing initialized global variables?
Which of the following describes how the stack grows in memory?
Which of the following describes how the stack grows in memory?
What happens when a program attempts to access a memory address that is not mapped?
What happens when a program attempts to access a memory address that is not mapped?
How do static libraries differ from shared libraries?
How do static libraries differ from shared libraries?
What is the key difference between static and dynamic events in the context of C programming?
What is the key difference between static and dynamic events in the context of C programming?
In C, if an integer variable exceeds its maximum value, what behavior can be expected?
In C, if an integer variable exceeds its maximum value, what behavior can be expected?
What is the role of the &
operator when used with a variable in C?
What is the role of the &
operator when used with a variable in C?
What is the purpose of the #include
preprocessor directive in C?
What is the purpose of the #include
preprocessor directive in C?
What is a ternary operator and what symbols does it use?
What is a ternary operator and what symbols does it use?
What are the three essential components of a function in C?
What are the three essential components of a function in C?
What is the purpose of a function prototype?
What is the purpose of a function prototype?
How does passing by value differ from passing by reference?
How does passing by value differ from passing by reference?
What is recursion?
What is recursion?
Flashcards
What is the kernel?
What is the kernel?
The kernel is like the 'brain' of the computer, it manages resources and performs many functions.
Kernel functions?
Kernel functions?
The kernel performs memory management, process management, input/output management and file system management.
What is a filesystem?
What is a filesystem?
A hierarchical structure that organizes files and directories. Represented by a tree structure with /
as the root.
What does sudo
do?
What does sudo
do?
Signup and view all the flashcards
What is gcc
?
What is gcc
?
Signup and view all the flashcards
How programs see memory?
How programs see memory?
Signup and view all the flashcards
Memory sections?
Memory sections?
Signup and view all the flashcards
Memory section storage?
Memory section storage?
Signup and view all the flashcards
What is the heap?
What is the heap?
Signup and view all the flashcards
What is the stack?
What is the stack?
Signup and view all the flashcards
Program address space?
Program address space?
Signup and view all the flashcards
Static libraries?
Static libraries?
Signup and view all the flashcards
Shared libraries?
Shared libraries?
Signup and view all the flashcards
Static vs Dynamic events?
Static vs Dynamic events?
Signup and view all the flashcards
Integer overflow in C?
Integer overflow in C?
Signup and view all the flashcards
printf()
vs scanf()
?
printf()
vs scanf()
?
Signup and view all the flashcards
What does &
do?
What does &
do?
Signup and view all the flashcards
Format specifier symbol?
Format specifier symbol?
Signup and view all the flashcards
What does #include
do?
What does #include
do?
Signup and view all the flashcards
What does #define
do?
What does #define
do?
Signup and view all the flashcards
Study Notes
- The midterm is comprehensive.
- Review all covered concepts and labs/code from class.
- Questions regarding covered code may be asked, but there is no coding required.
- The exam contains ~30 multiple-choice and ~5 open-ended questions, with a mix of theoretical and practical content.
Lecture 1
- The kernel in a computing system is practically like the brain performing functions.
Kernel Functions
- Medium between hardware and software.
- Memory management.
- Process management.
- Input/output management.
- File system management.
- The filesystem is represented as a tree.
- The root directory is represented by a forward slash /.
Common Linux Commands
ls
- list directory contentscd
- change directorymkdir
- make directorymv
- move files or directoriescp
- copy files or directoriesscp
- secure copy (for copying files over SSH)tar
- archive utilityrm
- remove files or directorieschmod
- change file permissionscat
- concatenate and display filesgrep
- search for patterns in filesless
- view file contents page by pagemore
- view file contents page by pageman
- display manual pages
Sudo
- "Super user do"
- Allows running commands with elevated privileges.
GCC
- GCC is a built-in compiler
- Part of the GNU Compiler Collection
- Compiles source code into machine code.
- Compilation command:
gcc -o <output_file_name> <source_file.c>
. -o
specifies the output file name.
Lecture 2
- A program sees memory as an array of bytes, in the range of
0 - 2^64 - 1
. - Memory is organized into sections known as memory mappings.
Memory Sections
- TEXT: Stores instructions.
- DATA: Stores initialized global variables.
- BSS: Stores uninitialized global variables, initialized to zeros until a value is assigned.
- HEAP: Memory returned by
malloc/new
. - STACK: Stores local variables and return addresses.
- Shared libraries (.so files): Not added to the executable file and dynamically loaded at runtime, consist of definitions.
- Heap: Space for dynamic memory allocation, grows upward.
- Stack: Stores local variables, grows downwards.
- Programs have their own memory view, independent of others.
- Memory sections can have gaps without memory mapping.
- Attempting to access a memory gap results in a SEGV signal, killing the program and dumping a core file.
Libraries
- Static Libraries: Not shared, one instance per process, represented as
.a
files added to the executable. - Shared Libraries: Shared across processes, only one instance for the entire system.
Events
- Static: Happen during program building.
- Dynamic: Happen while a program is running.
C Variable Types
int
char
double
pointer
string
float
short
long
unsigned
- Integer overflow in C is handled via two's complement, wrapping to the beginning of the value range.
- Inputting 2,147,483,648 results in an output of -2,147,483,648.
Lecture 3
Functions
- printf(): Prints variables and expressions to standard output.
- scanf(): Takes input from standard input.
- &: Address of operator and acquires memory address of a paired variable.
- %: Format specifier for printing and scanning.
#include
: Declares preprocessor directives for libraries or files used in code.#define
: Preprocessor directive used to create macros.- Macros: Constant values or expressions substituted into code before compilation.
Lecture 4
- Ternary expressions are single-line if-else statements.
- Ternary symbols:
?
(if-then) and:
(otherwise). - Ternary expression formats:
variable = (condition) ? Expression2 : Expression3;
(condition) ? (Expression2) : (Expression3);
variable = Expression1 ? Expression2 : Expression3;
Lecture 5
- Functions: Blocks of code that perform specific tasks when called.
Function Aspects
- Declaration
- Definition
- Function Call
- Function prototypes tell the compiler about a function's existence and how it can be called.
- Without a prototype, an implicit declaration warning occurs if a function isn't defined at the top.
- Prototypes tell the compiler the function exists elsewhere and provide its signature (return type, name, parameters).
Passing parameters
- Passing by value: values passed to functions have their values copied (new memory is created to store them for the duration of the function call)
- Passing by reference: the function has the ability to operate on the original data
####Recursion
- Recursion is a technique where a function calls itself to solve smaller problems.
- Recursive functions typically call themselves in their own function body and need a base case (condition)
Lecture 6
- Pointers are variables that point to a memory address.
- Memory: 8 bytes for 64-bit systems, 4 bytes for 32-bit systems
- Integer: 4 bytes
- Character: 1 byte
*
(asterisk) operator: Dereferences a pointer to access its memory address- Pointers store: The memory addressed they are pointing to
Pointer Declaration and Arithmetic
int *ptr = &var
:*ptr = &var
points to the memory address of var.ptr
gives the memory address of var*ptr
gives the value at that memory address.
- Example: The difference between
(*ptr)++
and*ptr++
(*ptr)++ dereferences first and add 1 to the value, *ptr++ we move to the next memory address - Unary operators read right to left
Double Pointers
- Store the memory address of another pointer.
- Used in dynamic memory management, passing pointers to functions, or complex data structures.
- Consider
int var = 10;
int *ptr = &var;
int **dptr = &ptr;
- var is initialized to 10
- ptr is initialized to &var
- dptr is initialized to &ptr
- Dereferencing dptr twice will access var
Arrays
- When passing an array to a function, array decay occurs
- The array name becomes a pointer to the first element.
- Instead of copying the entire array, the function receives the memory address of the first element.
- The size of the array needs to be explicitly passed to the function.
- When passing an array to a function we only pass the memory address of the first element and so are essentially passing a pointer
Sizeof() and Pointers
sizeof()
: Returns the total bytes of a variable.- Used to determine array size via
sizeof(array) / sizeof(array[0])
. - Arrays passed to a function decay into pointers.
- Pointers can iterate through arrays with arithmetic.
- Add a value to a pointer to move X positions: ex.
*(ptr + 2)
Lecture 7
- The stack stores: information for passing control/data and allocating memory
The stack
- Function local variables
- Temporary variables
- Arguments for next functions
- Location where the last function ends
- Data is added to the stack using push, and removed using pop.
- Each time a function is called, the compiler manages the stack created during function call. Destroyed when function exits
Stack Issues
- Running out of stack space.
- Unintentionally changing values.
- Accessing memory after frame deallocation.
- Stack overflow occurs when the stack runs out of space, the program crashes with a segmentation fault or stack overflow error.
Buffer Overflow
- Buffer overflow occurs when a program/system process receives more data than anticipated.
- Pointers are more efficient for array indexing because they don't require multiplication.
Malloc
- To allocate a single large block of memory, dynamically use
malloc()
which returns a pointer to the memory block on the heap. - Must cast the appropriate pointer data type as malloc returns a pointer to void
Pointers
- Size: 4 bytes in a 32-bit system
- Equivalent ways of indexing when a
int a[20]
exists:A[i]
,*(a+i)
, and*(&a[0]+i)
.
Lecture 8
- To deallocate memory allocated by malloc or calloc, use
free()
. - This is to prevent memory leaks.
- Memory leaks are objects in memory that are no longer used by the program.
- Failing to deallocate memory can lead to memory leaks.
- Memory should not be accessed after its freed
- Double free is a free memory action that happens twice
- It could cause crashing or corruption and result in a memory leak
Lecture 9/10
- Draw multidimensional arrays with double/triple pointers
Lecture 11
- Premature frees happen when trying to prematurely free an object that a function still requires
- Double frees happen when trying to free an object that has already been freed
- Wild frees happen when a program attempts to free a pointer in memory that was not returned by malloc
- Memory smashing happens when less memory is allocated than the memory that is used
- Memory fragmentation occurs when there is aggregate heap memory but no single free block is large enough.
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.