Linux Kernel and Commands

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to Lesson

Podcast

Play an AI-generated podcast conversation about this lesson

Questions and Answers

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?

  • 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?

  • 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?

<p><code>ls</code> (B)</p> Signup and view all the answers

What is the primary purpose of the sudo command?

<p>To execute a command with elevated privileges. (B)</p> Signup and view all the answers

In the context of compiling C code with gcc, what is the function of the -o option?

<p>It specifies the output file name. (A)</p> Signup and view all the answers

How does a program typically view memory?

<p>As a single, continuous array of bytes. (D)</p> Signup and view all the answers

Which memory section is responsible for storing initialized global variables?

<p>Data (C)</p> Signup and view all the answers

Which of the following describes how the stack grows in memory?

<p>Grows downwards, toward lower memory addresses. (C)</p> Signup and view all the answers

What happens when a program attempts to access a memory address that is not mapped?

<p>The operating system sends a segmentation fault signal, typically terminating the program. (C)</p> Signup and view all the answers

How do static libraries differ from shared libraries?

<p>Each process gets its own copy of a static library, while shared libraries can be used by multiple processes. (A)</p> Signup and view all the answers

What is the key difference between static and dynamic events in the context of C programming?

<p>Static events occur during program building, while dynamic events take place while the program is running. (A)</p> Signup and view all the answers

In C, if an integer variable exceeds its maximum value, what behavior can be expected?

<p>The value will wrap around to the minimum value due to two's complement representation. (D)</p> Signup and view all the answers

What is the role of the & operator when used with a variable in C?

<p>It retrieves the memory address of the variable. (C)</p> Signup and view all the answers

What is the purpose of the #include preprocessor directive in C?

<p>To insert the contents of a header file into the current source file. (D)</p> Signup and view all the answers

What is a ternary operator and what symbols does it use?

<p>A conditional expression using <code>?</code> and <code>:</code>. (C)</p> Signup and view all the answers

What are the three essential components of a function in C?

<p>Declaration, definition, and invocation. (C)</p> Signup and view all the answers

What is the purpose of a function prototype?

<p>To inform the compiler about the function's existence, return type, name, and parameters. (D)</p> Signup and view all the answers

How does passing by value differ from passing by reference?

<p>Passing by value creates a copy of the variable, while passing by reference allows the function to operate on the original variable. (A)</p> Signup and view all the answers

What is recursion?

<p>A function that calls itself to solve a smaller subproblem. (D)</p> Signup and view all the answers

Flashcards

What is the kernel?

The kernel is like the 'brain' of the computer, it manages resources and performs many functions.

Kernel functions?

The kernel performs memory management, process management, input/output management and file system management.

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?

sudo stands for 'Super User Do'. It is used to run commands with elevated privileges.

Signup and view all the flashcards

What is gcc?

gcc is a compiler. gcc -o output_file source_file.c compiles code. -o specifies the output file name.

Signup and view all the flashcards

How programs see memory?

A program sees memory as an array of bytes, ranging from 0 to 2^64 - 1.

Signup and view all the flashcards

Memory sections?

Memory is organized into sections called memory mappings.

Signup and view all the flashcards

Memory section storage?

TEXT: instructions, DATA: initialized globals, BSS: uninitialized globals, HEAP: dynamic memory, STACK: local variables/returns, Shared Libraries: shared code.

Signup and view all the flashcards

What is the heap?

Space for dynamic memory allocation that grows upward in memory.

Signup and view all the flashcards

What is the stack?

Stores local variables and return addresses; grows downwards in memory.

Signup and view all the flashcards

Program address space?

Each program has its own independent view of memory.

Signup and view all the flashcards

Static libraries?

Libraries that are not shared, represented as .a files. Exist as one instance per process.

Signup and view all the flashcards

Shared libraries?

Libraries shared across processes with only one instance for the system.

Signup and view all the flashcards

Static vs Dynamic events?

Events that happen during program building (static) or while the program is running (dynamic).

Signup and view all the flashcards

Integer overflow in C?

Overflowing the maximum value of an integer variable causes a rollover to the minimum value.

Signup and view all the flashcards

printf() vs scanf()?

printf() prints to the terminal, scanf() reads input from the console.

Signup and view all the flashcards

What does & do?

The & operator gets the memory address of a variable.

Signup and view all the flashcards

Format specifier symbol?

% is the format specifier symbol used for printing and scanning variables.

Signup and view all the flashcards

What does #include do?

#include declares preprocessor directives for libraries or files.

Signup and view all the flashcards

What does #define do?

#define is a preprocessor directive used to create macros.

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 contents
  • cd - change directory
  • mkdir - make directory
  • mv - move files or directories
  • cp - copy files or directories
  • scp - secure copy (for copying files over SSH)
  • tar - archive utility
  • rm - remove files or directories
  • chmod - change file permissions
  • cat - concatenate and display files
  • grep - search for patterns in files
  • less - view file contents page by page
  • more - view file contents page by page
  • man - 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.

Quiz Team

Related Documents

More Like This

Introduction to Linux Commands and Concepts
23 questions
Linux Commands and Kernel
20 questions
Linux Kernel and Commands
27 questions
Use Quizgecko on...
Browser
Browser