Friday, October 21, 2011

Implement semaphore in C.

#include <stdio.h>
#include <pthread.h>

void* thread_a( void* data )
{
    pthread_mutex_t* pmtx = (pthread_mutex_t*)data;
    pthread_mutex_lock( pmtx );
    FILE *fp;
    fp = fopen ("thread_a.txt", "w+");
    fprintf(fp,"%s","thread_a");
    fclose(fp);
    pthread_mutex_unlock( pmtx );
    return NULL;
}

void* thread_b( void* data )
{
    pthread_mutex_t* pmtx = (pthread_mutex_t*)data;
    pthread_mutex_lock( pmtx );
    FILE *fp;
    fp= fopen ("thread_b.txt", "w+");
    fprintf(fp,"%s","thread_b");
    fclose(fp);
    pthread_mutex_unlock( pmtx );
    return NULL;
}

void* thread_c( void* data )
{
    pthread_mutex_t* pmtx = (pthread_mutex_t*)data;
    pthread_mutex_lock( pmtx );
    FILE *fp;
    fp= fopen ("thread_c.txt", "w+");
    fprintf(fp,"%s","thread_c");
    fclose(fp);
    pthread_mutex_unlock( pmtx );
    return NULL;
}

int main()
{
    pthread_mutex_t mtx;
    pthread_t tA;
    pthread_t tB;
    pthread_t tC;
    pthread_mutex_init( &mtx, NULL );
    pthread_create( &tA, NULL, &thread_a, &mtx );
    pthread_join( tA, NULL );  /* wait for the thread_a to finish */
    pthread_create( &tB, NULL, &thread_b, &mtx );
    pthread_join( tB, NULL );  /* wait for the thread_b to finish */
    pthread_create( &tC, NULL, &thread_c, &mtx );
    pthread_join( tC, NULL );  /* wait for the thread_c to finish */
   
    return 0;
}

Monday, October 10, 2011

Friday, October 7, 2011

What is the difference between process and thread?

Both processes and threads are independent sequences of execution. The typical difference is that threads (of the same process) run in a shared memory space while processes run in separate memory spaces.

All the threads running within a process share the same address space, file descriptor, stack and other process related attributes. onthe other hand, each process has thier own virtual address space, executable code, open handles to system objects, a security context, a unique process identifier, environment variables, a priority class, minimum and maximum working set sizes, and at least one thread of execution.

What is the difference between static and shared library?

In Unix, Static library has an extension of ".a" which is equivalent to ".lib" in Windows. on the contrary, dynamic or shared library has got ".so" as extension equivqlent to ".dll" in Windows.

Static libraries increase the size of the code in the binary. It is directly linked into the program at compile time. A program using a static library takes copies of the code from the static library and makes it part of the program. They're always loaded with the currently compiled version of the code. As the code is connected at compile time there are not any additional run-time loading costs.

Dynamic libraries are stored and versioned separately. Dynamic libraries aren't necessarily loaded; they are usually loaded when first called and can be shared among components that use the same library (multiple data loads, one code load). It allows you to replace the shared object with one that is functionally equivalent, but may have added performance advantages without needing to recompile the program that makes use of it.Shared libraries will, however have a small additional cost for the execution of the functions as well as a run-time loading cost as all the symbols in the library need to be connected to the things they use. Additionally, shared libraries can be loaded into an application at run-time, which is the general mechanism for implementing binary plug-in systems.

Wednesday, October 5, 2011

How do find out max of 3 numbers using conditional/ternary operator?

#include <stdio.h>

main()
{
    int a, b, c, max;
    printf("Enter three numbers : ") ;
    scanf("%d %d %d", &a, &b, &c);
    max = (a > b) ? (a > c ? a : c) : (b > c ? b : c) ;
    printf("\nThe biggest number is : %d", max) ;
}