From bc759a4b6d7a6ae8bae1f6ce29dca541823a7adc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Odd=20Anders=20Hatl=C3=B8y=20Hapnes?= Date: Tue, 26 Aug 2014 09:35:05 +0200 Subject: [PATCH 1/4] Done part 1 & 2 --- ANSWERS.md | 77 ++++++++++++++++++++++++++++++++---------------------- 1 file changed, 46 insertions(+), 31 deletions(-) diff --git a/ANSWERS.md b/ANSWERS.md index 9e5261a..c0fea73 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* +**Answer:** mkdir backups ###Exercise 1b @@ -27,31 +27,42 @@ 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:** ls -al +cd unixstuff +pwd +cd ~ +cd ~/unixstuff/backups ###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 +^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:** cat list1 list2 | grep p | sort ###Exercise 5a @@ -59,59 +70,62 @@ 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* +**Answer:** chmod go -w science.txt +ls -l +chmod 0777 backups ##Shell questions 1. What option with the command `rm` is required to remove a directory? - - **Answer:** *YOUR ANSWER HERE* + - **Answer:** rm -rf 1. What is the command used to display the manual pages for any command? - - **Answer:** *YOUR ANSWER HERE* + - **Answer:** man command 1. What command will show the first 5 lines of an input file? - - **Answer:** *YOUR ANSWER HERE* + - **Answer:** head -5 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:** ls -a 1. What will the command `cat -n file` do? - - **Answer:** *YOUR ANSWER HERE* + - **Answer:** view content of file with numbered lines 1. What will the command `echo -n hello` do? - - **Answer:** *YOUR ANSWER HERE* + - **Answer:** display hello but not on a seperate line 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 (or passwd user) 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:** The command less writes the contents of a file onto the screen a page at a time. 1. With `less` how do you navigate? - - **Answer:** *YOUR ANSWER HERE* + - **Answer:** Press the [space-bar] if you want to see another page, and type [q] if you want to quit reading. 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 mode, insert mode, command line mode 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 line 1. How do you undo the most recent changes? - - **Answer:** *YOUR ANSWER HERE* + - **Answer:** press 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:** gcc -c + the object file contains the machine code for the input file, the object file does not contain addresses of external functions and needs to be linked to be able to run. The object file can be compared to a library. 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* 1. Give a brief pros and cons discussion for the two methods to add debug code shown in Listing 2 and Listing 3. @@ -125,7 +139,7 @@ Use `ls -l` to check that the permissions have changed. 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* 1. How does `make` know if a file must be recompiled? - - **Answer:** *YOUR ANSWER HERE* + - **Answer:** it checks the file modification date 1. Provide a `make` command to use a file named `mymakefile` instead of the default `makefile`. - **Answer:** *YOUR ANSWER HERE* 1. How do you implement an *include guard*, and why is it needed? @@ -141,6 +155,7 @@ Insert your code between the brackets `{}`: double tab_sort_sum( double *tab, int tab_size ) { - } + /* There is a library for doing this */ +} From f56870d59d01668e97f8a79593b4e1c901c778bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Odd=20Anders=20Hatl=C3=B8y=20Hapnes?= Date: Tue, 26 Aug 2014 09:47:19 +0200 Subject: [PATCH 2/4] test --- conditinal_debug.c | 10 ++++++++++ selectable_debug.c | 12 ++++++++++++ 2 files changed, 22 insertions(+) create mode 100644 conditinal_debug.c create mode 100644 selectable_debug.c diff --git a/conditinal_debug.c b/conditinal_debug.c new file mode 100644 index 0000000..595d387 --- /dev/null +++ b/conditinal_debug.c @@ -0,0 +1,10 @@ +#include +int main(void) +{ +#ifdef DEBUG +printf("Some smart debugcomment\n"); +#endif +printf("Hello, world!\n"); +return 0; +} + diff --git a/selectable_debug.c b/selectable_debug.c new file mode 100644 index 0000000..8a41646 --- /dev/null +++ b/selectable_debug.c @@ -0,0 +1,12 @@ +#include +int debug = 0; +int main(void) +{ +if( debug ) +{ +printf("Some smart debugcomment\n"); +} +printf("Hello, world!\n"); +return 0; +} + From 2c52d81fec25c9abad1a30aa22cedcaa7798a05a Mon Sep 17 00:00:00 2001 From: oddhap Date: Sun, 31 Aug 2014 13:29:48 +0200 Subject: [PATCH 3/4] almost done --- ANSWERS.md | 83 ++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 75 insertions(+), 8 deletions(-) diff --git a/ANSWERS.md b/ANSWERS.md index c0fea73..7d47f75 100644 --- a/ANSWERS.md +++ b/ANSWERS.md @@ -127,23 +127,88 @@ chmod 0777 backups - **Answer:** gcc -c the object file contains the machine code for the input file, the object file does not contain addresses of external functions and needs to be linked to be able to run. The object file can be compared to a library. 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 debug enabled: gcc -D DEBUG input.c -o filename + listing 2 debug disabled: gcc input.c -o filename + listing 3 debug enabled: compile normally with gcc input.c -o output, but set int debug = 1; in the code + listing 3 debug disabled: comiple normally with gcc input.c -o output + 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:** +pros using #ifdef DEBUG: +debug code is dropped from executable unless debug flag is set by compiler making the executable smaller +cons: +you have no way to extract/display debug info at runtime using a debugger + +pros using int debug = 0: +debug code is accessible at runtime unless the gcc -O flag is used +cons: +unnessesary data makes the file size larger + + 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:** gcc -o filename -Wl,-Map=mapname.map input.c + the linker ld produces the map file 1. What is the content of each of the sections in a *map* file. Explain briefly. - - **Answer:** *YOUR ANSWER HERE* + - **Answer:** from man ld: + A link map provides information about the link, + including the following: + · Where object files are mapped into memory. + · How common symbols are allocated. + · All archive members included in the link, with a mention of the symbol which caused the + archive member to be brought in. + · The values assigned to symbols. +.text contains the code +.data contains global tables, variables, etc +.bss contains uninitialized arrays, variables etc +.rodata contains strings +.comment & .note contains comments put by the linker/compiler +.stab & .stabstr contains debugging symbols etc + + 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 +/* These are both global vars going into .data */ +int uninitialized_int; //.bss +char string[]="somestring"; //.rodata + + + +int main(void) +{ +printf("Hello, world!\n"); +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:** +double moves the data with movsd and multiplies with mulsd +float moves the data with movss and multiplies with mulss +Other than that the functions looks exactly the same + +mulsd: MULSD xmm1, xmm2/m64 Multiply the low double-precision floating-point value in xmm2/mem64 by low double-precision floating-point value in xmm1. +mulss: MULSS xmm1, xmm2/m32 Multiply the low single-precision floating-point value in xmm2/mem by the low single-precision floating-point value in xmm1. +source: +http://www.jaist.ac.jp/iscenter-new/mpc/altix/altixdata/opt/intel/vtune/doc/users_guide/mergedProjects/analyzer_ec/mergedProjects/reference_olh/mergedProjects/instructions/instruct32_hh/vc211.htm +http://www.jaist.ac.jp/iscenter-new/mpc/altix/altixdata/opt/intel/vtune/doc/users_guide/mergedProjects/analyzer_ec/mergedProjects/reference_olh/mergedProjects/instructions/instruct32_hh/vc212.htm + +In other words: double uses 64bit integers while float uses 32bit 1. How does `make` know if a file must be recompiled? - **Answer:** it checks the file modification date 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:** an include guard is a way to prevent files to get included more than once. +its implemented using special pre-processor code: + +#ifndef SOMEFILE_H +#define SOMEFILE_H +... +#endif + +The way it works is that it checks if SOMEFILE_H is defined, and if its not run the code between #define and #endif. If the file is included twice, SOMEFILE_H will already be defined when running the 2nd inclusion, causing the code below not the be compiled twice. + ##Library Task @@ -151,6 +216,8 @@ Insert your code between the brackets `{}`: void main( int argc, char *argv[] ) { + /* Requires pointers */ + } double tab_sort_sum( double *tab, int tab_size ) From 9597faaf12337695c7a60b18299036f5d4b4cdc0 Mon Sep 17 00:00:00 2001 From: oddhap Date: Mon, 1 Sep 2014 18:14:40 +0200 Subject: [PATCH 4/4] Done --- ANSWERS.md | 86 ++++++++++++++++++++++++++++++++++++++++++---- conditinal_debug.c | 10 ------ l1.c | 23 +++++++++++++ l1.h | 1 + main.c | 67 ++++++++++++++++++++++++++++++++++++ selectable_debug.c | 12 ------- 6 files changed, 170 insertions(+), 29 deletions(-) delete mode 100644 conditinal_debug.c create mode 100644 l1.c create mode 100644 l1.h create mode 100644 main.c delete mode 100644 selectable_debug.c diff --git a/ANSWERS.md b/ANSWERS.md index 7d47f75..95ab930 100644 --- a/ANSWERS.md +++ b/ANSWERS.md @@ -214,15 +214,87 @@ The way it works is that it checks if SOMEFILE_H is defined, and if its not run Insert your code between the brackets `{}`: - void main( int argc, char *argv[] ) - { - /* Requires pointers */ + int main(int argc, char *argv[] ) +{ +//Make a table of size give by an argument on the command line. +////Fill the table with random numbers between MIN and MAX +////MIN = 0.0 if not specified with another value on command line, i.e. optional argument +////MAX = 100.0 if not specified with another value on command line, i.e. optional argument +////Call tab_sort_sum() in lib1 +////Print out table and sum + +//argc = number of command line arguments + +int tblsize; +int MIN = 0; +int MAX = 100; +double *tbl; + +int i; //for loop + + //assume that argv0 = program name + if(argc < 2){ + printf("Usage: %s tablesize min(optional) max(optional)\n", argv[0]); + return 2; + } + tblsize = atoi(argv[1]); + + if(argc > 2){ + MIN = atoi(argv[2]); } - - double tab_sort_sum( double *tab, int tab_size ) - { - /* There is a library for doing this */ + + if(argc > 3){ + MAX = atoi(argv[3]); + } + + + printf("tblsize: %d, min: %d, max: %d\n", tblsize, MIN, MAX); + + tbl = (double*) malloc (tblsize +1); + + //generate random int and fill table + srand(time(NULL)); + for(i = 0; i < tblsize; i++){ + double rnd = rand() % MAX + MIN; + tbl[i] = rnd; + } + + //sort table and get sum + double sum = tab_sort_sum(tbl, tblsize); + + //print table + for(i = 0; i < tblsize; i++){ + printf("%f\n", tbl[i]); + } + + //print sum + printf("Sum: %f\n", sum); + + + return 0; +} + +//http://www.tutorialspoint.com/c_standard_library/c_function_qsort.htm +int cmp(const void * a, const void * b) +{ + return ( *(double*)a - *(double*)b ); +} + +double tab_sort_sum (double *tab, int tab_size){ + //sort the table, return the sum and the sorted table + qsort(tab, tab_size, sizeof(double), cmp); + + double sum = 0; + int i; + + + for (i = 0; i < tab_size; i++){ + sum = sum + tab[i]; + } + + return sum; } + diff --git a/conditinal_debug.c b/conditinal_debug.c deleted file mode 100644 index 595d387..0000000 --- a/conditinal_debug.c +++ /dev/null @@ -1,10 +0,0 @@ -#include -int main(void) -{ -#ifdef DEBUG -printf("Some smart debugcomment\n"); -#endif -printf("Hello, world!\n"); -return 0; -} - diff --git a/l1.c b/l1.c new file mode 100644 index 0000000..7c99841 --- /dev/null +++ b/l1.c @@ -0,0 +1,23 @@ +#include + +//http://www.tutorialspoint.com/c_standard_library/c_function_qsort.htm +int cmp(const void * a, const void * b) +{ + return ( *(double*)a - *(double*)b ); +} + +double tab_sort_sum (double *tab, int tab_size){ + //sort the table, return the sum and the sorted table + qsort(tab, tab_size, sizeof(double), cmp); + + double sum = 0; + int i; + + + for (i = 0; i < tab_size; i++){ + sum = sum + tab[i]; + } + + return sum; +} + diff --git a/l1.h b/l1.h new file mode 100644 index 0000000..11914fd --- /dev/null +++ b/l1.h @@ -0,0 +1 @@ +double tab_sort_sum( double *tab, int tab_size); diff --git a/main.c b/main.c new file mode 100644 index 0000000..5f689f3 --- /dev/null +++ b/main.c @@ -0,0 +1,67 @@ +#include //printf NULL +#include //malloc +#include +#include "l1.h" + +int main(int argc, char *argv[] ) +{ +//Make a table of size give by an argument on the command line. +////Fill the table with random numbers between MIN and MAX +////MIN = 0.0 if not specified with another value on command line, i.e. optional argument +////MAX = 100.0 if not specified with another value on command line, i.e. optional argument +////Call tab_sort_sum() in lib1 +////Print out table and sum + +//argc = number of command line arguments + +int tblsize; +int MIN = 0; +int MAX = 100; +double *tbl; + +int i; //for loop + + //assume that argv0 = program name + if(argc < 2){ + printf("Usage: %s tablesize min(optional) max(optional)\n", argv[0]); + return 2; + } + + tblsize = atoi(argv[1]); + + if(argc > 2){ + MIN = atoi(argv[2]); + } + + if(argc > 3){ + MAX = atoi(argv[3]); + } + + + printf("tblsize: %d, min: %d, max: %d\n", tblsize, MIN, MAX); + + tbl = (double*) malloc (tblsize +1); + + //generate random int and fill table + srand(time(NULL)); + for(i = 0; i < tblsize; i++){ + double rnd = rand() % MAX + MIN; + tbl[i] = rnd; + } + + //sort table and get sum + double sum = tab_sort_sum(tbl, tblsize); + + //print table + for(i = 0; i < tblsize; i++){ + printf("%f\n", tbl[i]); + } + + //print sum + printf("Sum: %f\n", sum); + + + return 0; +} + + diff --git a/selectable_debug.c b/selectable_debug.c deleted file mode 100644 index 8a41646..0000000 --- a/selectable_debug.c +++ /dev/null @@ -1,12 +0,0 @@ -#include -int debug = 0; -int main(void) -{ -if( debug ) -{ -printf("Some smart debugcomment\n"); -} -printf("Hello, world!\n"); -return 0; -} -