Thursday, March 25, 2010

Thread stack in heap (pthreads)

I had known that a thread has a stack. But never thought where is this stack lie in the memory?

Well, it should be process stack. But you can set the thread stack to a heap area.

How to do that?

#include "pthread.h"
#include "stdio.h"
#include "unistd.h"

pthread_mutex_t mutex1;

void* foo(void *arg)
{
int arr[1024*8];

int x = 0;
while(x++ < (8*1024) )
{ arr[x] = x; }

pthread_mutex_lock(&mutex1);
puts("Thread created");
pthread_mutex_unlock(&mutex1);
}

int main()
{
int *p;
void *ret;

p = malloc(1024);

free(p);

pthread_attr_t attr;

pthread_attr_setstack(&attr, p, 2);

pthread_t thread1;

pthread_create(&thread1, NULL, foo, NULL);

pthread_join(thread1, &ret);

free(p);

return 0;
}

But still I am not very sure of this concept.

Stay tuned for more!

Monday, March 22, 2010

Some thoughts on Linux malloc()

* Always check for equal number of malloc()/free() calls.
• Use calloc() instead of malloc() + memset()
• Instead of allocation memory in a piece-meal manner, try to allocate a bigger chunk with malloc() and keep on resizing it with realloc() if needed.
• malloc() over-commits the memory i.e. it gives you an address which actually does not exist!
• malloc() grouping & reallocing with "more than you want"

Thursday, March 11, 2010

Cut short your web-addresses: TinyURL

An utility called "tinyurl" makes your life easier with shorter URL without creating an all new website. Many times it become cumbersome(i.e. for a long string) and unsafe(if you miss to copy contents of the link) to send a link to your friend.

TinyURl helps you with providing an alias for a long link. Concept is very simple but effective.
e.g. a link is: "http://http://www.blogger.com/post-create.g?blogID=4233584295994918105"

You may get an easier name for this link as: http://tinyurl.com/abc

Here is more information this:
http://tinyurl.com

Saturday, February 20, 2010

Exceptional exception handling in C++

#include

using namespace std;

//Global
int a = 20;

void foo()
try {
throw a;
}
catch(int &x)
{
// You can't modify value of "a"
x = 10;
}

int main()
{
foo();
cout<<"A="< }

A=20 !!!!
When you write "throw a", an invisible, phantom exception object gets created. You pass reference to that object and hence any change made to original object have no effect.

Friday, February 19, 2010

Mutex v/s Semaphore v/s Spinlock

Similarity

- All are used for synchronization

Difference

Mutex provides one person to access a resource at a time, others must wait in a queue. Once person is done, the guy next in the queue acquire the resource.
So access is serial, one guy after other.

Semaphore provides more than one(up to N) guys to access N resources. So it gives you parallel access to a resource.

Spinlock is an aggressive mutex. In mutex, if you find that resource is locked by someone else, you (the thread/process) switch the context and start to wait(non-blocking).
Whereas spinlocks do not switch context and as soon as resource is free, they go and grab it. In this process of spinning, they consume many CPU cycles. Also, on a uni-processor machine they are useless and perform very badly (do I need to explain that?).