Linux Commands and Kernel

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

In the context of operating systems, the kernel is often compared to the 'brain' of a computer. Which of the following analogies most accurately reflects the kernel's role in a modern, complex system?

  • A high-frequency trading algorithm, reacting instantaneously to market changes without needing a comprehensive understanding of global economics.
  • The electrical grid of a city, distributing power to various sectors but unaware of the specific appliances being powered.
  • The engine control unit (ECU) in a modern automobile, managing all sensor data, actuator controls, and safety systems to ensure optimal performance.
  • A project manager within a large construction firm, delegating tasks to specialized teams and ensuring adherence to the architectural blueprint. (correct)

In C, accessing argv[argc] will always result in a segmentation fault due to out-of-bounds access.

False (B)

Explain the security implications if a system's /tmp directory is mounted with the noexec option inadvertently disabled.

If /tmp's noexec is disabled, it allows execution of arbitrary code placed in the directory. This can be exploited by attackers to run malicious scripts or binaries, potentially leading to system compromise through privilege escalation.

In the context of memory management in C, the function ______ is specifically designed to change the size of a previously allocated memory block.

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

Match the following Linux commands with their specific functions related to file system operations:

<p>chmod = Modifies file permissions. chown = Changes the owner and group of a file. df = Displays disk space usage. du = Estimates file space usage.</p> Signup and view all the answers

Given the command gcc -o myprogram source.c -lmath -dynamic, what is the most accurate interpretation of the -dynamic flag's impact on the resulting executable?

<p>It explicitly instructs the linker to perform dynamic linking with the <code>libmath.so</code> library at runtime, reducing the executable size but requiring the library to be present on the target system. (A)</p> Signup and view all the answers

In a system utilizing Position Independent Executables (PIE), the base address of the executable is fixed at load time, providing no ASLR benefits.

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

Describe the potential consequences of using strcpy inappropriately when handling user-supplied input in a C program.

<p><code>strcpy</code> does not perform bounds checking, so using it to copy user-supplied input into a fixed-size buffer can easily lead to a buffer overflow. This can overwrite adjacent memory regions, potentially corrupting data, hijacking control flow, or causing a crash, making it a critical security vulnerability.</p> Signup and view all the answers

In the x86-64 architecture, the register typically used to pass the first floating-point argument to a function, conforming to the System V AMD64 ABI, is ______.

<p>%xmm0</p> Signup and view all the answers

Match the memory region with its primary purpose in a typical Linux process:

<p>Stack = Storing local variables and function call information. Heap = Dynamic memory allocation. BSS = Uninitialized global variables. Text = Executable code.</p> Signup and view all the answers

Consider a scenario where a multi-threaded C application is experiencing performance bottlenecks due to excessive contention. Which of the following strategies would be MOST effective in mitigating these issues at design level?

<p>Implementing lock-free data structures and algorithms using atomic operations to minimize the need for explicit locking. (C)</p> Signup and view all the answers

The primary advantage of using memory-mapped files over traditional read/write operations is that memory-mapped files always result in lower memory consumption.

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

Explain the concept of 'thundering herd' in the context of concurrent programming and describe a technique to alleviate it.

<p>The 'thundering herd' problem occurs when multiple threads or processes are blocked waiting for a single event, and when that event occurs, all of them wake up and contend for the same resource, overwhelming the system. A technique to alleviate this is using an event notification mechanism where only one waiter is woken up, such as using a conditional variable with signal instead of broadcast.</p> Signup and view all the answers

In the context of C++, the ______ keyword is used to prevent a virtual function from being overridden in derived classes, effectively finalizing the method in the inheritance hierarchy.

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

Match the common CPU vulnerabilities with their corresponding mitigation techniques:

<p>Spectre = LFENCE, retpoline, and compiler-based mitigations. Meltdown = Kernel page-table isolation (KPTI). Rowhammer = Increased memory refresh rates. Cache Timing Attacks = Constant-time algorithms.</p> Signup and view all the answers

What is the most accurate description of the potential impact of memory fragmentation on a long-running server application?

<p>It can cause gradual performance degradation as memory allocation requests fail despite sufficient free memory, potentially leading to application instability. (A)</p> Signup and view all the answers

In C, the volatile keyword guarantees atomicity of read and write operations on a variable across all architectures.

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

Explain the difference between a preemptive and a non-preemptive (cooperative) multitasking operating system, highlighting a potential disadvantage of non-preemptive multitasking.

<p>In preemptive multitasking, the OS can interrupt running processes and switch to another, ensuring fair CPU time allocation. In non-preemptive multitasking, a process must voluntarily yield control. A major disadvantage of non-preemptive multitasking is that a single misbehaving process can monopolize the CPU, causing the entire system to become unresponsive.</p> Signup and view all the answers

In the context of distributed systems, the CAP theorem states that it is impossible for a distributed data store to simultaneously provide more than two out of the following three guarantees: Consistency, Availability, and ______.

<p>Partition Tolerance</p> Signup and view all the answers

Match the Inter-Process Communication (IPC) mechanism with its characteristic:

<p>Pipes = Unidirectional data flow between related processes. Message Queues = Asynchronous communication with message persistence. Shared Memory = Fastest IPC method, requiring explicit synchronization. Sockets = Communication across network boundaries.</p> Signup and view all the answers

Flashcards

What is the kernel?

The core of the OS; like the brain, it manages the system's functions.

Kernel functions

Acts as the medium between hardware and software, manages memory and processes.

What is a filesystem?

A structure that organizes files. Root is represented by /.

ls Command

Lists files.

Signup and view all the flashcards

cd Command

Changes directory.

Signup and view all the flashcards

mkdir Command

Makes a new directory.

Signup and view all the flashcards

What does sudo do?

Gives elevated privileges.

Signup and view all the flashcards

What is gcc?

A built-in compiler that turns source code into machine code.

Signup and view all the flashcards

What does -o do in gcc?

Specifies the name of the output file during compilation.

Signup and view all the flashcards

How a program sees memory

An array of bytes with address range 0 to 2^64 - 1.

Signup and view all the flashcards

Memory Organization

Sections are known as memory mappings; areas in memory.

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 and return addresses; grows downwards.

Signup and view all the flashcards

Program address space

Each program has its own memory view, independent of others.

Signup and view all the flashcards

Static vs Shared?

Static libraries are not shared, shared libaries are.

Signup and view all the flashcards

C variable Types

Integer, Char, Double, Pointer, String, Float, Short, Long

Signup and view all the flashcards

How C handles overflow

Wraps around using two's complement.

Signup and view all the flashcards

printf() and scanf()

Functions for printing to and taking input from the console.

Signup and view all the flashcards

& operator in C

The address of operator; acquires memory address when paired with a variable.

Signup and view all the flashcards

#include does what?

Declares preprocessor directives.

Signup and view all the flashcards

Study Notes

General Notes

  • The midterm will be fully comprehensive of covered content
  • Review labs and class codes, since there will be no questions from zybooks
  • Code understanding is necessary; code writing is not
  • The exam includes ~30 multiple choice and ~5 open-ended questions, testing theoretical and practical knowledge

Lecture 1: Kernel, Filesystem, and Linux Commands

  • The kernel is like the brain of a computer system that performs many functions
  • Kernel functions include acting as a medium between HW and SW, memory/process/input-output/file system management
  • The root of the filesystem is represented by a forward slash (/)
  • The filesystem has a tree representation
  • The computer organizes and accesses files through the filesystem

Common Linux Commands

  • ls: Lists directory contents
  • cd: Changes the current directory
  • mkdir: Creates a new directory
  • mv: Moves or renames 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: Views file contents page by page
  • more: Displays file contents
  • man: Displays the manual page for a command

Sudo and GCC

  • sudo stands for "Super User Do", and provides elevated privileges to run commands
  • gcc is a built-in compiler, part of the GNU Compiler Collection, and compiles source code into machine code
  • gcc -o <output_file name> <source_file.c> compiles code with GCC
  • -o specifies the output file name during GCC compilation

Lecture 2: Memory Organization

  • 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

Memory Sections

  • TEXT stores instructions that the program runs
  • DATA stores initialized global variables
  • BSS stores uninitialized global variables, initialized to zeros until assigned a value
  • HEAP stores memory returned by malloc/new
  • STACK stores local variables and return addresses
  • Shared libraries (.so files) are not added to the executable, consist of definitions only, and are dynamically loaded at runtime

Heap and Stack

  • The heap is a space for dynamic memory allocation that grows upward
  • The stack stores local variables and grows downwards

Program Memory

  • Each program has an independent view of memory
  • There could be memory gaps without memory mapping between each section
  • If a program tries to access a memory gap, the OS sends a SEGV signal which terminates the program and dumps a core file

Static and Shared Libraries

  • Static libraries are not shared, with an instance per process, and represented as (.a) files added to the executable
  • Shared libraries are shared across different processes, with only one instance for the entire system

Events

  • Static events occur during program building
  • Dynamic events occur while the program is running

C Variable Types

  • Common variable types in C : int, char, double, pointer, string, float, short, long, unsigned

Integer Overflow

  • C handles integer overflow by conducting two's complement, wrapping back to the beginning of the value range
  • Inputting 2,147,483,648 results in an output of -2,147,483,648

Lecture 3: Printf, Scanf, and Preprocessor Directives

  • printf() prints variables and expressions to standard output (terminal)
  • scanf() takes input from standard input (console)
  • The & operator is the address-of operator
  • When paired with a variable, & acquires the memory address
  • % (percent sign) is the format specifier for printing and scanning
  • #include declares preprocessor directives for libraries or files intended for utilization
  • #define is a preprocessor directive that creates macros
  • Macros are constant values or expressions substituted into code before compilation

Lecture 4: Ternary Expressions

  • Ternary expressions are single-line if-else statements
  • The two symbols used in ternary expressions are ? and :
  • ? can be considered "then" and : can be considered "otherwise"
  • variable = (condition) ? Expression2 : Expression3 and (condition) ? (Expression2) : (Expression3) are both ways to read a ternary expression
  • variable = Expression1 ? Expression2 : Expression3 is also a way to read a ternary expression

Lecture 5: Functions

  • Functions are blocks of code performing specific tasks when called
  • Functions consist of a declaration, definition, and 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, an implicit declaration warning is given and a function prototype should be used

Passing by Value vs. Reference

  • Passing by value copies the values to function parameters, creating new memory
  • Passing by reference allows functions to operate on the original data
  • Pass by reference is useful when modifying variables or avoiding copying large data structures

Recursion

  • Recursion is a technique where a function calls itself
  • Recursion breaks down complex problems into simpler ones

Lecture 6: Pointers

  • A pointer is a variable that points to an address in memory
  • A pointer occupies 8 bytes in a 64-bit system and 4 bytes in a 32-bit system
  • An integer occupies 4 bytes and a character 1 byte in a 64-bit system
  • * is the dereference operator and accesses data stored at a memory address
  • Pointers store a memory address
  • int *ptr = &var initializes a pointer to point at the memory address of var
  • ptr alone produces the memory address of var
  • *ptr produces the value residing at the memory address of var

Pointer Arithmetic

  • (*ptr)++ dereferences the pointer first and adds 1 to the value
  • *ptr++ moves to the next memory address
  • Unary operators are read right to left

Double Pointers

  • A double pointer stores the memory address of another pointer
  • Double pointers help manage dynamically allocated memory, pass pointers to functions, or deal with complex data structures

Code Example

  • In the code, a var is allocated an assiged the value 10. ptr ( a pointer) , is initialized and assigned the memory address of var,. and dptr (a double point) is initialized and assigned to the memory address of ptr
  • If dptr is dereferenced twice, the contents of var is accessed

Array Decay

  • When passing an array to a function, array decay occurs which means the array name decays into a pointer to the first element
  • Instead of copying the entire array, the memory address of the first element is passed

Array Size

  • To pass an array to a function you must include the size of the array

Sizeof()

  • sizeof() returns the total number of bytes
  • sizeof() is used to determine the size of an array
  • sizeof(array) / sizeof(array[0]) formula gets the actual size of the array

Arrays and Pointers

  • Passing an array to a function passes the memory address of the first element, turning the array into a pointer

Array Iteration

  • Arrays can be iterated over with pointer arithmetic by adding a value to move x positions through the array
  • To move and access data within the array you must encapsulate the pointer and the value like so *(ptr + 2)

Lecture 7: Stack

  • The stack stores information for passing control and data and allocating memory
  • The stack stores function local, temporary, and return variables, as well as arguments for the next function call
  • The stack adds data with push and removes data with pop

Compiler Stack Management

  • The compiler utilizes the stack as follows
  • Allocates one stack frame each time a function is called or exits
  • Stacking frames on top or under one another
  • Stack frames are created and destroyed when a function is called and at function exit

Potential Stack Issues

  • The stack can run out of stack space
  • There is potential to unintentionally change values on the stack such as, functions, variables, the return address of a function
  • It can access memory, even after the frame is deallocated
  • When it runs out of space the program will crash because of a segmentation fault or stack overflow error

Buffer Overflow

  • A buffer overflow occurs when more data than anticipated is placed by a program or system process on the stack, causing excess data to overflow

Lecture 8: Malloc, Memory Allocation, and Pointers

  • Pointers are more efficient for array indexing because multiplication is not required to find the correct index
  • malloc dynamically allocates a single large block of memory with a specified size and the memory allocated by malloc is stored in the heap, and returns a pointer to a suitably aligned memory block
  • A pointer type cast is needed to be able to appropriately cast the pointer data, since the malloc function returns a pointer to void
  • A pointer in a 32-bit system is 4 bytes

Memory Deallocation

  • Memory allocated by malloc or calloc can be deallocated with free, preventing memory leaks
  • Memory leaks are objects in memory no longer in use, but not freed

Memory Issues

  • Failing to deallocate memory results in memory leaks
  • You can run out of space in the heap and malloc returns 0
  • You can unintentionally change other heap data by clobbering the data, which means a registers if overwritten during execution
  • You can access or free memory twice which can cause crashing or memory corruption of the heap, creating a memory leak

Lecture 9/10: Memory relationships

  • Assuming "int a[20]", then A[i] is equivalent to *(a+i), which is equivalent to *(&a[0]+i)

Lecture 11: Memory management

  • A premature free occurs when an object, still in use, is freed by the program
  • A double free happens when an object that is already free is again freed
  • Wild frees are a result of attempting to free memory that was not returned by malloc
  • Memory smashing occurs when less memory is allocated than will be used

Studying That Suits You

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

Quiz Team

Related Documents

More Like This

Use Quizgecko on...
Browser
Browser