Chapter Summary Q Flashcards
1: What is applications programming?
Programming of software to provide services for the user directly
1: What is systems programming?
Programming of software that provides services for other software or for the underlying computer system
3: What is a pointer?
It is a variable to store a memory address of another variable. They can be 4-8 bytes, and allow for Pass-by-reference.
Note that multiple pointers can point to the same address.
3: What are two possible errors associated with pointers?
Segmentation fault/NULL Pointer: A pointer is pointing to NULL, crashes program.
Dangling Pointer: Pointer is pointing to random data (cruft data), will cause strange behavior and is difficult to find.
3: How does a pointer to a array work?
A pointer to a array typically points to the first element of the array. As such the following is technically a pointer:
EXAMPLE: int AR[30] = { ------ } AR; //AR = pointer to AR[0] //Also means: AR == &AR[0]; AR+2 == AR[2];
3: How does adding to arrays to increment work?
*AR+4 == AR[0]+4 //So the VALUE at [0] has been incremented by 4
vs.
*(AR+4) == AR[4]
1: What are some things that the OS provides?
File I/O Device I/O Process Managements Virtual Memory Scheduling
1: What is a shell?
A command line user interface for accessing the OS
1: What are the three major Unix shells?
sh : bourne shell
bash: bourne again shell
csh: c shell
3: What are command line arguments and how are they used?
A command line argument is a variable inputed into a function/program from the command line b doing: ./program val1 val2 … valn
They are recieved in the programs main by: int main(int argv, char *argc[]) {.....}
Where: argv = number of cmd-line inputs
argc = the string/character array
1: What is a compiler?
Computer software that transforms source code written in one programming language into another
3: What does the size command do?
size results in a output of:
text = Text segment data = Init data size bss = Unit data size dec = sum of text+data+bss in base 10 hex = same as dec but in base 16 filename = name of .exe
1: What is the basic format for formatting variables with printf
%[flags][width][.precision][length]
1: What is the path from source code to a running program?
.c source file --compile .o object file --link executable file --run program is running!
3: What is stack overflow?
It is a error which occurs when a repeating function call occurs which depletes all free memory and fills the stack
1: What is the basic format for formatting variables with printf
%[flags][width][.precision][length]
3: How does adding to arrays to increment work?
*AR+4 == AR[0]+4 //So the VALUE at [0] has been incremented by 4
vs.
*(AR+4) == AR[4]
1: When formatting with %_, which characters denote:
- an integer?
- an unsigned integer?
- a float with 6 decimal precision?
- a float with exponential precision?
Integer: %d
Unsigned Int: %u
Float with 6 dec.: %f
Float with exp. prec.: %g
1: When formatting with %_, which characters denote:
- a char?
- a string?
- a hexadecimal?
- an octal?
Char: %c
String: %s
Hexa: %x
Octal: %o
1: How does the precision modifier affect the format when applied to:
- a float
- an exp. float
- a string
- %d, %u, %o, %x, %X
- float: precision is how many decimal places
- exp. float: precision is how many digits total
- string: precision is how many characters displayed
- how many leading zeroes are shown
1: What does the “continue” command do?
skip the current item in a for loop and go on to the next item
1: How do you define a constant in C?
define DAYS_IN_YEAR 365
3: If a dynamically allocated variable was modified how would you allocate it and free it?
//Allocate int *a; int *aTemp; a = aTemp = malloc(sizeof(int));
//Free
free(aTemp);
free(a);
1: What does the “break” command do?
break out of the current innermost loop
3: What are other ways to dynamically allocate memory?
malloc(SIZE OF MEMORY) = Assigns a random chunk of memory, be have cruft.
calloc(NUMBER OF ITEMS, SIZE OF ITEM) = Assigns a cleared random chunk.
realloc(PTR FROM C/MALLOC, NEW SIZE) = Re-sizes a previous malloc or calloc. If given SIZE is 0 it de-allocs the PTR. If given PTR is NULL it works the same as a malloc.
*If these fail they return NULL.
3: How are address assigned to bytes of blocks representing a var in the HEAP?
The heap assigns a BLOCK of addresses to a var depending on its required size (i.e. int = 4 bytes, char = 1 byte).
However a pointer only points to the FIRST address of the block, but will return the actual data from the whole block of addresses.
3: What is a pass-by-value in C?
When a non-primitive data type is passed to a function directly. The function will then copy the passed var, manipulate its copy, returns the copy, and discards its copy.
3: What is a pass-by-reference in C?
When a pointer to a variable is passed to a function. The function can then de-reference the pointer and manipulate the variable directly.
3: What are command line arguments and how are they used?
A command line argument is a variable inputed into a function/program from the command line b doing: ./program val1 val2 … valn
They are recieved in the programs main by: int main(int argv, char *argc[]) {.....}
Where: argv = number of cmd-line inputs
argc = the string/character array
1: How do you use the random function?
include
rand() function use:
srand(time(NULL));
double value = rand()/(double) RAND_MAX
1: How do you make an array in C?
int ages[ ]={34,20,18};
int ages[8]={34,20,18}; //the rest are filled with 0s
3: What is a dynamic array?
It is a array which can grow and shrink but does not require the overhead of a linked list. Defined by:
typedef struct {
int size;
Person **p;
} ArrayT
ArrayT myArr; myArr.size = 100; myArr.p = malloc(100*sizeof(struct ArrayT*)); for (...) { p[i] = ... }
3: What is a function pointer and how do you make one?
It is a var that stores a pointer to a functions code in memory so it can call the function even by passing it as a param somewhere else. Similar to eventhandlers in JAVA.
//Pointer: void (*fPtr) (int, float);
//Fnc pointed to: void add (int X, float Y) { .... }
//Call pointer/fnc by:
- add(2, 5.0); //Normally
- fPtr(2, 5.0); //As fPtr
- (*fPtr) (2, 5.0); //De-refed
- int (*fPtr[N] (int, int) = {fnc1, fnc2, …fncN); //As a array
2: What is 00010001 in octal?
21 [base 8]
2: What is 10101100 in hexadecimal?
AC [base 16]
2: What is a bit model?
A method if interpreting a sequence of bits
5: What is a multi-process?
It is when multiple processes execute simultaneously while communicating. This can even include multiple copies of the same program running.
Allows for scheduling of unique process for a group of many tasks. Can also assign specific resources, allowing for decrease in bottleneck and increase in efficiency.
5: What is a multi-thread?
It is a single process with multiple control flows, so multiple tasks can be performed by one CPU which shares the same virtual memory, address space and resources.
Unlike a multi-process, here tasks are usually dependent on each other but must be synchronized to avoid error conditions.
Multi-threading allows for decreased complexity while still allowing multiple events to be handled at the same time.
1: What should functions do?
Take data in, do something, return a results
Have a single purpose
Encapsulate
Be Reusable
5: What is a problem with sharing resources in concurrent computing?
If multiple processes or threads require the same resources then sharing must be coordinated to avoid faults. This can be done by “locking up” the resource with a semaphore.
5: What is a semaphore and what is a mutex?
Semaphore: A variable used to control access to a resource by multiple processes. Waiting threads/processes must be signaled by the thread/processes currently with the resource before they can access it.
Mutex (Mutual exclusion object): A program object which is a specific kind of semaphore where a thread/process currently “holding” the mutex can access it, this is done with binary count.
2: Floating point numbers have some special cases. What are the sign bits, exponent bits, and mantissa bits for the following special cases? 0 infinity -infinity NaN
- | 00000000 | all zeros
0 |11111111 | all zeros
1 |11111111 | all zeros - |11111111 | non-zero
1: What is the coding convention for naming Data Types?
PersonType
BankAccount
2: What are the ASCII and unicode bit models?
Maps binary numbers to characters. 32-> space 62->A 48->0 00->NULL
2: What are the bases of each of the following number systems? What digits/chars does each use?
Binary
Octal
Decimal
Hexadecimal
Binary 2
0 1
Octal 8
0 1 2 3 4 5 6 7
Decimal 10
0 1 2 3 4 5 6 7 8 9
Hexadecimal 16
0 1 2 3 4 5 6 7 8 9 A B C D E F
5: How can a process be manually controlled?
This can be done through shell commands such as:
& - Append to end of command to hide result ps - List PID's kill (PID) - Kills specified PID jobs - Lists all running jobs fg - Continue last suppressed program bf - Same as fg but in background CTRL-Z - Suppress a program CTRL-C - Kill foreground program
5: How can a process be controlled in code?
Instead of shell commands functions can be used:
FORK - fork() will create a child process from that point in the code, it returns the childs PID. A recursive or infinite number of forks is a fork-bomb this will crash the program.
SLEEP - usleep() will sleep a program for a specified number of seconds
PID - getpid() will return the PID of the current process that is running the code
EXEC - This has many variations and allows different code to run with the same PID, causing code to go run a different program instead and not continue to the next line.
WAIT - wait() Waits for a specified child to return, will return the child PID which was a output param.
EXIT - exit() Will exit the current process
WAITPID - waitpid() will wait for a specified PID (child process)
SYSTEM - system() allows us to run a specified shell command, and returns value of system call or -1. i.e. can run system(“ls -a”);
5: What is Inter-process Communication (IPC)?
It is the sending and receiving of information between processes, there are two main methods of doing this which are signals and sockets.
5: In IPC what is a signal?
It is a int-value sent by one process to another on the SAME host machine. It can be used to represent basic commands.
C uses a fixed set of signals found in signal.h, two of which can be user defined:
- # define SIGUSR1
- # define SIGUSR2
To code a signal do:
i) Install signal handler
ii) Send signal
5: In IPC what is a socket?
It is a alternate IPC method that acts as a endpoint for sending and receiving data.
It works by assigning a IP to each host and a port number to each process.
It then uses two layers to handle data:
Transport Layer: A conceptual layer to indicate how data is sent, either TCP or UDP
Network Layer: The method of sending packets from src-host to dest-host.
3: How do you remove from the stack?
First all local vars are removed.
Then it returns control of memory addresses corresponding to return address and pops of the stack.
Params are then removed from the stack.
3: What is dynamic memory allocation?
It is the manual allocation and de-allocation of memory for variables into the heap.
5: What is the TCP Transport Protocol, how does it work?
It is a Client-Server Model which has a server process receiving and actively listening and a client process sending/performing tasks.
Steps to build a server: 1. Include sys/socket.h 2. socket(domain, type, TCP) 3. bind(serv sock ,address, address_length) 4. listen(serv sock, back log) while (true) { 5. accept(serv sock, client addr, client addr_length) while (client up) { 6. recv(client sock, buffer*, buffer_length, flags) 7. send(client sock, buffer, buffer_length, flags) } close(client) } close(server)
Steps to build a client:
*Same except use connect() NOT bind(), set sock_addr to inet_addr(“127.0.0.1”)
5: What is the UDP Transport Protocol, how does it work?
It is a Client-Server Model which has a server process receiving but NOT actively listening and a client process sending/performing tasks.
Steps to build a server:
1. Include sys/socket.h
2. socket(domain, type, TCP)
3. bind(serv sock ,address, address_length)
while (true) {
4. accept(serv sock, client addr, client
addr_length)
5. select(num Desc, read FDS, write FDS, except
FDS=NULL, timeout)
6. recvfrom(sock, buffer, buffer_length, flags,
client_Addr, clientAdd_Length)
7. sendto(sock, buffer, buffer_length, flags,
client_Addr, clientAddr_Length)
close(client)
}
close(server)
Steps to build a client:
*Same method as server.