Linux Kernel & Commands: Midterm Review

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 component of a computer system is often referred to as the 'brain'?

  • The Hard drive
  • The Kernel (correct)
  • The Memory
  • The CPU

Which of the following is NOT a typical function performed by the kernel?

  • Memory management
  • Hardware manufacturing (correct)
  • Process management
  • Input/output management

In the context of a computer's file system, what symbol typically represents the root directory?

/

The command rm in Linux is used to create a new directory.

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

What does the abbreviation 'sudo' stand for in Linux?

<p>Super user do (D)</p> Signup and view all the answers

In the command gcc -o myprogram main.c, the -o option specifies the ______ file name.

<p>output</p> Signup and view all the answers

In C programming, how does a program view memory?

<p>As an array of bytes (D)</p> Signup and view all the answers

Memory mappings are the different sections in which memory is organized.

<p>True (A)</p> Signup and view all the answers

Match the memory sections with their descriptions:

<p>TEXT = Instructions that the program runs DATA = Initialized global variables BSS = Uninitialized global variables HEAP = Memory returned when calling malloc/new</p> Signup and view all the answers

Which memory section is used for dynamic memory allocation?

<p>Heap (D)</p> Signup and view all the answers

The stack grows upwards in memory.

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

What is meant by the 'address space' of a program?

<p>Each program has its own view on the memory that is independent of each other</p> Signup and view all the answers

What signal does the OS send when a program tries to access a memory gap?

<p>SIGSEGV (D)</p> Signup and view all the answers

Static libraries are shared across different processes.

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

Which type of events happen during program building?

<p>Static (A)</p> Signup and view all the answers

Which of the following is NOT a valid variable type in C?

<p>string (A)</p> Signup and view all the answers

How does C handle integer overflow?

<p>By wrapping around using two's complement (B)</p> Signup and view all the answers

printf() is used to take input from the console.

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

In C, what does the & operator do when used with a variable?

<p>acquire the memory address</p> Signup and view all the answers

Which symbol is used as the format specifier for printing and scanning variables in C?

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

The #include directive is used to define macros in C.

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

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

<p>create macros</p> Signup and view all the answers

What symbols are used in a ternary operator?

<p>? and : (D)</p> Signup and view all the answers

In C, what are the three essential aspects of a function?

<p>function declaration, function definition, and function call</p> Signup and view all the answers

What is the purpose of a function prototype in C?

<p>To tell the compiler about the function's existence and how it can be called (B)</p> Signup and view all the answers

Passing by value allows a function to operate on the original data of the passed variable.

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

How does recursion, as a programming technique, typically work?

<p>By having a function call itself (B)</p> Signup and view all the answers

What is a pointer in C programming?

<p>variable that points to an address in memory</p> Signup and view all the answers

How much memory does a pointer occupy in a 64-bit system, independent of the data type it points to?

<p>8 bytes (A)</p> Signup and view all the answers

In a 64-bit system, an integer always occupies 8 bytes of memory.

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

The * symbol is known as the ______ operator and is used to access the data stored at a memory address.

<p>dereference</p> Signup and view all the answers

What is typically stored in a pointer?

<p>A memory address (C)</p> Signup and view all the answers

The expressions (*ptr)++ and *ptr++ perform the same operation.

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

What is a double pointer?

<p>A pointer that stores the memory address of another pointer (C)</p> Signup and view all the answers

When passing an array to a function in C, what is actually being passed?

<p>memory address of the first element</p> Signup and view all the answers

Pointers are less efficient for array indexing.

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

How does the malloc function work?

<p>It dynamically allocates a block of memory. (A)</p> Signup and view all the answers

We use cast type because malloc always returns a pointer to ______

<p>void</p> Signup and view all the answers

Which of the following are equivalent to A[i] assuming int a[20]?

<p>both are correct (D)</p> Signup and view all the answers

Why is it important to free?

<p>good measure to prevent memory leaks (B)</p> Signup and view all the answers

What is a wild free?

<p>freeing a pointer in memory that was not returned by malloc (C)</p> Signup and view all the answers

Flashcards

What is the kernel?

Practically the brain of the computer, performing many functions.

Kernel functions

Acts as a medium between hardware and software; includes memory, process, input/output, and file system management.

What is a filesystem?

How the computer organizes and accesses files.

Root directory symbol

The root directory is represented by this symbol.

Signup and view all the flashcards

ls command

Lists contents of a directory.

Signup and view all the flashcards

cd command

Changes the current directory.

Signup and view all the flashcards

mkdir command

Creates a new directory.

Signup and view all the flashcards

mv command

Moves a file or directory.

Signup and view all the flashcards

cp command

Copies a file or directory.

Signup and view all the flashcards

scp command

Securely copies files between hosts.

Signup and view all the flashcards

tar command

Archives files into a single file (tape archive).

Signup and view all the flashcards

rm command

Removes files or directories.

Signup and view all the flashcards

chmod command

Changes file access permissions.

Signup and view all the flashcards

cat command

Displays the content of a file.

Signup and view all the flashcards

grep command

Searches for a pattern in files.

Signup and view all the flashcards

less command

Views a file one screen at a time.

Signup and view all the flashcards

more command

Same as less, but with more features

Signup and view all the flashcards

man command

Displays the user manual for command.

Signup and view all the flashcards

sudo

Stands for "Super User Do"; allows running commands with elevated privileges.

Signup and view all the flashcards

gcc

Built-in compiler, part of the GNU Compiler Collection, used to compile source code into machine code.

Signup and view all the flashcards

gcc -o flag

Specifies the output filename when compiling with gcc.

Signup and view all the flashcards

How program sees memory

An array of bytes.

Signup and view all the flashcards

Memory mappings

Sections into which memory is organized are known as...

Signup and view all the flashcards

TEXT Section

Stores instructions the program runs.

Signup and view all the flashcards

DATA Section

Stores initialized global variables.

Signup and view all the flashcards

BSS Section

Stores uninitialized global variables initialized to zero.

Signup and view all the flashcards

HEAP Section

Memory returned when calling malloc/new.

Signup and view all the flashcards

STACK Section

It stores local variables and return addresses.

Signup and view all the flashcards

What is the heap?

Space for dynamic memory allocation; grows upward.

Signup and view all the flashcards

What is the stack?

Stores local variables; grows downwards.

Signup and view all the flashcards

Address space

Each program has its independent view on memory.

Signup and view all the flashcards

Accessing memory gap

OS sends a SEGV signal, killing the program.

Signup and view all the flashcards

Static libraries

Libraries that are duplicated for ever process using them.

Signup and view all the flashcards

Shared libraries

Libraries shared by multiple processes.

Signup and view all the flashcards

Static events

Events during program building/compiling.

Signup and view all the flashcards

Dynamic events

Events that happen while program is running.

Signup and view all the flashcards

Integer overflow

C handles this by two's complement, wrapping to the beginning of the range.

Signup and view all the flashcards

printf()

Prints variables and expressions to output.

Signup and view all the flashcards

scanf()

Takes input from standard input (console).

Signup and view all the flashcards

& operator

Represents the address of a variable in memory.

Signup and view all the flashcards

Study Notes

  • This midterm is fully comprehensive of the content covered so far
  • Ensure you fill out the midterm study guide and familiarize yourself with all concepts
  • Review labs and codes covered in class; the exam will not include material from zybooks
  • Questions regarding covered code may be asked; you won't have to write code
  • Understanding how the codes work is essential
  • Code will be provided, and you will be asked for the output or to demonstrate operations
  • The exam consists of ~30 multiple-choice and ~5 open-ended questions
  • The exam is a mix between theoretical and practical questions

Lecture 1

  • The kernel of a computer system is analogous to the brain and performs many functions
  • Kernel functions include:
  • Act as the medium between hardware and software
  • Memory management
  • Process management
  • Input/output management
  • File system management
  • The file system is represented as a tree, with the forward slash / representing the root

Common Linux Commands

  • ls: list
  • cd: change directory
  • mkdir: make directory
  • mv: move
  • cp: copy
  • scp: secure copy
  • tar: tape archive
  • rm: remove
  • chmod: change mode
  • cat: concatenate
  • grep: global regular expression print
  • less: less is more
  • more: more is less
  • man: manual
  • sudo stands for "Super User Do"
  • Sudo elevates privileges to run commands

Compiling Code Using GCC

  • gcc is a built-in compiler that is part of the GNU Compiler Collection (GCC) to compile source code into machine code
  • gcc -o <output_file name> <source_file.c> compiles code using GCC
  • -o specifies the output file name

Lecture 2

  • A program sees memory as an array of bytes in the range of 0 to 2^64 - 1
  • Memory is organized into sections known as memory mappings
  • Segments and what they store:
  • TEXT: instructions that the program runs
  • DATA: initialized global variables
  • BSS: uninitialized global variables initialized to zeros until a value is assigned
  • HEAP: memory returned when calling malloc/new
  • STACK: local variables and return addresses
  • Shared Libraries: definitions that are dynamically loaded at runtime and not added to the executable file, so they don't add size to the executable file
  • Heap: space for dynamic memory allocation that grows upward
  • Stack: stores local variables and grows downwards
  • Each program has its own independent view of memory
  • Gaps between memory sections may not have any memory mapping
  • If a program tries to access a memory gap, the OS sends a SEGV signal, killing the program and dumping a core file

Static and Shared Libraries

  • Static Libraries:
  • Not shared; each process has its instance
  • Represented as .a files added to the executable
  • Shared Libraries:
  • Shared across different processes
  • Only one instance exists for the entire system
  • Static events happen during program building while dynamic events happen while the program is running
  • Types of variables in C programming:
  • int
  • char
  • double
  • pointer
  • string
  • float
  • short
  • long
  • unsigned

Integer Overflow

  • C handles integer overflow by using two's complement and wrapping back to the beginning of the value range
  • If 2,147,483,648 is inputed then the output would be -2,147,483,648

Lecture 3

  • printf() prints variables and expressions to standard output (terminal)
  • scanf() takes input from standard input (console)
  • & is the address operator
  • When paired with a variable, it returns the memory address
  • % is the symbol uses for printing and scanning format specifiers
  • #include declares preprocessor directives for using libraries or files within your code
  • #define is a preprocessor directive to create macros, which are constant values or expressions substituted during compilation

Lecture 4

  • A ternary expression is a single-line if-else statement
  • Two symbols used in a ternary expression are ? and :
  • ? can be considered "if a then b"
  • : can be considered "otherwise c"
  • Ternary expressions can be read in multiple ways
  • variable = (condition) ? Expression2 : Expression3;
  • (condition) ? (Expression2) : (Expression3);
  • variable = Expression1 ? Expression2 : Expression3;

Lecture 5

  • Functions perform a specific task when called, and are made up of three aspects:
  • Function declaration
  • Function definition
  • Function call
  • A function prototype tells the compiler about a function's existence and how it can be called
  • If a function is not defined at the top of the code without a function prototype a warning indicates calling a function without the compiler knowing what it looks like
  • A function prototype tells the compiler a function exists elsewhere, providing its signature (return type, name, parameters)
  • Passing by value means function parameters have their values copied, with new memory created for the function call's duration
  • Passing by reference allows the function to operate on the original data, modifying the value of a variable or avoiding copying large data structures
  • Recursion is a technique where a function calls itself to break down complex problems into simpler ones
  • A recursive function typically calls itself in its body
  • Recursive function structure: specifying type, function name, arguments, function statements, base condition and the recursive (recursive call)

Lecture 6

  • A pointer is a variable that points to an address in memory
  • independent of a casted data type, a pointer is:
  • 8 bytes in a 64 bit system
  • 4 bytes in a 32 bit system
  • An integer is 4 bytes in a 64-bit system
  • A character is 1 byte
  • The * operator is the dereference operator, used to access the data stored at a memory address
  • Pointers store a memory address
  • *ptr = &var: intializing ptr to the memory address of var
    • ptr alone produces the memory address of var
    • *ptr produces the value at that memory address
  • Pointer Arithmetic:
  • (*ptr)++ dereferences the pointer first and then increments the value contained
  • *ptr++ increments the pointer itself to point to the next memory address
  • Unary operators read right to left

Double Pointers

  • A double pointer stores the memory address of another pointer
  • Helpful for managing dynamically allocated memory, passing pointers to functions, or handling complex data structures
  • Code breakdown:
  • int var = 10; initializes var and assigns it the value 10
  • int *ptr = &var; initializes the pointer ptr and assigns it the memory address of var
  • int **dptr = &ptr; initializes the double pointer dptr and assigns it the memory address of ptr
  • Double pointer dereferencing accesses the contents of var
  • When passing an array to a function array decay occurs.
  • Instead of passing the entire array, the memory address of the first element is passed
  • Need to also pass the size of the array
  • Sizeof() returns total amount of bytes Size of an array can be determined with the following code: sizeof(array)/sizeof(array[0])
  • When passing an array to a function, the function receives the memory address of the first element so the array essentially becomes a pointer
  • To iterate an array using pointer arithmetic add a value to move x positions through the array to the pointer
  • The expression *(ptr + 2) encapsulates the pointer and returns the value at the index that is aimed at

Lecture 7

  • The stack stores information for passing control and data and stores:
  • Function local variables
  • Temporary variables
  • Arguments for the next function call
  • Where to return after the function ends
  • Push is how data is added to the stack
  • Removed with pop
  • The compiler manages the stack by:
  • Creating one stack frame each time a function is called
  • Creation when function called
  • Stacking on top
  • Destroying at function exist
  • Stack errors
  • Run out of stack space
  • Unintentionally change value to memory on the stack
  • Access memory even after the frame deallocation
  • When stack space runs out, the program crashes and a segmentation fault / stack overflow occurs
  • A buffer overflow occurs when more data than anticipated by a program or system process is placed on the stack, causing data to overflow

Array Indexing

  • Pointers are more efficient for array indexing because normal indexing requires multiplication, but with pointers there is no need to multiply to get the correct index
  • Malloc dynamically allocates a single large block of memory with specified size
  • The malloc function returns a suitably aligned pointer to a block of memory for containing any kind of data object
  • Memory allocated by malloc is stored in the heap
  • Malloc function returns a void pointer, so it will need to be casted to the appropriate pointer type for the data
  • A pointers size in a 32 but system is 4 bytes
  • There are all equivalent ways of indexing, understand why all of these are equivalent
  • Example of this is int a[20]
  • A[i] is equivalent to
  • *(a+i) is equivalent to
  • *(&a[0]+i) is equivalent to
  • Memory previously allocated needs to be deallocated using free, to prevent memory leaks
  • Memory leaks occur objects in memory that are no longer in use by the program but that are not freed

Lecture 9/10 And Beyond

  • Premature frees occur when an object that is still in use by the program is freed
  • Double frees occur when an object that is already free is freed again
  • Wild frees occur 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 will be used
  • Memory fragmentation: which occurs when there is enough aggregate heap memory, but no single free block is large enough
  • Memory can be ran out of in the heap, where malloc returns 0
  • A register can ve overwritten in code during execution leading to unintentionally changed heap data
  • Memory can be accessed after being freed
  • freeing memory twice can cause crashing and corruption
  • Creare a memory leak is also a risk when working with memory
  • To review are, review multidimensional arrays and how to draw multidimensional arrays, 2D and 3D using double pointers and triple pointers

Studying That Suits You

Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

Quiz Team

Related Documents

More Like This

Linux Kernel Introduction
33 questions

Linux Kernel Introduction

InvincibleBagpipes8262 avatar
InvincibleBagpipes8262
Introduction to Linux Operating Systems
13 questions
Introduction to Linux Commands and Concepts
23 questions
Use Quizgecko on...
Browser
Browser