diff --git a/ANSWERS.md b/ANSWERS.md index 9e5261a..0f5f5a1 100644 --- a/ANSWERS.md +++ b/ANSWERS.md @@ -19,7 +19,7 @@ Please use [markdown](https://help.github.com/articles/markdown-basics) formatin Make another directory inside the `unixstuff` directory called `backups` -**Answer:** *YOUR ANSWER HERE* +cd unixstuff, mdir backups, (, indicates return press) ###Exercise 1b @@ -27,31 +27,31 @@ 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* +cd NAME gets you to directory NAME, ls lists all files and directory in the current working directory, pwd shows me where i am working ###Exercise 2a Create a backup of your `science.txt` file by copying it to a file called `science.bak` -**Answer:** *YOUR ANSWER HERE* +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* +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* +cat > list2, orange, plum, mango, grapefruit, (ctrl+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* +cat list1 list2 | grep p | sort ###Exercise 5a @@ -59,71 +59,74 @@ Try changing access permissions on the file `science.txt` and on the directory ` Use `ls -l` to check that the permissions have changed. -**Answer:** *YOUR ANSWER HERE* +chmod go-rwx backups, chmod a+rw science.txt ##Shell questions 1. What option with the command `rm` is required to remove a directory? - - **Answer:** *YOUR ANSWER HERE* + - it has to be empty 1. What is the command used to display the manual pages for any command? - - **Answer:** *YOUR ANSWER HERE* + man command 1. What command will show the first 5 lines of an input file? - - **Answer:** *YOUR ANSWER HERE* + - head -5 filename 1. What command can be used to rename a file? - - **Answer:** *YOUR ANSWER HERE* + - mv currentfilename newfilename 1. What option can we given to `ls` to show the hidden files? - - **Answer:** *YOUR ANSWER HERE* + - ls -a 1. What will the command `cat -n file` do? - - **Answer:** *YOUR ANSWER HERE* + - displays content inside the file file and with line numbers 1. What will the command `echo -n hello` do? - - **Answer:** *YOUR ANSWER HERE* + - it adds "hello" to the start of your username, so when you write next time it says helloUsername@computername: 1. What command will display s list of the users who currently logged in in the system? - - **Answer:** *YOUR ANSWER HERE* + - who 1. How do you change password on your account? - - **Answer:** *YOUR ANSWER HERE* + - passwd enter oldpassword enter newpassword enter newpassword enter (enter = enterbutton) 1. How can you list a file in reverse order? - - **Answer:** *YOUR ANSWER HERE* + - ls -r 1. What does the `less` command do? - - **Answer:** *YOUR ANSWER HERE* + - writes out content of a file one page at a time 1. With `less` how do you navigate? - - **Answer:** *YOUR ANSWER HERE* + - space bar for another page and q for quit 1. What command will display the running processes of the current user? - - **Answer:** *YOUR ANSWER HERE* + - ps 1. What command can be used to find the process(es) consuming the most CPU? - - **Answer:** *YOUR ANSWER HERE* + - sleep 100 + C (control c), kill PID_number ##vi questions 1. How do we save a file in `vi` and continue working? - - **Answer:** *YOUR ANSWER HERE* + - :w filename 1. What command/key is used to start entering text? - - **Answer:** *YOUR ANSWER HERE* + - "i"for insert, escape for going back to command 1. What are the different modes the editor can be in? - - **Answer:** *YOUR ANSWER HERE* + - insert and command 1. What command can be used to place the cursor at the beginning of line 4? - - **Answer:** *YOUR ANSWER HERE* + - :# 4 # 1. What will `dd` command do (in command-mode)? - - **Answer:** *YOUR ANSWER HERE* + - deletes the line that the cursor is on 1. How do you undo the most recent changes? - - **Answer:** *YOUR ANSWER HERE* + - "u" for undo 1. How do you move back one word? - - **Answer:** *YOUR ANSWER HERE* + - "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* + - gcc -c hello.c -o hello.o, you dont specify the exe filename and hello.o isnt runnable 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* + - gcc hello.c -D DEBUG hello -o (debug on) gcc hello.c hello -o (debug off). For the other one we can change it off by setting int debug = 0; and take debug on by typing in 1 instead of 0 and compile and runt the program. 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* + - the first one you can run just by taking in the -D DEBUG false or true when you compile, the other one is a little more work since you need to change int debug which is inside the c file. 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* + - gcc hello.c -o hello -Wl,-Map=output.map, linkeren 1. What is the content of each of the sections in a *map* file. Explain briefly. - - **Answer:** *YOUR ANSWER HERE* + - initialized (int somthing set to somthing that is not 0) plomme + - uninitialized (int somthing set to 0) eple + - read only (int const) appelsin 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* + - int eple =0;(.bss) int plomme = 1;(.data) const int appelsin = 2;(.rodata) 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* + - (gcc hello.c -S -o hello.assembly) + - assembly for float: movss and mulss, for double movsd and mulsd, they have different type of ssembly but the start of the assembly are thesame (mov x3 and mul x1) 1. How does `make` know if a file must be recompiled? - **Answer:** *YOUR ANSWER HERE* 1. Provide a `make` command to use a file named `mymakefile` instead of the default `makefile`. diff --git a/hello.c b/hello.c new file mode 100644 index 0000000..09ea4c7 --- /dev/null +++ b/hello.c @@ -0,0 +1,30 @@ + +#include +int debug = 1; +int eple =0; +int plomme = 1; +const int appelsin = 2; + +int main(void) + +{ + if ( debug ) + { + printf("Debugging\n"); + } + printf("Hello, world!\n"); + return 0; + +} + +float fmultiply(float a, float b){ + return a*b;} + + +double dmultiply(double a, double b){ + return a*b; + +} + + +