User Tools

Site Tools


opus:spring2014:lwall1:journal

C/C++ Programming Journal

January 2014

Week One

*Learned some basic C programming etiquette and vocabulary.

  • Preprocessor director
    • Pulls in a file needed for a process before the process starts, allowing it to run properly, for example:
 #include <stdio.h>
  • It's important to compile C files so that they can actually run. For this you use the gcc command with some modifiers. Example:
$ gcc -o hello hello.c
  • It's important to make sure that whatever ends in .c comes last so that your program doesn't get written over by blank file.
  • Now, you can run the program:
$ ./hello
Week Two

There are things called file pointers. They show where things will display to/from.

  1. STDIN - from the keyboard
  2. STDOUT - to terminal
  3. STERR - to terminal

Some commands:

  1. printf makes stuff appear on the screen via the work of sorcery. It's actually a shortcut for a longer command , fprintf(stdout, “yourmessage”);.

There are two ways to approach problems that you need to solve in C.

  • Mathematically
  • Logically
    • some logical commands
      • or (inclusive or)
      • xor (exclusive or)
      • and
      • not

There are ways of specifying formats using printf:

  • %d - shows signed integer values
  • %u - unsigned integer values
  • %c - ASCII character values (char)
  • %s - array of characters (string)
  • %f - floating point
  • %lf - double
  • %lu - unsigned int
  • %hu - unsigned half int
  • %hhu - unsigned half half int (char)
  • %x - lowercase hexadecimal
  • %X - uppercase hexadecimal

Anything you see in the format of _x_ _ indicates that it is a hexadecimal value. For example:

  • 0x8F represents the binary number 10001111
  • It will likely be important to know the transfer tables for hexadecimal and binary for most any computer science course after a certain level.
hexadecimaloctalunsigned decimalsigned decimalunsigned binary
0 00 00 +0* 0000
1 01 01 +1 0001
2 02 02 +2 0010
3 03 03 +3 0011
4 04 04 +4 0100
5 05 05 +5 0101
6 06 06 +6 0110
7 07 07 +7 0111
8 10 08 -8 1000
9 11 09 -7 1001
A 12 10 -6 1010
B 13 11 -5 1011
C 14 12 -4 1100
D 15 13 -3 1101
E 16 14 -2 1110
F 17 15 -1 1111

*obviously zero has no sign, this is just how the computer handles it.

A lot of your regular punctuation are actually operators in C. There are two main forms:

  • Mathematical
    • +,-,*,/
  • Bitwise Logical
    • | - bitwise or
    • & - bitwise and
    • ^ - bitwise xor
    • ! - bitwise not

Here is an example of the use of arrays in code form:

  1 #include <stdio.h>                                                                    
  2 #include <stdlib.h>
  3 int main ()
  4 {
  5     int i, *scores, sum=0, total;
  6     float average;
  7     printf("How big is the array?\n");
  8     scanf("%d", &total);
  9     scores=(int *)malloc(sizeof(int)*total);
 10     for (i=0; i<total; i++)
 11     {
 12         printf("enter test score %d/%d: ",(i+1), total);
 13         scanf("%d", (scores+i));
 14     }
 15     for (i=0; i<total; i++)
 16         sum= sum + *(scores+i);
 17     average =(float)sum/(float)total;
 18     printf("average is: %3.2f\n", average);
 19     return(0);
 20 }
Some Formatting Nonsense

*so, if you want your output to have all sorts of “organization” and “readability” and other such nonsense spewed by bureaucratic hippies (communists), there is a variety of tools you can utilize in the C/C++ programming system, published by Nintendo.

  • for padding, add this to your print commands in your file:
    • ~$ printf("%4hhd\n", a);

    • the output will look like this:
    4
    5
    6
    7
    8
    9

etc…

  • as you may or may not have been able to tell, each number was padded by four spaces.
  • Moreover, you can do similar but different padding patterns;
  • ~$ printf("%.4hhd\n", a);

  • output:
00004
00005
00006
00007
00008
00009

etc…

  • as you may have guessed, this adds zeroes to the padding.
  • more to come.

HPC Fundamentals Journal

January 2014

Week 1

*Tuesday

  • Learned about log files. They contain basically a log of everything that has ever happened in whatever system's log files you are looking at. For this, it is useful to use the tail command, in the -f format, for example:
-$ tail -f var/log/daemon.log
  • This will show a continuous, running list of all of the attempted actions that are occurring within a certain location. This may include logins and logouts, running of scripts, and failed script runtimes. This can expand, showing some very interesting phenomena, such as dictionary attacks from china (you get their IP address in your reports).

-Thursday

  1. began the process of messing around with virtual machines. We put four towers together and put debian 7 (wheezy) on them, essentially.
    1. First, we attained the towers from the Pile O' Computers located in the middle of the room. We then attained all of the necessary materials, e.g. working hard drives, memory, power cords, monitors, keyboards, etc. Then we plugged everything in and started them up, and manually installed Debian 7 via an ethernet port in the offbyone subnet. This took some time. We will likely continue this project further to do other cool things with it.

Week 2

*We've continued working on the Virtual Machine project. We got onto our new machines and began installing important extras, like vim and Xen.

  • To get these packages installed, we first had to ensure that the machine could effectively connect to the servers so that we could download the extensions without saturating our connection. To do this, we had to edit the file “sources.list” so that all of the paths ended with “contrib non-free”.
  • After that, we had to use a new command pretty extensively: aptitude. This command is developed from the earlier format, apt, which required more modifiers in order to use. For example:
$ apt-get install package

or

$ apt-cache search package

versus:

$ aptitude search package

or

$ aptitude install package

-Some other formats of aptitude are:

  1. aptitude update
  2. aptitude upgrade
  • We began with installing vim, which just makes editing files easier.
    • command line:

      $ aptitude install vim

  • We also installed Xen, a VMM (Virtual Machine Manager), which will be useful for setting up the actual virtual machines on the computer. This involved a few steps.
    • First, we simply got Xen using Aptitude, set up like so: aptitude install xen-linux-system
    • Next, we had to prioritize booting Xen. Every start-up process is assigned a number which determines the priority of the process, or in what order they start up. The higher the number, the lower the priority. The typical, native booting process that takes Xen's place has a number of 10, so in order to have Xen start successfully, it needs to have at least a 9 or above. We used this command to switch Xen from 20 to 8:
      • $ dpkg-divert --divert /etc/grub.d/08_linux_xen --rename /etc/grub.d/20_linux_xen

    • In the future, we will have to run the update-grub command to ensure that we're using the most current processes.
    • Next, we had to edit the /etc/network/interfaces file so that it looks like this:
      • #The loopback network interface
        auto lo
        iface lo inet loopback
        
        iface eth0 inet manual
        
        auto xenbr0
        iface xenbr0 inet dhcp
           bridge_ports eth0

    • We also configured the guest behavior on host reboots, so that the last pages that were up before rebooting don't get printed again. We find this process extraneous and unclean.
    • We don't like unclean things.
    • The file that we had to change was /etc/default/xendomains. We changed it by modifying these parameters:
      • XENDOMAINS_RESTORE=false
        XENDOMAINS_SAVE=""

  • Today(Thursday) I created my virtual machine, heretic. This was done using this/these command(s):
xen-create-image --verbose --hostname=test --dhcp --vcpus=2 --pygrub --dist=wheezy --force
  • after this, I did various little bits of customizations, such as:
    • adding a user (horus)
      • I did this with the useradd command, and then the passwd command:
$ useradd horus
$ passwd horus
  • installing vim using aptitude install
    • I also customized my vim experience by creating a .vimrc file in my home directory, and adding parameters that would include colored syntax, number lines, a cursor line, and some modified widths for shift and tab.
  • creating a .bashrc file that would modify my general usage of the shell, adding colored file and directory names, and also taking advantage of some of the alias customizations, such as shortening “aptitude search” to “aps” and “aptitude install” to “api”.
  • I also applied these .vimrc and .bashrc changes to the horus profile, so that users of that profile could enjoy these lovely additions.
  • I also installed bsdgames and bsdgames-nonfree, for the funtimes.
Taking Dumps
  • sometimes you just gotta eliminate some waste. Administratively, this usually comes in the form of “taking dumps”. Yes. You did not misread. You can take these dumps in a variety of ways with a degree of efficiency and cleanliness ranging from the finest gold-engraved porcelain thrones in a sanctuary of privacy to squatting down in the middle of a street in Dubai.
    • These dumps have many levels, ranging from about 0 to 10 or so.
    • There are several modifiers that you can use for altering this command
      • -f will send the dump to a specific file. For this command, you have to have a specific format:
        • [dump] [modifiers] [location/name] [what you're dumping]
        • ex:

          ~$ dump -f /home/temp/trash badfile.c 

      • Dump can also be paired with other commands with Good Guy Grep:
        • ~$mkdir home/temp/trash|dump -f /home/temp/trash badfile.c 

opus/spring2014/lwall1/journal.txt · Last modified: 2014/04/14 12:13 by lwall1