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 serves as the primary interface for networking and internet connectivity.
- It directly manages user applications and their interfaces.
- It acts as an intermediary between hardware and software, managing system resources. (correct)
- It is responsible for compiling source code into executable programs.
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
- Memory management
- Web browsing (correct)
- File system 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 starting point for all files and directories, represented by a backslash `\`
- The core of the operating system, represented by `.`
- The top-level directory, represented by a forward slash `/` (correct)
- The user's home directory, represented by `~`
Which of the following Linux commands is used to create a new directory?
Which of the following Linux commands is used to create a new directory?
What does sudo
stand for in Linux, and what is its primary purpose?
What does sudo
stand for in Linux, and what is its primary purpose?
In the context of compiling C code with gcc
, what is the purpose of the -o
option?
In the context of compiling C code with gcc
, what is the purpose of the -o
option?
From a program's perspective, how is memory typically viewed, and what is its address range in a 64-bit system?
From a program's perspective, how is memory typically viewed, and what is its address range in a 64-bit system?
Which memory section is used to store initialized global variables in a C program?
Which memory section is used to store initialized global variables in a C program?
What is stored in the 'Stack' memory section?
What is stored in the 'Stack' memory section?
In terms of memory growth, how do the heap and stack typically expand?
In terms of memory growth, how do the heap and stack typically expand?
What is the likely outcome if a program attempts to access a memory address within a gap that has no memory mapping?
What is the likely outcome if a program attempts to access a memory address within a gap that has no memory mapping?
How do static libraries differ from shared libraries?
How do static libraries differ from shared libraries?
What distinguishes static events from dynamic events in programming?
What distinguishes static events from dynamic events in programming?
What is the consequence of an integer overflow in C, and how does C handle this?
What is the consequence of an integer overflow in C, and how does C handle this?
What is the purpose of the &
operator when used with a variable in C?
What is the purpose of the &
operator when used with a variable in C?
What is the function of the #include
preprocessor directive in C?
What is the function of the #include
preprocessor directive in C?
What is a ternary operator, and what symbols does it primarily use?
What is a ternary operator, and what symbols does it primarily use?
Why is a function prototype useful in C programming?
Why is a function prototype useful in C programming?
How does 'passing by value' differ from 'passing by reference' when passing arguments to a function?
How does 'passing by value' differ from 'passing by reference' when passing arguments to a function?
What is recursion, and how does a recursive function typically operate?
What is recursion, and how does a recursive function typically operate?
Flashcards
What is the kernel?
What is the kernel?
The core of a computer system, managing hardware and software interactions.
Kernel Functions
Kernel Functions
A medium between hardware and software. Manages memory, processes, input/output, and file systems.
What is a File System?
What is a File System?
A hierarchical structure for organizing files on a storage device. Root is represented by '/'.
What does 'sudo' stand for?
What does 'sudo' stand for?
Signup and view all the flashcards
What is GCC?
What is GCC?
Signup and view all the flashcards
Program Memory View
Program Memory View
Signup and view all the flashcards
Memory Sections
Memory Sections
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
Accessing a memory gap
Accessing a memory gap
Signup and view all the flashcards
Static vs. Shared Libraries
Static vs. Shared Libraries
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
What is a ternary operator?
What is a ternary operator?
Signup and view all the flashcards
What is a function?
What is a function?
Signup and view all the flashcards
Function Prototype
Function Prototype
Signup and view all the flashcards
Passing by value
Passing by value
Signup and view all the flashcards
Passing by reference
Passing by reference
Signup and view all the flashcards
Recursion
Recursion
Signup and view all the flashcards
Study Notes
General Exam Information
- The midterm is comprehensive
- Review class labs and code, Zybooks material are excluded from the exam
- Questions may involve code, but coding is not required
- Understanding code operation and output will be tested
- The exam consists of ~30 multiple choice and ~5 open ended questions
Lecture 1: The Kernel
- It acts as the brain
- It performs many functions within a computer system
- It acts as a medium between hardware and software
- It handles memory, process, input/output, and file system management
File System
- It is represented as a tree
- Root directory symbol: /
- A file system manages file organization and access
Common Linux Commands
- ls: Lists directory contents
- cd: Changes directory
- mkdir: Makes a new directory
- mv: Moves files or directories
- cp: Copies files or directories
- scp: Securely copies files between hosts
- tar: Archives files
- rm: Removes files or directories
- chmod: Changes file permissions
- cat: Concatenates and displays files
- grep: Searches for patterns in files
- less: Displays file contents one page at a time
- more: Displays file contents one page at a time (similar to less)
- man: Displays the manual page for a command
Sudo
- Stands for "Super User Do"
- Used to execute commands with elevated privileges
GCC
- It is a built-in compiler
- Part of the GNU Compiler Collection
- It compiles source code into machine code
- Compilation command:
gcc -o <output_file name> <source_file.c>
-o
specifies the output file name
Lecture 2: Memory
- A program views memory as an array of bytes ranging from 0 to 2^64 - 1
- Organized into memory mappings
Memory Sections
- TEXT: Stores the program's instructions
- DATA: Stores initialized global variables
- BSS: Stores uninitialized global variables (initialized to zero)
- STACK: Stores local variables and return addresses
- HEAP: Stores memory allocated by malloc/new
- Shared Libraries: (
.so
files) are not included in the executable and are dynamically loaded at runtime
Heap and Stack
- Heap: Allocates dynamic memory, grows upward in memory
- Stack: Stores local variables, grows downwards in memory
Program Address Space
- Each program has its own isolated memory view
Memory Gaps
- Gaps may exist between memory sections without memory mappings
- Attempting to access a memory gap triggers a SEGV signal, terminating the program and creating a core file
Static and Shared Libraries
- Static Libraries: Not shared, each process has its own instance with
.a
files added to the executable - Shared Libraries: Shared across processes, only one instance exists for the entire system
Static and Dynamic Events
- Static: Events during program building
- Dynamic: Events during program execution
C Variable Types
- int, char, double, pointer, string, float, short, long, unsigned
Integer Overflow
- C handles it by using two’s complement
- It wraps the value back to the beginning of the range.
- Inputting 2,147,483,648 results in an output of -2,147,483,648
Lecture 3: Printf and Scanf
- printf(): Prints variables and expressions to standard output (the terminal)
- scanf(): Reads input from standard input (the console)
& Operator
- It represents the address of a variable
- It is used to get the memory address of a variable
Format Specifier for Printing
- % (percent sign) is used for format specifiers in printing and scanning
#include
- It declares preprocessor directives to include libraries or files
#define
- Used to create macros
- Macros are constant values or expressions that are substituted during compilation
Lecture 4: Ternary Operator
- It is a one-line if-else
- Uses
?
and:
symbols ?
acts as "then,":
acts as "else."variable = (condition) ? Expression2 : Expression3
Lecture 5: Functions
- Functions: Code blocks for specific tasks
- Composed of: declaration, definition, call
- Function Prototype: Notifies the compiler of a function's existence and how to call it
- If function is not defined before being called, the compiler will throw an implicit declaration warning
- Using a function prototype resolves this issue
- It provides the function's signature: return type, name, parameters
Passing by Value vs. Passing by Reference
- Value: Copies the value to the function, creating new memory
- Reference: Passes a reference allows the function to operate on the original data which modify values or avoid copying large data structures
Recursion
- A function calls itself
- Allows breaking down complex problems
- Typically includes a base condition and a recursive call condition
Lecture 6: Pointers
- Points to a memory address
- Pointers size: 8 bytes on 64-bit systems and 4 bytes on 32-bit systems
Data Types and Pointers
- Integer Size: 4 bytes
- Character Size: 1 byte
Dereference Operator *
- Accesses data at a memory address
Pointers Store
- Memory addresses
int *ptr = &var
*ptr = &var
: makes ptr point to the memory address of varptr
: produces the memory address of var*ptr
: produces the value at the memory address of var
Pointer Arithmetic
(*ptr)++
: Increments the value at the pointed address*ptr++
: Moves the pointer to the next memory address because unary operates read right to left
Double Pointer
- Stores the memory address of another pointer
- Useful for dynamic memory management
Array Decay
- Occurs when an array is passed to a function
- Array name decays into a pointer to the first element of the array
- Only the memory address is passed, not a copy of the entire array
Array Size
- It is needed to be passed as additional information in function
Sizeof()
- Returns the total bytes of a type
- It is used to calculate the size of an array:
sizeof(array) / sizeof(array[0])
Passing Arrays
- When passing an array becomes a memory address
Array Iteration
- It is done via Pointer arithmetic
- To iterate, a value is added to the pointer to move positions
- Access data using
*(ptr + 2)
Lecture 7: Stack
- The stack stores information needed for control, data passing, and memory allocation
- It holds local and temporary variables, arguments for the next function call, and return addresses
Stack actions
- Push: Adds data
- Pop: Removes data
Stack Management
- Managed via the compiler
- Creates one stack frame per function call
- Stack frames are stacked on top of each other and destroyed at function exit
Stack Issues
- Running out of stack space
- Corrupting values
- In other functions frames
- In the function’s return address
- Accessing memory after deallocation
- Stack overflow
- The program cashes
Buffer Overflow
- Occurs when a program writes more data to a buffer than it can hold
Efficient Array Indexing
- Pointers are more efficient than normal indexing
Malloc
- Malloc dynamically allocates memory with a pointer
- Returns a pointer that is stored in the heap
Pointer Cast Type
- Requires a cast to the data type because malloc returns a void pointer
Pointer Size
- The size of a pointer on a 32 bit system is 4 bytes
Lecture 8: Indexing
- There are many equivalent ways
A[i]
is equivalent to*(a+i)
which is equivalent to*(&a[0]+i)
Deallocating Memory
- The memory must be cleared by
free()
to prevent memory leaks
Problems
- If memory is not deallocated then memory leaks can occur
Lecture 9/10 Heaps
- When the heap runs out of space
malloc
returns 0 - There can be changes to other heap data
- Or data clobber which is data that gets overwritten
- Memory can be accessed after it is freed, or freed twice
- Freeing memory twice lead to crashing or corruption
- Memory leaks can occur
Lecture 11: Common Frees
- Premature frees: when an object still in use is freed
- Double free: freeing an already freed object
- Wild frees: freeing a pointer not returned by malloc
- Memory smashing: allocating less memory than needed
- Memory fragmentation: insufficient contiguous memory despite enough aggregate memory
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.