Using Mutex to implement atomicity

/***********************************************************************
* OBJECTIVE : Implement atomicity in case of threads
* FILE NAME : mutex.c
* DATE      : 10-09-2012
* AUTHOR    : Abhishek Kumar Singh
************************************************************************/

#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<unistd.h>
#include<pthread.h>

/* Gobal data */
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
int counter = 0;

void *increment(void *);

int main()
{
    pthread_t thread1,thread2;
    int iret1,iret2;

    /* Create two threads that executes independently and parallely */
    iret1 = pthread_create(&thread1,NULL,increment,NULL);
    iret2 = pthread_create(&thread2,NULL,increment,NULL);

    if(iret1 != 0)
        fprintf(stderr,"thread1 can't created");
    if(iret2 != 0 )
        fprintf(stderr,"thread2 can't be created");

    /* Waiting for the threads to terminate */
    pthread_join(thread1,NULL);
    pthread_join(thread2,NULL);

    return EXIT_SUCCESS;
}

void *increment(void *ptr)
{
    /* Locking Resources for other threads */
    pthread_mutex_lock(&mutex);
    counter ++;
    printf("counter:%d\n",counter);
    pthread_mutex_unlock(&mutex);
    /* Unlocking Resources so that other threads can acess */
    return NULL;
}

Cracking passwords using Jhon The Reaper

Download and Install John the reaper using

$ sudo apt-get install john

Cracking Linux password

being root run the following command:

$ /usr/bin/unshadow /etc/shadow /etc/passwd>/tmp/crackpass.db

Now being normal user run the following command

$ john /tmp/crackpass.db

It will take some time depending on the strength of password to crack it.

cracking MD5 passwords

$ john <passwdfile> –format MD5

Recording & playback of terminal session

Here is a simple tip to record and playback a terminal session.It is done by command script and scriptreplay.
You will find it very useful in making good tutorials.

Recording:

$ script -t 2> timing.log -a output.session
now type your commands;
…..
…..
…..
$ exit
script command here takes two files as arguments ‘timing.log(stores timing information at which each commands are run)’ and ‘output.session(stores the output of the commands)’

Playback:

$ scriptreplay timing.log output.session

Note:
timing.log and output.session can be shared with anyone who wants to replay a terminal session in their terminal.