Sunday, September 26, 2010

Threading and Mutexes

So the overall assignment this week was more or less the same as last week, make your main/parent program call a thread to do the fibbonacci work. That was easy, so easy I did it at 3am friday morning when I couldn't sleep, in about 30 minutes, it took a little bit of rework from the fork program I wrote last week, but was simple to implement.

This morning I was thinking about mutex's.  I know that you use a mutex as a semaphore of sorts in that when you've locked it other processes needing to use it (or really use some variable you're about to modify) won't be able to.

So what I did was lock it in the child thread, then when the sibling thread attempts to lock it, it can't, and then forces it to wait until it's unlocked once the child unlocks then the sibling can print the elements of the shared memory.

Remember to include pthreads, you simply have to #include
then to compile it would be gcc -pthread -o filename.o

Good luck!

Here are a few screen shots of the results.
Fibonacci Threaded

Fibonacci with Mutexes

Tuesday, September 21, 2010

Books I would like to read 9/21/2010

Here is a list of the books I would like to sit down and spend some time reading front to back.

Let me know if you have one on your list that I would like.
Pthreads Programming: A POSIX Standard for Better Multiprocessing (O'Reilly Nutshell)
I've poked through this a bit, my buddy Phil lent me the book, and I would really like to get through it, maybe I'll knock a few chapters down this weekend if I don't have too much homework.

C Programming Language (2nd Edition) (K and R C, the definitive guide)
I borrowed this from the library at work, but only got a little into it, unfortunately work and school come first.

Mastering Regular Expressions
I borrowed this one from work, and got through the first 3 or 4 chapters, but then it started getting into the greedy etc matching and I kinda slacked off, I would like to finish this book as what I've learned from it so far has been INVALUABLE.

Linux Device Drivers, 3rd Edition
I have the second edition of this book, and it's a great book, but it only covers up to the Kernel version 2.4, the 3rd edition however covers up to 2.6 so that's great news. There is a free version of the book here: Linux Device Drivers 3rd Edition *Free

I'll give you my next list. If you've read these books give me your opinion, or maybe suggest a better one in place of the ones I've selected.

Saturday, September 18, 2010

Operating Systems -- A shared memory project

Well I decided to start hacking away at my homework for my operating systems class, in the course of about 3 hours, I managed to finish the project and make breakfast....All in all I would say it was a productive morning.

I am not going to share my source code, for reasons I'm debating about writing a post about, to make a long story short, is it ethical to post about your homework? I do think that sharing knowledge in this day and age is what will enable us (humanity) to become smarter as a whole (I think), however there is a line, explaining about shared memory for example and how it works, with examples is valid, literally posting my code up for people to simply copy and paste into their HW not so awesome.

Lets get into it, as part of the assignment we were asked to record our observations and the likes.

First off, the first problem wasn't really all that hard, initially I started to do it recursively, but decided not to, as I was just not thinking right this morning, and writing a simple for loop to do this was WAY easier.

Lets get into the meat of the project, the two main goals were forking and shared memory, the forking was relatively easy, simply creating a variable to hold your pid and then checking whether it's 0 or some other number tells you whether you are the parent or not. Then simply doing something different based on whether you are a parent or not (or if there was a pid error) was all that was required.

Shared memory was a little trickier, and I think I finally get it. Here is the deal, first we create a numerical identifier for the shared memory. Now we can begin to fork, and in the child process we create an attachment to the memory in each child process, THEN in the parent we do the same (This way we KNOW we aren't using the shared variable, it's actually memory, because we're using two different variables and I didn't assign them as pointers).

Now when you're done in the children we just detach from the processes.

In the parent we will detach then deallocate the memory. I was thinking about it, and I decided in my mind that if i deallocate that memory, then still try to point to it with my variable, I should get a segfault, guess what... I did, so that's how my program ends, with a seg fault, but it's on purpose, and I actually know why, and it just plain makes sense. Heres the output of each of the parts of my program.


Part 1
Fibbonacci with only forking
Part 2
Fibbonacci 2 with shared memory

Thursday, September 16, 2010

Kernel Modules

My ECE 437 class was assigned the monumental task of writing a filesystem driver for the linux kernel. I did a LOT of reading and finally started writing a few things, but realized I needed to be able to simply compile a kernel module just to see how to do it so I started following along with the kernel module how to here Kernel Module Tutorial

When I ran into the problem where when I called make, I would see the following error:
make[2]: *** No rule to make target `kernel/bounds.c', needed by `kernel/bounds.

I chased around the interwebs for a bit. I found out that it looks like $(PWD) wasn't returning the present directory, so I updated it to shell pwd per this site: Labjack Forums

Here is my makefile (note that in front of commands are TABS, if you replace them with spaces your makefile won't work, you've been warned)

#!/usr/bin/make -f

obj-m += hello_world.o

KERNELDIR=/lib/modules/$(shell uname -r)/build

all:
make -C $(KERNELDIR) M=$(shell pwd) modules

clean:
make -C $(KERNELDIR) M=$(shell pwd) clean




And finally here is my hello world (keep in mind it's pulled from the above tutorial)


/*  
 *  hello_world.c - The simplest kernel module.
 */
#include /* Needed by all modules */
#include /* Needed for KERN_INFO */

int init_module(void)
{
printk("<1>Hello world 1.\n");

/* 
* A non 0 return means init_module failed; module can't be loaded. 
*/
return 0;
}

void cleanup_module(void)
{
printk(KERN_ALERT "Goodbye world 1.\n");
}



Check it out. and give it a try. hopefully it works for you!

calling make all at the command line should compile it all,

and then a simple sudo insmod hello_world.ko

check out if your module was loaded via lsmod, look for it.

Now check your /var/log/messages file for your alert!

Finally do a sudo rmmod hello_world.ko to remove it from the kernel.

Thank you Caleb for the help various commands and the makefile!