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 the primary storage device for user data and applications.
  • It acts as an intermediary between hardware and software, managing resources. (correct)
  • It manages the computer's network connections and communications.
  • It is responsible for rendering graphics and handling display outputs.

The root directory in a filesystem is represented by a forward slash ''.

True (A)

Which command is used to create a new directory in Linux?

  • `mv`
  • `mkdir` (correct)
  • `ls`
  • `cd`

What does sudo stand for, and what is its primary function in a Linux system?

<p>Super User Do; to execute commands with elevated privileges.</p> Signup and view all the answers

In the context of compiling C code with gcc, what is the purpose of the -o flag?

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

A program sees memory as a single contiguous array of bits.

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

Match the following memory sections with their descriptions:

<p>TEXT = Instructions that the program runs. DATA = Initialized global variables. BSS = Uninitialized global variables, initialized to zeros. HEAP = Memory for dynamic allocation. STACK = Local variables and return addresses.</p> Signup and view all the answers

The ______ is a memory area used for dynamic memory allocation, while the ______ is used to store local variables and return addresses.

Signup and view all the answers

The root of the filesystem is represented by a backslash \.

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

What does the sudo command primarily achieve in Linux?

<p>It allows a user to execute commands with elevated privileges. (A)</p> Signup and view all the answers

The gcc -o <output_file> <source_file.c> command uses the -o flag to specify the ______ file name.

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

As seen by a program, what is memory primarily structured as?

<p>A contiguous array of bytes (B)</p> Signup and view all the answers

Match the following memory sections with their typical contents:

<p>TEXT = Executable instructions of the program DATA = Initialized global variables BSS = Uninitialized global variables STACK = Local variables and return addresses</p> Signup and view all the answers

What is the primary outcome when a program attempts to access a memory address outside of its allocated memory regions?

<p>Segmentation Fault</p> Signup and view all the answers

Which statement accurately describes how static and shared libraries differ in their utilization?

<p>Static libraries result in multiple instances of the library in each process, while shared libraries have one instance shared across processes. (C)</p> Signup and view all the answers

In C, an integer overflow is automatically detected and throws an exception to prevent incorrect calculations.

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

How does the #include preprocessor directive function in C?

<p>It includes the contents of a specified file or library into the current source file. (D)</p> Signup and view all the answers

Describe the function of a ternary operator and provide a general form of its syntax.

<p>A ternary operator is a shorthand way of expressing a simple <code>if-else</code> conditional statement in a single line. Its syntax is <code>condition ? expression_if_true : expression_if_false;</code>.</p> Signup and view all the answers

What is the principal role of a function prototype in C programming?

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

When passing parameters by _______, the function operates on a copy of the original data, whereas passing by _______ allows the function to directly modify the original data.

<p>value, reference</p> Signup and view all the answers

Recursion involves a function calling another function, creating a call stack that grows until a base case is met.

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

In C, what is the fundamental purpose of a pointer?

<p>To store the memory address of a variable directly. (D)</p> Signup and view all the answers

Match the following terms related to operators and pointers with their descriptions:

<p><code>*</code> = Dereference operator: accesses the value stored at a memory address <code>&amp;</code> = Address-of operator: retrieves the memory address of a variable <code>int *ptr</code> = Declaration of a pointer: creates a pointer variable that can store the address of an integer</p> Signup and view all the answers

Explain the difference in operation between (*ptr)++ and *ptr++ in C.

<p><code>(*ptr)++</code> increments the value that <code>ptr</code> points to, while <code>*ptr++</code> increments the pointer <code>ptr</code> itself to point to the next memory location.</p> Signup and view all the answers

What is 'array decay' when passing an array to a function in C?

<p>The implicit conversion of an array name to a pointer to its first element. (D)</p> Signup and view all the answers

The sizeof() operator, when applied to an array, returns the number of elements in the array.

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

In C, memory allocated using malloc is stored in the ______ section of memory.

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

Flashcards

What is the kernel?

The brain of the computer. Performs many OS functions.

Kernel functions

Acts as the medium between hardware and software. Manages memory, processes, input/output, and the file system.

What is a filesystem?

Represented as a tree. It dictates how the computer organizes and accesses files. The root is represented by '/'.

What is 'sudo'?

Super User Do. It allows you to run a command with elevated privileges.

Signup and view all the flashcards

What is GCC?

GCC is a built-in compiler, which is a part of the GNU Compiler Collection. It turns source code into machine code.

Signup and view all the flashcards

GCC '-o' meaning

The '-o' argument specifies the output file name in GCC.

Signup and view all the flashcards

Program's view of 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 are known as...

Memory mappings

Signup and view all the flashcards

Memory Section Storage

Text stores instructions, DATA stores initialized global variables, BSS stores uninitialized global variables, Heap stores memory from malloc/new, Stack handles local variables/returns, Shared Libraries consist of definitions loaded at runtime.

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?

It grows downwards and stores local variables and return addresses.

Signup and view all the flashcards

Program address space

Each program has its own isolated view of memory.

Signup and view all the flashcards

Memory gap access

If the program tries to access a memory gap, the OS will send a SEGV signal which terminates the program and may dump a core file.

Signup and view all the flashcards

Static vs Shared Library

Static libraries are not shared and are represented as (.a) files that are added to executable; shared libraries are shared across processes.

Signup and view all the flashcards

Static vs dynamic events

Static events occur during program building; dynamic events occur while the program is running.

Signup and view all the flashcards

What is a ternary?

A single line if/else statement. Uses '?' and ':'.

Signup and view all the flashcards

Function definition

Functions are blocks of code for specific tasks with a declaration, definition, and call.

Signup and view all the flashcards

Function Prototype

A prototype tells the compiler about a function's existence and calling method.

Signup and view all the flashcards

Pass by value vs reference

Passing by value copies data; passing by reference operates on original data.

Signup and view all the flashcards

What is recursion?

A technique where a function calls itself to solve smaller problems.

Signup and view all the flashcards

Study Notes

General Midterm Information

  • This midterm is comprehensive.
  • Familiarize yourself with all concepts.
  • Review labs and codes from class; there will be nothing from zybooks.
  • Code understanding will be tested, but coding will not be required.
  • The exam includes theoretical and practical questions.
  • There will be approximately 30 multiple-choice and 5 open-ended questions.

Lecture 1: The Kernel and Linux Commands

  • The kernel is like the brain of the computer system, performing core functions.
  • Kernel functions include acting as a medium between hardware and software.
  • Memory management is a kernel function.
  • Process management is a kernel function.
  • Input/output and file system management are kernel functions.
  • The file system is represented as a tree.
  • The root of the file system is represented by "/".
  • The filesystem organizes and accesses files.
  • Common Linux commands include:
  • ls: list directory contents
  • cd: change directory
  • mkdir: make directory
  • mv: move files or directories
  • cp: copy files or directories
  • scp: secure copy (remote file copy)
  • tar: archive files
  • rm: remove files or directories
  • chmod: change file permissions
  • cat: concatenate and display files
  • grep: search for a pattern in files
  • less: view file contents page by page
  • more: view file contents page by page
  • man: display manual pages for commands
  • sudo stands for "Super User Do"
  • sudo allows running commands with elevated privileges.
  • gcc is a built-in compiler and part of the GNU Compiler Collection
  • gcc compiles source code into machine code.
  • To compile code use: gcc -o <output_file name> <source_file.c>
  • -o specifies the output file name when compiling with gcc.

Lecture 2: Memory Organization

  • A program sees memory as an array of bytes, ranging from 0 to 2^64 - 1.
  • Memory is organized into memory mappings
  • The TEXT section stores instructions that the program runs.
  • The DATA section stores initialized global variables.
  • The BSS section stores uninitialized global variables, which are initialized to zeros until a value is assigned.
  • The HEAP stores memory returned by malloc/new.
  • The STACK stores local variables and return addresses.
  • Shared libraries (.so files) are not added to the executable file and are dynamically loaded at runtime
  • Shared libraries consist of definitions only, so they do not increase the executable file size.
  • The heap is space for dynamic memory allocation and grows upward.
  • The stack grows downwards and stores local variables.
  • Each program has an independent view of memory.
  • Gaps between memory sections may lack memory mapping.
  • Accessing a memory gap results in a SEGV signal, which typically terminates the program and creates a core file.
  • Static libraries are not shared and have an instance for each process; they are represented as (.a) files added to the executable.
  • Shared libraries are shared across different processes with only one instance for the entire system.
  • Static events happen during program building.
  • Dynamic events happen while a program is running.
  • Variable types in C programming include int, char, double, pointer, string, float, short, long, and unsigned.

Lecture 3: Operators, and Preprocessor Directives

  • With integer overflow, C uses two's complement and wraps back to the beginning of the value range.
  • Inputting 2,147,483,648 results to an output of -2,147,483,648 due to integer overflow.
  • printf() prints variables and expressions to standard output (the terminal).
  • scanf() takes input from standard input (the console).
  • The "&" operator is the address of operator.
  • When paired with a variable, "&" is used to acquire the memory address.
  • "%" (percent sign) is the format specifier for printing and scanning.
  • #include is a preprocessor directive that declares libraries or files for use in the code.
  • #define is a preprocessor directive and creates macros: constant values or expressions that the preprocessor substitutes before compilation.

Lecture 4: Ternary Operations

  • Ternary operations use the '?' and ':' symbols.
  • ? can be considered as 'then', and : can be considered as 'otherwise'.
  • Ternary expressions can be read as:
  • variable = (condition) ? Expression2 : Expression3;
  • (condition) ? (Expression2) : (Expression3);
  • variable = Expression1 ? Expression2 : Expression3;

Lecture 5: Functions

  • Functions perform specific tasks when called and contains
    • Declaration
    • Definition
    • Function call
  • A function prototype is a declaration that tells the compiler about a function's existence and signature (return type, name, parameters).
  • Prototypes prevent implicit declaration warnings, which occurs when a function is called before its definition without the compiler knowing its structure.
  • Passing arguments by value results copies the values into function parameters; changing the parameters does not modify the original values.
  • Passing by reference allows the function to operate on the original data, allowing modification of the original variable, and avoiding copying large data structures.
  • Recursion is a technique where a function calls itself to solve smaller subproblems.

Lecture 6: Pointers

  • A pointer is a variable that stores a memory address.
  • A pointer uses 8 bytes in a 64-bit system and 4 bytes in a 32-bit system, regardless of the data type it points to.
  • An integer uses 4 bytes and a character uses 1 byte in a 64-bit system.
  • "*" is the dereference operator and is used to access the data stored at a memory address.
  • Pointers store a memory address.
  • int *ptr = &var; means *ptr = &var will point at the memory address of &var.
  • ptr alone will produce the memory address of var.
  • *ptr will produce the value that resides at that memory address.
  • (*ptr)++ dereferences and adds 1 to the value. *ptr++ increments the pointer to the next memory address.
  • Unary operators read from right to left.
  • A double pointer stores the memory address of another pointer.
  • Double pointers are useful for managing dynamically allocated memory as well as dealing with complex data structures.
  • int var = 10; int *ptr = &var; int **dptr = &ptr; initializes var to 10, ptr to the memory address of var, and dptr to the memory address of ptr.
  • Dereferencing dptr twice accesses the contents of var.
  • When passing an array to a function, or any part thereof, array decay occurs; the array name decays into a pointer to the first element, we are passing the memory address and need to know the size.

Lecture 7: Arrays and the Stack

  • sizeof() returns the total bytes of what is in the parentheses.
  • sizeof() is used to determine the size of the array to get the actual array size by doing sizeof(array)/sizeof(array[0]).
  • When passing an array to a function, the memory address of the first element is the parameter.
  • When passing an array, it then becomes a pointer.
  • Iteration of an array using pointer arithmetic can be done by adding a value to a base pointer to move positions in memory.
  • The expression *(ptr + 2) produces the value at the index.
  • The stack stores information for passing control and data and allocating memory, including:
  • Function local variables
  • Temporary variables
  • Arguments for the next function call
  • The return address, when the function ends.
  • Push is used to add data and Pop is used to remove data from the stack.
  • The compiler manages the stack by:
  • Creating one stack frame each time a function is called.
  • Stack frames are created when the function is called.
  • Frames are stacked on top of one another.
  • Frames are destroyed at function exit.
  • Errors that can occur in the stack:
  • Running out of stack space
  • Unintentionally changing values on the stack (in other function frames or return addresses)
  • Accessing memory, even after the frame is deallocated
  • Stack overflow occurs when the stack space runs out.
  • Buffer overflow occurs when more data than anticipated gets placed by a program or system process to the stack, overflowing the stack
  • Pointers are more efficient for array indexing because the normal indexing requires multiplication to find the correct index.

Lecture 8: Malloc and Memory

  • malloc dynamically allocates a single large block of memory with a specified size.
  • malloc returns a pointer to a block of memory and stores the memory allocated by malloc is stored in the heap
  • The reason for a pointer cast type is because the malloc function returns a pointer to void.
  • The size of a pointer in a 32-bit system is 4 bytes.

Lecture 9/10: Memory Allocation

  • Ways of indexing:
  • Assuming int a[20] is defined:
  • A[i] is equivalent to *(a+i) and *(&a[0]+i)
  • Memory previously allocated by malloc or calloc is deallocated by free. Doing so prevents memory leaks.
  • Memory leaks are objects in memory that are no longer in use by the program but that are not freed.
  • Not deallocating memory leads to memory leaks.
  • The heap can run out of space, in which case malloc will return 0.
  • One can unintentionally change other heap data, or clobber heap data by overwriting a register during execution.
  • Other heap errors are Accessing memory after it has been freed or freeing memory twice, which could cause crashing or corruption.
  • Such heap errors create a memory leak.

Lecture 11: Memory Management Errors

  • A premature free is caused when an object still used by the program is freed.
  • A double free is caused by freeing an object that is already free.
  • Wild frees happen when a program attempts to free a pointer in memory that was not returned by malloc.
  • Memory smashing occurs when less memory is allocated than will be used.
  • Memory fragmentation occurs when aggregation of memory lacks a single large block.

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 Hardware Commands Quiz
48 questions
Introduction to Linux Commands and Concepts
23 questions
Linux Kernel & Commands: Midterm Review
41 questions
Operating Systems Midterm Prep
43 questions
Use Quizgecko on...
Browser
Browser