midterm superdeck Flashcards

1
Q

Range of a typical short int in C.

A

-2^15 to 2^15-1
This is 4 bytes.
-32768 to 32767

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

3 Components of a makefile.

A

target, dependency, recipe

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

Makefile, what determines if the recipe is performed?

A

The timestamp difference between the target and dependency. (I.e. If the dependency has changed, i.e. an extra file that hasn’t changed and doesn’t need to be recompiled).

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

Bash loop structure.

A

for condition
do
done

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

Bash if statement structure.

A

if
then
else
fi

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

How might one iterate through every file that starts with the command-line argument in bash?

A

for i in $1*

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

General command format

A

command -[options] [arg1] [arg2] … [argn]

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

What are the sources of input and output for commands?

A

stdin 0
stdout 1
stderr 2
These are 3 special files

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

Where does everything in Unix start?

A

The root (/).

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

What are the two main ways to get information about a command?

A

Using:
man command
or
command –help

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

cat command.

A

cat file
Takes input from file and puts it to stdout

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

Redirection in bash.

A

Output redirection
> overwrites a file
» appends a file

Input redirection
<
for example
command [arguments] < filename

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

How to type EOF in bash?

A

ctrl-d

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

echo command

A

Takes input (arguments) and puts it in stdout.

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

Bash comparison.

A

==, !=, <, etc for strings.
-eq, -ne, -lt, etc for math.

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

Bash else if.

A

if
then

elif
then

else

fi

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

What are the steps to the full process of building a file?

A

Preprocessor. Take definition, insert header files, make a program.
Compiler. Syntactically correct program translated into lower form (assembly).
Assembler. Translate assembly language statements into actual instructions. object code or .o file.
Linker. All object files linked together with system libraries to produce executable file.

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

all in a makefile.

A

all must be the first target.
Conventionally builds the entire project.
Has one dependency.
No recipe.

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

clean in a makefile.

A

Has no dependencies.
Conventionally removes everything made by the build process (executables and .o files)

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

What is .PHONY: used for?

A

This says that its dependencies, such as all and clean, are not real targets and do not produce files named after themselves.

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

Minimum size of int.

A

2 bytes. 16 bits.

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

Float and double format specifier.

A

%f, %e, and %g.

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

Booleans in C.

A

_Bool type (since C99 standard).
0 is false and 1 is true (or rather, anything other than 0 is true). false is 0, true is 1.

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

Making a long int literal.

A

A number followed by ‘L’. For example, making a long int of 2024 is
2024L
Such as
long int x = 2024L;

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

Long format specifiers.

A

Use a preceding lowercase L.
Such as %li

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

Integer format specifer.

A

ints
%i, %d, %x, %o

unsinged int
%u, %x, %o

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

Difference between i++ and ++i

A

i++ increments i once the expression is complete. ++i increments i before the expression is complete.
These can be combined.

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

C switch case?

A

switch (expression)
{
case val1:
statement;

break;

....

{

Note, this only works when expression is some form of int.

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

for statement

A

for (initial-statement; loop-condition; loop-statement)

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

C, conjunctions and disjunction.

A

&& AND, conjunction
|| OR, disjunction

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

Initializing arrays with value

A

type name[] = {t1, t2, t3};

or

type name[] = { [0]=t1, [37]=t2, [101]=t3};

The compiler will infer a size when initialized with values.

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

Initializing multidimensional arrays in C.

A

type name[][];

Odd notes:
Inner braces are optional when assigning a value, but are strongly preferred for the ease of reading.
You can still do
type name[][] = { [0][0] = t1, [37][42] = t2 };

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

How does C treat arguments to function?

A

Always, call by value. C always receives a copy of the argument.

Note that if this argument happens to be a pointer, then it can still alter the original by accessing it with the pointer.

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

How to ensure a value inside a function remain once the function ends?

A

Use the static keyword.

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

How to make a variable accessible everywhere?

A

Define it in the higher (I’m not certain of the correct term) scope.

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

How to make an object file with GCC.

A

Use -c option.

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

List of useful options for this course in makefiles.

A

-Wall
-Werror
-std=c99 (double check this)
-pedantic
-g

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

Variables to use in makefiles.

A

CC (C compiler)
CFLAGS (Things like -Werror, -pedantic, etc).
CPPFLAGS (C preprocessor flags? I don’t know if we’ve done these yet).
LDFLAGS (libraries like -lm)

Note, -c and -o are kept outside of these.

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

What must be done before using gdb?

A

Must have included the -g flag.

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

gdb commands

A

step (single step)
next (next line)
info break (list breakpoints)
clear line-or-function-name (clears breakpoint at some line or function)
delete breakpoints (figure it out)
info locals (list local variables)
set var x=4 (sets x to 4 in this example)
bt (prints a stacktrace)

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

Initializing an instance of a struct

A

struct struct_name var_name = { t1, t2, t3 };
or
struct struct_name var_name = {.a1=t1, .a3=t3 };

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

Struct literal.

A

I think this is correct. Not 100% certain though.
var_name = (struct struct_name) {t1, t2, t3};

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

Making a single struct.

A

You can optionally omit the name.
i.e.
struct
{
int p1;
int p2;
} name[10];
Makes 10 of these structs in an array named name (I think that’s what this does).

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

MEMORY STUFF

A

Complete this. I’m too tired to think about what it means now.

45
Q

Bitwise.

A

& AND
| OR
^ XOR
~ ONES COMPLEMENT (flips all bits)
«X LEFT SHIFT by X (1 if X omitted)
»X RIGHT SHIFT by X (1 if X omitted)

46
Q

Bit shifting by negative amounts.

A

Undefined behavior.

47
Q

Three types of users that can access a file

A

The owner/user. A member of the group associated with the file. Everyone else (other).

48
Q

Three ways to attempt accessing a file

A

Read write execute

49
Q

Quick command to see file permissions

A

ls -l

50
Q

chmod arguments for types of users.

A

a all
g group
o others
Note: g and o can be combined into go

51
Q

Chmod. How to replace letters with numbers?

A

Use octal. Each bit (remember octal is 3 bits) corresponds to a permission.
I.e. 7 is 111 is rwx
5 is 101 is r-x
I.e.
777 gives user, group, other all permissions.

52
Q

Is there a way around permissions?

A

Yes. Someone with root privileges has access to everything, regardless of permissions. Note that files can still be encrypted so they may not be able to understand them anyways.

53
Q

Access permissions for directories.

A

Read lets you list contents.
Write lets you add new files and subdirectories.
Execute lets you do things like cd into it and examine files you have read permission from.

54
Q

What is a file link?

A

A pointer to a file. It associates a filename with a place on the disc.

55
Q

What is a hard link?

A

Appears as another file.
Created with ln existing-file new-link

56
Q

What happens when you remove a hardlink?

A

If it was the last link to a file, the file is unaccessible and removed from the system. If there are other hardlinks to the file, it remains.

57
Q

What is a symbolic link?

A

Like a double pointer to a file. A pointer to a hard link to the file.
Symbolic links can link to a directory, hardlinks can’t. A symbolic link can also point to a different filesystem. They can even point to non-existent files.

58
Q

See file type command.

A

file

59
Q

Change the group associated with a file.

A

chgrp

60
Q

Change owner of a file.

A

chown (admin only).

61
Q

What is a PID?

A

Process identification number.

62
Q

How is the process structure organized?

A

In a hierachy with parents and children and a root (also orphans as taught in unix crash course).

63
Q

What is the root process?

A

the init daemon.

64
Q

fork() and exec()

A

fork() splits a process in 2 (a parent and child process).
exec() causes a process’s image to be replaced by a differente executable.

65
Q

fork() how to tell what is the parent process and what is the child process?

A

include <sys/types.h>

#include <unistd.h></unistd.h>

pid_t pid;
pid = fork();

pid is 0 for child, non-zero for parent, and -1 for failure.

66
Q

Command to list users logged on.

A

who, finger, w

67
Q

Command to send message to users currently logged on?

A

write

68
Q

How to block and enable messages?

A

to block: mesg n
to enable: mesg y

69
Q

What does the compiler use to automatically allocate memory?

A

The stack.

70
Q

Command to see the contents of the stack?

A

Within gdb, bt will show the contents of the stack.
for example: bt –full

70
Q

What happens to the stack when a function is called?

A

When a funciton is called, it is pushed onto the stack that contains the functions arguments, space for automatically allocated variables inside the funciton, and a return address (for when the function is done).

71
Q

What happens to the stack when a function returns?

A

An entry is popped from the stack.

72
Q

What is a stack overflow?

A

When too many functions are recursively called, the stack may run out of space or may run into the heap.

73
Q

What is the heap?

A

Used for dynamic memory allocation. Programmers can use this to allocate data that is not automatically allocated.

74
Q

How does the Stack and Heap progress through memory?

A

The stack usually starts at a high address and goes lower.
The heap usually starts at a low address and goes higher.

75
Q

How does the stack work with threads?

A

Each thread needs its own stack.

76
Q

-Wall

A

Enable warnings for questionable things. Can help prevent potential sources of bugs.

77
Q

-Werror

A

Treat all warnings as errors. Forces you to fix questionable code.

78
Q

-std=

A

Select a specific C standard.

79
Q

-pedantic

A

Issue all warnings demanded by strict C standard.

80
Q

diff command

A

compare two files line by line

81
Q

cmp command

A

compare two files byte by byte

82
Q

gdb how to do one step

A

step

83
Q

gdb how to jump the next line

A

next

84
Q

gdb how to list breakpoints

A

info break

85
Q

gdb how to clear the breakpoints

A

delete breakpoints

86
Q

gdb how to see local variables

A

info locals

87
Q

gdb how to set a variable’s value?

A

set var name=val

88
Q

gdb to to print the stacktrace?

A

bt

89
Q

Parts of program memory as explained in class.

A

Text segment: Instructions reside here.
Initialized data segment: Holds literals and constants in code.
Uninitialized data segment: Contains static and global variables.
The stack: Automatically allocated variables. Nested scopes and functions.
The heap: Dynamic memory allocation.

90
Q

Program memory layout diagram. Lower to higher.

A

Lower address.
Start of static memory layout.
Text/code segment.
Initialized data segment
Uninitialized data segment
End of static memory layout.

Start of dynamic memory layout.
Heap
Stack
Command-line arguments.
End of dynamic memory layout.
Higher address.

91
Q

Defining an unsigned int literal?

A

Put a u on the end.
I.e. 2024u

Can be combined with long.
I.e.
2024ul
or long longs
i.e.
2024ull

92
Q

Float literals with exponents?
Double literals with exponents?

A

Floats:
20.24e2f

The f forces it to be a float since it’s usually a double by default.

Doubles
20.24e2

Note: could also use e-2 for an exponent of -2.

93
Q

How to use exponentials in hexadecimal?

A

Do not use e, since e is a hexadecimal digit.
Use p (for ‘power’). Note, this power is of 2, not 10 or 16.

94
Q

char size

A

1 byte. 8 bits.

Unsigned is the same but goes to 255.

95
Q

int size.

A

2 bytes. 16 bits. Minimum.

96
Q

short int size.

A

2 bytes. 16 bits.

97
Q

long (or long int) size

A

4 bytes. 32 bits.

98
Q

long long (or long long int) size

A

8 bytes. 64 bits.

99
Q

float size

A

Usually 32 bits.

100
Q

double size

A

Usually 64 bits.

101
Q

long double size

A

80 bits, but typically 96 bits or 128 bits in memory with padding bytes.
This one was a bit unclear.

102
Q

C, what happens when trying to assign to a _Bool type?

A

Anything other than 0 is 1. This helps prevent overflows and ensures that equality makes sense.
i.e.
_Bool a = 1;
_Bool b = 2;
(a == b) /* Evaluates to true */

103
Q

C, assigning a function pointer to a variable.

A

example:
int (*my_int_f)(int) = &abs;

Note that & is optional here, but makes it clear we are taking the address of a function.

104
Q

When calling
ls -l
What does the format mean?

A

Example:
-rwxr-xr-x. 1 tim pubs 766 04-15 13:05 ex_file

In order:
type: - or d for file or directory
permissions by group
. ACL flag (i.e. may be + instead)
Links (1 here)
owner (tim)
group (pubs)
size (766)
date and time of modification
file name

105
Q

What is an ACL?

A

A way to provide finer control over which users can access specific directories.

106
Q

What are required for ACLs?

A

They must be enabled for the file system.

107
Q

What do you need to be careful of when using ACLs?

A

They can reduce performance, so they probably shouldn’t be used on system files. Traditional permissions are sufficient for those anyways.
Be careful when moving, copying, and archiving files, since not all utilities will preserve the ACL.

108
Q

When/how does one create a pointer to a file?

A

Using vim, touch, cp, and other means, you create a file by putting a pointer in a directory. These pointers associate filenames with a place on the disk.