Operating Systems Midterm Prep

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?

  • The primary storage device for user data.
  • Acts as the medium between hardware and software, managing system resources. (correct)
  • The central processing unit responsible for executing instructions.
  • The graphical user interface that allows users to interact with the system.

Which of these options is NOT typically a function performed by the kernel?

  • Input/output management.
  • Process management.
  • Memory management.
  • Direct user application development. (correct)

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

True (A)

In Linux, what does the command mkdir do?

<p>Creates a new directory. (D)</p> Signup and view all the answers

What does sudo stand for, and what is its primary use?

<p>Super User Do; to run a command with elevated privileges</p> Signup and view all the answers

In gcc -o output_file source_file.c, the -o option specifies the ______ file name.

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

From a program's perspective, how is memory primarily viewed?

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

What is the range of memory addresses a program can access in a 64-bit system?

<p>0 - 2^64 - 1</p> Signup and view all the answers

Match the following memory sections with their typical contents:

<p>TEXT = Instructions that the program runs 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 purpose of the Heap memory section?

<p>Dynamic memory allocation. (D)</p> Signup and view all the answers

The stack grows upwards in memory.

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

Each program has its own view on memory that is ______ of each other.

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

What happens when a program attempts to access a memory address outside of its allocated memory?

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

Which statement is true regarding static libraries?

<p>Each process has its own instance of the static library. (B)</p> Signup and view all the answers

Dynamic events occur during program building, while static events occur when the program is running.

<p>False (B)</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 conducting two's complement, wrapping back to beginning of value range</p> Signup and view all the answers

Match the function with what it allows us to perform:

<p>printf() = Prints variables and expressions to standard output scanf() = Takes input from standard input</p> Signup and view all the answers

What does the & operator signify in C?

<p>Address of operator (A)</p> Signup and view all the answers

In C, the symbol used for the format specifier when printing and scanning variables is the ______ sign (%).

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

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

<p>To insert the contents of another file, such as a header file, into the current source file. (B)</p> Signup and view all the answers

The #define directive creates macros which are essentially constant values or expressions.

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

What is a ternary operator?

<p>A shorthand way to express a simple <code>if-else</code> statement. (B)</p> Signup and view all the answers

What symbols are used in a ternary expression?

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

In a ternary expression, the ? can be considered as '______ then' and the : can be considered as 'otherwise'.

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

Which option correctly identifies the three key aspects of a function?

<p>Declaration, Definition, Call (B)</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. (A)</p> Signup and view all the answers

Explain the difference between passing by value and passing by reference.

<p>Passing by value copies the value of the variable, while passing by reference allows the function to operate on the original data.</p> Signup and view all the answers

What is recursion?

<p>A function calling itself. (A)</p> Signup and view all the answers

What is a pointer?

<p>A variable that points to an address in memory. (B)</p> Signup and view all the answers

How much memory does a pointer occupy in a 64-bit system?

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

In a 64-bit system, an integer typically occupies ______ bytes.

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

What is the * symbol known as when used with pointers?

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

What does a pointer typically store?

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

In the declaration int *ptr = &var;, what is ptr initialized with?

<p>The memory address of <code>var</code>.</p> Signup and view all the answers

What is the difference between (*ptr)++ and *ptr++?

<p><code>(*ptr)++</code> increments the value pointed to, while <code>*ptr++</code> increments the pointer. (C)</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

Explain what is happening in the following lines of code: int var = 10; int *ptr = &var; int **dptr = &ptr;

<p>Variable <code>var</code> is initialized to 10. Pointer <code>ptr</code> stores the address of <code>var</code>. Double pointer <code>dptr</code> stores the address of <code>ptr</code>.</p> Signup and view all the answers

When passing an array to a function, the entire array is copied to the function's local memory.

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

When an array is passed to a function, it decays into a ______ to the first element of the array.

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

What additional information is typically needed when passing an array to a function in C?

<p>The size of the array. (B)</p> Signup and view all the answers

Why is sizeof() used when passing arrays to functions?

<p>To determine the size of the array. (C)</p> Signup and view all the answers

When passing an array to a function, what is essentially passed?

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

Flashcards

What is the kernel?

The core of an operating system; like the brain, it manages resources and performs functions.

Kernel functions

Acts as interface between hardware and software. Includes memory, process, input/output, and file system management.

What is the filesystem?

Hierarchical structure for file organization, with '/' representing the root directory.

ls command

Lists directory contents.

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 or renames files or directories.

Signup and view all the flashcards

cp command

Copies files or directories.

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 compressed file.

Signup and view all the flashcards

rm command

Removes files or directories.

Signup and view all the flashcards

chmod command

Changes file permissions.

Signup and view all the flashcards

cat command

Displays file content.

Signup and view all the flashcards

grep command

Searches for patterns in files.

Signup and view all the flashcards

less command

Views file content one page at a time.

Signup and view all the flashcards

more command

Displays file content one page at a time.

Signup and view all the flashcards

man command

Displays command documentation.

Signup and view all the flashcards

sudo command

Super user do, allows command execution with administrative privileges.

Signup and view all the flashcards

What is gcc?

GNU Compiler Collection, compiles source code into machine code.

Signup and view all the flashcards

gcc option: -o

Specifies the output file name during compilation.

Signup and view all the flashcards

Program memory view

A program sees memory as a linear array of bytes.

Signup and view all the flashcards

Memory sections

Memory divided into TEXT, DATA, BSS, STACK, HEAP, Shared libraries.

Signup and view all the flashcards

TEXT section

Stores program instructions.

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

Memory for dynamic allocation (malloc/new); grows upward.

Signup and view all the flashcards

STACK

Stores local variables and return addresses; grows downwards.

Signup and view all the flashcards

Shared libraries

Contain definitions used at runtime and are dynamically loaded.

Signup and view all the flashcards

Address space

Each program has its own independent view of memory.

Signup and view all the flashcards

Accessing a memory gap

OS sends a SEGV signal, terminating the program.

Signup and view all the flashcards

Static libraries

Not shared; each process has its own instance.

Signup and view all the flashcards

Shared libraries

Shared across processes; a single instance for the entire system.

Signup and view all the flashcards

Static events

Happen during program building.

Signup and view all the flashcards

Dynamic events

Happen while a program is running.

Signup and view all the flashcards

C variable types

int, char, double, pointer, string, float, short, long, unsigned.

Signup and view all the flashcards

Integer Overflow in C

Uses two's complement and wraps around to the beginning of the value range.

Signup and view all the flashcards

printf() and scanf()

printf(): prints to standard output; scanf(): reads input from standard input.

Signup and view all the flashcards

& operator

The address of operator; gives the memory address of a variable.

Signup and view all the flashcards

% (percent sign)

The format specifier symbol.

Signup and view all the flashcards

Study Notes

General

  • The midterm will be comprehensive
  • Familiarize yourself with all concepts
  • Review labs and codes that have been covered in class
  • Exam questions may involve code, but you won't have to write any
  • Code understanding is important; you might be asked for output or to show operations
  • The exam includes theoretical and practical questions
  • The exam contains ~30 multiple-choice and ~5 open-ended questions

Lecture 1: Kernel and Filesystem

  • The kernel is like the brain of a computer system, performing many functions
  • Kernel functions include acting as a medium between hardware and software
  • Kernel functions include memory management
  • Kernel functions include process management
  • Kernel functions include input/output management
  • Kernel functions include file system management
  • The filesystem is represented as a tree
  • The root directory is represented by a forward slash: /

Lecture 1: Linux Commands

  • ls lists directory contents
  • cd changes the current directory
  • mkdir creates 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 also displays file contents one page at a time
  • man displays the manual page for a command

Lecture 1: Sudo and GCC

  • sudo stands for "Super User Do"
  • sudo allows you to run a command with elevated privileges
  • gcc is a built-in compiler
  • GCC (GNU Compiler Collection) compiles source code into machine code
  • gcc -o <output_file name> <source_file.c> compiles a C source file
  • -o specifies the output file name

Lecture 2: Memory Organization

  • A program views memory as an array of bytes, ranging from 0 to 2^64 - 1
  • Memory is organized into sections known as memory mappings
  • 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 file
  • Shared libraries consist of definitions and are dynamically loaded at runtime
  • The heap is space for dynamic memory allocation, and grows upward
  • The stack grows downwards
  • Stack stores local variables
  • Each program has its own independent view of memory

Lecture 2: Memory Access

  • There can be gaps between memory sections without mapping
  • If a program accesses a memory gap, the OS sends a SEGV signal
  • A SEGV signal typically kills the program and dumps a core file
  • Static libraries are not shared, with an instance for each process
  • Static Libraries are represented as (.a) files added to the executable
  • Shared libraries are shared across different processes
  • Only one instance of each shared library exists for the entire system

Lecture 2: Events and Variable Types

  • Static events occur during program building
  • Dynamic events occur while the program is running
  • Variable types in C programming include int, char, double, pointer, string, float, short, long and unsigned

Lecture 2: Integer Overflow

  • C handles integer overflow by using two's complement
  • The value wraps 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 Operators

  • printf() prints variables and expressions to standard output (the terminal)
  • scanf() takes input from standard input (the console)
  • & is the address of operator
  • When & is paired with a variable, the memory address is acquired
  • % (percent sign) is the format specifier for printing and scanning

Lecture 3: Include and Define

  • #include declares preprocessor directives for libraries or files to be used in your code
  • #define is a preprocessor directive used to create macros
  • Macros are constant values or expressions substituted into your code before compilation

Lecture 4: Ternary Operator

  • A ternary is a single-line if-else statement
  • The ternary operator uses ? and : symbols
  • ? can be read as "if a then b"
  • : can be read as "otherwise c"
  • variable = (condition) ? Expression2 : Expression3; is one way of reading "ternary expression"
  • There are multiple ways of reading a ternary expression (condition) ? (Expression2) : (Expression3); , and variable = Expression1? Expression2 : Expression3;

Lecture 5: Functions

  • Functions are blocks of code performing a specific task when called
  • A function call has function declaration, function definition and an actual function call
  • A function prototype tells the compiler about a function's existence and how to call it
  • Without a function defined at the top, there is an implicit declaration warning
  • A function prototype tells the compiler that the function exists elsewhere, providing its signature

Lecture 5: Pass by Value vs. Reference

  • Passing by value copies the values of function parameters
  • New memory is created to store passed-by-value parameters for the function call's duration
  • Passing by reference allows a function to operate on the original data
  • Pass by reference to modify a variable's value in a function, or to avoid copying large data structures

Lecture 5: Recursion

  • Recursion is when a function calls itself
  • Recursion breaks complicated problems into smaller, simpler ones
  • A recursive function calls itself in its function body and must include the function statments, the base condition and the recursive call

Lecture 6: Pointers

  • A pointer is a variable that points to an address in memory
  • A pointer uses 8 bytes on a 64-bit system and 4 bytes on a 32-bit system
  • An integer uses 4 bytes, and a character uses 1 byte in a 64 bit system
  • * is the dereference operator and used to access data stored at a memory address
  • Pointers store a memory address that we are pointing at
  • int *ptr = &var initializes a pointer to point at a variable
  • *ptr = &var will point to the memory address of &var
  • ptr alone produces the memory address of var
  • *ptr produces the value at that memory address
  • (*ptr)++ dereferences first, adding 1 to the value
  • *ptr++ moves to the next memory address
  • Unary operators read from right to left

Lecture 6: Double Pointers, Arrays & Functions

  • A double pointer is a pointer that stores the memory address of another pointer
  • Double pointers manage dynamically allocated memory, pass pointers to functions, or deal with complex data structures
  • When passing an array to a function, array decay occurs and the array decays into a pointer to the first element
  • The memory address of the first element is passed, so the array isn't copied

Lecture 6: Parameter Sizes

  • sizeof() is the total number of bytes of the argument
  • sizeof() helps determine the size of an array
  • Size of an array can be determined by sizeof(array)/sizeof(array[0])

Lecture 7: Stack

  • The stack stores function local variables, temporary variables, arguments for the next function call and where to return when a function ends
  • Push adds data
  • Pop removes data
  • The compiler manages the stack by having one stack frame each time a function is called, created when a function is called, stacked one on top of another and destoryed at function exit

Lecture 7: Stack Errors

  • The stack can run out of stack space
  • Code can unintentionally change values on the stack, in some other functions frame or the return address of function
  • Memory can be accessed even after the frame is deallocated

Lecture 7: Buffer Overflow

  • Occurs when more data gets placed by a program or system process to the stack, and the extra data overflows

Lecture 7: Pointers Efficient for Array Indexing

  • Normal indexing needs multiplication to find the correct index
  • With pointers, there is no need to multiply

Lecture 7: Malloc

  • Malloc dynamically allocates a single large block of memory with a specified size.
  • Malloc initializes stored memory in the heap
  • Malloc returns a pointer to a block of memory that is suitably aligned for any kind of data object that might be contained in the block

Lecture 7: Pointer Cast Type

  • (cast_type*) is needed because malloc returns a pointer
  • The pointer must cast the appropriate pointer data type to void

Lecture 8: Size of Pointer, Memory Deallocation

  • Size of a pointer in a 32 bit system is 4 bytes
  • Memory allocated by malloc or calloc is deallocated with free
  • Prevents memory objects in memory that are no longer in use by the program but that are not freed
  • Not deallocating can result in memory leaks

Lecture 9/10: The Heap Errors

  • Heap can run out of space, where malloc returns 0
  • Heap data can be unintentionally changed or clobbered
  • Memory can be accessed after it is freed
  • Code can free memory twice which could cause crashing or corruption
  • Failing to deallocate creates a memory leak

Lecture 11: Freeing Memory Errors

  • A premature free happens when an object still in use by the program is freed
  • A double free happens by freeing an object that is already free
  • A wild free happens when there 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

Studying That Suits You

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

Quiz Team

Related Documents

More Like This

Operating Systems Overview and Functionality
16 questions
Operating Systems Quiz
15 questions

Operating Systems Quiz

ProfuseJasper7320 avatar
ProfuseJasper7320
Machine Learning Concepts Quiz
47 questions
Operating System: Components, Functions and Types
24 questions
Use Quizgecko on...
Browser
Browser