SO, today we are talking about the producer-consumer problem, and we are going to solve it utilizing a bounded-buffer and pthreads.

Let’s talk about a producer-consumer relationship for a second, shall we? Basically, the producer produces goods while the consumer consumes the goods and typically does something with them.

In our case our producer will produce an item and place it in a bound-buffer for the consumer. Then the consumer will remove the item from the buffer and print it to the screen.

At this point you may be asking yourself what exactly is a “bound-buffer”? Well a buffer is a container of sorts; a bound-buffer is a container with a limit. We have to be very careful in our case that we don’t over fill the buffer or remove something that isn’t there; in c this will produce a segmentation fault.

The last thing we need to talk about before I explain the problem is semaphores. What, where, when, why and how???

What: A synchronization tool used in concurrent programming

Where: We will use semaphores in any place where we may have concurrency issues. In other words any place where we feel more than one thread will access the data or structure at any given time.

When: To help solve concurrency issues when programming with threads.

Why: Think about how registers work in the operating system for a second. Here is an example of how registers work when you increment a counter-

register1 = counter;

register1 = register1 + 1;

counter = register1;

Now image two threads manipulating this same example but one thread is decrementing –

(T1) register1 = counter; [register1 = 5]

(T1) register1 = register1 + 1; [register1 = 6]

(T2) register2 = counter; [register2 = 5]

(T2) register2 = register2 – 1; [register2 = 4]

(T1) counter = register1; [counter = 6]

(T2) counter = register2; [counter = 4]

WOW, that does look good, does it?! Because both threads were allowed to run without synchronization our counter now has a definitely wrong value. With synchronization the answer should come out to be 5 like it started.

How: We implement a semaphore as an integer value that is only accessible through two atomic operations wait() and signal(). Defined as follows:

The operation wait() tells the system that we are about to enter a critical section and signal() notifies that we have left the critical section and it is now accessible to other threads.

Therefore:

wait(mutex);

//critical section where the work is done

signal(mutex)

//continue on with life

Mutex stands for mutual exclusion. Meaning only one process may execute the section at a time.

Are we starting to get it? Well if not we have an example that demonstrates how semaphores are used in reference to pthreads coming up right after this problem walk-through.

Basically, we are going to have a program that creates an N number of producer and consumer threads. The job of the producer will be to generate a random number and place it in a bound-buffer. The role of the consumer will be to remove items from the bound-buffer and print them to the screen. Sounds simple right? Well, yes and no. Remember the big issue here is concurrency so we will be using semaphores to help prevent any issues that might occur. To double our efforts we will also be using a pthread mutex lock to further guarantee synchronization.

The user will pass in three arguments to start to application: <INT, time for the main method to sleep before termination> <INT, Number of producer threads> <INT, number of consumer threads>

We will then use a function in initialize the data, semaphores, mutex lock, and pthread attributes.

Create the producer threads.

Create the consumer threads.

Put main() to sleep().

Exit the program.

If you are kind of rusty on how pthreads work, I have a previous tutorial that may be some help in understanding them:

Ever wonder what the word enum means in [C]? I know most of us don’t spend the majority of our days pondering over its definition but still I thought I would add a simple tutorial to my repertoire depicting its uses.

So basically, enum creates a list of aliases. What are aliases? Well an alias is something that assumes the identity of something else. Think about your favourite spy assuming the alias of a henchmen to infiltrate the enemy base. The spy in this case assumed the identity of someone else. Got it? Great!

When we say:

enum person

You are getting ready to list a bunch of aliases for a person. Like so:

enum person {henchmen, guard, janitor};

Next we declare a variable for our person called spy and now aliases for a spy are a henchmen, guard, or janitor.

person spy;

The spy just isn’t going to use the same alias all the time, he may want to switch one out for another. So to apply an alias to a spy we write:

spy = henchmen;

Now the spy assumes the identity of the henchmen.

If we were to print out the value of the spy to the command line we would see that the value of a spy is 0. Consequently if we were to assign the spy the alias of the guard he we have a value of 1 and finally if he were to assume the janitor alias he would have a value of 2. Now thats kool right?! By not defining an initial value for a henchmen you are saying that you want a henchmen to be equal to zero and every alias after that to be one plus the previous number. If you decide to define the first value like so:

enum person {henchmen=5, guard, janitor};

Then henchmen will be 5, guard 6, and janitor 7.

Okay, now that you understand what enum does lets drive it home with an interactive program written using one. This simple program uses the months of the year defined 1 – 12. The user inputs the number for a month and the program uses aliases to print out which month was inputed.

Output:lee@isis:~/programming/c/enum$ ./month.out 1
January is the 1st month in the year.
lee@isis:~/programming/c/enum$ ./month.out 2
February is the 2nd month in the year.

Now, you may be asking yourself why you would want to use enum in the first place? Well enum will make your code more readable and cut down on the amount of code that you would have to write to sequentially initialise all those variables.

This concludes the tutorial on enum. I hope you have enjoyed and happy coding!

So, today I downloaded a torrent that contained a bunch of files that had a file type of: R00, R 01, R02… and I was like ???? I hadn’t seen the .RAR file yet so at this point I had no idea what was going on. It turns out these are a bunch of mini archives that are associated with a RAR archive file. To un-archive RAR content you will have to download:

Here, we have a 2 x 3 Matrix A and a 3 x 3 Matrix B. This will give us a 2 x 3 Matrix C.

[Rows by columns]

1,1 : (2 * 6) + (4 * 2) + (-1 * 0) = 20

1,2 : (2 * 0) + (4 * 1) + (0 * 1) = 4

1,3 : (2 * 3) + (-4 * -2) + (-1 * 1) = -3

2,1 : (3 * 6) + (-2 * 2) + (-1 * 0) = 14

2,2 : (3 * 0) + (-2 * 1) + (-1 * 0) = -2

2,3 : (3 * 3) + (-2 * -2) + (-1 * 1) = 12

Matrix C:

20

4

–3

14

–2

12

Okay, great, now we understand matrix multiplication. If you are still having issues I recommend visiting the link above, its basically amazing.

Now on to PThreads on linux. For this next code tutorial we are going to be exploring pthreads a little further then last time. This time our goal is to calculate every matrix coordinate with a different thread. In my previous post,

We are first going to define our matrices statically, and then define a structure that will hold row and column information for each thread. Important question alert!! Why do we need a structure to hold row and column information for each thread??? Well, like I said before we need to assign each thread its own coordinate to figure out in the resulting matrix. Remember when we were learning how to multiply matrices earlier? Rows by columns, rows by columns? I practically beat you over the head with it, I’m sure you remember. Well that’s exactly how each thread is going to compute its coordinate, each thread needs a row and a column to multiply. Then we will create two “for” loops, one for rows and an inner one for columns. The inner one will create a new thread and call the join method so the parent will wait for all the threads to complete before it prints out the resulting matrix (the parent can print the resulting matrix because threads share all resources with its parent, remember?).

Output:

lee@isis:~/programming/c/matrix$ ./matrix.out

28 23 18

41 34 27

54 45 36

Yay! Look at that it worked! Go figure, haha. If you are still having problems understanding how matrices work maybe you should write this code out by hand and see exactly what its doing and how its traversing the rows and columns.

Quick reminder, in order to compile c programs with the use of pthread in linux you need to add the -lpthread arg to gcc like so:

Prime Numbers

So, first lets talk a little about prime numbers. What makes a number prime? Well a prime number is prime when it is only divisible by one and itself. Take for example the number 3, 3 can only be divided by one and three and have a remainder of zero. If you try to divide by two then you will get a remainder of one. Therefore 3 is a prime number. Lets take another example the number 4, 4 is not a prime number because when you divide it by 2 you get a remainder of zero. Our definition states that a number can only have a remainder of zero when divided by one and itself, the number 4 clearly breaks this rule when divided by 2. 5 on the other hand can’t be divided by anything but one and itself so it’s prime. Get the hang of it yet?

So now that we understand what prime numbers are lets try and make a simple sudo code algorithm that depicts this behaviour. Lets say someone comes up to you on the street and asks you to list every prime number that comes before what ever number (we are going to call this the “upper limit”) they ask you.

FOR every number X that comes before our upper limit

FOR every number Y that comes before X

IF X MOD Y doesn’t equal zero

THEN this number must be prime

ELSE X must be divisible by some Y so it is NOT a prime number

[REMEMBER] Mod or Modulus (%) division is remainder division. EX 1: 3 %(MOD) 1 = 0; Because when you divide 3 by 1, 1 goes into 3, 3 times. This gives us a remainder of zero. EX 2: 5 % 3 = 2; Because 3 goes into 5 once, so 2 is left over as the remainder.

Modulus division is really going to help us with this prime number problem.

POSIX Threads

Okay, so you got the prime number thing down, you understand it (or will shortly 😉 ). Now it is time to talk about POSIX threads in linux. They kind of work like java threads in the sense that java has the run method it calls after you use the start function. Well, POSIX threads in linux have a create method that accepts a function as one of its parameters, this function is not called directly but rather the creation process calls it for you.

As you can see we made use of the POSIX pthread API. We set default attributes on the thread then created it. When we created it we passed in the thread ID, attributes, the function we need to call as the thread, and finaly the params. In this case we passed in the users input because it was already a pointer so it was easily passed with out casting.

In our prime number algorithm we don’t check 1 or the number itself to see if it divides cleanly, mainly because we already know it will so really there is no point in checking; it would also make our algorithim more complex for no reason and thats never a good thing.