Tuesday, December 6, 2011

What does AWK stand for?

awk = "Aho, Weinberger and Kernighan"
This command was named by its authors Al Aho, Peter Weinberger and Brian Kernighan.

Friday, November 18, 2011

How do you convert a chunk of obfuscated or just plain messy and hard to read Perl code into readable format?

We can take advantage of the "Deparse" module. It compiles, then decompiles the program it is given, expanding it out and formatting it nicely.

To run it at the command line, type "perl -MO=Deparse prog.pl". Here is an example
of its usage, showing the input program (red) and the output of Deparse (blue).

$ cat messy.pl
for(74,117,115,116){$::a.=chr};(($_.='qwertyui')&&
(tr/yuiqwert/her anot/))for($::b);for($::c){$_.=$^X;
/(p.{2}l)/;$_=$1}$::b=~/(..)$/;print("$::a$::b $::c hack$1.");


$ perl -MO=Deparse messy.pl
foreach $_ (74, 117, 115, 116) {
    $a .= chr $_;
}
;
$_ .= 'qwertyui' and tr/eiqrtuwy/nr oteah/ foreach ($b);
foreach $_ ($c) {
    $_ .= $^X;
    /(p.{2}l)/;
    $_ = $1;
}
$b =~ /(..)$/;
print "$a$b $c hack$1.";

Tuesday, November 8, 2011

How can we access environmenttal variables in Perl?

Perl provides access to environment variables via the global %ENV hash. However, if using the Env module, it will create global scalars for all the environment variables.

use Env; print "$USER uses $SHELL";
If required to get only a few selected variables then it can be done like:
 
use Env qw[@PATH $USER]; print "$USER's path is @PATH";

How can we call another program without invokation of shell in Perl?

The system function is Perl's simplest method for accomplishing this. However, one point that should be noted here is: both this program and the Perl program from where we will be running it, will be sharing the same STDIN and STDOUT.

The syntax is like:

$status = system("myprog arg1 arg2");

This particular syntax will invoke the shell, to interpret the command line. In order to avoid the shell's involvement, the arguments for the program being executed should be passed as a list, like:
$status = system("myprog", "arg1", "arg2);

The system function does not return the program's STDOUT, rather its return value is either the program's exit status or the signal number that the process might have died from.

How can we dynamic activate the debugger in Perl?

while (<INPUT>)
   {
   $DB::trace = 1, next if /debug/;
   $DB::trace = 0, next if /nodebug/;
   # more codes
   }
When run under the debugger, this enables tracing when the loop encounters an input line containing "debug" and ceases tracing upon reading one containing "nodebug". We can even force the debugger to breakpoint by setting the variable $DB::single to 1, which also happens to provide a way you can debug code in BEGIN blocks (which otherwise are executed before control is given to the debugger).

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) ;
}