CSIT 231: Function Pointers and Structs

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to Lesson

Podcast

Listen to an AI-generated conversation about this lesson
Download our mobile app to listen on the go
Get App

Questions and Answers

What is the primary purpose of function pointers in C?

  • To define new data types.
  • To implement polymorphism. (correct)
  • To manage memory allocation.
  • To create dynamic arrays.

In C, if the pointer name isn't encapsulated in parentheses, the compiler will always understand it as intended.

False (B)

What is a struct in C programming?

A user defined data type that is used to group items of different types into a single type

When accessing members of a structure using pointers, you must use the ______ operator.

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

Match the following memory allocation types with their characteristics:

<p>Explicit Memory Allocation = Requires less memory but may throw more errors. Implicit Memory Allocation = Typically faster but may use more memory.</p>
Signup and view all the answers

What do enums primarily represent?

<p>A group of global constants (B)</p>
Signup and view all the answers

Using an enum provides type checking and obeys scope rules, unlike #define.

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

What is the default value assigned to the first member of an enum if no explicit value is given?

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

The size of a union is determined by the size of its ______ member.

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

Match the following concepts related to C unions:

<p>Unions = Save space and memory by overlapping storage. Members of a Union = Share the same memory space.</p>
Signup and view all the answers

What operator is used to access members of a union in C?

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

BASH is exclusively a high-level programming language and not a command interpreter.

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

What special sequence of characters is typically found on the first line of a BASH script, and what is it called?

<p>Shebang (#!)</p>
Signup and view all the answers

In BASH, the permission combination 777 grants ______ , ______ , and ______ to the owner, group, and others.

<p>read, write, execute</p>
Signup and view all the answers

Match the following BASH permission codes with their corresponding access rights:

<p>777 = Read, write, and execute for everyone. 666 = Read and write for everyone. 444 = Read-only for everyone.</p>
Signup and view all the answers

In the context of BASH scripting, what does 'redirection' refer to?

<p>Altering where the standard input/output of a command goes. (D)</p>
Signup and view all the answers

A file descriptor is simply a name for a file.

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

What is the purpose of the /etc/passwd file in Linux systems?

<p>to read and identify users</p>
Signup and view all the answers

Actual passwords are not stored directly in /etc/passwd; instead, they are stored ______.

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

Match the file descriptor with its corresponding description:

<p>Standard input = Where a program receives input from. Standard output = Where a program sends regular output. Standard error = Where a program sends error messages.</p>
Signup and view all the answers

Which of the following best describes 'polymorphism' as implemented using function pointers?

<p>The ability of a function pointer to point to different functions with the same signature at runtime. (B)</p>
Signup and view all the answers

Explicit memory allocation is always faster and more efficient than implicit memory allocation.

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

What operator accesses members of a structure when using a pointer to that structure?

<p>The arrow operator (-&gt;)</p>
Signup and view all the answers

Enums offer safety over #define by providing ______ and obeying ______.

<p>type checking, scope rules</p>
Signup and view all the answers

Match each file permission number with its corresponding permission set (for a user, group, or others category):

<p>4 = Read 2 = Write 1 = Execute</p>
Signup and view all the answers

Which data structure can be built using dynamic memory allocation?

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

Header files do not need to be included to utilize the functions within them.

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

What is the purpose of symbol resolution in the context of linking?

<p>Associate each symbol reference with exactly one symbol definition</p>
Signup and view all the answers

The process of collecting and combining various pieces of code and data into a single file that can be copied into memory and executed is called ______.

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

Match the type of symbol with its scope:

<p>Global Symbol = Accessible from any module in the program. External Symbol = Declared in one module but defined in another. Local Symbol = Limited to the scope of the module in which it is defined.</p>
Signup and view all the answers

What is the primary disadvantage of using static libraries?

<p>Duplication in stored executables. (D)</p>
Signup and view all the answers

Dynamic linking occurs only at compile time.

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

What is a 'shebang' and why is it important in scripting?

<p>Tells the operating system which shell should execute the file</p>
Signup and view all the answers

The simplest form of control flow in a computer system is from ______ to ______.

<p>startup, shutdown</p>
Signup and view all the answers

Match each kind of exception with its basic description:

<p>Interrupts = Caused by signals from hardware devices. Traps = Intentional exceptions, such as system calls. Faults = Potentially recoverable errors, such as page faults. Aborts = Unrecoverable errors leading to program termination.</p>
Signup and view all the answers

What is the term for an instance of a program in execution?

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

In a multitasking system, processes always run to completion without interruption.

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

What does it mean for a process to be 'preempted'?

<p>That its temporarily suspended</p>
Signup and view all the answers

The first process initiated in a Linux system is typically named ______.

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

Match the process state to its description:

<p>Ready/Runnable = The process is waiting to use the CPU. Running = The process is currently executing. Zombie = Process execution is complete but entry still exists in the process table. Sleeping = The process is waiting for an event.</p>
Signup and view all the answers

What is stored in the process table?

<p>All information about processes (D)</p>
Signup and view all the answers

The Process Identifier (PID) is assigned after the context switch takes place.

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

What is the general goal of multiprogramming?

<p>To have some process always running, maximizing CPU utilization</p>
Signup and view all the answers

A synchronization construct that allows one thread at a time to access a shared resource is known as a ______.

<p>mutex lock</p>
Signup and view all the answers

Match each term related to process management with its correct description:

<p>Context Switch = The mechanism to switch the CPU from one process to another. Thread = A path of execution within a process. Race Condition = Threads can access shared data and try to change it only one thread should use</p>
Signup and view all the answers

Flashcards

What is Polymorphism?

Ability to change behavior at run-time.

What is a struct?

A user-defined data type to group different types of items into a single type.

What are struct members?

Items declared inside a struct.

How are struct values stored?

Values stored sequentially in memory.

Signup and view all the flashcards

What is dynamic memory allocation useful for?

Used to build data structures like linked lists, trees and graphs.

Signup and view all the flashcards

How to access struct members with pointers?

Arrow operator (->)

Signup and view all the flashcards

Why pass a pointer to a function?

Only memory address passed, not the entire struct.

Signup and view all the flashcards

Is explicit allocation faster than implicit?

Yes, explicit memory allocation is faster.

Signup and view all the flashcards

Explicit allocation, regarding memory?

Less memory but throws more errors.

Signup and view all the flashcards

What do enums represent?

A group of global constants.

Signup and view all the flashcards

Why might it be better to use an Enum?

It does not establish any type checking and does not obey scope rules

Signup and view all the flashcards

What issues may arise from #define?

We cannot group related variables when using #define

Signup and view all the flashcards

Enum default value?

The default value is 0, increments by 1.

Signup and view all the flashcards

What is a union?

User defined data type consisting of a sequence of members whose storage overlaps.

Signup and view all the flashcards

How are union members stored?

Since its all-shared memory space, only one member can hold a value at any given time.

Signup and view all the flashcards

How to determine the size of the union?

The size of the largest member.

Signup and view all the flashcards

Why are unions useful?

Unions save space and memory

Signup and view all the flashcards

What is BASH?

A Command interpreter and a high-level programming language

Signup and view all the flashcards

Special sequence of characters to start scripting?

We need shebang (#!)

Signup and view all the flashcards

What is shebang for?

Telling the OS which shell to execute the file

Signup and view all the flashcards

What is redirection?

Encompasses the ways you can cause the shell to alter where the standard input of a command comes from and where the standard output goes to

Signup and view all the flashcards

What is a file descriptor?

The place a program sends its output to and gets its input from

Signup and view all the flashcards

What do header files include?

They include #include <header.h>

Signup and view all the flashcards

What does #include do?

To load the contents of a specific header file

Signup and view all the flashcards

What is double inclusion?

When you include the same header file in multiple files

Signup and view all the flashcards

What is the solution for double inclusion?

Include the same header file twice, or a Header Guard

Signup and view all the flashcards

How do header guards work?

It will check if it's been defined before.

Signup and view all the flashcards

What is linking?

The process of collecting and combining various pieces of code and data into a single file

Signup and view all the flashcards

When can linking happen?

Compile time, load time, runtine

Signup and view all the flashcards

Benefits of linking multiple files?

It leads to better optimization.

Signup and view all the flashcards

What is symbol resolution?

Associate each symbol reference with exactly one symbol definition.

Signup and view all the flashcards

What is the symbol table?

An array of structs.

Signup and view all the flashcards

Three types of object files?

Relocatable, Executable, Shared object.

Signup and view all the flashcards

Types of symbols?

Global, External, Local.

Signup and view all the flashcards

Bad to put all C functions in one object?

Huge waste of space; requires recompilation.

Signup and view all the flashcards

How static libraries are stored on disk?

Archive files

Signup and view all the flashcards

Static libraries disadvantages?

Duplication in stored and running exe; requires explicit relink.

Signup and view all the flashcards

Dynamic linking's timing?

During runtime or load time.

Signup and view all the flashcards

Simplest form of control flow?

Startup to shutdown

Signup and view all the flashcards

Abrupt changes in system control?

Exceptional Control Flow (ECF)

Signup and view all the flashcards

Study Notes

  • These notes cover topics from CSIT 231 Final Exam Study Guide

Lecture 11

  • Pointers to functions often implement Polymorphism
  • This changes behavior because it can change at run-time, which differs from static function calls
  • Without parentheses around the pointer name in a function pointer, the compiler will make a mistake
  • A struct is a user-defined data type used to group items of different types into a single type
  • Items inside a struct are known as members
  • Values in structs are stored contiguously in memory
  • Members of a structure can be directly accessed using the "." operator
  • Dynamic memory allocation is useful when the size or number of structures is unknown at compile time
  • Dynamic memory allocation is useful for building data structures like linked lists, trees, and graphs
  • When accessing members of a structure using pointers, the arrow operator (->) must be used
  • Passing a pointer to a function is more efficient than passing the entire structure because only the memory address is passed, not a copy of the entire struct
  • Explicit memory allocation is faster than implicit memory allocation
  • Explicit memory allocation uses less memory but throws more errors

Lecture 12

  • Enums are a user-defined data type to represent a group of global constants
  • "The Days of the Week" is an example of an Enum
  • Using an Enum instead of #define is better because it includes type checking and obeys scope rules
  • #define can cause issues as it cannot group related variables
  • The default value assigned to each member of the enum is 0
  • Each value after the first increases by 1
  • For the enum Weekdays { MONDAY = 20, TUESDAY = 30, WEDNESDAY = 40, THURSDAY = 50, FRIDAY = 60 };, Tuesday = 30, Thursday = 50
  • A union is a user-defined data type consisting of a sequence of members whose storage overlaps
  • When a union is defined, code can store a variety of data types, but since memory is all-shared memory space, only one member can hold a value at any given time
  • Only one member can hold a value at a time
  • The size of a union is based on the size of its largest member
  • Unions are useful because they save space and memory and are useful for plugins and slicing network packets
  • Members of a union are accessed using the "." operator

Lecture 13

  • BASH, the Bourne Again Shell, is a command interpreter and a high-level programming language
  • The special sequence of characters needed on the first line when scripting is the shebang (#!) / (#!/bin/bash)
  • It tells the OS which shell should execute the file and follows the pathname
  • When creating a script, permissions must be updated to read and execute the instructions
  • Permission combinations:
    • 777 → Reads, writes, and executes for the owner, group, and others
    • 666 → Reads and writes for everyone
    • 444 → Read-only for everyone
  • Permissions can be adjusted using numbers to set specific permissions for each category: owner, group, others
  • Redirection encompasses the ways to alter where the standard input of a command comes from and where the standard output goes
    • < and > symbols are used to represent redirection
  • A file descriptor is where a program sends its output and gets its input from
  • There are three: Standard input, Standard output, standard error
  • "/etc/passwd" is important for reading and identifying users
  • The location where actual passwords are stored is important
  • The first four fields of a /etc/passwd entry represent something important

Lecture 14

  • The steps needed to add a node to the front of a list in a doubly-linked list are important
  • The steps needed to add a node to the tail of a list in a doubly-linked list are important
  • A doubly linked list is defined using a struct:
    • struct Node { int data; struct Node* next; struct Node* prev; };
    • struct Node* createNode (int data) { struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); newNode->data = data; newNode->next = NULL; newNode->prev = NULL; return newNode; }

Lecture 14_3

  • Header files include #include <header.h>
  • The C preprocessing directive #include requests the compiler to load the contents of a specific header file so it can be used in the program
  • Double inclusion is when the including header file is in multiple files
  • To solve double inclusion, include the same header file twice or use a Header Guard
  • Header guards check if a header has been defined before and prevents being defined more than once
  • Linking is the process of collecting and combining various pieces of code and data into a single file to be copied into memory and executed
  • Linking can be done at compile time, load time, and run time
  • Linking multiple files together leads to better optimization compared to creating one large source file

Lecture 15

  • Symbol resolution associates each symbol reference with exactly one symbol definition
  • A symbol table is an array of structs
  • Each entry contains the name, size, and location of the symbol
  • There are three object files:
    • Relocatable object (.o file)
    • Executable object file (a.out file)
    • Shared object file (.so file)
  • There are global, external, and local symbols
  • Rules for dealing with duplicate symbols:
    • Multiple strong symbols are not allowed, and each item can be defined only once
    • Using multiple strong symbols causes a Linker Error
    • Given a strong symbol and multiple weak symbols, choose the strong symbol
    • If there are multiple weak symbols, pick an arbitrary one

Lecture 16_2

  • Putting all standard C functions into a single relocatable object module causes huge waste of space, and a change would require recompilation of the entire source code
  • Static libraries are stored on disk in an archive file format
  • Disadvantages of static libraries:
    • Duplication in stored executable files
    • Duplication in running executable files
    • Minor bug fixes of system libraries require each application to explicitly relink
  • Shared Libraries are a modern solution

Lecture 17

  • Dynamic linking occurs during runtime or load time, when a library is loaded at an arbitrary memory address and linked with a program in memory
  • Shared libraries are "shared" either through one .so file or a single copy of the text section of a shared library in memory
  • The simplest form of control flow is startup to shutdown, where a CPU reads and executes a sequence of instructions one at a time which is CPU Control Flow
  • Events causing abrupt changes in the system: Instruction divides by zero, data from disk or network adapter, control, and hardware timer goes off
  • These abrupt changes are referred to as Exceptional Control Flow (ECF)
  • Exceptional control flow can occur at the hardware, OS, and application level
  • An exception is the transfer of control to the OS kernel in response to some event, like a change in the processor state
  • Events are triggered by any significant change

Cont.

  • Each type of event has a unique exception number k
  • k = index into exception table, also known as an interrupt vector
  • Handler k is called each time exception k occurs
  • The four classes of exceptions:
    • Interrupts
    • Traps
    • Faults
    • Aborts
  • A process is an instance of a program executing
  • While processes are running, using a logical control flow, they take turns using the processor
  • While processes take turns, they are preempted, meaning they are temporarily suspended
  • A logical flow whose execution overlaps in time with another flow is called a concurrent flow

Lecture 18

  • Init. is the first process in the system
  • The kernel assigns a unique ID number to every process
    • The unique ID is called the PID (Process ID)
    • Relationships between processes are named Parent-child
  • The states a process may be in:
    • Ready/Runnable
    • Running
    • Zombie
    • Stopped
    • Terminated
    • Sleeping
  • The process table contains all the info for processes
  • a process control block (PCB) stores process info
  • A context switch forcefully takes resources or the CPU from a process
  • A context switch happens approximately every 1/100sec
  • A thread is a path execution
  • Every thread has its own:
    • Stack
    • PC - Program Counter
    • Set of registers
    • State
  • Multithreading Advantages:
    • Fast creation, context switch, and communication across
  • Multithreading Disadvantages:
    • Less robust or more sync problems
  • Multiprocessing Advantages:
    • Reduced Execution Time, Parallel exec, Process Isolation
  • Multiprocessing Disadvantages:
    • Memory and CPU Overhead *higher usage
  • Fork: A running child process that is called once but returns twice

Lecture 19

  • Code will be provided to read, understand multithreading and multiprocessing codes, along with a process graph
  • A race condition is when threads can access shared data and try to change it simultaneously
  • A mutex lock, also known as a mutual exclusion, is an operation used to prevent simultaneous possession of the shared resources
  • The critical section is the portion of the code only one thread should execute at a time

Cont.

  • Semaphores coordinate access to resources
    • The two types of semaphores are binary and counting
  • The functions that lock/unlock a semaphore are Sem_post(), sem_wait()

Lecture 20

  • A signal is a small message that notifies a process that an event of some type has occurred in the system
  • A signal may be sent by sending or receiving
  • When receiving a process, the process can do one of three things: Ignore, Catch, Terminate
  • A pending signal has been sent but not yet received
  • Blocked signals are signals a process can selectively block the receipt of certain signals
  • There can be at most one pending signal of a particular type, the rest of the signals that may be of the same type are discarded
  • Each signal type has a predefined default action. The four actions:
    • The process terminates
    • The Process terminates and dumps core
    • The process stops (suspends) until restarted by a SIGCONT signal
    • The process ignores the signal
  • A transaction between a client and a server consists of four steps:
    • The client initiates a request to the server
    • The server verifies and approves the request
    • The server then sends a response to the client and waits
    • The client receives the response then processes it
  • A network is a hierarchical system organized by geographical proximity
    • The three proximities: SAN, LAN, WAN
  • IP addresses are stored in memory for a network and are referred to as "Dotted Decinal notation"
  • Usually IPs are presented to humans in a dotted decimal format, which is a conversion from its original hex form
  • A socket is an endpoint of a connection
    • A socket pair is (IP_adress:port)
  • An Ephemeral port is assigned automatically by the client kernel when a client makes a connection request
    • Well-known port is associated with some service provided by a server
  • Byte order is converted between the network and the client
  • AF_INET is used for the internet

Lecture 21

  • The main objective of multiprogramming is to have some process always running, maximizing CPU utilization
  • CPU sitting idle is a waste, no work is being accomplished
  • When one process waits, the OS gives the CPU to another process
  • The CPU scheduler selects a process from the processes in memory that are ready to execute and allocates the CPU to that process when the CPU becomes idle
  • The process control block shows all the process' stuffs
  • A preemptive scheme is when the OS can interrupt a running process and switch to another process before the current one finishes
  • A nonpreemptive scheme is when scheduling takes place under 1 and 4, The CPU holds, The OS does not forcibly take the CPU away
  • First know is to create the gantt chart and calculate the average wait time using each process when the algorithms used:
    • First come first serve
    • Shortest job first (preemptive and nonpreemptive)
    • Round robin
  • Then calculate exponential averaging using a formula

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