Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ragnvaldf lab1 submission #22

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
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
121 changes: 83 additions & 38 deletions ANSWERS.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,117 +19,162 @@ 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:** *mkdir -p unixstuff/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:**
```
$ mkdir -p unixstuff/backups
$ cd unixstuff
$ ls
backups
$ cd backups
$ ls
$ cd ../../
$ pwd
/home/ragnvald/labs/lab1
```

###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 science.txt 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 tempstuff*

###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
$ cat list2
orange
plum
mango
grapefruit
```

###Exercise 3b

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

**Answer:** *YOUR ANSWER HERE*
**Answer:** *cat list1 list2|grep p|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 700 unixstuff/backups/
$ chmod 600 science.txt
$ ls -la science.txt unixstuff/backups/
-rw------- 1 ragnvald ragnvald 0 Aug 27 15:45 science.txt

unixstuff/backups/:
total 8
drwx------ 2 ragnvald ragnvald 4096 Aug 27 15:23 .
drwxr-xr-x 3 ragnvald ragnvald 4096 Aug 27 15:23 ..
```

##Shell questions

1. What option with the command `rm` is required to remove a directory?
- **Answer:** *YOUR ANSWER HERE*
- **Answer:** *-r*
1. What is the command used to display the manual pages for any command?
- **Answer:** *YOUR ANSWER HERE*
- **Answer:** *man*
1. What command will show the first 5 lines of an input file?
- **Answer:** *YOUR ANSWER HERE*
- **Answer:** *head -n5 file*
1. What command can be used to rename a file?
- **Answer:** *YOUR ANSWER HERE*
- **Answer:** *mv*
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:** *View line numbers in addition to the text in the file*
1. What will the command `echo -n hello` do?
- **Answer:** *YOUR ANSWER HERE*
- **Answer:** *print hello without trailing newline*
1. What command will display s list of the users who currently logged in in the system?
- **Answer:** *YOUR ANSWER HERE*
- **Answer:** *who*
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*
1. What does the `less` command do?
- **Answer:** *YOUR ANSWER HERE*
- **Answer:** *view file contents*
1. With `less` how do you navigate?
- **Answer:** *YOUR ANSWER HERE*
- **Answer:** *j,k,up,down,pgup,pgdn,enter,space*
1. What command will display the running processes of the current user?
- **Answer:** *YOUR ANSWER HERE*
- **Answer:** *ps x*
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*
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:** *command and insert*
1. What command can be used to place the cursor at the beginning of line 4?
- **Answer:** *YOUR ANSWER HERE*
- **Answer:** *:4*
1. What will `dd` command do (in command-mode)?
- **Answer:** *YOUR ANSWER HERE*
- **Answer:** *Delete the line with the cursor on it*
1. How do you undo the most recent changes?
- **Answer:** *YOUR ANSWER HERE*
- **Answer:** *u*
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:** *Use -c. This will combile and assemble, but not link the object file into an executable*
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:** *Enabling debug in the first file is done by passing -DDEBUG to command line. In the second example it's enabled by setting debug to 1 at runtime.*
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:** *First method will not include debug code in the resulting executable unless explicitly enabled, which will lead to a smaller and more efficient binary. The second method will include debug code, but it does not need to ever run. Enabling the code can be done at runtime if neccessary.*
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=out.map : ld is responsible for producing it.*
1. What is the content of each of the sections in a *map* file. Explain briefly.
- **Answer:** *YOUR ANSWER HERE*
- **Answer:** *.rodata=constants : .bss=uninitialized and zero-initialized variables : .data=Initialized variables*
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:**
```
#include <stdio.h>
const int one = 1;
int two = 2;
int zero = 0;
int uninitialized;
int main(void) {
printf("Hello, world! %d %d %d %d\n",zero,uninitialized,one,two);
return 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:** *movq, movss,mulss,movl is used in float example. movq,movsd,mulsd is used in double example. Instead of using rax for result, eax is used in float example. pushq is used to start multiply routine, and popq to return*
1. How does `make` know if a file must be recompiled?
- **Answer:** *YOUR ANSWER HERE*
- **Answer:** *It keeps track of which files have changed since last compile, and recompiles only what is neccessary*
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:** *#ifdef, #define, #endif preprocessing constructs in headers can act as an include guard. It's needed to prevent files from being included more than once*

##Library Task

Expand Down
19 changes: 19 additions & 0 deletions applib/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
CC=gcc
CFLAGS=-c -Wall -O2
LDFLAGS=
SOURCES=main.c l1.c
OBJECTS=$(SOURCES:.c=.o)
EXECUTABLE=tabsort

all: $(SOURCES) $(EXECUTABLE)

$(EXECUTABLE): $(OBJECTS)
$(CC) $(LDFLAGS) $(OBJECTS) -o $@

.c.o:
$(CC) $(CFLAGS) $< -o $@

clean:
rm *.o ${EXECUTABLE}


18 changes: 18 additions & 0 deletions applib/l1.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include <stdlib.h>
#include <stdio.h>

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

double tab_sort_sum(double *table, int size) {
double sum = 0.0;
int i;
for (i=0; i<size; i++) {
sum+=table[i];
}
qsort(table, size, sizeof(double), cmpfunc);
return sum;
}
4 changes: 4 additions & 0 deletions applib/l1.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#ifndef L1_H
#define L1_H
double tab_sort_sum(double*, int);
#endif
28 changes: 28 additions & 0 deletions applib/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "l1.h"

#define TABLE_SIZE 30

int main(int args, char *argv[]) {
double table[TABLE_SIZE];
srand(time(NULL));
int i;
double min = 0.0;
double max = 100.0;
double sum;
if (args == 3) {
min = atof(argv[1]);
max = atof(argv[2]);
}
for (i=0; i<TABLE_SIZE; i++) {
table[i] = (double)rand() / (double) RAND_MAX * (max-min) + min;
}
sum = tab_sort_sum(table, TABLE_SIZE);
for (i=0; i<TABLE_SIZE; i++) {
printf("%f\n", table[i]);
}
printf("%f\n",sum);
return 0;
}
8 changes: 8 additions & 0 deletions debug1.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include <stdio.h>
int main(void) {
#ifdef DEBUG
printf("Debug\n");
#endif
printf("Hello, world!\n");
return 0;
}
9 changes: 9 additions & 0 deletions debug2.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include <stdio.h>
int debug = 0;
int main(void) {
if(debug) {
printf("Debug\n");
}
printf("Hello, world!\n");
return 0;
}
17 changes: 17 additions & 0 deletions hello.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include <stdio.h>
const int one = 1;
int two = 2;
int zero = 0;
int uninitialized;


float multiply(float x1, float x2) {
return x1*x2;
}


int main(void) {
printf("Hello, world! %d %d %d %d\n",zero,uninitialized,one,two);
printf("%f\n", multiply(3,4));
return 0;
}