Final Superdeck Flashcards

1
Q

3 special files with commands

A

stdin
stdout
stderr
0, 1, 2, respecitively

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

List of special/default directories.

A

/bin
/sbin
/lib
/usr
/etc
/home
/boot
/dev
/opt
/var
/tmp
/proc

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

/bin

A

binaries or executables. System programs like gcc, vim. Essential for the operation of the system itself.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

/sbin

A

System binaries that should only be executed by the root (admin) user.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

/lib

A

Libraries

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

/usr

A

user binaries, libraries, etc.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

/etc

A

Editable text configurations, where configuration files are found

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

/home

A

User home (or personal) directories are here.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

/boot

A

contains files needed to boot the system like the OS kernel

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

/dev

A

Device files. Allows interfacing with I/O devices with special files that are mapped to directories.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

/opt

A

Optional software. Rarely used.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

/var

A

Variables files that are changed as the system operates. Like log files.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

/tmp

A

Temporary files not persisted between system rebootst.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

/proc

A

An imaginary directory made by the kernel to keep track of processes.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How to go to home directory?

A

~ (tilde)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What does cat do?

A

Takes in a file as an argument, and outputs the contents to stdout. If a file is not given, it takes from stdin.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

How to EOF in a command?

A

ctrl-d

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

What does > do in Bash?

A

Output redirection.
Redirects outputs of a command to a file.
Note that > creates (or overwrites).
» creates (or appends)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

What does < do in bash?

A

Input redirection.
Tells the shell to get the input for a command from a specified file instead of a keyboard.
I.e.
command [arguments] < filename

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

Pipelines

A

Take stdout prior command and uses it as stdin to another.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

How to increment a number (variable) in bash?

A

let x=x+1

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

Bash if else

A

if test-command
then
commands
elif test-command
then
commands
else
commands
fi

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

Bash while

A

while test-command
do
commands
done

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

Assembly is a ___ level language.

A

Low-level, since there is a 1-to-1 correspondence between statements and machine instructions.
Assembly is also machine dependent.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Q

What does the OS do?

A

Controls the entire operation of the system. All input and output performed on a computer system are channeled through the OS.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
26
Q

Steps to getting an executable from source code.

A

Pre-processor takes and expands symbol definitions. Inserts header file text.
Compiler - Syntactically correct programs are translated into a lower form, assembly.
Assembler - Translates assembly language statements into actual machine instructions (object code or .o files).
Linker - links all object files together with system libraries to produce an executable file.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
27
Q

Building

A

Compiling/linking a program. Leads to an executable.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
28
Q

Parts of a makefile.

A

target dependencies recipe

recipe must be tab-indented

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
29
Q

Integer format character

A

%i or %d (d for decimal, i detects base)
or %o or %x for octal and hexadecimal

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
30
Q

int minimum size

A

Guaranteed to be no smaller than 16 bits

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
31
Q

Float format specifier

A

%f
or %e for exponential notation

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
32
Q

In class, how many bits were floats and doubles?

A

Float: 32
Double: 64

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
33
Q

How to make a constant (literal) long int?

A

Append an L on the end.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
34
Q

Unsigned int format specifier.

A

%u

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
35
Q

Short-<type> format specifier</type>

A

%h<typespecifier></typespecifier>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
36
Q

int to float and vice versa?

A

Floats are truncated (i.e 3/2 is 1).
Ints have no change.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
37
Q

Control structures with only 1 line

A

Simply omit curly braces. Ends at the semi-colon.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
38
Q

do while

A

do
statement;
while (condition);

or

do {
statements;
} while (condition);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
39
Q

if else with single lines?

A

if (condition)
statement;
else
statement;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
40
Q

C switch case.

A

switch (expression)
{
case value1:
statement1;
….
statementn;
break;
case value2:
statement1;
….
statementn;
break;
default:
statement1;
….
statementn;
break;
}

Note:
ONLY WORKS FOR INTEGER TYPES i.e. int, short, long, long long, char…

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
41
Q

Ways to initialize arrays.

A

type name[N] = …
{1, 2, 3, 4,…} initialize in order (doesn’t have to be all of them)
or
{[0] = 1, [2] = 3,…} specific indices

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
42
Q

In C, strings are…

A

Just char arrays.
Must always end with null-terminator (sometimes called zero-terminator).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
43
Q

Defining a multidimensional array in C

A

type name[n][m]

Notes:
int A[2][2] = {1 2 3 4} is equivalent to { {1,2}, {3,4}} (but not recommended).

can also do
int A[2][2] = {[1][0]=3, [2][2]=4};

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
44
Q

Variables passed to a function are passed…

A

By value.
This means that giving it an int x gives it an equivalent value, but not x. However, giving it a pointer to x lets it access the value at the pointer to change x.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
45
Q

Global variables.

A

Outside of other scopes.
I.e. same scope as the include statements.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
46
Q

Multidimensional arrays implicit size.

A

Cannot be explicit, aside from first.
I.e. A[][] is invalid.
A[10][10] is valid
A[][10] is valid

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
47
Q

What do git commits include?

A

added/removed files
files renamed/moved
insertions/deletions in a file (lines changed)
timestamp
user message
more…

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
48
Q

CFLAGS we use

A

-Wall -Werror -std=c99 -pedantic -g

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
49
Q

-Wall

A

Enables all warnings. can catch questionable constructions.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
50
Q

-Werror

A

Turns warnings into errors. Forces you to fix questionable code.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
51
Q

-std=

A

Selects a specific standard. Standards keep things portable.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
52
Q

-pedantic

A

Issue all warnings demanded by strict C standard.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
53
Q

-g

A

Generate a debugging section in the executable. Needed to use gdb

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
54
Q

How to reference a Makefile variable?

A

$(VAR)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
55
Q

Useful gdb commands

A

step
next (may jump over one line loops, function calls, etc).
info break (list and give info about breakpoints).
clear line-or-funciton-name (removes a breakpoint)
delete breakpoints (duh)
info locals
set var x=2 (sets a variables value)
bt (stacktrace (backtrace). useful when many functions)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
56
Q

Structs without names

A

struct
{
stuff;
} var_name = {stuff};
Note, could use commas on the last line to declare many variables.

This is not recommended.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
57
Q

Program memory layout.

A

Top to bottom. (high to low address)

Command line arguments.
Stack

Heap
Uninitialized data segment
initialized data segment
text/code segment

Dynamic includes from command line arguments to heap. Static elsewhere.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
58
Q

What is the text segment in program memory?

A

Instructions reside here.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
59
Q

What is initialized data segment in program memory?

A

Holds literals and constants.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
60
Q

What is uninitialized data segment in program memory?

A

Contains static and global variables.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
61
Q

What is the stack in program memory?

A

Only can push and pop, which makes stack perfect for nested scopes (functions calling functions).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
62
Q

What happens in program memory when a function is called?

A

An entry is pushed onto the stack including arguments, space for automatically allocated variables inside the function, and a return address.

Once done, it’s popped from the stack.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
63
Q

What is a stack overflow?

A

When the stack overflows. i.e. too many recursive functions.
The stack runs out of space, or runs into the heap.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
64
Q

What is the heap used for in program memory?

A

Dynamic memory allocation.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
65
Q

What happens when a C program is compiled and run?

A

From high level language, compiler translates to assembly. Assembler then translates to machine code to make an object file. Linker combines objects and libraries, resolves references, and makes an executable.

Finally, the loader loads the executable into a suitable memory location and begins execution (which may involve more linking during runtime).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
66
Q

What is a process?

A

An OS concept that can be though of as a container for a single executable. Programs usually only have 1 process, but they could have many.
There can be many running processes at the same time.
The OS provides a method to switch between them (scheduling).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
67
Q

What is a thread?

A

A path of execution.
One process can have one or many threads.
Many threads can run at any given time.
Like processes, they are scheduled by the OS or within each process.
Each thread will need its own stack if multiple in a program.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
68
Q

Where do commands live?

A

Most in standard directories; /bin, /usr/bin, usr/local/bin, /opt/X11/bin
Some commands aren’t binaries, but are shell programs.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
69
Q

How can you find where a binary, header, or source file, is?

A

locate command

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
70
Q

How can you find where a command binary/shell-script is?

A

Use which or whereis command

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
71
Q

What does
char * const charPtr = &c;
do?

A

Makes a constant pointer. c can change, but charPtr will always point to it.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
72
Q

What does
const char *charPtr = &c;
do?

A

Notes that the contents of &c do not change, although charPtr can change.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
73
Q

What does
const char * const charPtr = &c;
do?

A

Makes the pointer and the value both constant.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
74
Q

How do you make a pointer to an array?

A

Make it point to the type held by the array.
I.e. A pointer to an int array is the same as a pointer to an int.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
75
Q

What does the name of an array do without any other operators?

A

Represents the pointer.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
76
Q

Can pointers be compared?

A

Yes. This can be useful at times, for example when checking if something may exceed the bounds of an array.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
77
Q

Function pointer variable declaration syntax.

A

return-type (*pointer-var-name) (arg-type1, …);

i.e. int (*fnPtr) (void);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
78
Q

How are who can access a file controlled?

A

Traditional access permissions and ACLs (Access Control Lists).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
79
Q

Three types of users who can access a file.

A

The owner of the file (owner/user),
a member of a group that the file is associated with (group),
everyone else (other)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
80
Q

Three ways an ordinary file can be accessed

A

read form it
write to it
execute it

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
81
Q

How to see permissions?

A

ls -l

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
82
Q

What is the first bit that comes before permissions?

A
  • for ordinary files
    d for directories
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
83
Q

How to use chmod?

A

symbolic arguments
i.e.
chmod a+rw gives (+) read-write (rw) permissions to all users (a).

Also can use o (other) g (group) u (user or owner)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
84
Q

chmod numeric arguments

A

just give it an octal number.
i.e.
chmod 600 somefile
somefiles permissions would be rw——-
Note that this sets permissions.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
85
Q

Who can access any file on the system?

A

A user with root privileges.

Note that if something is encrypted, they may not be able to understand what’s in a file anyways.

86
Q

What does read write and execute mean for directories?

A

read - list contents
write - add new files/subdirectories to it
execute - lets user cd into it

87
Q

What are ACLs good for?

A

Finger-grained control over which users can access specific directories or files.
Note that ACLs must be enabled for the system.
Note that not all utiliites preserve ACLs when moving, copying, or archiving.
Note that ACLs are not recommended on system files where traditional permissions are sufficient due to performance reasons.

88
Q

What is a file link?

A

A pointer to a file.

89
Q

How to create a hard link to an existing file?

A

ln existing-file new-link
The absence of the -s or –symbolic option means it makes a hard link and not a symbolic link.

90
Q

What is a hard link?

A

Appears as another file. Must have a different name if in the same directory. Can only make a hard link from within the filesystem that holds the file.

Editing the file from any hard link edits the same file.

All hard links are of equal value.

As long as one hard link remains, the file exists. It is removed when all hard links disappear.

91
Q

What is a symbolic link.

A

A pointer to a file. Indirect (like a double pointer). Contains the pathname of the pointed-to file.

Can link to non-existent files, files on different file systems, link to a directory (unlike a hard link).

92
Q

ls

A

list directory contents

93
Q

mkdir

A

make a directory

94
Q

rmdir

A

rmdir remove an empty directory

95
Q

cp

A

copy files

96
Q

mv

A

move or rename files

97
Q

rm

A

delete files/directories (rm -r for non-empty directories)

98
Q

ln

A

create links between files

99
Q

file

A

determine file type

100
Q

stat

A

display file status

101
Q

chmod

A

change access permissions

102
Q

chgrp

A

change group of a file/dir

103
Q

chown

A

change owner of a file/dir (admin only)

104
Q

Process structure

A

Like a file structure, the process structure is hierarchical with parents, children, and a root.

A parent process forks (or spawns) a child process, which in turn can fork other processes.

Initially, the forks are identical, but one is identified as a parent, and the other the child.
fork()

105
Q

What is the init daemon?

A

A Linux system begins execution by starting the init daemon, a single process (called a spontaneous process) with PID 1.

The ancestor of all processes the system and users work with.

106
Q

Process structure, login

A

When a system is in multi-user mode, init (recall the init daemon) calls getty or mingetty processes that display logic prompts on terminals and virtual consoles.

Then, when a user responds (and pushes RETURN), getty or mingetty passes control to the login utility, that checks the user-name and password. After that, the login process becomes the user’s shell process.

107
Q

What happens when the name of a program is given to the command line?

A

The shell forks (fork()) a new process, which create a duplicate of the shell (a subshell).

Recall how fork makes a parent and child process.

The new process attempts to exececute (exec()) the program.

exec() causes a process’s image (the entire memory layout) to be replaced by that of a different executable file (invoking the loader of the system).

Remember:
fork()
exec()
load

108
Q

enums

A

enum nameofnewtype { list, of, permissible, identifiers }

can then assign values like
enum nameofnewtype varname = permisible;
and do things like
if (varname == of)

109
Q

How are enumeration identifiers treated internally?

A

The C compiler treats them as integer constants. Each name sequentially assigned an integer starting with 0.

Note that you can manually control these too.
i.e.
enum whatever { thingone, thingtwo = 42, thingthree };

Note that thingthree then gets value 43.

Can use type cast operators to cast integers to enums.
i.e. thisMonth = (enum month) (monthValue - 1);
makes thisMonth 1 less than monthValue.

110
Q

Unnamed enumerated data types?

A

enum {thing1, thing2, thing3…} varname;

111
Q

typedef

A

Lets you assign an alternate name to a data type.
i.e.
typedef int Counter;
defines the name Counter to be equivalent to int.

Some uses beyond readability.
i.e.
typedef char Linebuf[81];
defines an array of 81 chars, so
Linebuf text;
is equivalent to
char text[81];

112
Q

How to use typedef?

A

Write a statement as if declaring a variable of the underlying type.
Write the name of the type definition where the variable name would normally be.
Put typedef in front of everything.

i.e.
typedef struct
{
int a;
int b;
} nameofnewtype;

113
Q

How are preprocessor statements identified?

A

By the # sign which must be the first non-space character on the line.
Note that their syntax differs.

114
Q

The define statement.

A

define ABCDEF GHIJKL

would replace all instances of ABCDEF with GHIJKL. ABCDEF is the name, GHIJKL is the value.

Note that this doesn’t apply to the inside of character strings.

Note that this is a text replacement, not a variable.

Conventionally, #define names are in capital letters to distinguish them.

115
Q

Reasons to divide a program into many files.

A

It takes longer to compile a larger file.
Larger programs often take the efforts of many programmers.
A single source file can become unmanageable.

116
Q

What is gcc?

A

A C preprocessor, C compiler, and linker.

117
Q

What does gcc -c do?

A

Performs steps 1-3. It doesn’t link files together.

To link them together, omit the -c.

118
Q

When does a file need to include a header file?

A

When it uses, or implements the functions listed in a particular header file.

119
Q

What does the #include statement do?

A

It asks the C preprocessor to literally “include” the code found.

120
Q

Difference between <…> and “…” in an include statement.

A

<…> tells the preprocessor to search system locations. Usually for standard libraries and libraries your program builds.

“…” tells the preprocessor to search first in the current directory, and then in system locations. This is usually used for header files local to your project.

121
Q

How do you add a place to search to the preprocessor’s list?

A

-I (capital i) and a directory.
i.e.
-I/abs/path/to/project
-I. for example tells it to consider the current directory in its list of places to search.

122
Q

How does -I behave differently between the <> and “” versions of #include?

A

-I checks given path before system paths with <>.

-I checks the current directory, then the given path, then system paths.

123
Q

Were does the -I option go in a makefile?

A

CPPFLAGS

124
Q

How to link a static library together?

A

ar rcs libname.a mod1.o mod2.o…
Makes a static library called libname.a that contains mod1.o mod2.o…

125
Q

How must libraries be named?

A

Must start with lib, and end with .a for static or .so for dynamic.

126
Q

How do you link a dynamic library?

A

Use the -L and -l (lowercase L) opinions when invoking gcc in the linking step.
-L followed by a directory (like -L/home/pathstuff) says where to look for library files.
-I (like -Inameoflib) tells gcc to link a specific library from one of the locations given by -L.
For example, -larrayutil (looks for libarrayutil.a)

127
Q

Where to put -L and -I (lowercase L)

A

LDFLAGS

128
Q

Difference between static and dynamic linking?

A

Static linking: Linking of a library before a program is run.

Dynamic linking: Delaying linking until program is run. Uses less space (can share libraries), reduced load time (not all libraries are needed every time the program runs). Allows plugins (dynamically-linked libraries with a standard interface). There are more, but these are examples. Must still use -L and -l (lowercase L) to make an executable that knows where to search for a particular dynamic library.

129
Q

Shell commands in Makefile to make the directories as explained in class.

A

dirname/$(shell uname -s)-$(shell uname -m)

130
Q

How to get exit code of the last process in Bash?

A

$?
Is a special variable that gets the exit code of the last process

131
Q

test expressions

A

test EXPRESSION
where EXPRESSION is
= strings match
!= strings don’t match
-eq integers are equal
-ne integers are not equal
-gt int1 greater than int2
-ge int1 greater than or equal to int2
-lt less than
-le less than or equal to
-e file exists
-f file exists and is an ordinary file
-d file exists and is a directory
-a AND
-o OR

132
Q

How to test if something is a file?

A

-e FILE for if it exists
-f FILE for if it exists and is an ordinary file
-d FILE for if it exists and is a directory

133
Q

Using test with control structures

A

if tests <command></command>
then
stuff
fi

134
Q

What exit code does test use?

A

0 if expression is true.
1 if expression is false.

Note that bash if and while statements check the return code and treat 0 as true and 1 as false*verify.

135
Q

Difference between [ EXPRESSION ] and test EXPRESSION in bash.

A

They are synonymous. However, if you want to use AND and OR, you must use [[ ]] which uses && and ||

Note that there is also [[ ]] which

136
Q

Bash wildcards.

A
  • any character, 0 or more
    ? any character, just 1
    […] Any character in the brackets, 1

Causes bash to generate all matches. I.e. stat myfiles/* calls stat on every file in myfiles/

137
Q

Pattern matching in bash

A

= in [[ ]] tests for pattern match, not just equality.
[[ string = pattern ]]
NOTE: pattern MUST appear on right side of the equal sign.

i.e. abc = a* is true
abc = a?c is true

138
Q

Bash switch case

A

case teststring in
pattern1)
stuff
;;
pattern2)
stuff
;;
esac

139
Q

Bash while loop with inverted condition

A

until. loops until a test holds.
i.e. until i=2 runs as long as i is not 2

until testcommand
do
stuff
done

140
Q

Bash, how can you use spaces in arithemetic expressions?

A

let “val=val * 10 + new”
Enclose it in quotes

141
Q

Bash for loop.

A

for (( expr1 ; expr2 ; expr3 ))
do
commands
done

expre1 is first evaluated
expr2 is the condition
expr3 is run whenever expr2 is true

Recall int i=0; i<10; i++ from C

142
Q

How to make an integer in bash?

A

You don’t all variables are string, but interpretations of those change.

Strings can also be interpreted as lists by separating them with newlines or spaces. Note that lists are different than arrays in bash.

143
Q

Bash
For … in …

A

for loopindex in list
do
commands
done

i.e.
list=”1 2 3 4 5”
for i in $list
do
echo $i
done

or
for i in a b c d e f

144
Q

Bash, how to tell how many variables were passed.

A

$#

145
Q

How to get all parameters passed as a list in bash?

A

$@

146
Q

Bash, how to get the name of the calling program?

A

$0
i.e. ./abc
$0 is ./abc

i.e. /home/name/abc
$0 is /home/name/abc

147
Q

How to move positional parameters?

A

Using the shift command.
$2 goes into $1,
$3 goes into $2…
Repeatedly using shift can be a convenient way to loop over all arguments that expect an arbitrary number of arguments

148
Q

Bash, how can you execute many commands on the same line?

A

Separate with ;
i.e.
x ; y ; z

149
Q

Bash control operators

A

&& and ||

&& causes the shell to test the exit status of the preceding command. If exit code is 0 (indicating success) run the command, else skip it.

|| does the opposite. Runs if it fails.

150
Q

Bash functions.

A

fname ()
{
stuff
}

or

function fname ()
{
stuff
}

Arguments to a function become positional parameters within it.

$# holds number of positional parameters
$0 is unchanged

when return is executed in a function, the function complete and execution continues after where the function was called. Note that you can give a numeric argument to return for an exit status

151
Q

How do variables work with scope in Bash?

A

Normally shared, but can be declared local with the local builtin.
local variables are only visible to the funciton and the commands it invokes
they can shadow variables with the same name declared at previous scopes

152
Q

How to declare an indexed array in bash?

A

declare -a name

153
Q

How to declare an associative array in bash?

A

declare -A name

154
Q

How to automatically create an array and assign a value in bash?

A

name[subscript]=value

name=(value1,value2,…) automatically creates an array and assigns values

155
Q

Bash, how to append to an array?

A

name+=(value1,value2,…)

156
Q

How to reference an element of an array in Bash?

A

$(name[subscript]} gets an element
$(name[@]} obtains the entire array as a list
*verify

157
Q

What does uniq do?

A

Detects repeated lines (they must be adjacent though, so consider using sort).

158
Q

What does find do?

A

Searches for files in a directory hierarchy.
-L to follow symbolic links,
-H to follow links in arguments only
find [-H|-L] path… [operand_expression…]

159
Q

Command substitution.

A

‘command’
or
$(command) but the slides say this one is rarely used.

i.e. $(< file) does the same thing as cat

160
Q

for … in with text substitution

A

i.e.
for i in @([a-z][0-9])

161
Q

Arithmetic evaluation in BASH

A

(( )) is a synonym for let
Can have spaces in the expression without needing double quotes.

162
Q

How to evaluate many arithmetic expressions in one line? In bash

A

Using let, separate with spaces.
Using (( )), separate with commas

163
Q

Difference between malloc and calloc

A

malloc() allocates uninitialized memory
calloc() does the same but initialized to all 0s

calloc() example:
void * calloc(size_t nmemb, size_t size);
the block of memory has nmemb elements each that are size bytes long

164
Q

How do void pointers work?

A

No type. Compiler doesn’t associate one with it.
Cannot be de-referenced with * since the data type it contains is unknown.
Cannot do pointer arithmetic since that needs the C compiler to know the size of each element in bytes.
Pointer comparison is still allowed.
Must be cast into other types before use.

165
Q

How to modify the value held beind a void pointer?

A

typecast
*(int *) ptr) = 4;

166
Q

What does the sizeof operator do?

A

Returns the size of the data element in bytes.

Its arguments can be
a variable
an array name
a basic data type
the name of a derived data type
an expression

167
Q

What do you need to remember with malloc()/calloc()?

A

Always check the returned pointer.

It may return NULL if it fails, in which case something very bad has happened. I.e. the system has run out of memory, or some other error.

168
Q

How does free() know how much memory to free?

A

According to Stack Overflow.

The specified amount of memory to allocate when using malloc() is more than what you give it. It includes extra information, including how big the block is. free() can look at this extra information.

169
Q

What is a memory leak?

A

Memory that has been allocated by the programmer but never free()ed.

170
Q

What is a wild pointer?

A

An uninitialized pointer.

171
Q

What is a dangling pointer?

A

A pointer left pointing to a region of memory that has been free()ed.

172
Q

What does realloc() do?

A

Lets you change the size of a previously allocated memory block (keeping the data).
void * realloc(void *ptr, size_t size);

Note: reallocarray() does the same, but better syntax for arrays.
void * reallocarray(void *ptr, size_t nmemb, size_t size);

173
Q

What happens when realloc called on NULL?

A

Acts like malloc.

174
Q

Does realloc initialize data?

A

If the new size is greater, no.

175
Q

How to move memory between locations?

A

memcpy() and memmove()

void *memcpy(void *restrict dst, cont void *restrict src, size_t n);

void memmove(void (dst, cont void *src, size_t len);

memcpy does not work on overlapping regions
memmove does work on overlapping regions

Note that neither deallocate (free) the src. The memory there is still valid.

Be vary careful when deallocating if regions overlap.

176
Q

What are fast copies about?

A

When moving moving data around, it is fastest when able to move things in words and multiples of words. If it does not align, the compiler will determine the necessary bit shifts and masks to move it anyways.

It can be faster to move bytes until it aligns and then use words until bytes are necessary again…

177
Q

What can you do if you want to break at some point in a program with gdb but only if a condition is met?

A

break filename.c:linenum if condition_involving_vars

178
Q

What does memset do?

A

void *memset(void *s, int c, size_t n);

179
Q

How can you detect memory leaks?

A

valgrind

180
Q

How can floating point variables overflow?

A

Overflow: values too large (towards +infinity or -infinity)
Underflow: very tiny fractions (close to 0 from either positive or negative)

181
Q

git how to make a new branch?

A

git branch newbranchname

182
Q

git how to switch to a new branch?

A

git switch <branch></branch>

or

git switch -c <newbranch></newbranch>

or

git switch -c <newbranch> <startpoint></startpoint></newbranch>

183
Q

git how to merge?

A

git merge <branch></branch>

184
Q

difference between git reset and git reset –hard

A

–hard erases your work too.
just reset unstages changes

185
Q

How to see the commit and other of each line of a file?

A

git blame <file></file>

or

git praise <file></file>

186
Q

What files are opened when a process starts?

A

stdin stdout and stderr

187
Q

When is a file created when you try to open it?

A

When you open it in write or append mode. If in read mode, and it doesn’t exist, and error occurs.

Note: Always check return values from system calls and library funcitons.

188
Q

How to open a file?

A

FILE *someFile = fopen(const char *pathname, const char *mode);

189
Q

fopen() modes.

A

r: read
r+: read and write.
w: truncate and write.
w+: read and write. Creates file if it doesn’t exist.
a: appending. Created if file doens’t exit.
a+: reading and appending. File is created if it doesn’t exist.

190
Q

How to read and write from a file?

A

fprintf() and fscanf() do the same as printf() and scanf(), but take FILE* as the first argument.

For entire lines of data, fputs() and fgets()
fgets(buffer, n, filePtr); (1 less than n chars)
fputs(buffer, filePtr); (note doesn’t include null terminator by default)

191
Q

How to redirect stderr in BASH?

A

command > outFile 2>errorFile
Redirects stdout to outFile and stderr to errorFile

command > outFile 2>&1 redirects stdout to outFile and stderr to stdout.

192
Q

How to close a file?

A

fclose(inputFile);

193
Q

When should you close a file?

A

Best practice is as soon as you are done with it.

194
Q

How to tell if you have attempted to read pas the end of a file?

A

feof(filtePtr)
returns non-zero if an attempt has been made to read past the file end

195
Q

Low level file management. Opening a file.

A

Use open()
int open(const char *pathname, int flags);
int open(const char *pathname, int flags, mode_t mode);

Returns -1 if an error occured.

pathname and flags similar to fopens arguments. when a file is created, mode (access permissions) must be given

Mandatory flags are one of O_RDONLY O_WRONLY O_RDWR
Additionally, can use O_APPEND O_CREAT O_TRUNC
can be combined with bitwise OR

196
Q

Low level I/O, how to close a file?

A

close()
int close(int fd);

Returns non-zero if an error occured.

197
Q

How to get a file descriptor from a FILE *?

A

fileno()

198
Q

How to get a FILE * from a file descriptor?

A

fdopen()

199
Q

How to get the error code for the last system call/library function?

A

errno()

Can check it yourself or use perror() or strerror() to get a description of the error.

200
Q

Low level I/O reading and writing.

A

ssize_t read(int fd, void *buf, size_t count);
ssize_t write(int fd, const void *buf, size_t count);

Let you read from and write to a file descriptor. Return value is number of bytes actually read/written (which may be less than requested).

201
Q

How to change the file offset for a file descriptor?

A

lseek()
off_t lseek(int fd, off_t offset, int whence);

returns the associated file offset.

whence:
SEEK_SET: bytes from beginning of file
SEEK_CUR: bytes from current position
SEEK_END: bytes form end of the file

202
Q

Benefits of OpenSSH?

A

Encrypts all traffic, including passwords, which can thwart attackers.

203
Q

How can a simple awk program be ran?

A

Put in in quotes.

i.e.
awk ‘/h/’ cars

204
Q

What is the basic thing awk does?

A

Pattern matches like grep, but can make changes to the lines.

205
Q

How to make a macro?

A

define MACRO_NAME(arguments) (replacement)

Remember to use parenthesis to prevent accidentally mixing in unexpected ways. I.e. passing x+1 to x * x gets 2x+1 when x^2+2x+1 is likely meant.

i.e.
#define SQUARE(x) ((x) * (x))

206
Q

What does # do in front of a parameter in a macro definition?

A

Replaces the macro argument with a string constant. It literally just inserts double quotes around it. Note that it preserves other quotes and backslashes automatically.

207
Q

Define statment, what does ## do?

A

Joins two tokens together.

208
Q

Conditionals with #define statements?

A

I.e.
#ifdef UNIX
# define DATADIR “/some/path”
#else
# define DATADIR “\some\path”
#endif

209
Q

Conditional operator in C.

A

condition ? isTrueExpression : isFalseExpression

210
Q

Register type qualifier

A

If a function makes heavy use of a variable, you can ask it to make it fast as possible by using register.
i.e.

register int index;
register char *textPtr;

211
Q

Volatile type qualifier

A

Like the opposite of const
Says that the specified variable will change its value explicitly (which prevents it optimizing it out which can be useful for things like device I/O).