User Tools

Site Tools


opus:fall2011:kkrauss1:part2

Table of Contents

Part 2

Entries

October 5, 2011

  • Finally have a solid understanding of doubly linked lists and have a libary of basic functions. I am going to attempt to either write a stack using current library or modify my doubly linked list library to better suit a stack. There honestly just seems like there is too many things to do in such a short period of time. The opus for the first month was quite extensive and I only finished because I did a mediocre job on some of the objectives and keywords. I havent even done any projects for data comm, and am a little behind in discrete(programming functions in c). Hopefully I can be more productive this month.

October 5, 2011

  • Ok so I am confident I understand the concept of a stack, in fact the concepts are usually the simple part. I took the library I made for a doubly linked list(another simple concept yet difficult to implement) and combined it with the basic code we did in class for stacks. I had to make some changes but it functions as it sits. Now if I can make some progress in other areas I will be good.

October 14, 2011

  • Fashizzle, its time to take my stack library and convert it into a queue library. Certain things seem to be getting easier, but there is still sooooooooo much work to do, I do not know of a single student who does not think the work load is a bit extreme. The learning is great but its getting to be quite stressful to keep up.

October 31, 2011

  • So it is that time, the end of the second part of the opus is upon us. We are moving into binary trees. The concept, as usual, is quite simple. Let's just see how simple the implementation of the concept really is. For the most part classes are going well but in many I have hit a brick wall and feel a lack of direction. I just hope I can get it all figured out before the end of the semester.

cprog Topics

Keyword 1: Selection Structures

  • Selection structures AKA if statements(case/switch) is a key element in virtually any programming. It allows you to select a specific path to follow in your code.

Example code:

#include <stdio.h>
 
int main()
{
	int number;
 
	printf("Please enter a number between 1 and 10: ");
	scanf("%d", &number);
 
	//here is the selection statement
	if (number < 5 && number >= 1)
	{
		printf("You entered a number between 1 and 4\n");
	}
	else if (number >=5 && number <=10)
	{
		printf("\nYou entered a number between 5 and 10\n");
	}
	else
	{
		printf("\nYou entered an invalid number.\n");
	}
 
	return 0;
 
}

Keyword 2:Repetition/iteration structures

  • Repeition/iteration structures allow you to do a specific task a set number of times. For loops and while loops are how these are done.

Example code:

#include <stdio.h>
 
int main()
{
	int number = 10;
	int x;
	printf("\n");
	for(x = 1; x <= number; x++)
	{
		printf("%d\n", x);
	}
 
 
	return 0;
 
}

Keyword 3: Multi-Dimensional arrays

  • Multi-Dimensonal arrays much like single dimensional arrays are used for managing large amounts of data. As with any array the data type has to be the same however with Multi-Dimensional you can store several instances of data. Another way to think of it is to say an array of arrays. A single dimensional array has one index noted by array[] where as multi dimensional will have more than one group of brackets. Here is an example of a two dimensional array, char names[5][10]. This could be used to store 5 different strings with a max of 9 characters per string(the tenth being the null terminator).

Keyword 4:File access

File access was covered in Sys prog.

Keyword 5:typedef, enum, union

  • Typedef allows us to define our own variable types. As an example it is often used with structures. When you define a structure and give it a name(struct example) you will need to type struct example every time you declare a new version. But if you typedef it to say myStruct, then you can just type myStruct.
  • enum defines an enumerated data type which is a way to give a group of variable names a const num value instead of manually setting each variable equal. enum day_of_the_week {SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY,FRIDAY, SATURDAY}; Sunday = 0, monday =1, etc.
  • Union allows for different data names and types to be assigned to the same storage location. Similar to a structure except now only one variable can be store at a time. You might declare an int and a char within a union but only one can have a set value at any given time, never both.

Keyword 6: Functions

  • Functions are simply blocks of code that perform a specific task, and typicall(not always) return a value.

Keyword 7: Parameters

  • Parameters are often used to pass information to a called function. In C they can be passed by value or by address. In other words you can actually pass the value of a specific variable(aka int) to a function, or pass an address, passing by address allows you to pass more data since you can point to data structures.

Keyword 8: return types

  • Functions in C will typically be written to return some value. Any valid data type can be returned, including pointers. Programmers will often have functions return a data type as a way to use it in the main program, or some other function. They will also often return a basic int value to check and see if the function completed properly.

Keyword 9: Recursion

  • Recursion is when a function calls itself. Basically it is when a function is called to run before it is finished. This it to be typically avoided as it can lead to memory access problems.

Keyword 10: Command line arguments

  • Command line arguments/options allow for certain options of program to be run from the command line. To use this ability you would use argc and argv in main. Lets say you have a program that takes a string and appends it to another, you can run the program then prompt the user to enter each string, but with argc and argv it would allow the user to type the name of the program a space, the first string, a space the second string and the program will do its job without being prompted.

Keyword 11: Code stages(source,object, Binary)

  • Code typically goes through stages that start with what the programmer writes, all the way down to an executable program.
  • source code is what the user writes in human readable language
  • object code is source code that has been compiled as it was written in source, it has not been linked to any included libraries
  • binary code is the final stage and is compiled object code that has now been linked to all necessary libraries and can be run if coded properly

Keyword 12: Compiler, preprocessor, flags, assembler, linker

  • When first learning how to take your source code and make it a runnable program we often say we are compiling, but in fact there are several things that happen when compiling a program. When using the compile program(in this example we will use gcc) the first step is to add any flags you might want. These are basically command line options that allow you to compile the program exactly how you want to. Ex: give a specific name, compile with warnings, add debugging options etc etc. After the compiler is started the source code first gets passed by the preprocessor this is what processes any includes, conditional compilations instructions and any macros. Then what the preprocessor gives us is passed by the compiler which takes all of that code and turns it into assembly language. At this point the assembly version of our code is passed by the assembler which turns it into an object file. We are almost done but first we need to take our object file and link it to any needed libraries to make it binary, and this is done with the linker.

data Topics

Keyword 1: Doubly Linked Lists

Doubled linked lists are not much different than linked lists except now each node not only points to the next node but to the previous as well.

Keyword 2: Stacks(array and linked list based)

A stack is a last in first out data structure, meaning the “top” of the stack is all you have immediate access to. You can store data on top of the stack, remove it, or view it.

  • An array based stack you would use an array of whatever data type you wanted and the last element of the array that has a stored value would be the top of the stack
  • A link list based stack uses “nodes” and the last node is the top of the stack

Keyword 3: Pushing

Pushing is when you add to the top of a stack or initializes the stack if it is empty

Keyword 4: Popping

Popping simply removes the data stored at the top of the stack.

Keyword 5: Top(stack top, peek, whatever you wish to call it)

Top typically simply returns the value at the top of the stack but does not delete it like pop does.

Keyword 6: Overflow Condition

Overflow is when a stack is full and cannot accept anymore data

Keyword 7: Underflow condition

Underflow is when the stack is empty and there is no data to be removed

Keyword 8: LIFO or FIFO

LIFO means last in first out. Whatever the last thing that was stored will be the first one out. As stated before stacks are LIFO

FIFO means first in first out. Whatever the first thing that was stored will be the first one out. Queues(which will be discusses later) are FIFO

Keyword 9: Queues(Array and linked list based)

A queue is a first in first out data structure. Where with a stack you could only add or remove from the top, with a queue you add to the top(or back, depending on if you like to envision a queue as a sideways stack) but you remove from the bottom(front).

  • In a stack you can push, pop and peek, in a queue you enqueue, dequeue, and peek
  • In an array based queue the last unused element of the array is where you will store new data and the first used element of the array is where you remove data
  • In a linked list based queue you append to the end of the list to store new data and delete the first node to remove data.

Keyword 10: Enqueuing

Enqueuing is a queues version of pushing. You simply add new data to the “end” of whatever type of queue you are using.

Keyword 11

Dequeuing is the process of removing the FIRST thing that was stored in your queue, unlike a stack where the last thing stored is removed.

Keyword 12:Overrun and Underrun conditions

Overrun(queue overflow) occurs when you try to add data onto a queue that is full Underrun(queue underflow occurs when you trying to remove data from a queue that is empty

hpc1 Topics

Keyword 1

Identification and definition of the chosen keyword.

If you wish to aid your definition with a code sample, you can do so by using a wiki code block, an example follows:

/*
 * Sample code block
 */
#include <stdio.h>
 
int main()
{
    return(0);
}

Keyword 2

Identification and definition of the chosen 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$ 

Keyword 3

Identification and definition of the chosen keyword. Substitute “keyword” with the actual keyword.

If you wish to aid your definition with a code sample, you can do so by using a wiki code block, an example follows:

/*
 * Sample code block
 */
#include <stdio.h>
 
int main()
{
    return(0);
}

Keyword 4

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$ 

Keyword 5

Identification and definition of the chosen keyword. Substitute “keyword” with the actual keyword.

If you wish to aid your definition with a code sample, you can do so by using a wiki code block, an example follows:

/*
 * Sample code block
 */
#include <stdio.h>
 
int main()
{
    return(0);
}

Keyword 6

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$ 

Keyword 7

Identification and definition of the chosen keyword. Substitute “keyword” with the actual keyword.

If you wish to aid your definition with a code sample, you can do so by using a wiki code block, an example follows:

/*
 * Sample code block
 */
#include <stdio.h>
 
int main()
{
    return(0);
}

Keyword 8

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$ 

Keyword 9

Identification and definition of the chosen keyword. Substitute “keyword” with the actual keyword.

If you wish to aid your definition with a code sample, you can do so by using a wiki code block, an example follows:

/*
 * Sample code block
 */
#include <stdio.h>
 
int main()
{
    return(0);
}

Keyword 10

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$ 

Keyword 11

Identification and definition of the chosen keyword. Substitute “keyword” with the actual keyword.

If you wish to aid your definition with a code sample, you can do so by using a wiki code block, an example follows:

/*
 * Sample code block
 */
#include <stdio.h>
 
int main()
{
    return(0);
}

Keyword 12

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$ 

sysprog Topics

Keyword 1: Device files

Everything in unix is a file and that includes any device on the system. Depending on the device, device files can be written to and read from just like any other file. The monitor device file can be written to but not read from. A keyboard can be read from but not written to.

Keyword 2: Race condition

A race condition is when multiple processes are able to access shared data and they try to change it at the same time. It is called a race condition because the result is depending on the timing of events. page 149 of understanding unix/linux programming has a good example.

Keyword 3:Atomic operations

An atomic operation is when the kernel joins two or more system calls into one indivisible unit. Meaning the entire sequence of calls has to be performed or none at all. Page 150 of understanding unix/linux programming talks briefly about it.

Keyword 4: Streams

The STREAMS model of data flow and connection attributes allows for modularity of processing. You can write or remove modules as you see fit.

Keyword 5: Blocking vs. Non-Blocking

Blocking is when a call waits for input. If there is no input then the program waits for it. Non-Blocking is when when blocking is turned off thus if there is no input the program will still continue.

Keyword 6: Signals

A Signal can be defined as a one word message sent by the kernel. Each signal has a numerical code. Requests for signalcs can come from processes, exceptions and user keystrokes. Many signals cause processes to die, but there are other types of signals. You can typically find the list of signals in /usr/include/signal.h

Keyword 7: Event driven programming

  • Event driven programming is a type of programming in which the flow of the written programmed is determined by certain events. User interaction, responses from other programs etc.

Keyword 8: Alarms, interval timers

  • Alarm is a simple unix function that allows you to schedule an action in the future, thus allowing for an action to happen automatically. it has a resolution down to 1 second.
  • Interval timers is a system that was added to unix that has a higher resolution(down to microseconds) and has 3 seperate timers. The three timers are real which ticks in real time, virtual which only ticks when a process is being run in user mode, and prof which ticks when a process is running in user mode and when the kernel is running system calls made by said process.

Keyword 9: reentrant code, critical sections

  • Code that can be called when it's already running and not cause any problems is said to be reentrant.
  • Critical sections are sections of code that if interrupted can cause incomplete or damaged data.

Keyword 10: Asynchronous i/o

  • Asynchronous i/o allows for other processes to continue before the transmission is finished, it is non blocking i/o.

Keyword 11: Programs and Processes

  • A Program is a list of machine-language instructions. When a program runs the “list” is simply loaded into memory and the processor executes the instructions.
  • A Process is simply a program in action, more specifically it is the memory space and settings with which the program runs.

Keyword 12:Parent/Child processes

  • A Child process is a process that is created by the parent process, typically using fork(). When initially created it is virtually a copy of the parent process.

cprog Objective

Objective: structures vs. classes

What is the difference between a structure and a class?

  • A structure is used to bundle different data types together, and that is about all it can do. Its members(variables) are public by default. A class is an extended version of a structure used in c++ that is allowed to contain functions as well, basically its a structure that has executable code within it, something a struct cannot do. By default all the members of a class are private, another major difference between the two.

data Objective

Objective: data structs in memory

How are data structures allocated and used in memory?

Just like any variable type data structures are given a memory address. Data structures typically have nodes and each node has its own address, this is done in C with the malloc() function.

hpc1 Objective

Objective

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 Objective

Objective: signals

Design a program that handles signals.

#include <signal.h>
#include <stdio.h>
 
void intu();
void seg();
 
 
int main()
{
	char *array;
	signal(SIGINT, intu); //interrupt aka ctrl c
	signal(SIGSEGV, seg);// good ol seg fault why is it V instead of F?
	getchar();
	printf("%c\n", *array);// why does this continue to segment?
 
	return 0;
}
 
void intu()
{
	printf("You tried to interrupt me but I won't let you!\n");
}
 
void seg()
{
	printf("I segmented!\n");
 
}

Experiments

Experiment 1 Structure variables

Question

When defining a struct, can you give a value to any variables within the struct? If not, then why not?

Below is the code I attempted to compile:

#include <stdio.h>
 
 
 
struct list
{
	int variable = 57;
};
 
int main()
{
 
	/*struct list fashizzle;
	fashizzle.variable = 10;
	printf("\nThe variable is %d\n", fashizzle.variable);*/
 
	return 0;
}

Here is what happens when attempting to compile:

lab46:~/Desktop/experiments$ gcc -Wall structvar.c 
structvar.c:7: error: expected ':', ',', ';', '}' or '__attribute__' before '=' token
lab46:~/Desktop/experiments$ 

So the answer to the question is: No you cannot give value to a variable when defining a struct.

The reason for this is quite simple, as I have stated in previous parts of my opus a variable is simply a human name given to an address. When you are defining a structure, you are not assigning an address yet, that does not happen until you actually declare a structure in the main program. At that point a memory address is given and now you can give it a name and store a value.

Experiment 2 Enum 0 or 1

Question

After learning about enum I was curious as to what value it starts at, is it 0 or 1? Or possibly some other random value?

Here is my code:

#include <stdio.h>
 
int main()
{
	enum days{sunday, monday, tuesday, wednesday, thursday, friday, saturday};
	printf("%d\n", sunday);
	printf("%d\n", monday);
 
	return 0;
}

Here is the output:

lab46:~/Desktop/experiments$ ./a.out 
0
1
lab46:~/Desktop/experiments$

It would appear that the default for enum is to start at 0

Experiment 3: Enum + 1

I was curious if enumerated values would still allow you to math operators. Basically I am going to take an enumerated variable and see if I can add one and get the next enumerated variable.

Using the same code in previous experiment except instead of printing Monday, I will try to print sunday + 1

#include <stdio.h>
 
int main()
{
	enum days{sunday, monday, tuesday, wednesday, thursday, friday, saturday};
	printf("%d\n", sunday);
	printf("%d\n", sunday+1);
 
	return 0;
}

Here is the output:

lab46:~/Desktop/experiments$ ./a.out 
0
1
lab46:~/Desktop/experiments$ 

As we can see it worked the same way as the previous experiment meaning enumerated variables can still have math ops used on them.

opus/fall2011/kkrauss1/part2.txt · Last modified: 2011/11/18 19:20 by kkrauss1