1/175
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
---|
No study sessions yet.
What is C?
A procedural, compiled language that all modern OSes are currently written in.
How do strings work in C?
They don't (technically)! Instead we have char[] or char*, arrays of characters
("hello" would be split into "h", "e", "l", "l", and "o")
What is null termination in C?
Strings must have one additional char accommodated, and have the last byte be 0x00 (escape sequence "\0"), otherwise standard functions will read past what's desired.
How do booleans work in C?
They don't (technically)! Instead we have 0 = false and !0 = true
(or if you REALLY want "true" and "false", import stdbool.h)
What are pointers in C?
An integer that "points" to the location of something in memory
What does & signify in pointers?
Gets the address of an operator
Ex. int X = 12; int* Y = &X = 5000
What does * signify in pointers?
Dereferences a pointer address to get what it's pointing to
Ex. int X = 12; int* Y = &X = 5000
Y = 5000 = 12 = X
How does pointer addition/subtraction work?
Adding/subtracting to a pointer increments/decrements the address's value by the data type's size (usually ints, so usually 4)
Ex. int* Y = 5000; Y + 1 = 5000 + (sizeOf(int)) = 5004
What is a segmentation fault?
when you try to resolve an invalid pointer
What is argc?
Argument COUNT; an integer that represents the number of arguments passed to the program from a command line
What is argv?
Argument VECTOR; an array of character pointers/strings that essentially contain the program's name and the arguments it has
What is a function stack?
A stack made up of stack frames, used to decide the order of functions being called
What is a stack frame?
An allocated space on a function stack for a single function. Stores variables and arguments needed
How are file permissions represented?
In order: Read (r), Write (w), and Execute (x)
Ex. rwx r-x r--
How are file types represented?
The first bit in a file permission string:
Regular file: -
Directory: d
Symbolic link: l
What are the file permission groups?
Increasing order:
User (owner),
Group (anyone matching the GID of the file),
World (any user)
What does > do?
Overwrites a file if it exists
Ex. [echo "Hello!" > Hello.txt] will either create Hello.txt or overwrite an existing file with the contents "Hello!"
What does >> do?
Appends to a file if it exists
Ex. [echo "Hello!" >> Hello.txt] will either create Hello.txt or append "Hello!" to an existing file
What do users in Linux generally have?
User ID (uid)
Group ID (gid)
Groups they belong to
A home path
A default shell
What does a root user have?
A user ID and group ID of 0, meaning they can essentially do anything
What are the /usr directories?
/usr: where user binaries (programs) and read-only data is stored
/usr/bin: user binaries
/usr/sbin: system admin binaries (addusr, cron, chpasswd)
/usr/lib: libraries for user binaries
What is /boot?
Contains all the programs and code needed to boot the OS
What is /etc?
Stores configuration files (program/boot configs, passwd and shadow, network configs...)
What is /dev?
Exposes device files, some of which can be used to communicate with hardware and some represent some utility
What is /proc?
Exposes device files, but kernel related
/proc/cpuinfo: exposes info about the cpu
/proc/slabinfo: exposes info related to the kernel memory manager
/proc/NUMBER: information about a running process
What is /home and /root?
Where all of the user's/root user's directories are stored, such as Documents, Downloads, Desktop, etc.
What is /lost+found?
A directory populated with corrupted files whenever Linux boots
What are /var, /tmp, and /run?
Readable and writable storage directories, where /var stores log files from running applications while /tmp and /run store temporary files
What is /opt?
A directory for optional packages, often third party software or software that doesn't conform to Linux storage conventions
What are /mnt and /media?
Directories for external media; /mnt mounts other file systems like external hard drives, while /media mounts removable media like flash drives or DVDs
What is a shell?
A command line interpreter that operates in a "current working directory"
What does the man command do?
Pulls up a manual for any command or function
What does the pwd command do?
Prints the directory your shell is currently operating under
What does the cd command do?
Changes your directory to another one
Ex. $ cd path -> changes your directory to the path
Ex. $ cd ../ -> moves one directory up
Ex. $cd ~ -> changes to your home directory
What does the ls command do?
Lists all of the files in a directory
What does the cat command do?
ConCATenate: prints the content of a file. The command "tac" also does this, but in reverse
What does the echo command do?
Displays a line of text in the command shell
What does the mkdir command do?
Makes a directory file
What does the rm command do?
Deletes a file at a path
What does the chmod command do?
Changes the file mode bits with a file. Two main ways to do it:
Ex. chmod +x myCode -> adds "executable" to myCode
Ex. chmod u=rwx,g=r,o= myCode -> directly changes owner, group, and world permissions
(You can also use numbers 0, 1, 2, and 4 as none, execute, write, and read permissions respectively)
What does the mv command do?
Moves a file by renaming a file's source to a destination
What does the cp command do?
Copies a file by copying a file at it's source into a destination
What does the find command do?
Searches for files on a system using different arguments:
-name: searches by name
-iname: searches by name, but case insensitive
-user: finds files owned by user
What does the strings command do?
Finds sequences of human readable characters. Can use -n to specify the minimum length
What does the uniq command do?
Removes or reports on duplicate lines in a file
What does the chown command do?
Changes ownership of a file
What does the less command do?
Opens up a file in a text viewer, where you use arrow keys to navigate, press / to pull up a text search, and press q to quit
What does the ln command do?
Creates a symbolic link, hard or soft
How does memory management work in C?
Make a call to malloc that gives a void pointer to the memory you requested, use that memory however you please, then free it once you're done
What does malloc() do?
Gets a pointer to an amount of memory using void * malloc(size_t size), where size is how large the memory is (usually obtained by sizeof(datatype))
What does calloc() do?
Allocates a block of memory using void* calloc(size_t nitems, size_t size), making a block large enough to hold all of the elements and initializes the memory to 0. Best for arrays
What does realloc() do?
Given a pointer previously allocated by malloc, calloc, or another realloc, it uses void realloc(void ptr, size_t size) to attempt to re-allocate a block in the requested size. If it succeeds, it returns a ptr to the new memory and frees the old memory. If it fails, it just returns null
What does free() do?
Takes a pointer allocated by malloc, calloc, or realloc and sets the memory free using free(void* ptr), allowing it to be overridden by future requests
What could go wrong with C memory management?
Memory leaks: failing to free memory allocated by malloc, often caused by losing a reference to a pointer
Null return: malloc may return a null ptr if it fails, and accidentally dereferencing it creates a seg fault
Use-after-free: Trying to use allocated memory after already using free() on it
Double-free: Trying to use free() on a pointer that's already been free()'d
What are some tips for C memory management?
-Ensure that every malloc() has a paired free() logically
-Be sure to avoid use of a pointer after it's been freed
-Always check for a null pointer
What is a stack in program memory?
Made up of static data allocations like local variables, function arguments, and return addresses. Memory is allocated in blocks within the stack, and it's automatically freed after execution finishes
What is a heap in program memory?
Made up of dynamic allocations like large data structures. Memory is allocated into heap memory that needs to be freed by a garbage collector after it's done being used
What is a system call?
how a user-space program requests a service from the kernel, such as reading from a file or creating new processes.
How does a system call work in C programming?
A C program calls a library function like read() or write(), which acts as a wrapper around a system call. Arguments are placed in CPU registers, a system call number is placed in register RAX, the CPU switches to kernel mode, the kernel performs the action, and then switches back to user mode.
What is the flow of a system call?
User Program → C Library → Registers Set → Kernel Mode → Action Done → Return to User.
What are common use cases for system calls?
File operations, process control, memory management, and network I/O .
What is a file descriptor?
A file descriptor (FD) is a non-negative integer that uniquely identifies an open file or other I/O resource in a process.
What are the common file descriptors in every program?
stdin (0) for standard input
stdout (1) for standard output
stderr (2) for standard error
What headers in C provide wrappers for system calls?
<unistd.h> and <fcntl.h>
What is the role of errno in system calls?
when a system call fails and is relevant only on failure, helping to diagnose what went wrong.
What does the open() system call return on success?
file descriptor (an integer). On error, it returns -1.
What do read() and write() return on success?
number of bytes read or written. on error, they return 0.
What does the socket() system call return on success?
On success, socket() returns a socket descriptor. On error, it returns -1.
What does close() return on success?
On success, close() returns 0; on error, it returns -1.
What is file I/O in the context of Linux systems?
allows a program to read from or write to files on disk, treating files as resources.
What is the hierarchy analogy for file I/O?
The disk is like a filing cabinet, directories are drawers, and files are folders.
What functions are used for high-level file I/O in C?
fopen() to open a file
fread(), fwrite(), fgets() for input/output
fclose() to close the file.
What are the benefits of using Unix system calls for file I/O?
work with file descriptors, providing more power and flexibility compared to high-level I/O functions.
What is the importance of checking return values in system calls?
Checking return values is crucial as -1 often indicates failure, allowing for error handling.
What does the term 'file I/O concepts and behavior' encompass?
how files are accessed, organized, and safely interacted with in a system that treats nearly everything as a file.
What is the analogy used to explain system calls?
The OS kernel is likened to a librarian who must be requested to fetch resources for the program.
What happens when a system call is made?
The CPU switches to kernel mode to perform the requested action and then switches back to user mode.
What is the significance of system calls in user programs?
the user programs can interact meaningfully with hardware.
What is the role of the C library in system calls?
provides functions that wrap around system calls, making them easier to use in programs.
What is the root directory in Linux?
/, which is the top of the hierarchy.
How are files organized in Linux?
tree-like hierarchy starting at the root.
What are the types of file paths in Linux?
Absolute paths (e.g., /home/user/notes.txt) and relative paths (e.g., ./notes.txt).
What are the types of files accessed through file I/O in Linux?
Regular files, directories, devices, pipes, and sockets.
What does the open system call do in file I/O?
It checks permissions and returns a file descriptor.
What is the difference between buffered and unbuffered I/O?
Buffered I/O (e.g., fread, fgets) stores data temporarily before processing, while unbuffered I/O (e.g., read, write) deals with data immediately.
What are the three categories of permissions in Linux?
Owner (user), group, and others (world).
What do the permission symbols 'r', 'w', and 'x' stand for?
'r' = read, 'w' = write, 'x' = execute.
What happens when you try to access a file without permission?
You cannot open the file—you need permission.
What is the difference between 'w' and 'a' modes in file operations?
'w' = write only (overwrites existing file), 'a' = append only (adds to existing file).
What does the 'r+' mode do when opening a file?
Opens the file for reading and writing; the file must exist.
What is the definition of a network?
a group of two or more computers connected so they can share information.
What is the significance of /dev/null and /dev/random in Linux?
They are examples of device files.
What is the purpose of the close system call in file I/O?
It frees the OS resource associated with the file descriptor.
What does the 'w+' mode do when opening a file?
Opens the file for reading and writing; will create or overwrite the file.
What does the 'a+' mode do when opening a file?
Opens the file for reading and writing; will append at the end.
What is the role of the kernel in file permissions?
checks file permissions before allowing reading or writing.
What does the term 'directories are files that list other files' mean?
Directories in Linux serve as containers that organize and list files within them.
What is a local network example?
home Wi-Fi.
What is the internet described as?
a network of networks.
Why is structure important in networking?
different devices, operating systems, and programs need to communicate, often using different hardware and software.