C

Network Programming Homeworks

Posted by Strainu on February 23, 2008
C, Java, Software / No Comments

Network Programming Homeworks from the Automatics and Computer Science Faculty, UPB, 5th year, prof. Valentin Cristea. The archive contains a homework made in Java. Themes: RPC (Remote Procedure Call), Sorting trees using MPI, Secure chat server in JAVA, Bidding system using CORBA.
Continue reading…

Share and Enjoy:
  • Facebook
  • Twitter
  • Identi.ca
  • LinkedIn
  • Reddit
  • Add to favorites

Tags: , ,

Parallel Algorithms Homeworks

Posted by Strainu on February 23, 2008
C, Java, Software / No Comments

Parallel Algorithms Homeworks from the Automatics and Computer Science Faculty, UPB, 5th year, prof. Valentin Cristea. The archive contains a homework made in Java. Themes: Parallel Game of Life with OpenMP, Sudoku with Replicated Workers, image manipulation (filters, Huffman coding), routing tables.
Continue reading…

Share and Enjoy:
  • Facebook
  • Twitter
  • Identi.ca
  • LinkedIn
  • Reddit
  • Add to favorites

Tags: , ,

Operating Systems design homeworks

Posted by Strainu on August 24, 2007
C, Software / No Comments

Operating Systems design homeworks from the Automatics and Computer Science Faculty, UPB, 4th year, prof. Octavian Purdilă. Themes: system calls, UART driver, file system driver, firewall, RAID software.
Continue reading…

Share and Enjoy:
  • Facebook
  • Twitter
  • Identi.ca
  • LinkedIn
  • Reddit
  • Add to favorites

Tags: , , ,

Parallel Computing homeworks

Posted by Strainu on June 30, 2007
C, Software / No Comments

Parallel Computing homeworks from the Automatics and Computer Science Faculty, 4th year, prof. Nicolae Tapuş. Subjects:

  • MPI,
  • OpenMPI,
  • pthreads,
  • parallel Sudoku solver and generator.

Continue reading…

Share and Enjoy:
  • Facebook
  • Twitter
  • Identi.ca
  • LinkedIn
  • Reddit
  • Add to favorites

Tags: , ,

The const keyword (part 2)

Posted by Strainu on May 29, 2007
C, C++ / No Comments

In part 1 we introduced the const keyword. Today we’ll talk about constant pointers.

Let’s say you want to define a constant pointer. Which of the following declarations will you use?

const int* v1;
int* const v2;
const int * const v3;

The answer is: the second or the third. The first declaration defines a pointer to a constant integer and the third one defines a constant pointer to a constant integer.

Another interesting case is when defining a constant char array (credits go to Ulrich Drepper, link via RazvanD):

int main(void)
{
const char s[] = "hello";
strcpy (s, "bye");
puts (s);
return 0;
}

Although this code will give a warning (passing `const char *’ as argument 1 of `strcpy(char *, const char *)’ discards qualifiers is the exact message on Dev-C++), it will run, because s is allocated in the heap, so it is treated much like a pointer. You can force the value to be constant by adding the static keyword, wich will force the compiler to allocate s in read-only memory:

static const char s[] = "hello";
Share and Enjoy:
  • Facebook
  • Twitter
  • Identi.ca
  • LinkedIn
  • Reddit
  • Add to favorites

Tags: ,

The const keyword (part 1)

Posted by Strainu on May 24, 2007
C, C++ / 1 Comment

This article will not actually present any tricks, it will be an introduction in the const keyword. In part 2, we will present the const and volatile pointers, which behave a little weird.

First of all, let’s see what the const modifier means in the C standard. Basically, a const variable is one who’s value can’t be changed. Actually, things are not so simple – as we’ll see later, you can change a constant variable. The standard states that “If an attempt is made to modify an object defined with a const-qualified type through use of an lvalue with non-const-qualified type, the behavior is undefined.” On some architectures, constant variables are put in a special section (sometimes called .rodata – from Read Only DATA) of the program by the compiler.

If you want to define a constant in C/C++, you can write:

const int v1 = 0; //the usual way
int const v2 = 1; //also legal

Both declarations mean the same thing: define a new integer with a fixed value. So, if you want to change the value of v1, how would you do it? By using pointers:

const int v1 = 0; //define a constant
int* v2 = &v1; //define a pointer to v1
*v2 = 5; //change the value

Of course, you shouldn’t do that, as the are no guarantees that the result will be what you expect it to be, but with most compilers, v1 will be 5 after running the code presented above.

Share and Enjoy:
  • Facebook
  • Twitter
  • Identi.ca
  • LinkedIn
  • Reddit
  • Add to favorites

Tags: ,

Simple debugging in kernel programming

Posted by Strainu on May 17, 2007
C / No Comments

When programming Linux kernel modules, you have limited debugging options. The main way is to use the printk function (the kernel equivalent of printf). If you want to give as much information as possible, you could use some of the macros that the language offers you, such as __FILE__ or __line__. Here is a small snippet you could use in your modules:

#define DEBUG 1
#if DEBUG
#define Dprintk(format, ...) \
printk (KERN_ALERT "[%s]:FUNC:%s:line:%d: " format, __FILE__, \
__func__, __LINE__, __VA_ARGS__)

#else
#define Dprintk(format, ...) do {}while(0)
#endif

You can use the same code in userspace programs by replacing printk with printf. And just in case you’re wondering what’s with the empty do-while, you might want to take a look at this older article.

Share and Enjoy:
  • Facebook
  • Twitter
  • Identi.ca
  • LinkedIn
  • Reddit
  • Add to favorites

Tags: , ,

A little fun with cdecl

Posted by Strainu on May 15, 2007
C / No Comments

One of my teachers decided one day that he should write a pointer to an array of functions receiving a pointer to a function receiving an int and returning a pointer to a function receiving an array of pointers to int and returning int and an int and returning a pointer to a function receiving a pointer to a function receiving a pointer to an array of int and an int and returning a pointer to int and returning a function receiving int and returning int.

Fun, huh? 😀 Let’s try one more time:

a pointer to an array of functions
|  receiving a pointer to a function
|               |  receiving an int
|               |  returning a pointer to a function
|               |               |  receiving an array of pointer to int
|               |               |  returning int
|            an int
|  returning a pointer to a function
|               |  receiving a pointer to a function
|               |               |  receiving a pointer to an array of int
|               |               |            an int
|               |               |  returning a pointer to int
|               |  returning a function
|               |               |  receiving int
|               |               |  returning int

It does sounded quite tricky to me. Still, he managed to write it down in just 10 minutes. The secret? Cdecl. Cdecl reads files for statements which should be translated to C or C++. Six different statement are supported: declare composes a C type-declaration, cast composes a C type-cast, and so on. The grammar is described in full in the man page.

And here is the result for the function described above:

int ((*(*f)[] (int (*( * )(int))(int *[]), int))(int * ( * ) (int ( * )[], int)))(int)
Share and Enjoy:
  • Facebook
  • Twitter
  • Identi.ca
  • LinkedIn
  • Reddit
  • Add to favorites

Tags: , ,

How to read the bits in a IEEE 754 number?

Posted by Strainu on May 08, 2007
C / No Comments

That’s one of those questions you always wanted to know the answer to, but never found somebody who knew, right? 😛

Well, in C it’s pretty simple. All you have to do is to declare an union, like this:

union{
float f;
int bits;
}

This tells the compiler that both f and bits should be hold in the same memory zone. This is pretty useful to save some memory, if you know for sure that you wown’t need f and bits in the same time.

As a side effect, if you write a value in f then read bits, you will have all the bits represented in the IEEE 754 standard. Now you can use bit operations (& , | , ^) with your number, you can extract the different parts of the number, etc. Do keep in mind that this is bad programming practice.

Share and Enjoy:
  • Facebook
  • Twitter
  • Identi.ca
  • LinkedIn
  • Reddit
  • Add to favorites

Tags: ,

Macros using do{}while(0);

Posted by Strainu on May 04, 2007
C / 3 Comments

If you ever had the chance to look in the Linux kernel sources, you might have seen macros defined like this:

#define foo(params) \
do{ \
    //instructions \
}while(0)

This basicly means that the code is executed exactly once, so the first idea is that do{}while(0); is useless.
In fact, there are a number of reasons for writing the macros this way:

  • Empty statements give warnings from the compilers, so rather than writing
    #define FOO

    you might want to write

    #define FOO do{}while(0)
  • It gives you a basic block in which to declare local variables. You could simply use curly brackets, but this could cause serious problems in conditional statements. Let’s take the following example:
    #define exch(a,b) {int t; t = a; a = b; b = t;}

    You then use the macro in the following code:

    if(a[i]<a [i+1])
        exch(a[i],a[i+1]);
    else
        ready = 1;

    This is what the copiler will get:

    if(a[i]<a [i+1]){
        int t;
        t = a[i];
        a[i] = a[i+1];
        a[i+1] = t;
    };
    else
        ready = 1;

    This is interpreted as an if statement whithout else and you will get an error like “else without matching if”. However, if you write the macro like this:

    #define exch(a,b) do{int t; t = a; a = b; b = t;}while(0)

    the code will behave as expected.

You can find more informations about this in the Kernelnewbies FAQ.

Share and Enjoy:
  • Facebook
  • Twitter
  • Identi.ca
  • LinkedIn
  • Reddit
  • Add to favorites

Tags: ,