Blog Archives

NewMalloc – Assignment

This is an assignment done for the OS module at the university. The assignment was to make a replacement for malloc/free library functions, by using an array. Since the problem statement did not ask for brk support, there is no support for brk system call(may be in the future). This package contains 3 testers and the NewMalloc routines. The three testers are,

1. My infamous AVL tree benchmark – Adding, removing and finding elements from AVL tree using malloc for all memory it requires.

Glib Default malloc

ADDING 10000 strings
ADD 14706us
AVG LOOKUP 0.653400us
REMOVE 0.5 4711us
ADD 0.5 6558us

NewMalloc

ADDING 10000 strings
ADD 17638us
AVG LOOKUP 1.217400us
REMOVE 0.5 7596us
ADD 0.5 8927us

Tested on Ubuntu 11.10

2. malloctest – A malloc tester that makes random allocations and write to them and free them at random.

3. simpletest – A simple test, includes basic tests for malloc and free.

Download

How to build:  ‘make’, it’ll make all three tests. This project should build on any platform that supports a *Standard* C compiler, including Windows if and only if you use a standard C compiler.

My Own Shell – Assignment

Features,

1. Pipes

2. Environment variable setting(setvar, setenv)

3. Redirection(2>,1>,> and <)

And more, that I do not remember adding.

Download

Permutation and Combination…again.

These here are updated versions to my previous attempts at listing permutations and combinations, and these are a lot shorter than those and easier to understand. Even though it is in C++, converting to pure C is not a huge endevour either.

Combinations

#include <iostream>
#include <string>
#include <cstdio>
#define MAX 100
using namespace std;
int count;
int r;
void comb(char *arr, int i, int j, const char *line){
    if (j<r) {
        for (int x=i;x<count;x++){
            char newline[MAX];
            sprintf(newline, "%d %s",x+1, line );
            comb(arr, x+1, j+1, newline);        
        }
    } else {
        cout<<line<<endl;
    }
}

int main(){
    cin>>count>>r;
    char bits[count];

    for (int i=0;i<count;i++) bits[i] = 0;
    comb(bits,0,0,"");
    return 0;
}

Permutations

#include <iostream>
#include <string>
#include <cstdio>
#define MAX 100
using namespace std;
int count;

void perm(char *arr, int i, const char *line){

    if (i<count) {
        for (int x=0;x<count;x++){
            if (arr[x] != 1) {
                char newline[MAX];
                sprintf(newline, "%d %s",x+1, line );

                arr[x] = 1;
                perm(arr, i+1, newline);
                arr[x] = 0;
            }
        }
    } else {
        cout<<line<<endl;
    }
}

int main(){
    cin>>count;
    char bits[count];

    for (int i=0;i<count;i++) bits[i] = 0;
    perm(bits,0,"");
    return 0;
}