======Part 1====== =====Entries===== ====Month Day, Year==== This is a sample format for a dated entry. Please substitute the actual date for "Month Day, Year", and duplicate the level 4 heading to make additional entries. As an aid, feel free to use the following questions to help you generate content for your entries: * What action or concept of significance, as related to the course, did you experience on this date? * Why was this significant? * What concepts are you dealing with that may not make perfect sense? * What challenges are you facing with respect to the course? Remember that 4 is just the minimum number of entries. Feel free to have more. =====CPROG Topics===== ==== Standard I/O ==== stdin (Standard Input): Accepts user input from keyboard typically, but can also be from mouse, joystick, etc. stdout (Standard Output): Displays/Prints (data type) information to the terminal or screen. stderr (Standard Error): Mainly used for printing/displaying error messages indicating something went wrong. #include "standard.h" /* My Custom Header File takes care of the usual header files i use for my programs which will be included in the next keyword*/ int main() { char input[16]; char p[2]; int i,j; char num[10]; for(i=0;i<(sizeof(input));i++) { input[i] = 0; } printf("Enter a character: "); scanf("%s", *&input); char n[2]; for(i=0;i<10;i++) { num[i] = i + 48; } for(i=0;i 57)) // { // fprintf(stderr, "%c[%d;%dm[ERROR]: %c[%dm Input is not a number!\n", 0x1B, BOLD, RED, 0x1B, 0); // return -1; // } } } for(i=0;i<(sizeof(input));i++) { printf("0x%x(%d) ",input[i], input[i]); } printf("\n"); return 0; } And when ran outputs this as an example, F5 was pressed as input andoryuu@andoryuu-laptop:~/src/lab46/cprog$ ./stdio Enter a character: ^[[15~ 0x1b(27) 0x5b(91) 0x31(49) 0x35(53) 0x7e(126) 0x0(0) 0x0(0) 0x0(0) 0x0(0) 0x0(0) 0x0(0) 0x0(0) 0x0(0) 0x0(0) 0x0(0) 0x0(0) andoryuu@andoryuu-laptop:~/src/lab46/cprog$ ==== Header Files ==== Header Files are needed so code can be easily included in ones program, so one doesn't have to keep retyping the same code over and over again to get their program to work. Local Header File Include #include "standard.h" System Header File Include #include Local Header File example: standard.h // Standard include files #include #include #include #include #include #include #include #include //Boolean Logic #define FALSE 1 #define TRUE (!FALSE) //Text Attributes #define AAOFF 0 #define BOLD 1 #define UNDERSC 4 #define BLINK 5 #define RVSEVID 7 #define CONCEAL 8 //Foreground Colors #define BLACK 30 #define RED 31 #define GREEN 32 #define YELLOW 33 #define BLUE 34 #define MAGENTA 35 #define CYAN 36 #define WHITE 37 //Background Colors #define BG_BLACK 40 #define BG_RED 41 #define BG_GREEN 42 #define BG_YELLOW 43 #define BG_BLUE 44 #define BG_MAGENTA 45 #define BG_CYAN 46 #define BG_WHITE 47 ==== typedef, enum, union ==== typedef: declare new datatype including struct, union, and enum if needed. enum: enumerate words to a certain value union: "merge" data types into a unique new one typedef example typedef union num { double d; int i; } num; enum example enum fgcolor { BLACK = 30, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE } fg; enum bgcolor { BG_BLACK = 40, BG_RED, BG_GREEN, BG_YELLOW, BG_BLUE, BG_MAGENTA, BG_CYAN, BG_WHITE } bg; union example typedef union num { double d; int i; } num; ==== Code Stages ==== Source: #include "dll.h" #include "standard.h" #include #include #include #include #include #include #include #define ESC 0x1B #define F1 0x70 #define F2 0x71 #define F3 0x72 #define F4 0x73 #define F5 0x74 #define F6 0x75 Node *start, *end, *tmp, *tmp2; int i = 0, input3, input4, onep; int menu; int getkey() { int character; struct termios orig_term_attr; struct termios new_term_attr; tcgetattr(fileno(stdin), &orig_term_attr); memcpy(&new_term_attr, &orig_term_attr, sizeof(struct termios)); new_term_attr.c_lflag &= ~(ECHO|ICANON); new_term_attr.c_cc[VTIME] = 0; new_term_attr.c_cc[VMIN] = 0; tcsetattr(fileno(stdin), TCSANOW, &new_term_attr); character = fgetc(stdin); tcsetattr(fileno(stdin), TCSANOW, &orig_term_attr); return character; } int main() { printf("1. New Doubly Linked List\n"); printf("2. Insert Node into Linked List\n"); printf("3. Remove Node from Linked List\n"); printf("4. Display contents of Linked List\n"); printf("5. Inverted Display of Linked List\n"); printf("6. Quit\n"); for(;;) { menu = getkey(); if(menu == 0x31) { printf("You pressed 0x%x\n!", menu); printf("Enter a value (-1 to quit): "); scanf("%d", &input3); while(input3 != -1) { if(start == NULL) { snode(input3); } else { anode(input3); } printf("Enter a value (-1 to quit): "); scanf("%d", &input3); } display(); } if(menu == 0x32) { printf("Enter a new node to insert data into: "); scanf("%d", &input3); printf("Enter a value: "); scanf("%d", &input4); insertnode(input3, input4); display(); } if(menu == 0x33) { { printf("Enter a node to delete: "); scanf("%d", &input3); delnode(input3); display(); } if(menu == 0x34) { display(); } if(menu == 0x35) { invertdisplay(); } if(menu == 0x36) { exit(0); break; } if(menu == 0x1B) { break; } else { // just chill } } return 0; } Object: lab46:~/src/data$ gcc -c dllmain.c lab46:~/src/data$ ls Makefile cli.c dll.h doubly filesandargs.c fork2 fscanf.c libdlinklist.o linkedlists mtest rank1 rank4 screentest size.c blah dlinklist.h dllmain.c doubly.c fork fork2.c getopt.c libdll.a linkedlists.c mtest.c rank2 revisedlinkedlists screentest.c standard.h cli dll dllmain.o filesandargs fork.c fprintf.c libdlinklist.c libdll.so mod-doubly.c passwdcombo.c rank3 revisedlinkedlists.c size stringmanip.c lab46:~/src/data$ Binary: lab46:~/src/data$ ./dll 1. New Doubly Linked List 2. Insert Node into Linked List 3. Remove Node from Linked List 4. Display contents of Linked List 5. Inverted Display of Linked List 6. Quit You pressed 0x31 !Enter a value (-1 to quit): 2 Enter a value (-1 to quit): 4 Enter a value (-1 to quit): 6 Enter a value (-1 to quit): -1 node[0]: 2 node[1]: 4 node[2]: 6 lab46:~/src/data$ ==== Command Line Arguments ==== CLA: is a program that prior to runtime needs to have additional parameters appending it to do certain things with each flag typically My first attempt at a password cracker though it pretty much failed the first attempt andoryuu@andoryuu-laptop:~/src/passwordcracker$ ./a.out Syntax: ./a.out [password size] [debug flag] 0 andoryuu@andoryuu-laptop:~/src/passwordcracker$ ./a.out 10 0 0 andoryuu@andoryuu-laptop:~/src/passwordcracker$ andoryuu@andoryuu-laptop:~/src/passwordcracker$ ./a.out 10 1 0 char[0]: char[1]: char[2]: char[3]: char[4]: char[5]: char[6]: char[7]: char[8]: char[9]: andoryuu@andoryuu-laptop:~/src/passwordcracker$ ==== Arrays ==== arrays: usually involves a int/char/float/double datatype[10] as an example which is defined to hold 10 characters or 10 valid values for(i=0;i<10;i++) { num[i] = i + 48; } for(i=0;i 57)) // { // fprintf(stderr, "%c[%d;%dm[ERROR]: %c[%dm Input is not a number!\n", 0x1B, BOLD, RED, 0x1B, 0); // return -1; // } } } for(i=0;i<(sizeof(input));i++) { printf("0x%x(%d) ",input[i], input[i]); } printf("\n"); ==== Multidimensional Arrays ==== multidimensional arrays: usually used for like matrix multiplication or anything else that harnesses 2 dimensional or greater dimensional arrays int M,N,P; int i, j, k; int verbose; int **matrix1, **matrix2, **matrix3; int m1rows, m1cols, m2rows, m2cols, m3rows, m3cols; int sum; int portnum; /*****************************************************************/ /* Generating Matrix 2 */ printf("[SERVER-SIDE]: Generating Matrix2...\n"); matrix2 = malloc(m2rows * sizeof(*matrix2[i])); for(i=0;i ==== File Access (Read, Write, Append) ==== Read file: read the contents or a file and either print it to stdout, save it to another file, send it to server/client socket connection, or it is read for variables it contains for the program to run, change variables from file read at runtime lab46:~/src/sysprog$ ./confviewer confviewer.c #include #include #include int main(int argc, char *argv[ ]) { char *filename; strcpy(filename, argv[1]); FILE *file = fopen(filename, "a+"); if(file != NULL) { char line[256]; line[sizeof(file)]; while(fgets(line,sizeof(line), file) != NULL) { fputs(line, stdout); } fclose(file); } else { perror(filename); } return 0; } lab46:~/src/sysprog$ Write file: write contents to a file from stdin, from another file, a server/client connection, etc. #include #include #include int main(int argc, char *argv[ ]) { FILE *fp; size_t bytes; char contents[256]; fp = fopen(argv[1], "a+"); if(fp == NULL) { printf("%c[%d;%dm[ERROR]: %c[%dm File %s could not be opened.\n", 0x1B, 1, 31, 0x1B, 0, argv[1]); return -1; } scanf("%s", &contents); bytes = fwrite(contents, 1, strlen(contents), fp); bytes += fwrite("\n", 1, strlen("\n"), fp); printf("Wrote %zu bytes total.\n", bytes); fclose(fp); return 0; } lab46:~/src/sysprog$ ./fwrite test Doom Wrote 5 bytes total. lab46:~/src/sysprog$ cat test Doom lab46:~/src/sysprog$ Append file: adds data to the bottom of the file #include #include #include #include #include #include struct data { char c[1024]; struct data *next; }; typedef struct data Data; Data *data2; int main(int argc, char *argv[ ]) { FILE *fp; int i; data2 = (Data *) malloc(sizeof(Data)); fp = fopen(argv[1], "a+"); if(fp == NULL) { printf("%c[%d;%dm[ERROR]: %c[%dm File %s could not be opened.\n", 0x1B, 1, 31, 0x1B, 0, argv[1]); return -1; } while(strcmp(data2->c,"EOF\n") != 0) { scanf("%s", data2->c); strcat(data2->c, "\n"); if(strcmp(data2->c, "EOF\n") != 0) { fprintf(fp, "%s", data2->c); } else { // Send EOF to the void and do not copy it to file } } free(data2); fclose(fp); return 0; } lab46:~/src/sysprog$ ./fwrite dood2 3mz 4abc 6y87fd87f6d8bg 7bf86v876 EOF lab46:~/src/sysprog$ cat dood2 g g g 3mz 4abc 6y87fd87f6d8bg 7bf86v876 lab46:~/src/sysprog$ ==== Typecasting ==== Typecasting: the usage of typecasting is to make a variable of one type, act like another type for one single operation. float a; a = (float)5 / 3; ==== Recursion ==== Recursion: A function that calls itself in its own code block. [[http://shirtoid.com/wp-content/uploads/2009/10/recursion.jpg|{{http://shirtoid.com/wp-content/uploads/2009/10/recursion.jpg}}]] void recursion() { // Do something here recursion(); } ==== Repetition/Iteration Structures ==== Repetition/Iteration Structures: used when something needs to be done x amount of times. for loop: for(i=0;i<(numthreads);i++) { thread_args[i] = i; printf("In Main: creating thread %d\n", i); rc = pthread_create(&threads[i], NULL, &taskcode, NULL); assert( 0 == rc); } while loop: while(tmp != NULL) { printf("node[%d]: %d\n", i, tmp -> value.sint); i++; tmp = tmp -> next; } ==== Structures ==== Structures: makes several things easier to manage code wise. struct struct node { union { int sint; char *schar; } value; struct node *next; struct node *prev; }; typedef struct node Node; =====DATA Topics===== ==== Doubly Linked Lists ==== doubly linked lists: // Build our list while(input != -1) { if(start == NULL) //empty list { start = (Node *) malloc (sizeof(Node)); end = start; // only one node, start and end are same start -> next = NULL; // nothing after start -> prev = NULL; // nothing before tmp = start; // tmp points to beginning of list start -> value = input; // enter v alue into node } else // There is already a list started { tmp = (Node *) malloc (sizeof(Node)); end -> next = tmp; // tack new node onto end of list tmp -> prev = end; // new node points to current end end = end -> next; // advance end to new end end -> value = input; // put input in node end -> next = NULL; // nothing beyond end } printf("Enter a value (-1 to quit): "); scanf("%d", &input); } ==== Linked Lists ==== linked lists: void newll() { start = tmp = NULL; printf("Enter a value (-1 to quit): "); scanf("%d", &input); input = (int)input; printf("%d\n",input); do { if(input == -1) break; if(start == NULL) { start = (Node *) malloc (sizeof(Node)); start -> value = input; start -> next = NULL; tmp = start; } else { tmp -> next = (Node *) malloc (sizeof(Node)); tmp -> next -> value = input; tmp -> next -> next = NULL; tmp = tmp -> next; } printf("Enter a value (-1 to quit): "); scanf("%d", &input); } while ((input != -1) || (isalpha(input) != 0)); } ==== Graphs ==== Graphs (2 Dimensional Linked Lists): They are a grid like structure of nodes made with structs that hold data can be a unique data type or a custom one, can also be used in graphics for a 2D map for a game like environment. Structure for Graph struct node { union { int n; char c; double d; } val; struct node *east; struct node *west; struct node *north; struct node *south; }; ==== Memory Allocation ==== Memory Allocation: defining a certain amount of memory for a data type/struct to use from the system to do a particular task usually done with malloc(); start = (Node *) malloc (sizeof(Node)); ==== Memory De-Allocation ==== Memory De-Allocation: usually done after a task is done in a program too free up memory and give it back to the system usually uses free() in c else { // Send EOF to the void and do not copy it to file } } free(data2); fclose(fp); return 0; ==== Void Pointers ==== Void Pointers: cannot be dereferenced, normally used for function prototypes, no initial size defined prior usually struct t { char *s; }; ... struct t x; ... x = malloc(sizeof *x); ==== Pointers (arrays) ==== Pointers (arrays): a pointer linking to an array of characters/integers/floats or any other data types int *array[3]; int x = 10, y = 20, z = 30; int i; array[0] = &x; array[1] = &y; array[2] = &z; for (i=0; i< 3; i++) { printf("The value of %d= %d ,address is %u\t \n", i, *(array[i]), array[i]); } ==== Pointers to Pointers ==== Pointers to Pointers: direct pointers to another pointer and usually to either another pointer or a value to modify else { tmp -> next = (Node *) malloc (sizeof(Node)); tmp -> next -> value = input; tmp -> next -> next = NULL; tmp = tmp -> next; } ==== NULL Pointers ==== NULL Pointers: to Null out a pointer to make it point to NULL value which could eventually be reassigned to a node/struct later, to disassociate it from the other struct(s), also used in the steps to freeup a node from being pointed to if(start == NULL) { start = (Node *) malloc (sizeof(Node)); start -> value = input; start -> next = NULL; tmp = start; } ==== Pointers (Address of) ==== To find out the memory address associated to a pointer printf("The value of %d= %d ,address is %u\t \n", i, *(array[i]), array[i]); ==== Structures ==== Structures: They make creating your own "data types" a lot easier for example: struct node { int value; struct node *next; struct node *prev; }; typedef struct node Node; ==== Structure Pointer ==== Structure Pointer: a struct created and in order to alter its variables that go with it you have to point to its corresponding value name then set it to whatever you want it to be eitehr from stdin, a file, or a socket connection start -> value = input; =====SYSPROG Topics===== ==== Zombies ==== Zombie Process: Is a process that has forked or the like and its child process just stays running and shows up as Z #include #include #include int main () { pid_t child_pid; child_pid = fork (); if (child_pid > 0) { sleep (60); } else { exit (0); } return 0; } ==== Threads ==== Threads: They are helpful for getting other functions done when others to to continue to run #include #include #include #include #include int tid; int numthreads; pthread_t threads[2]; int thread_args[2]; int rc; void *taskcode(void *); int i; int main() { numthreads = 2; for(i=0;i<(numthreads);i++) { thread_args[i] = i; printf("In Main: creating thread %d\n", i); rc = pthread_create(&threads[i], NULL, &taskcode, NULL); assert( 0 == rc); } for(i=0;i<(numthreads);i++) { rc = pthread_join(threads[i], NULL); assert(0 == rc); } exit(0); } void *taskcode(void *argument) { printf("Hello World! It's me, thread 0x%x!\n", &argument); } 1,1 A lab46:~/src/sysprog$ ./threads In Main: creating thread 0 In Main: creating thread 1 Hello World! It's me, thread 0x8f2f7ed8! Hello World! It's me, thread 0x8eaf6ed8! ==== Multi-threading ==== Multi-threading: Takes advantage of multicore systems to get several tasks done a lot faster than with just a single-core system. #include #include #include #include #include int tid; int numthreads; pthread_t threads[200]; int thread_args[200]; int rc; void *taskcode(void *); int i; int main() { numthreads = 200; for(i=0;i<(numthreads);i++) { thread_args[i] = i; printf("In Main: creating thread %d\n", i); rc = pthread_create(&threads[i], NULL, &taskcode, NULL); assert( 0 == rc); } for(i=0;i<(numthreads);i++) { rc = pthread_join(threads[i], NULL); assert(0 == rc); } exit(0); } void *taskcode(void *argument) { printf("Hello World! It's me, thread 0x%x!\n", &argument); } The code above after execution lab46:~/src/sysprog$ ./multithreaded In Main: creating thread 0 In Main: creating thread 1 Hello World! It's me, thread 0xfd8e3ed8! In Main: creating thread 2 Hello World! It's me, thread 0xfd0e2ed8! In Main: creating thread 3 Hello World! It's me, thread 0xfc8e1ed8! In Main: creating thread 4 Hello World! It's me, thread 0xfc0e0ed8! In Main: creating thread 5 Hello World! It's me, thread 0xfb8dfed8! In Main: creating thread 6 In Main: creating thread 7 Hello World! It's me, thread 0xfa8dded8! Hello World! It's me, thread 0xfb0deed8! In Main: creating thread 8 Hello World! It's me, thread 0xfa0dced8! In Main: creating thread 9 Hello World! It's me, thread 0xf98dbed8! In Main: creating thread 10 In Main: creating thread 11 Hello World! It's me, thread 0xf88d9ed8! Hello World! It's me, thread 0xf90daed8! In Main: creating thread 12 Hello World! It's me, thread 0xf80d8ed8! In Main: creating thread 13 Hello World! It's me, thread 0xf78d7ed8! In Main: creating thread 14 In Main: creating thread 15 Hello World! It's me, thread 0xf68d5ed8! Hello World! It's me, thread 0xf70d6ed8! In Main: creating thread 16 Hello World! It's me, thread 0xf60d4ed8! In Main: creating thread 17 Hello World! It's me, thread 0xf58d3ed8! In Main: creating thread 18 In Main: creating thread 19 Hello World! It's me, thread 0xf48d1ed8! Hello World! It's me, thread 0xf50d2ed8! In Main: creating thread 20 Hello World! It's me, thread 0xf40d0ed8! In Main: creating thread 21 Hello World! It's me, thread 0xf38cfed8! In Main: creating thread 22 In Main: creating thread 23 Hello World! It's me, thread 0xf28cded8! In Main: creating thread 24 Hello World! It's me, thread 0xf20cced8! In Main: creating thread 25 Hello World! It's me, thread 0xf18cbed8! Hello World! It's me, thread 0xf30ceed8! In Main: creating thread 26 Hello World! It's me, thread 0xf10caed8! In Main: creating thread 27 Hello World! It's me, thread 0xf08c9ed8! In Main: creating thread 28 Hello World! It's me, thread 0xf00c8ed8! In Main: creating thread 29 In Main: creating thread 30 Hello World! It's me, thread 0xef0c6ed8! Hello World! It's me, thread 0xef8c7ed8! In Main: creating thread 31 Hello World! It's me, thread 0xee8c5ed8! In Main: creating thread 32 Hello World! It's me, thread 0xee0c4ed8! In Main: creating thread 33 In Main: creating thread 34 Hello World! It's me, thread 0xed0c2ed8! Hello World! It's me, thread 0xed8c3ed8! In Main: creating thread 35 Hello World! It's me, thread 0xec8c1ed8! In Main: creating thread 36 Hello World! It's me, thread 0xec0c0ed8! In Main: creating thread 37 In Main: creating thread 38 Hello World! It's me, thread 0xeb0beed8! Hello World! It's me, thread 0xeb8bfed8! In Main: creating thread 39 Hello World! It's me, thread 0xea8bded8! In Main: creating thread 40 Hello World! It's me, thread 0xea0bced8! In Main: creating thread 41 In Main: creating thread 42 Hello World! It's me, thread 0xe90baed8! In Main: creating thread 43 Hello World! It's me, thread 0xe88b9ed8! In Main: creating thread 44 Hello World! It's me, thread 0xe80b8ed8! In Main: creating thread 45 Hello World! It's me, thread 0xe78b7ed8! In Main: creating thread 46 Hello World! It's me, thread 0xe70b6ed8! Hello World! It's me, thread 0xe98bbed8! In Main: creating thread 47 Hello World! It's me, thread 0xe68b5ed8! In Main: creating thread 48 Hello World! It's me, thread 0xe60b4ed8! In Main: creating thread 49 Hello World! It's me, thread 0xe58b3ed8! In Main: creating thread 50 In Main: creating thread 51 Hello World! It's me, thread 0xe48b1ed8! Hello World! It's me, thread 0xe50b2ed8! In Main: creating thread 52 Hello World! It's me, thread 0xe40b0ed8! In Main: creating thread 53 In Main: creating thread 54 Hello World! It's me, thread 0xe30aeed8! In Main: creating thread 55 Hello World! It's me, thread 0xe28aded8! In Main: creating thread 56 Hello World! It's me, thread 0xe20aced8! In Main: creating thread 57 Hello World! It's me, thread 0xe18abed8! In Main: creating thread 58 Hello World! It's me, thread 0xe10aaed8! Hello World! It's me, thread 0xe38afed8! In Main: creating thread 59 Hello World! It's me, thread 0xe08a9ed8! In Main: creating thread 60 Hello World! It's me, thread 0xe00a8ed8! In Main: creating thread 61 In Main: creating thread 62 Hello World! It's me, thread 0xdf0a6ed8! Hello World! It's me, thread 0xdf8a7ed8! In Main: creating thread 63 Hello World! It's me, thread 0xde8a5ed8! In Main: creating thread 64 Hello World! It's me, thread 0xde0a4ed8! In Main: creating thread 65 In Main: creating thread 66 Hello World! It's me, thread 0xdd0a2ed8! Hello World! It's me, thread 0xdd8a3ed8! In Main: creating thread 67 Hello World! It's me, thread 0xdc8a1ed8! In Main: creating thread 68 Hello World! It's me, thread 0xdc0a0ed8! In Main: creating thread 69 Hello World! It's me, thread 0xdb89fed8! In Main: creating thread 70 Hello World! It's me, thread 0xdb09eed8! In Main: creating thread 71 Hello World! It's me, thread 0xda89ded8! In Main: creating thread 72 Hello World! It's me, thread 0xda09ced8! In Main: creating thread 73 In Main: creating thread 74 Hello World! It's me, thread 0xd909aed8! In Main: creating thread 75 Hello World! It's me, thread 0xd8899ed8! In Main: creating thread 76 Hello World! It's me, thread 0xd8098ed8! In Main: creating thread 77 Hello World! It's me, thread 0xd7897ed8! In Main: creating thread 78 Hello World! It's me, thread 0xd7096ed8! In Main: creating thread 79 Hello World! It's me, thread 0xd6895ed8! In Main: creating thread 80 Hello World! It's me, thread 0xd6094ed8! In Main: creating thread 81 Hello World! It's me, thread 0xd5893ed8! Hello World! It's me, thread 0xd989bed8! In Main: creating thread 82 Hello World! It's me, thread 0xd5092ed8! In Main: creating thread 83 Hello World! It's me, thread 0xd4891ed8! In Main: creating thread 84 In Main: creating thread 85 Hello World! It's me, thread 0xd388fed8! In Main: creating thread 86 Hello World! It's me, thread 0xd308eed8! In Main: creating thread 87 Hello World! It's me, thread 0xd288ded8! In Main: creating thread 88 Hello World! It's me, thread 0xd208ced8! In Main: creating thread 89 Hello World! It's me, thread 0xd188bed8! In Main: creating thread 90 Hello World! It's me, thread 0xd108aed8! In Main: creating thread 91 Hello World! It's me, thread 0xd0889ed8! In Main: creating thread 92 Hello World! It's me, thread 0xd0088ed8! In Main: creating thread 93 Hello World! It's me, thread 0xcf887ed8! In Main: creating thread 94 Hello World! It's me, thread 0xcf086ed8! In Main: creating thread 95 Hello World! It's me, thread 0xce885ed8! In Main: creating thread 96 Hello World! It's me, thread 0xce084ed8! In Main: creating thread 97 Hello World! It's me, thread 0xcd883ed8! In Main: creating thread 98 Hello World! It's me, thread 0xcd082ed8! In Main: creating thread 99 Hello World! It's me, thread 0xcc881ed8! In Main: creating thread 100 Hello World! It's me, thread 0xcc080ed8! In Main: creating thread 101 Hello World! It's me, thread 0xcb87fed8! In Main: creating thread 102 Hello World! It's me, thread 0xcb07eed8! In Main: creating thread 103 Hello World! It's me, thread 0xca87ded8! In Main: creating thread 104 Hello World! It's me, thread 0xca07ced8! In Main: creating thread 105 Hello World! It's me, thread 0xc987bed8! In Main: creating thread 106 Hello World! It's me, thread 0xc907aed8! In Main: creating thread 107 Hello World! It's me, thread 0xc8879ed8! In Main: creating thread 108 In Main: creating thread 109 Hello World! It's me, thread 0xc8078ed8! In Main: creating thread 110 Hello World! It's me, thread 0xc7877ed8! Hello World! It's me, thread 0xd4090ed8! Hello World! It's me, thread 0xc7076ed8! In Main: creating thread 111 Hello World! It's me, thread 0xc6875ed8! In Main: creating thread 112 Hello World! It's me, thread 0xc6074ed8! In Main: creating thread 113 Hello World! It's me, thread 0xc5873ed8! In Main: creating thread 114 Hello World! It's me, thread 0xc5072ed8! In Main: creating thread 115 Hello World! It's me, thread 0xc4871ed8! In Main: creating thread 116 Hello World! It's me, thread 0xc4070ed8! In Main: creating thread 117 In Main: creating thread 118 Hello World! It's me, thread 0xc306eed8! Hello World! It's me, thread 0xc386fed8! In Main: creating thread 119 Hello World! It's me, thread 0xc286ded8! In Main: creating thread 120 Hello World! It's me, thread 0xc206ced8! In Main: creating thread 121 Hello World! It's me, thread 0xc186bed8! In Main: creating thread 122 In Main: creating thread 123 Hello World! It's me, thread 0xc0869ed8! Hello World! It's me, thread 0xc106aed8! In Main: creating thread 124 Hello World! It's me, thread 0xc0068ed8! In Main: creating thread 125 Hello World! It's me, thread 0xbf867ed8! In Main: creating thread 126 In Main: creating thread 127 Hello World! It's me, thread 0xbe865ed8! In Main: creating thread 128 Hello World! It's me, thread 0xbe064ed8! In Main: creating thread 129 Hello World! It's me, thread 0xbd863ed8! Hello World! It's me, thread 0xbf066ed8! In Main: creating thread 130 Hello World! It's me, thread 0xbd062ed8! In Main: creating thread 131 Hello World! It's me, thread 0xbc861ed8! In Main: creating thread 132 In Main: creating thread 133 Hello World! It's me, thread 0xbb85fed8! Hello World! It's me, thread 0xbc060ed8! In Main: creating thread 134 Hello World! It's me, thread 0xbb05eed8! In Main: creating thread 135 Hello World! It's me, thread 0xba85ded8! In Main: creating thread 136 In Main: creating thread 137 Hello World! It's me, thread 0xb985bed8! Hello World! It's me, thread 0xba05ced8! In Main: creating thread 138 Hello World! It's me, thread 0xb905aed8! In Main: creating thread 139 Hello World! It's me, thread 0xb8859ed8! In Main: creating thread 140 Hello World! It's me, thread 0xb8058ed8! In Main: creating thread 141 In Main: creating thread 142 Hello World! It's me, thread 0xb7056ed8! In Main: creating thread 143 Hello World! It's me, thread 0xb6855ed8! In Main: creating thread 144 Hello World! It's me, thread 0xb6054ed8! Hello World! It's me, thread 0xb7857ed8! In Main: creating thread 145 Hello World! It's me, thread 0xb5853ed8! In Main: creating thread 146 In Main: creating thread 147 Hello World! It's me, thread 0xb4851ed8! Hello World! It's me, thread 0xb5052ed8! In Main: creating thread 148 Hello World! It's me, thread 0xb4050ed8! In Main: creating thread 149 Hello World! It's me, thread 0xb384fed8! In Main: creating thread 150 In Main: creating thread 151 Hello World! It's me, thread 0xb284ded8! Hello World! It's me, thread 0xb304eed8! In Main: creating thread 152 Hello World! It's me, thread 0xb204ced8! In Main: creating thread 153 Hello World! It's me, thread 0xb184bed8! In Main: creating thread 154 In Main: creating thread 155 Hello World! It's me, thread 0xb0849ed8! Hello World! It's me, thread 0xb104aed8! In Main: creating thread 156 Hello World! It's me, thread 0xb0048ed8! In Main: creating thread 157 Hello World! It's me, thread 0xaf847ed8! In Main: creating thread 158 In Main: creating thread 159 Hello World! It's me, thread 0xae845ed8! Hello World! It's me, thread 0xaf046ed8! In Main: creating thread 160 Hello World! It's me, thread 0xae044ed8! In Main: creating thread 161 Hello World! It's me, thread 0xad843ed8! In Main: creating thread 162 In Main: creating thread 163 Hello World! It's me, thread 0xac841ed8! In Main: creating thread 164 Hello World! It's me, thread 0xac040ed8! Hello World! It's me, thread 0xad042ed8! In Main: creating thread 165 Hello World! It's me, thread 0xab83fed8! In Main: creating thread 166 In Main: creating thread 167 Hello World! It's me, thread 0xaa83ded8! Hello World! It's me, thread 0xab03eed8! In Main: creating thread 168 Hello World! It's me, thread 0xaa03ced8! In Main: creating thread 169 Hello World! It's me, thread 0xa983bed8! In Main: creating thread 170 Hello World! It's me, thread 0xa903aed8! In Main: creating thread 171 In Main: creating thread 172 Hello World! It's me, thread 0xa8038ed8! Hello World! It's me, thread 0xa8839ed8! In Main: creating thread 173 Hello World! It's me, thread 0xa7837ed8! In Main: creating thread 174 In Main: creating thread 175 Hello World! It's me, thread 0xa6835ed8! In Main: creating thread 176 In Main: creating thread 177 Hello World! It's me, thread 0xa6034ed8! In Main: creating thread 178 In Main: creating thread 179 In Main: creating thread 180 Hello World! It's me, thread 0xa4831ed8! Hello World! It's me, thread 0xa5032ed8! In Main: creating thread 181 Hello World! It's me, thread 0xa382fed8! In Main: creating thread 182 Hello World! It's me, thread 0xa302eed8! In Main: creating thread 183 Hello World! It's me, thread 0xa282ded8! In Main: creating thread 184 Hello World! It's me, thread 0xa202ced8! In Main: creating thread 185 Hello World! It's me, thread 0xa182bed8! In Main: creating thread 186 Hello World! It's me, thread 0xa102aed8! In Main: creating thread 187 Hello World! It's me, thread 0xa0829ed8! In Main: creating thread 188 Hello World! It's me, thread 0xa0028ed8! Hello World! It's me, thread 0xa4030ed8! Hello World! It's me, thread 0xa5833ed8! In Main: creating thread 189 Hello World! It's me, thread 0xa7036ed8! In Main: creating thread 190 Hello World! It's me, thread 0x9f026ed8! In Main: creating thread 191 Hello World! It's me, thread 0x9e825ed8! In Main: creating thread 192 Hello World! It's me, thread 0x9e024ed8! In Main: creating thread 193 Hello World! It's me, thread 0x9d823ed8! In Main: creating thread 194 Hello World! It's me, thread 0x9d022ed8! In Main: creating thread 195 Hello World! It's me, thread 0x9c821ed8! In Main: creating thread 196 Hello World! It's me, thread 0x9c020ed8! In Main: creating thread 197 Hello World! It's me, thread 0x9b81fed8! In Main: creating thread 198 Hello World! It's me, thread 0x9b01eed8! In Main: creating thread 199 Hello World! It's me, thread 0x9a81ded8! Hello World! It's me, thread 0x9a01ced8! Hello World! It's me, thread 0x9f827ed8! lab46:~/src/sysprog$ ==== Server Sockets ==== Server Sockets: are the host connection program used for several different services like apache2, ssh, or anything that really uses server sockets int create_sock(int portnum, int backlog) { server = (SN *) malloc(sizeof(SN)); server->sock_id = socket(PF_INET, SOCK_STREAM, 0); if(server->sock_id == -1) { printf("%c[%d;%dm[ERROR]: %c[%dm Could not create socket!!!\n", 0x1B, BOLD, RED, 0x1B, 0); return -1; } bzero((void *)&saddr, sizeof(server->saddr)); gethostname(server->hostname, 256); server->hp = gethostbyname(server->hostname); bcopy((void *)hp->h_addr,(void *)&saddr.sin_addr,hp->h_length); saddr.sin_port = htons(portnum); saddr.sin_family = AF_INET; if(bind(sock_id, (struct sockaddr *)&saddr, sizeof(saddr)) != 0) { printf("%c[%d;%dm[ERROR]: %c[%dm Could not bind socket!!!\n", 0x1B, BOLD, RED, 0x1B, 0); return -1; } if(listen(server->sock_id, backlog) != 0) { printf("%c[%d;%dm[ERROR]: %c[%dm Socket listening failed.\n", 0x1B, BOLD, RED, 0x1B, 0); return -1; } return server->sock_id; } ==== Client Sockets ==== Client Sockets: are used to connect to a host server socket to communicate data in between to points/ or local and remote system(s) usually though they both can be local int client_conn(char *host, int portnum) { client = (SN *) malloc(sizeof(SN)); client->sock = socket(AF_INET, SOCK_STREAM, 0); if(client->sock == -1) { printf("%c[%d;%dm[ERROR]: %c[%dm Socket not found!!!\n", 0x1B, BOLD, RED, 0x1B, 0); return -1; } bzero(&servadd, sizeof(servadd)); hp = gethostbyname(host); if(client->hp == NULL) { printf("%c[%d;%dm[ERROR]: %c[%dm Server Host not found.\n", 0x1B, BOLD, RED, 0x1B, 0); return -1; } bcopy(hp->h_addr,(struct sockaddr *)&servadd.sin_addr,hp->h_length); servadd.sin_port = htons(portnum); servadd.sin_family = AF_INET; if(connect(sock, (struct sockaddr *)&servadd, sizeof(servadd)) != 0) { printf("%c[%d;%dm[ERROR]: %c[%dm Could not connect to server or connection refused!\n", 0x1B, BOLD, RED, 0x1B, 0); return -1; } return client->sock; } ==== Client/Server Model ==== Client/Server Model: relates the functionality of both the client side connection and the hosting server side connection and how they communicate data lab46:~/code-src/[CLIENT]\[SERVER] Multmatrix$ ./mm-server 32000 & [1] 25021 lab46:~/code-src/[CLIENT]\[SERVER] Multmatrix$ ./mm-client [CLIENT-SIDE]: Syntax Error!!! Must be in this format ./mm-client hostname portnum M N P. lab46:~/code-src/[CLIENT]\[SERVER] Multmatrix$ ./mm-client localhost 32000 2 2 2 [CLIENT-SIDE] (connect) Could not connect to server or server refused connection. [CLIENT-SIDE]: Unable to connect to server at this time. lab46:~/code-src/[CLIENT]\[SERVER] Multmatrix$ ./mm-client lab46 32000 2 2 2 [SERVER-SIDE]: Received M Data. M Data equals 2. [SERVER-SIDE]: Received N Data. N Data equals 2. [SERVER-SIDE]: Received P Data. P Data equals 2. [SERVER-SIDE]: Setting matrix 1 and matrix 2 rows and cols to client set values. [SERVER-SIDE]: Generating Matrix1... [SERVER-SIDE]: Generating Matrix2... [SERVER-SIDE]: Creating random data for matrix 1 and matrix 2... [SERVER-SIDE]: Displaying Matrix 1 and Matrix 2 Data... 10 68 82 22 06 71 99 87 [SERVER-SIDE]: Generating Matrix 3 and calculating Matrix 3 Data... [SERVER-SIDE]: Displaying Matrix 3 Data... 06792 06626 02670 07736 lab46:~/code-src/[CLIENT]\[SERVER] Multmatrix$ ==== System Calls ==== System Calls: run a program on the system from within your own program and use its results as needed #include #include int main (void) { char *l; printf("Your libc version is:\n"); strcpy(l,system("/lib/libc.so.6 | head -n1 | awk '{print $10}' | sed 's/,//g'")); printf("%s", l); return 0; } when ran andoryuu@andoryuu-laptop:~/src/lab46/sysprog$ ../../checklibc Your libc version is: 2.12.1 Segmentation fault andoryuu@andoryuu-laptop:~/src/lab46/sysprog$ ==== Connections and Protocols ==== Connections and Protocols: SOCK_STREAM is TCP guaranteed (under normal circumstances) to get to its destination. SOCK_DGRAM is UDP and is usually not guarenteed to get to its destination but is faster none the less TCP sock_id = socket(PF_INET, SOCK_STREAM, 0); UDP sock_id = socket (AF_INET, SOCK_DGRAM, 0); ==== TCP vs. UDP ==== | ^ TCP ^ UDP ^ ^ Ordering | TCP rearranges data packets in the order specified. | UDP does not order packets. If ordering is required, it has to be managed by the application layer. | ^ Error Checking | TCP does error checking | UDP does not have an option for error checking. | ^ Header Size | TCP header size is 20 bytes | UDP Header size is 8 bytes. | ^ Usage | TCP is used in case of non-time critical applications. | UDP is used for games or applications that require fast transmission of data. UDP's stateless nature is also useful for servers that answer small queries from huge numbers of clients. | ^ Function | As a message makes its way across the internet from one computer to another. This is connection based. | UDP is also a protocol used in message transport or transfer. This is not connection based which means that one program can send a load of packets to another and that would be the end of the relationship. | ^ Weight | TCP requires three packets to set up a socket connection, before any user data can be sent. TCP handles reliability and congestion control. | UDP is lightweight. There is no ordering of messages, no tracking connections, etc. It is a small transport layer designed on top of IP. | ^ Stream of data by... | Data is read as a byte stream, no distinguishing indications are transmitted to signal message (segment) boundaries. | Packets are sent individually and are checked for integrity only if they arrive. Packets have definite boundaries which are honored upon receipt, meaning a read operation at the receiver socket will yield an entire message as it was originally sent. | ^ Speed of Transfer | The speed for TCP in comparison with UDP is slower. | UDP is faster because there is no error-checking for packets. | ^ Examples | HTTP, HTTPs, FTP, SMTP Telnet | DNS, DHCP, TFTP, SNMP, RIP, VOIP | ^ Data Reliability | There is absolute guarantee that the data transferred remains intact and arrives in the same order in which it was sent. | There is no guarantee that the messages or packets sent would reach at all. | ^ Connectivity | Two way Connection Reliable | One way Connection Reliable | ^ Flow Control | TCP does Flow Control | UDP does not have an option for flow control | ==== Creating/Destroying Threads ==== Creating Threads: pthread_create(&threadID , NULL, threadfunction, &value); Destroying Threads: pthread_cancel(&threadID); ==== Sharing Data between threads ==== Identification and definition of the chosen keyword. Substitute "keyword" with the actual keyword. If you want to demonstrate something on the command-line, you can do so as follows: lab46:~$ cd src lab46:~/src$ gcc -o hello hello.c lab46:~/src$ ./hello Hello, World! lab46:~/src$ =====CPROG Objectives===== ====Objective 1==== State the course objective; define what that objective entails. ===Method=== State the method you will use for measuring successful academic/intellectual achievement of this objective. ===Measurement=== Follow your method and obtain a measurement. Document the results here. ===Analysis=== Reflect upon your results of the measurement to ascertain your achievement of the particular course objective. * How did you do? * Room for improvement? * Could the measurement process be enhanced to be more effective? * Do you think this enhancement would be efficient to employ? * Could the course objective be altered to be more applicable? How would you alter it? =====DATA Objectives===== ====Objective 1==== State the course objective; define what that objective entails. ===Method=== State the method you will use for measuring successful academic/intellectual achievement of this objective. ===Measurement=== Follow your method and obtain a measurement. Document the results here. ===Analysis=== Reflect upon your results of the measurement to ascertain your achievement of the particular course objective. * How did you do? * Room for improvement? * Could the measurement process be enhanced to be more effective? * Do you think this enhancement would be efficient to employ? * Could the course objective be altered to be more applicable? How would you alter it? =====SYSPROG Objectives===== ====Objective 1==== State the course objective; define what that objective entails. ===Method=== State the method you will use for measuring successful academic/intellectual achievement of this objective. ===Measurement=== Follow your method and obtain a measurement. Document the results here. ===Analysis=== Reflect upon your results of the measurement to ascertain your achievement of the particular course objective. * How did you do? * Room for improvement? * Could the measurement process be enhanced to be more effective? * Do you think this enhancement would be efficient to employ? * Could the course objective be altered to be more applicable? How would you alter it? =====Experiments===== ==== Unsigned Double? ==== === Question === Can double be unsigned? === Resources === [[http://cboard.cprogramming.com/cplusplus-programming/5593-unsigned-double.html]] === Hypothesis === I do not think it will accept an unsigned double, but hell why not test it anyways, that and i always wondered if it would. === Experiment & Source code === #include #include #include int main() { unsigned double test = 9.9; printf("unsigned double test: %u\n", test); } ===Data=== lab46:~/src/sysprog$ gcc unsigneddouble.c unsigneddouble.c: In function 'main': unsigneddouble.c:7: error: both 'unsigned' and 'double' in declaration specifiers lab46:~/src/sysprog$ ===Analysis=== As you see it does not compile, meaning it doesn't work, but it was worth a try to see if it would. ===Conclusions=== That unsigned and double do not go together at all. ==== Union as a limiter? ==== ===Question=== Will a union limit how many integer place values there are when unioned with a char of size of 3 characters? === Hypothesis === It has the size of the largest data type, so that should be the limiting factor of it. === Experiment === #include #include union stat { char limit[1]; short int num; }; union stat ATK; int main() { int i; for(i=0; i<3; i++) { ATK.limit[i] = 0; ATK.num = 0; } ATK.num = 999; // strcpy(ATK.limit, "999"); printf("Integer: [%d]\nHex Address: [0x%x]\nChar: [%d]\n", ATK.num, &ATK.num, ATK.limit); ATK.num = 10000; // strcpy(ATK.limit, "10000"); printf("Integer: [%d]\nHex Address: [0x%x]\nChar: [%d]\n", ATK.num, &ATK.num, ATK.limit); return 0; } === Data === lab46:~/src/sysprog$ ./uniontest Integer: [999] Hex Address: [0x600970] Char: [6293872] Integer: [10000] Hex Address: [0x600970] Char: [6293872] lab46:~/src/sysprog$ === Analysis === It didn't seem to work as how i thought it was going to rather disappointing in a way. ===Conclusions=== Doesn't work exactly as to how i thought it would work. ====Experiment 3==== ===Question=== What is the question you'd like to pose for experimentation? State it here. ===Resources=== Collect information and resources (such as URLs of web resources), and comment on knowledge obtained that you think will provide useful background information to aid in performing the experiment. ===Hypothesis=== Based on what you've read with respect to your original posed question, what do you think will be the result of your experiment (ie an educated guess based on the facts known). This is done before actually performing the experiment. State your rationale. ===Experiment=== How are you going to test your hypothesis? What is the structure of your experiment? ===Data=== Perform your experiment, and collect/document the results here. ===Analysis=== Based on the data collected: * was your hypothesis correct? * was your hypothesis not applicable? * is there more going on than you originally thought? (shortcomings in hypothesis) * what shortcomings might there be in your experiment? * what shortcomings might there be in your data? ===Conclusions=== What can you ascertain based on the experiment performed and data collected? Document your findings here; make a statement as to any discoveries you've made. ====Retest==== If you're doing an experiment instead of a retest, delete this section. If you've opted to test the experiment of someone else, delete the experiment section and steps above; perform the following steps: ===State Experiment=== Whose existing experiment are you going to retest? Prove the URL, note the author, and restate their question. ===Resources=== Evaluate their resources and commentary. Answer the following questions: * Do you feel the given resources are adequate in providing sufficient background information? * Are there additional resources you've found that you can add to the resources list? * Does the original experimenter appear to have obtained a necessary fundamental understanding of the concepts leading up to their stated experiment? * If you find a deviation in opinion, state why you think this might exist. ===Hypothesis=== State their experiment's hypothesis. Answer the following questions: * Do you feel their hypothesis is adequate in capturing the essence of what they're trying to discover? * What improvements could you make to their hypothesis, if any? ===Experiment=== Follow the steps given to recreate the original experiment. Answer the following questions: * Are the instructions correct in successfully achieving the results? * Is there room for improvement in the experiment instructions/description? What suggestions would you make? * Would you make any alterations to the structure of the experiment to yield better results? What, and why? ===Data=== Publish the data you have gained from your performing of the experiment here. ===Analysis=== Answer the following: * Does the data seem in-line with the published data from the original author? * Can you explain any deviations? * How about any sources of error? * Is the stated hypothesis adequate? ===Conclusions=== Answer the following: * What conclusions can you make based on performing the experiment? * Do you feel the experiment was adequate in obtaining a further understanding of a concept? * Does the original author appear to have gotten some value out of performing the experiment? * Any suggestions or observations that could improve this particular process (in general, or specifically you, or specifically for the original author).