Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
208 changes: 162 additions & 46 deletions ANSWERS.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,128 +19,244 @@ Please use [markdown](https://help.github.com/articles/markdown-basics) formatin

Make another directory inside the `unixstuff` directory called `backups`

**Answer:** *YOUR ANSWER HERE*
**Answer:** `~/unixstuff$ mkdir backups`

###Exercise 1b

Use the commands `cd`, `ls` and `pwd` to explore the file system.

(Remember, if you get lost, type `cd` by itself to return to your home-directory)

**Answer:** *YOUR ANSWER HERE*
**Answer:** `cd backups` `pwd` `cd ..` `ls`

###Exercise 2a

Create a backup of your `science.txt` file by copying it to a file called `science.bak`

**Answer:** *YOUR ANSWER HERE*
**Answer:** `cp -T sci[tab] science.bak`

###Exercise 2b

Create a directory called `tempstuff` using `mkdir`, then remove it using the `rmdir` command.

**Answer:** *YOUR ANSWER HERE*
**Answer:** `mkdir tempstuff` `rmdir tem[tab]`

###Exercise 3a

Using the above method, create another file called `list2` containing the following fruit: orange, plum, mango, grapefruit. Read the contents of `list2`.

**Answer:** *YOUR ANSWER HERE*

**Answer:**
```
cat > list2
orange
plum
mango
grapefruit
^D
cat list2
```
###Exercise 3b

Using pipes, display all lines of `list1` and `list2` containing the letter 'p', and sort the result.

**Answer:** *YOUR ANSWER HERE*
**Answer:** `grep p list1 list2 | sort`

###Exercise 5a

Try changing access permissions on the file `science.txt` and on the directory `backups`.

Use `ls -l` to check that the permissions have changed.

**Answer:** *YOUR ANSWER HERE*
**Answer:** `chmod go+rw science.txt` `chmod go-rwx backups` `ls -l`

##Shell questions

1. What option with the command `rm` is required to remove a directory?
- **Answer:** *YOUR ANSWER HERE*
- **Answer:** `-r`, `-R` or `-recursive`
1. What is the command used to display the manual pages for any command?
- **Answer:** *YOUR ANSWER HERE*
- **Answer:** `man <cmd>` or `help <cmd>`, `whatis <cmd>`, `info <cmd>`
1. What command will show the first 5 lines of an input file?
- **Answer:** *YOUR ANSWER HERE*
- **Answer:** `head -n 5 <file>`
1. What command can be used to rename a file?
- **Answer:** *YOUR ANSWER HERE*
- **Answer:** `mv <oldfile> <newfile>` or `rename <old> <new> <file(s)>`
1. What option can we given to `ls` to show the hidden files?
- **Answer:** *YOUR ANSWER HERE*
- **Answer:** `-a`
1. What will the command `cat -n file` do?
- **Answer:** *YOUR ANSWER HERE*
- **Answer:** Prints contents of file with enumerated lines.
1. What will the command `echo -n hello` do?
- **Answer:** *YOUR ANSWER HERE*
- **Answer:** Prints "hello" without automatically appending a newline char.
1. What command will display s list of the users who currently logged in in the system?
- **Answer:** *YOUR ANSWER HERE*
- **Answer:** `who` or `users` (`users | sed 's/ /\n/g' | uniq`)
1. How do you change password on your account?
- **Answer:** *YOUR ANSWER HERE*
- **Answer:** `passwd`
1. How can you list a file in reverse order?
- **Answer:** *YOUR ANSWER HERE*
- **Answer:** `tac <file>` (linewise)
1. What does the `less` command do?
- **Answer:** *YOUR ANSWER HERE*
- **Answer:** Lazily reads files and allows for backtracing.
1. With `less` how do you navigate?
- **Answer:** *YOUR ANSWER HERE*
- **Answer:** [space] for next page
1. What command will display the running processes of the current user?
- **Answer:** *YOUR ANSWER HERE*
- **Answer:** `ps` `pstree` `top` `top??`
1. What command can be used to find the process(es) consuming the most CPU?
- **Answer:** *YOUR ANSWER HERE*
- **Answer:** `top`

##vi questions
1. How do we save a file in `vi` and continue working?
- **Answer:** *YOUR ANSWER HERE*
- **Answer:** `:w[Enter]` in normal-mode
1. What command/key is used to start entering text?
- **Answer:** *YOUR ANSWER HERE*
- **Answer:** `i`
1. What are the different modes the editor can be in?
- **Answer:** *YOUR ANSWER HERE*
- **Answer:** normal / insert / visual / select / command-line / ex-mode
1. What command can be used to place the cursor at the beginning of line 4?
- **Answer:** *YOUR ANSWER HERE*
- **Answer:** `4gg`, `4G` or `:4[Enter]`
1. What will `dd` command do (in command-mode)?
- **Answer:** *YOUR ANSWER HERE*
- **Answer:** dd is a linewise delete function
1. How do you undo the most recent changes?
- **Answer:** *YOUR ANSWER HERE*
- **Answer:** `u` `:undo[Enter]`
1. How do you move back one word?
- **Answer:** *YOUR ANSWER HERE*
- **Answer:** `b`

##The C Language and Make tool Questions

1. How do you use `gcc` to only produce the `.o` file? What is the difference between generating only the `.o` file, and building the `hello` executable done in the previous compilation above?
- **Answer:** *YOUR ANSWER HERE*
- **Answer:** `gcc -c <sourcefile>` The object file is not linked
1. Give the command for compiling with `debug` enabled instead of normal compilation for the two examples shown in Listing 2 and Listing 3. Explain how to turn debugging on/off for the two cases.
- **Answer:** *YOUR ANSWER HERE*
- **Answer:** Listing 2: `gcc -DDEBUG <sourcefile>`, `-g` is similar but does not define anything. *(NDEBUG may however NOT be defined)
Listing 3: global var `debug` must be changed in source in order to toggle.
1. Give a brief pros and cons discussion for the two methods to add debug code shown in Listing 2 and Listing 3.
- **Answer:** *YOUR ANSWER HERE*
- **Answer:** Listing 2 requires compile time config but is efficient. Listing 3 can potentially be toggled during runtime but may cause efficiency reduction in tight loops regardless of mode (if self modification is not allowed/implemented)
1. Provide the command for generating the *map* file. Which of the `gcc` tools is responsible for producing a *map* file?
- **Answer:** *YOUR ANSWER HERE*
- **Answer:** `-Wl,-Map=mymap.map` (`-Wl,-Map,mymap.map` for non-GNU). Arguments are passed on as linker is not part of gcc
1. What is the content of each of the sections in a *map* file. Explain briefly.
- **Answer:** *YOUR ANSWER HERE*
- **Answer:** The map shows address resolutions, .data, .code, etc sections size and position, archive members included and allocated symbols and their assigned value.
1. Rewrite `hello.c` to produce entries in the *map* file for `.data`, `.bss`, and `.rodata`. Hint: This can be done by adding one variable for each type to the file.
- **Answer:** *YOUR ANSWER HERE*
- **Answer:**
```
const char debug_cmt[] = "Some smart debugcomment\n";
const char hello[] = "Hello world!\n";
const int retval = 0;
```
1. Add the following function to `hello.c`: `double multiply(double x1, double x2)`, which returns `x1*x2`. Use `gcc` to generate an assembly code listing for the program, and examine the assembly code. What assembly instructions are used to do this? Repeat this task, but now replace `double` with `float`. Explain!
- **Answer:** *YOUR ANSWER HERE*
- **Answer:** `gcc -O -S dmult.c` (took the liberty of optimizing)
Examining dmult.s & fmult.s: `mulsd %xmm1, %xmm0` scalar double as opposed to `mulss %xmm1, %xmm0` scalar single mult. instruction. Single (float) operates on half the bit-size of double. It is thus curious how gcc chooses to use the 128bit XMM registers on both accounts.
1. How does `make` know if a file must be recompiled?
- **Answer:** *YOUR ANSWER HERE*
- **Answer:** The source file is 'newer' than the currently compiled file, or the file does not exist.
1. Provide a `make` command to use a file named `mymakefile` instead of the default `makefile`.
- **Answer:** *YOUR ANSWER HERE*
- **Answer:** `make -f mymakefile`
1. How do you implement an *include guard*, and why is it needed?
- **Answer:** *YOUR ANSWER HERE*

- **Answer:** Include guards prevent common libraries and headers being imported several times. It looks like this:
```
#ifndef IVEBEENHEREBEFORE
#define IVEBEENHEREBEFORE
// do header stuff
#endif
```
##Library Task

Insert your code between the brackets `{}`:

void main( int argc, char *argv[] )
{
**main.c:**
```
#include <stdio.h>
#include <stdint.h>
#include <time.h>
#include <stdlib.h>
// TODO: #include "l1.c"

int main( int argc, char *argv[] )
{
uint8_t debug = 0;
int size = 8;
double min = 0.0;
double range = 100.0;
int i;
char *p;
for ( i = 1; i < argc; i++ )
{
if ( !strcmp( argv[i], "-r" )) // range
{
range = strtod( argv[++i], &p );
if ( !range )
{
printf( "Argument to -r must be a positive number.\n" );
exit(1);
}
}
else if ( !strcmp( argv[i], "-m" )) // minimum
{
min = strtod( argv[++i], &p );
}
else if ( !strcmp( argv[i], "-s" )) // table size
{
size = strtod( argv[++i], &p );
}
else if ( !strcmp( argv[i], "-d" )) // debug
{
debug = -1;
for ( i = 1; i < argc; i++ )
{
printf( "argv[%i]: \"%s\"\n", i, argv[i] );
}
}
}
double div = RAND_MAX / range;
double table[size];

if ( debug )
{
printf( "table size: %i\nminimum: %.2f\nrange: %.2f", size, min, range );
}

double tab_sort_sum( double *tab, int tab_size )
{
}

srand( time( NULL )); // seed random generator
for ( i = size; i >= 0; i-- )
{
table[i] = min + ((double) rand()) / div;
}

double sum = 0.0; //TODO: tab_sort_sum( &table, size );

/*
int cell_width = 4; // TODO: log_2( max ) + 3
char *formatstr[6];
sprintf( formatstr, '%%%dd\n', cell_width ); // e.g. '%10d'
const char *format[] = (const char *) formatstr;
*/
const char format[] = "%8.2f\n";

for ( i = 0; i < size; i++ ) // each row
{
printf( format, table[i] );
}
printf( "SUM: %f\n", sum );
return 0;
}
```
**l1.c:**
unfinished
```
#include <stdlib.h>

int *cmp( const double *a, const double *b )
{
if ( *a > *b ) return 1;
if ( *a < *b ) return -1;
return 0;
}

double tab_sort_sum( double *tab, int tab_size )
{
qsort( tab, tab_size, sizeof(*tab), cmp );
double sum = 0;
int i;
for ( i = 0; i >= 0; i-- )
{
sum += tab[i];
}
return sum;
}
```
**makefile:**
```
main: main.c l1.c
gcc main.c -O -o main
```
3 changes: 3 additions & 0 deletions dmult.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
double multiply( double x, double y ) {
return x * y;
}
14 changes: 14 additions & 0 deletions dmult.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
.file "dmult.c"
.text
.globl multiply
.type multiply, @function
multiply:
.LFB0:
.cfi_startproc
mulsd %xmm1, %xmm0
ret
.cfi_endproc
.LFE0:
.size multiply, .-multiply
.ident "GCC: (GNU) 4.4.7 20120313 (Red Hat 4.4.7-4)"
.section .note.GNU-stack,"",@progbits
3 changes: 3 additions & 0 deletions fmult.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
float multiply( float x, float y ) {
return x * y;
}
14 changes: 14 additions & 0 deletions fmult.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
.file "fmult.c"
.text
.globl multiply
.type multiply, @function
multiply:
.LFB0:
.cfi_startproc
mulss %xmm1, %xmm0
ret
.cfi_endproc
.LFE0:
.size multiply, .-multiply
.ident "GCC: (GNU) 4.4.7 20120313 (Red Hat 4.4.7-4)"
.section .note.GNU-stack,"",@progbits
21 changes: 21 additions & 0 deletions l1.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include <stdlib.h>

int *cmp( const double *a, const double *b )
{
if ( *a > *b ) return 1;
if ( *a < *b ) return -1;
return 0;
}

double tab_sort_sum( double *tab, int tab_size )
{
qsort( tab, tab_size, sizeof(*tab), cmp );
double sum = 0;
int i;
for ( i = 0; i >= 0; i-- )
{
sum += tab[i];
}
return sum;
}

Loading