Dan Shadeck's fall2014 Opus
Hello! I am Dan, and Welcome to my Opus. I am currently a student pursing the HPC degree path with interests ranging from virtualization to web development. Outside of the realm of information technology I enjoy many hobbies such as fishing and playing music. If you have any questions, comments, or suggestions feel free to shoot me an email at dshadeck@corning-cc.edu.
August 26, 2014
Syllabus day…
August 28, 2014
Today we dove into writing our first simple, but extremely cool, program! This was a much needed change of pace after sitting through what seemed like endless “syllabus day” classes. Here is the source code!
After saving my newly written program it needed to be compiled with gcc!
lab46:~/src/cprog$ gcc -o hello hello.c
It works!
lab46:~/src/cprog$ ./hello Hello, World! lab46:~/src/cprog$
After getting my first C program compiled and tested I needed to push it to my bitbucket hosted mercurial repository. Myself and many other students soon discovered that doing so over https was not possible. Thanks to opus:fall2014:mgardne8 I was able to push to my repository over ssh! The https issue has since been fixed and pushing over https works great!
lab46:~/src/cprog/repo$ hg push http://dshadeck@bitbucket.org/dshadeck/CSCS1320 pushing to http://dshadeck@bitbucket.org/dshadeck/CSCS1320 http authorization required for https://bitbucket.org/dshadeck/CSCS1320 realm: Bitbucket.org HTTP user: dshadeck password: real URL is https://bitbucket.org/dshadeck/CSCS1320 searching for changes no changes found lab46:~/src/cprog/repo$
August 31, 2014
Discovered that submit is having an issue with the project names given in the assignment instructions.
lab46:~$ submit cprog intro http://lab46.corning-cc.edu/opus/fall2014/dshadeck/start ERROR! Invalid project for cprog! lab46:~$
An Email fix request has been sent to Matt!
September 2, 2014
Today Matt implemented a new note showcasing system. An author, designer, and reviewer will be chosen at the end of every class to create a copy of the notes for that day.
Aside from the note taking system Matt also introduced a weekly bonus point system!
Today we will be looking at variables.
Constants and data types ex: 73 (constant) integer
3.14 (constant)float 'c' Character '/n' is also a single char. "Hello" array of chars (aka sting) "Hello\0" \0 = null terminator 0xdeadbeef Memory addresses usually reped is hex (variable address)
Variables to make a variable we decale a the type and the name type name; (type name; name=0; or type name=0;
Int char are data types as well
y=x+2; format that c likes. put the variable you are interested in on the left hand side.
star denotes pointer which is a memory variable. int *d; integer pointer much different than variable pointer
d is integer pointer f is a char pointer int b=0x3D HEX, c= 073;OCTAL
in C pointers let us deal with memory addresses
using indirection
September 4, 2014
Var1.c
#include<stdio.h> int main() { int a=5; int b=0x3D, c=073; int *d; char e, *f; e = 'X'; d=&c; f=&e; printf("address of a is : %p\n",&a); printf("address of b is : %p\n",&b); printf("address of c is : %p\n",&c); printf("address of d is : %p\n",&d); printf("address of e is : %p\n",&e); printf("address of f is : %p\n",&f); printf("a contains: %d\n", a); printf("b contains: %d\n", b); printf("c contains: %d\n", c); printf("d contains: %p\n", d); printf("e contains: '%c'(%hhd)\n", e, e); printf("f contains: %p\n", f); printf("d dereferenced is: %d\n", *d); printf("f dereferenced is: '%c'(%hhd)\n", *f, *f); return (0); }
If we look at the var1.c we notice that a, b, c, d, e, and f are all variables available in the main() code block. This is an example of variable scope. Anything that is declared outside of the main() code block would be called global or file scope. With in our main() code block we can also have sub-blocks of code. This is called sub-scope.
Below is a representation of global, block, and sub-scope.
/* Ex: Global Scope */ int c; { /*Ex: Block Scope */ int a; { /*Ex: Sub-Scope */ int b; } }
Declaration and initialization of variables can be done all in one or as two separate things.
This small piece of Var1.c is a good example of this!
int a=5; int b=0x3D, c=073; int *d; char e, *f; e = 'X'; d=&c; f=&e;
Looking at line 4 & 5 in the above code also demonstrates the use pointers and characters.
Char e, *f; is the declaration e = 'X'; is the initialization
*I'm going to need some help on the pointer portion of this :/*
*Pointers Cont.*
To Define a pointer variable proceed with its name in asterisk. I.E.
int *ptr; The '*' informs the compiler that we want a pointer variable to set aside said allotment
of bites in memory.
int j, k;
k = 2; j = 7; <-- line 1 k = j; <-- line 2
In the above, the compiler interprets the j in line 1 as the address of the variable j (its lvalue) and creates code to copy the value 7 to that address. In line 2, however, the j is interpreted as its rvalue (since it is on the right hand side of the assignment operator '='). That is, here the j refers to the value stored at the memory location set aside for j, in this case 7. So, the 7 is copied to the address designated by the lvalue of k.
ptr + 1; would be a memory allocation of 4 decimal. using the unary ++ operator, either pre- or post-, increments the address it stores by the amount sizeof(type) where “type” is the type of the object pointed to. 4 for an integer
References for Chapter 1:
“The C Programming Language” 2nd Edition
B. Kernighan and D. Ritchie
Prentice Hall
ISBN 0-13-110362-8
Format Specifiers
“%p” substitutes a memory address.
We cannot know the actual address of variable as it changes everytime we run the program, so we reference the address of the variable.
a,b,c are integers
“%d” - a format specifier, “%d” is used for integers.
“%d” is for signed integers.
“%u” is for unsigned integers.
“%x/%X” hexidecimal values.
“%c” displays it as an ASCII character http://www.asciitable.com
American Standard Code for Information Interchange.
7-bit/8-bit ASCII
16-bit Unicode (first 8 bytes are ASCII)
other ones, IBM-EBCDIC
e = 'A';
same as:
e = 65;
4 bytes - integer
2 bytes - “%hd” - half signed int (cuts range and size in half)
1 byte - “%hdd” - half half signed int(cuts range and size in half, then cuts both in half again.)
All chars need to be placed in between single quotes as displayed above.
This is compiled with:
gcc var1.c -o var1
or
gcc -o var1 var1.c
as long as the source code file (var1.c)isn't directly after the -o flag.
program is run with:
./var1
September 9, 2014
Int a; we can only put one int in that memory space.
the need to have multiple numbers is evident.
if we have multiple values we would have multiple variables at this point in time.
name variable names relevantly. make them small and meaningful something that defines the code.
example: 4 test scores in 7th grade math class
int a; int b; int c; int d; float avg;
(float) is a type cast. imposing rules on somthing.
remember to upload notes from cscs1240 flash drive tomorrow.
1 #include <stdio.h> 2 int main() 3 { 4 printf("a char is %d bytes\n", sizeof(char)); 5 printf("a short int is %d bytes\n", sizeof(short int)); 6 printf("a int is %d bytes\n", sizeof(int)); 7 printf("a long int is %d bytes\n", sizeof(long int)); 8 printf("a long long int is %d bytes\n", sizeof(long long int)); 9
10 return(0); 11 } ~
atoi(3) man 3 atio
int main() { int i, quit=0; int j; SDL_Surface *screen; SDL_Surface *box; SDL_Surface *box2; SDL_Surface *background; SDL_Event event; SDL_Rect offset; SDL_Rect offset2; SDL_Init(SDL_INIT_EVERYTHING); screen=SDL_SetVideoMode(640,480,32,SDL_SWSURFACE); box=IMG_Load("box.bmp"); box2=IMG_Load("box.bmp"); background=IMG_Load("background.bmp"); offset.x = 320; offset.y = 240; offset2.x = 0; offset2.y = 0; while(quit==0) { SDL_BlitSurface(background, NULL, screen, NULL); if (SDL_PollEvent(&event)) { if(event.type==SDL_KEYDOWN) { switch(event.key.keysym.sym) { case SDLK_ESCAPE: quit=1; break; case SDLK_RETURN: offset.x=320; offset.y=240; offset.w=box->w; offset.h=box->h; break; case SDLK_UP: offset.y=offset.y-10; break; case SDLK_DOWN: offset.y=offset.y+10; break; case SDLK_LEFT: offset.x=offset.x-10; break; case SDLK_RIGHT: offset.x=offset.x+10; break; } } else if(event.type == SDL_QUIT) quit = 1; } if(offset2.x <= 0) i=10; else if(offset2.x >=640) i=-10; if(offset2.y<=0) j=10; else if(offset2.y>=480) j=-10; offset2.x +=i; offset2.y +=j; SDL_BlitSurface(box2,NULL,screen,&offset2); SDL_BlitSurface(box,NULL,screen,&offset); SDL_Flip(screen); SDL_Delay(20); } return(0); }
POINTERS ARE JUST MEMORY ADRESSES!
int score [num];
the closer you are to memory management the more you can do.
in the long run the better you become at pointers the easier data structures will become!
an array is “homogeneous type”
struct is a heterogeneous type
arrays and structs are modifiers
structs can be considered programmable datatypes: we can sepcifiy what we want in it
struct node {
int value struct node *next;
}; you can put an array inside of a struct
with a struct we use a data operator
data.value=12
when you see dots being used they are just nesting structs inside structs.
struct node *start; start=(strict node *) malloc(sizeof(struct node));
start=(struct node * if its not a pointer its a dot. if its not a dot is a structure pointer →
a good example of this is when we started using sdl start → next = (struct node*) malloc (sizeof(structnode));
linked list
google SDL_surface struct
and you will find
loops arrays functions structs all help control our sanity.
with object oriented programming
all a class is a struct with some extra capability
in c you cant but functions in structs but you can in classes
Read, write, open close, and append are the five main ideas that come to mind when talking about files.
we can also create, remove/delete, and seek.
lab46:~/src/cprog$ ./filefun cat filefun.c i just read a 'z' i just read a ' ' i just read a 'i' i just read a ' ' i just read a 'r' i just read a 'u' i just read a 'b' i just read a ' ' i just read a 'h' i just read a 'o' i just read a 't' i just read a 'o' i just read a 'g' i just read a 's' i just read a ' ' i just read a 'o' i just read a 'n' i just read a ' ' i just read a 'm' i just read a 'y' i just read a ' ' i just read a 'f' i just read a 'a' i just read a 'c' i just read a 'e' i just read a ' ' i just read a 'w' i just read a 'h' i just read a 'e' i just read a 'n' i just read a ' ' i just read a 'i' i just read a ' ' i just read a 'g' i just read a 'e' i just read a 't' i just read a ' ' i just read a 's' i just read a 't' i just read a 'r' i just read a 'e' i just read a 's' i just read a 's' i just read a 'e' i just read a 'd' i just read a '.' i just read a ' ' i just read a ' ' lab46:~/src/cprog$ cat filefun.c #include <stdio.h> #include <stdlib.h> int main() { char c; int i; int count=0; FILE *fptr; //file describes some important info that allows us to interact with a file. fptr=fopen("hotdog.txt","r"); //doulbe quotes are important because we are using a string if(fptr==NULL) // if statment to determin if the file opens correctly { fprintf(stdout, "Error opening file!\n"); exit(1); } for(i=0;i<48;i++) { c=fgetc(fptr); fprintf(stdout,"i just read a '%c'\n",c); } fclose(fptr); //closing our now open file return(0); } lab46:~/src/cprog$
“object oriented programming is only extra fluff to attempt to hide the code that has to be there out of your site” -Matt Haas
things to think about
polymorphism inheritance encapsulation classes objects
struct example
struct rectangle{
int length; int width;
};
struct rectangle rect1;
rectl.legnth=10; rect1.width=5;
class example
class rectangle{ this whole thing is a class declaration public: means this is accessible outside of the class (can be used like a struct)
int area(); int perimeter(); //member functions rectangle(); //constructor creating something upon immediate execution rectangle(int,int);//parameter list... two args private: //means int length; int width; //member variables
};
access control three levels: public protected private
these come into play when viewing things from the prospective of a class
used to instruct what info is available outside of the class
when we create an object from a class its called “instantiation”
make a header file
#ifndef
August 26, 2014
Syllabus day…
August 28, 2014
The UNIX Philosophy
Three Types of Files
Three Tiers of Ownership
August 31, 2014
Discovered that submit is having an issue with the project names given in the assignment instructions.
lab46:~$ submit Unix intro http://lab46.corning-cc.edu/opus/fall2014/dshadeck/start ERROR! Invalid project for Unix! lab46:~$
An Email fix request has been sent to Matt!
September 2, 2014
Matt implemented a new system of showcasing notes. Each class three students will be chosen for the roles of author, designer, and reviewer. This is a great way of making a organized set of well rounded class notes.
Everyone got to check out Alpine! This is the client used to check out our lab46 email accounts while logged into lab46.
Today we will be setting up our lab46 mercurial repos!
~ universal for home directory!
following the steps here was a great deal of help. http://lab46.corning-cc.edu/haas/fall2014/common/repo
September 4, 2014
Unix file system starts at / home is beneath / dshadeck beneath /home src is beneath that… soo it looks like this!
/home/dshadeck/src
devices such as flash drives etc show up in mnt.
We use to mount things by hand in unix/linux but now we have programs that do this for us.
/ is also a directory seperator
pwd at out home terminal shows the file path /home/dshadeck
changing directory to our src directory and running pwd shows cd pwd /home/dshadeck/src
relative path works off our current location.
absolute path references our entire path from the beginning of the file system
cd .. will take you back a directory.
cd will take you to your home directory “<“redirects stdin from file
”>” redirects stdout to file (writes)
“»” “ ” “ ” (appends)
“2>” redirects stderr to write
“2»”
tail is useful to see log files in real time.
./myprog < input
this can be useful if you have created a program and need to test it with standard input.
/dev/null
basic commands for the files system are held in /bin
Status
First, I would like to remind everybody of this useful command that is now available to us.
lab46:~$ status unix
this will display your progress on attendance, opus, and projects.
Cal
Next, we learned some interesting UNIX commands today.
lab46:~$ cal
This command has several arguments you can find some interesting things out with the day and the year. There is also another command
lab46:~$ ncal (year) -e
This tells you the date of Easter on the given year.
Date
Another command is
lab46:~$ date
This tells you the date. This command has several powerful arguments.
Pom
My personal favorite command we did was
lab46:~$ pom
This stands for phase of moon. It tells you the current state the moon in percentage.
Write
Finally, we learned a command to message other users. Cast a who to see whose on then do the following command.
lab46:~$ write (user)
This will allow you to send a message to the user.
Man
For more information on any commands, get to your shell and man them, man!
lab46:~$ man (optional page number) command
/
We also went through the / directories to get a feeling of what goes where. There is a proc folder in / and it has all the current processes running manifested into directories. It also has cpu information. If your curious on what a t flag at the end of file permissions does, it prevents deletion. The var folder in / has a variety of stuff in this is also a log info.
/usr
The /usr/include folder has header files. The /usr/lib has more library's that applications use. The /usr/local has local modification. The /usr/sbin has secondary tools for the administrator, has daemons a.k.a. servers and manipulation tools. Look under /usr/share if you want to learn about some installed software. /usr/src is where some people put source code that runs on the system.
Archives
After copying the archives to my home directory i extracted them
lab46:~/Documents$ tar -xvjf archive2.tar.bz2 image1.jpg image2.gif image3.png image4.txt lab46:~/Documents$
lab46:~/Documents$ unzip archive1.zip Archive: archive1.zip inflating: image1.jpg extracting: image2.gif inflating: image3.png extracting: image4.txt lab46:~/Documents$
After checking out the image files i determined that files image1 and image3 from archive1.zip and image2 and image4 from archive2.tar.bz2 were viewable/complete. I then arranged/named them according to the directions.
I then added the 4 files to a tar archive
lab46:~/Documents$ tar -cvf myarchive.tar small.txt smallest.jpg big.gif biggest.png small.txt smallest.jpg big.gif biggest.png lab46:~/Documents$
Then i compressed the tar archive with gzip to the directions standards.
lab46:~/Documents$ gzip -n -8 myarchive.tar lab46:~/Documents$
All done!
lab46:~/Documents$ submit unix archives myarchive.tar.gz Submitting unix project "archives": -> myarchive.tar.gz(OK) SUCCESSFULLY SUBMITTED lab46:~/Documents$
Puzzle Box
After copying file.txt to my home directory i check out the manual pages/help for file:
lab46:~$ file --help Usage: file [OPTION...] [FILE...] Determine type of FILEs. --help display this help and exit -v, --version output version information and exit -m, --magic-file LIST use LIST as a colon-separated list of magic number files -z, --uncompress try to look inside compressed files -b, --brief do not prepend filenames to output lines -c, --checking-printout print the parsed form of the magic file, use in conjunction with -m to debug a new magic file before installing it -e, --exclude TEST exclude TEST from the list of test to be performed for file. Valid tests are: apptype, ascii, cdf, compress, elf, encoding, soft, tar, text, tokens -f, --files-from FILE read the filenames to be examined from FILE -F, --separator STRING use string as separator instead of `:' -i, --mime output MIME type strings (--mime-type and --mime-encoding) --apple output the Apple CREATOR/TYPE --mime-type output the MIME type --mime-encoding output the MIME encoding -k, --keep-going don't stop at the first match -l, --list list magic strength -L, --dereference follow symlinks (default if POSIXLY_CORRECT is set) -h, --no-dereference don't follow symlinks (default if POSIXLY_CORRECT is not set) -n, --no-buffer do not buffer output -N, --no-pad do not pad output -0, --print0 terminate filenames with ASCII NUL -p, --preserve-date preserve access times on files -r, --raw don't translate unprintable chars to \ooo -s, --special-files treat special (block/char devices) files as ordinary ones -C, --compile compile file specified by -m -d, --debug print debugging messages Report bugs to http://bugs.gw.com/ lab46:~$
from this i determined that the three file commands i would need are:
lab46:~$ file file.txt file.txt: ASCII text lab46:~$ file -m file.txt file.txt, 1: Warning: offset `This is a simple text file. It contains ASCII text.' invalid file.txt, 1: Warning: type `This is a simple text file. It contains ASCII text.' invalid file: could not find any valid magic files! lab46:~$ file -i file.txt file.txt: text/plain; charset=us-ascii lab46:~$
We can see this is an ASCII text file:
lab46:~$ file file.txt file.txt: ASCII text
lab46:~$ cat file.txt This is a simple text file. It contains ASCII text. lab46:~$
After compressing file.txt this was files output
lab46:~$ gzip file.txt
lab46:~$ file file.txt.gz file.txt.gz: gzip compressed data, was "file.txt", last modified: Thu Sep 18 13:00:08 2014, from Unix lab46:~$
After recompressing file.txt according to the directions i received this output from file:
lab46:~$ gzip -1 file.txt lab46:~$ file file.txt.gz file.txt.gz: gzip compressed data, was "file.txt", last modified: Thu Sep 18 13:00:08 2014, max speed, from Unix lab46:~$
Quotes '-full quote, literal quote “-half qoute, allow for expansion `-back quote, backtick, command expansion
all upercase variables are environment variables
Practicing commands today with wildcards. Went into usr/bin to count files :D
Wildcards:
Wildcard Char | Meaning |
---|---|
? | matches any single character |
* | match 0 or more of any character |
[] | match any one of enclosed characters |
[^] | do not match any of enclosed chars |
Kill
ls -l /bin/ls would make the program “ls” active.
a process is a program in action vs program inaction heh
once it is ran it is given a PID (process identification number) equates to a 16 bit value starting at 1
the max number of processes would be 65536 (2^16)
1 is reserved for the first process on the system (usually INIT)
ps is a tool to view processes…
lab46:~$ ps USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND dshadeck 18549 0.0 0.0 14220 1212 pts/20 Ss Sep25 0:00 /bin/bash dshadeck 18552 0.0 0.3 120816 5192 pts/20 Sl+ Sep25 2:49 irssi dshadeck 22927 0.0 0.1 14044 1952 pts/72 Ss+ 15:54 0:00 -bash dshadeck 23596 0.0 0.1 14056 2072 pts/101 Ss 16:09 0:00 -bash dshadeck 24146 0.0 0.0 11332 1076 pts/101 R+ 16:28 0:00 ps u lab46:~$
to actually use the kill command we can launch cat - this gives us a process to run and later kill
lab46:~$ ps USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND dshadeck 18549 0.0 0.0 14220 1212 pts/20 Ss Sep25 0:00 /bi dshadeck 18552 0.0 0.3 120816 5192 pts/20 Sl+ Sep25 2:50 irs dshadeck 22927 0.0 0.1 14044 1952 pts/72 Ss+ 15:54 0:00 -ba dshadeck 23596 0.0 0.1 14060 2092 pts/101 Ss 16:09 0:00 -ba dshadeck 24283 0.0 0.1 14052 2056 pts/118 Ss 16:34 0:00 -ba dshadeck 24590 0.2 0.0 6424 348 pts/101 S+ 16:37 0:00 cat dshadeck 24598 0.0 0.0 11332 1080 pts/118 R+ 16:37 0:00 ps lab46:~$
we can see that cat is now running under PID 24590.
now to kill this process do:
lab46:~$ kill 24590 lab46:~$ ps USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND dshadeck 18549 0.0 0.0 14220 1212 pts/20 Ss Sep25 0:00 /bin/bash dshadeck 18552 0.0 0.3 120816 5192 pts/20 Sl+ Sep25 2:50 irssi dshadeck 22927 0.0 0.1 14044 1952 pts/72 Ss+ 15:54 0:00 -bash dshadeck 23596 0.0 0.1 14060 2092 pts/101 Ss+ 16:09 0:00 -bash dshadeck 24283 0.0 0.1 14052 2064 pts/118 Ss 16:34 0:00 -bash dshadeck 24661 0.0 0.0 11332 1080 pts/118 R+ 16:39 0:00 ps u lab46:~$
As we can see PID 24590 is no longer running.
kill PID will run the command with -15
Kill can end a process 64 ways. running the following displays them all:
lab46:~$ kill -l 1) SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL 5) SIGTRAP 6) SIGABRT 7) SIGBUS 8) SIGFPE 9) SIGKILL 10) SIGUSR1 11) SIGSEGV 12) SIGUSR2 13) SIGPIPE 14) SIGALRM 15) SIGTERM 16) SIGSTKFLT 17) SIGCHLD 18) SIGCONT 19) SIGSTOP 20) SIGTSTP 21) SIGTTIN 22) SIGTTOU 23) SIGURG 24) SIGXCPU 25) SIGXFSZ 26) SIGVTALRM 27) SIGPROF 28) SIGWINCH 29) SIGIO 30) SIGPWR 31) SIGSYS 34) SIGRTMIN 35) SIGRTMIN+1 36) SIGRTMIN+2 37) SIGRTMIN+3 38) SIGRTMIN+4 39) SIGRTMIN+5 40) SIGRTMIN+6 41) SIGRTMIN+7 42) SIGRTMIN+8 43) SIGRTMIN+9 44) SIGRTMIN+10 45) SIGRTMIN+11 46) SIGRTMIN+12 47) SIGRTMIN+13 48) SIGRTMIN+14 49) SIGRTMIN+15 50) SIGRTMAX-14 51) SIGRTMAX-13 52) SIGRTMAX-12 53) SIGRTMAX-11 54) SIGRTMAX-10 55) SIGRTMAX-9 56) SIGRTMAX-8 57) SIGRTMAX-7 58) SIGRTMAX-6 59) SIGRTMAX-5 60) SIGRTMAX-4 61) SIGRTMAX-3 62) SIGRTMAX-2 63) SIGRTMAX-1 64) SIGRTMAX lab46:~$
The nicest way to terminate a process is to use the hang up method also called SIGHUP.
you can do this by issuing:
lab46:~$ kill -1 PID
the next way is to issue a kill interrupt :
lab46:~$ kill -2 PID
To make learning about kill a little more interesting we copied cat to our home directories and renamed the command with a custom name.
mine was stuff:
lab46:~$ cp /bin/cat stuff lab46:~$ ./stuff
you can see it running in ps with its custom name:
lab46:~$ ps USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND dshadeck 18549 0.0 0.0 14220 1212 pts/20 Ss Sep25 0:00 /bin/bash dshadeck 18552 0.0 0.3 120816 5192 pts/20 Sl+ Sep25 2:50 irssi dshadeck 22927 0.0 0.1 14044 1952 pts/72 Ss+ 15:54 0:00 -bash dshadeck 23596 0.0 0.1 14060 2092 pts/101 Ss 16:09 0:00 -bash dshadeck 24283 0.0 0.1 14056 2096 pts/118 Ss 16:34 0:00 -bash dshadeck 24969 0.5 0.0 6424 348 pts/101 S+ 16:47 0:00 ./stuff dshadeck 24971 0.0 0.0 11332 1080 pts/118 R+ 16:47 0:00 ps u lab46:~$
A good order to try and terminate a process would be
kill -1 kill -15 kill -9
We can also use top to see a real time update stream of what processes are running on the system:
top - 16:57:43 up 12 days, 17:51, 48 users, load average: 0.56, 0.35, 0.25 Tasks: 397 total, 1 running, 391 sleeping, 5 stopped, 0 zombie %Cpu(s): 1.7 us, 4.6 sy, 0.0 ni, 80.7 id, 0.0 wa, 0.2 hi, 0.3 si, 12.4 st KiB Mem: 1535680 total, 1490940 used, 44740 free, 398044 buffers KiB Swap: 131068 total, 37104 used, 93964 free. 524652 cached Mem PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 28580 root 20 0 0 0 0 S 6.0 0.0 2:06.99 kworker/0:1 7 root 20 0 0 0 0 S 4.6 0.0 33:35.05 rcu_sched 26279 avolino 20 0 99900 2160 1032 S 3.0 0.1 0:00.27 sshd 23348 jhauck1 20 0 120780 5236 3164 S 2.0 0.3 2:35.68 irssi 24327 stiwari1 20 0 99900 2160 1028 S 1.7 0.1 0:00.13 sshd 26496 jcliteur 20 0 16044 1736 1052 S 1.7 0.1 0:00.16 top 12133 mp010784 20 0 45872 5804 1324 S 1.3 0.4 0:58.13 mosh-server 23012 mquesad1 20 0 100036 2164 1032 S 1.3 0.1 0:00.16 sshd 26236 acarson1 20 0 99900 2156 1028 S 1.0 0.1 0:00.16 sshd 26498 avolino 20 0 16044 1704 1052 S 1.0 0.1 0:00.14 top 26507 tmosgrov 20 0 16044 1720 1052 S 1.0 0.1 0:00.12 top 26512 acarson1 20 0 16044 1736 1052 S 1.0 0.1 0:00.07 top 26494 nvitull1 20 0 16044 1700 1052 S 0.7 0.1 0:00.18 top 26495 abuck4 20 0 16048 1744 1052 S 0.7 0.1 0:00.14 top 26499 mp010784 20 0 16212 1836 1084 S 0.7 0.1 0:00.14 top 26500 wedge 20 0 13992 1660 1000 S 0.7 0.1 0:00.15 top 26502 dshadeck 20 0 16160 1780 1060 R 0.7 0.1 0:00.15 top 26503 mquesad1 20 0 16044 1720 1052 S 0.7 0.1 0:00.14 top 26504 nsano 20 0 16044 1700 1052 S 0.7 0.1 0:00.08 top 26506 stiwari1 20 0 16044 1720 1052 S 0.7 0.1 0:00.20 top 9646 root 20 0 842960 2612 1568 S 0.3 0.2 1:44.81 nscd 18746 root 20 0 0 0 0 S 0.3 0.0 0:04.58 kworker/u4:1 23187 nsano 20 0 30984 4404 2152 S 0.3 0.3 0:00.36 vi 24255 vgarfiel 20 0 100036 2148 1020 S 0.3 0.1 0:00.52 sshd 24454 tmosgrov 20 0 99900 2152 1024 S 0.3 0.1 0:00.39 sshd 26493 jjacobs7 20 0 16044 1700 1052 S 0.3 0.1 0:00.22 top 26497 vgarfiel 20 0 16044 1700 1052 S 0.3 0.1 0:00.13 top 26501 ahoover3 20 0 16144 1760 1060 S 0.3 0.1 0:00.10 top 26505 ssmit133 20 0 16160 1768 1052 S 0.3 0.1 0:00.12 top 1 root 20 0 15452 36 8 S 0.0 0.0 0:23.28 init 2 root 20 0 0 0 0 S 0.0 0.0 0:00.00 kthreadd 3 root 20 0 0 0 0 S 0.0 0.0 24:33.08 ksoftirqd/0 5 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 kworker/0:0H 8 root 20 0 0 0 0 S 0.0 0.0 0:00.00 rcu_bh 9 root rt 0 0 0 0 S 0.0 0.0 4:47.09 migration/0 10 root rt 0 0 0 0 S 0.0 0.0 2:27.01 watchdog/0 11 root rt 0 0 0 0 S 0.0 0.0 2:09.93 watchdog/1 12 root rt 0 0 0 0 S 0.0 0.0 4:23.34 migration/1 13 root 20 0 0 0 0 S 0.0 0.0 27:45.70 ksoftirqd/1 14 root 20 0 0 0 0 S 0.0 0.0 0:00.00 kworker/1:0 15 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 kworker/1:0H 16 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 khelper 17 root 20 0 0 0 0 S 0.0 0.0 0:00.00 kdevtmpfs
Issuing the command ps aux | grep $USER will allow us to what processes we are running on the system as well:
lab46:~$ ps aux | grep $USER dshadeck 18548 0.0 0.1 66536 2680 ? Ss Sep25 0:01 SCREEN dshadeck 18549 0.0 0.0 14220 1212 pts/20 Ss Sep25 0:00 /bin/bash dshadeck 18552 0.0 0.3 120816 5192 pts/20 Sl+ Sep25 2:52 irssi root 22924 0.0 0.2 99900 4508 ? Ss 15:54 0:00 sshd: dshadeck [priv] dshadeck 22926 0.0 0.1 99900 2156 ? S 15:54 0:00 sshd: dshadeck@pts/72 dshadeck 22927 0.0 0.1 14044 1952 pts/72 Ss+ 15:54 0:00 -bash root 24229 0.0 0.2 99900 4552 ? Ss 16:34 0:00 sshd: dshadeck [priv] dshadeck 24282 0.0 0.1 100036 2144 ? R 16:34 0:01 sshd: dshadeck@pts/118 dshadeck 24283 0.0 0.1 14056 2100 pts/118 Ss 16:34 0:00 -bash dshadeck 26605 0.0 0.0 11332 1064 pts/118 R+ 17:00 0:00 ps u aux dshadeck 26606 0.0 0.0 15228 944 pts/118 S+ 17:00 0:00 grep dshadeck lab46:~$
when using top you can customize they way it behaves by pressing ? and then h:
Help for Interactive Commands - procps-ng version 3.3.9 Window 1:Def: Cumulative mode Off. System: Delay 3.0 secs; Secure mode Off. Z,B,E,e Global: 'Z' colors; 'B' bold; 'E'/'e' summary/task memory scale l,t,m Toggle Summary: 'l' load avg; 't' task/cpu stats; 'm' memory info 0,1,2,3,I Toggle: '0' zeros; '1/2/3' cpus or numa node views; 'I' Irix mode f,F,X Fields: 'f'/'F' add/remove/order/sort; 'X' increase fixed-width L,&,<,> . Locate: 'L'/'&' find/again; Move sort column: '<'/'>' left/right R,H,V,J . Toggle: 'R' Sort; 'H' Threads; 'V' Forest view; 'J' Num justify c,i,S,j . Toggle: 'c' Cmd name/line; 'i' Idle; 'S' Time; 'j' Str justify x,y . Toggle highlights: 'x' sort field; 'y' running tasks z,b . Toggle: 'z' color/mono; 'b' bold/reverse (only if 'x' or 'y') u,U,o,O . Filter by: 'u'/'U' effective/any user; 'o'/'O' other criteria n,#,^O . Set: 'n'/'#' max tasks displayed; Show: Ctrl+'O' other filter(s) C,... . Toggle scroll coordinates msg for: up,down,left,right,home,end k,r Manipulate tasks: 'k' kill; 'r' renice d or s Set update interval W,Y Write configuration file 'W'; Inspect other output 'Y' q Quit ( commands shown with '.' require a visible task display window ) Press 'h' or '?' for help with Windows, Type 'q' or <Esc> to continue
Shells
Traditional bash falls under sh- bourne shell (AT&T)(SYSTEMV) bash- bourne again shell (ps -ef) (pg)
csh -c shell (BSD)(ps aux)(more)
a feature of bash is fg. We launched cat then stopped it and brought it back to the foreground:
lab46:~/src/cprog$ cat ^Z [1]+ Stopped cat lab46:~/src/cprog$ fg 1 cat
Naming a file with a leading . makes the file a hidden file. To see these files do:
lab46:~$ ls -a . .exrc .pine-passfile.old .xsession-errors .. .fontconfig .pinerc .xsession-errors.old .ICEauthority .gconf .pinerc.old Desktop .addressbook .gnome2 .pulse Documents .adobe .gstreamer-0.10 .pulse-cookie Downloads .alpine-smime .gtk-bookmarks .selected_editor Maildir .bash_history .hgrc .signature Music .bash_logout .imsettings.log .spice-vdagent Pictures .bash_profile .indent.pro .ssh Public .bashrc .irssi .swp Templates .cache .lesshst .thumbnails Videos .ccache .local .viminfo closet .config .macromedia .vimrc mail .dbus .mozilla .xinitrc public_html .esd_auth .pine-passfile .xpaint src lab46:~$
To create an alias(in this instance we made one called bob:
lab46:~$ alias bob='echo boo'
now when we run it:
lab46:~$ bob boo lab46:~
Editing PS1 can give you a custom terminal prompt:
lab46:~$ PS1='C:\w> ' C:~>
To change it back:
C:~> PS1='\h:\w\$ ' lab46:~$
h stands for hostname, w stands for working dir
the path variable is a modern day convenience:
lab46:~$ echo $PATH /usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games lab46:~$ op=$PATH lab46:~$ PATH= lab46:~$ who bash: who: No such file or directory lab46:~$ ls bash: ls: No such file or directory lab46:~$ /bin/ls Desktop Documents Downloads Maildir Music Pictures Public Templates Videos closet mail public_html src lab46:~$ lab46:~$ echo $PATH bash: lab46:~$: No such file or directory lab46:~$ /usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games bash: /usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games: No such file or directory lab46:~$ lab46:~$ bash: lab46:~$: No such file or directory lab46:~$ PATH=$op lab46:~$ ls Desktop Documents Downloads Maildir Music Pictures Public Templates Videos closet mail public_html src lab46:~$
Shell Scripts Bash supports if statements and for loops! OMFG!!
include #!/bin/bash for bash! #!/bin/python for python!
A great example of shell scripting would be task5&6 from the dataproc assignment.
HighLVL bash, vbs, python
java
C++
C LowLVL
-gt greater than -ge greater than or equal too -lt lower than -le lower than or equal too -ne not equal -eq equal
we started today by running:
lab46:~/src/unix$ echo $RANDOM 2240 lab46:~/src/unix$ echo $RANDOM 10885 lab46:~/src/unix$ echo $RANDOM 8800 lab46:~/src/unix$ echo $RANDOM 23260 lab46:~/src/unix$ echo $RANDOM 17711 lab46:~/src/unix$
these numbers are “randomly generated” by an algorithm
If we wanted to expand our random number game script we could get the computer to pick the numbers for entry.
Modulus is fancy name for a remander %=modulus 34%5=4 34/5=6
using modulus we can use $RANDOM to only generate numbers between 0-99:
lab46:~/src/unix$ echo $(($RANDOM%100)) 95 lab46:~/src/unix$
by moddifying out command we can make the numbers generated fall between 1-100
lab46:~/src/unix$ echo $((($RANDOM%100)+1)) 100 lab46:~/src/unix$
1 #!/bin/bash 2 3 choice=$((($RANDOM%100)+1)) 4 guess=0 5 while [ "$guess" -lt 6 ];do 6 echo -n "Guess a number:" 7 read number 8 if [ "$number" -eq "$choice" ];then 9 echo "You are correct..." 10 exit 0 11 elif [ "$number" -lt "$choice" ];then 12 echo "Higher" 13 else 14 echo "Lower" 15 fi 16 let guess=$guess+1 17 done 18 exit 0
ASSESSMENT :(
regular expressions (regex) . - match any single char * - 0 or more of the previous [] - char class match one or enclosed [^] - do not match one of enclosed \< - match start of word \> - match end of word
$ - match end of line
( ) grouping \( sed \) - match one or more of the previous
to find all 4 char words in /usr/share/dict:
lab46:/usr/share/dict$ cat words | grep '^....$' | wc -l 3346 lab46:/usr/share/dict$
to find all 4 letter words that end in g:
lab46:/usr/share/dict$ cat words | grep '...g$' | wc -l 6999 lab46:/usr/share/dict$
to find all words 3 letters or more only containing lowercase letters:
lab46:/usr/share/dict$ cat words | grep '[a-z][a-z][a-z]' | wc -l 98182
to find words containing 3 or more lowercase vowels:
lab46:/usr/share/dict$ cat words | grep '.*[aeiouy].*[aeiouy].*[aeiouy]'| wc -l 64422
egrep=grep+moar fgrep= fast grep, no regex
find words that end in “ed” “ing”:
lab46:/usr/share/dict$ cat words | egrep '*(ed)$|(ing)$' | wc -l13412 lab46:/usr/share/dict$
lab46:~$ date -d "$( stat output |grep "Modify" | sed 's/^Modify://g')" +%s 1415743490 lab46:~$ ftime=`date -d "$( stat output |grep "Modify" | sed 's/^Modify://g')" +%s` lab46:~$ echo $ftime 1415743490 lab46:~$
mwhahaha
Finding out how many users have never logged in:
lab46:~$ lastlog | grep 'Never' | wc -l 61 lab46:~$