Pages

Prime check Algorithm

#include <stdio.h>
#include <stdlib.h>
int isPrime(int num);
int main(int argc, char* argv[])
{
int arg = 5;
if(argc > 1)
{
arg = atoi(argv[1]);
}
printf("The argument is %d\n", arg);
if(isPrime(arg))
{
printf("The number %d is prime\n", arg);
}
else
{
printf("The number %d is not prime\n", arg);
}
}
int isPrime(int num)
{
int i=2;
int mod=0;
for ( ; i<=(int)(num/2); i++)
{
mod=num%i;
printf("The modulus is %d\n", mod);

if(mod==0)
{
printf("%d divisible by %d\n", num, i);
return 0;
}
}
return 1;
}

Multi-Processing in bash

Advanced programmers and script writers must have been familier with the concepts  of multi threading/multi processing. Many of the high level commands support them. However, scripting languages also support them. Recently, I have been dealing with one of such interesting problem in bash scripts.
Given is an example of a script that spawns a new child process in the background and the main process continues.

#!/bin/sh
print_info(){
PROCESS_MESSGAE=$1
LAST_SPAWNED_PID=$!
DATE_TIME=`date`
echo "Process Info : $PROCESS_MESSGAE, TIME=$DATE_TIME ,PID=$LAST_SPAWNED_PID"
}
(
i=0
while [ "$i" -lt 100 ]
do
echo $i
sleep 2
i=$((i+1))
done
) &
PID_LAST_SPAWNED=$!
print_info "While loop and process info check"
sleep 10
kill $PID_LAST_SPAWNED
The block of code that is written between single braces “()”, will run in the new process. Any command followed by an ampersant “&” will run on background, independent with the parent process. So the program is creating a background process which prints the values of $i in a while loop.
The main process grabs the process_id ie “PID” of the process, and then kills it after 10 seconds. So the background process could only print up to 5.

Configure Jetty to trust a self signed certificate

I was having a problem connecting jasig-cas server from my local installation.
The cas server was in https, with a self signed certificate. My local application is in http mode running on maven.
Now I dont have the admin previliges to add the certificate to the java’s cacerts file as trusted file. So my application was failing.
Steps.
1. Download the certificate from browser to local computer.
2. use the java keytool to create a truststore file.
“%JAVA_HOME%\bin\keytool” -import -alias tomcat -file “D:\certificates\nvscmlinq1-cas.crt” -keystore nvscmlinq1-cas.jks
NOTE: this will create the nvscmlinq1-cas.jks file which is the truststore file.
3. Run the maven with command line java arguments to specify the truststore file and password.
mvn jetty:run -Djavax.net.ssl.trustStore=”D:\certificates\nvscmlinq1-cas.jks” -Djavax.net.ssl.trustStorePassword=changeit

Text Replacement through bash script

Normally, in Code-Projects, we write generic codes with hard coaded Variables whose values are known at the deployment time. For example, we may write START_PORT, STOP_PORT in pom.xml and these ports may very as per the environments are being deployed on.
Linux based build system have Makefile, where you type something like
make init profile=dev && make run profile=dev
and as per the profile supplied, the internal values in the project has to adjust.
One way of doing it is by using the linux sed command. sed is a command for editing io-streams.
Example:
Create a file named file1.txt
Write some content. (My name is <Name>)
Now with a command prompt, type
cat file1.txt |sed s/""/Rajan/g |cat > file2.txt
First check the content of file2.txt.
Now let me explain you what the command is doing.
First the cat command in left extracts the content of file1.txt and gives it to sed command. The sed command is taking an input stream and replacing the pattern “” with “Rajan” text. Not the stream is again piped to cat command as an input which outputs to the file2.txt file.

Tunneling | a trick

Sometimes in some environment, some of the websites may be restricted. There are lots of softwares eg. mcafee, which acts as a proxy of your network in your company network and restricts some category sites to be browsed.
What you could do is, you could tunnel the request to some remote machine and browse the contents from there. This will work if the proxy filtering is not applied in that network where the remote machine is located.
Steps
1. Open your putty, type the server where you want to connect
2. Click the “Tuenneling” link in Putty, select “Dynamic” as the option
3. Enter a port number in the port sectioin, and add. eg : 8080
4. Hit enter, enter username and password in the terminal. Connect to the machine
5. In the browser, set the proxy as “localhost:8080″
6. Browse and enjoy.

My online Work on oDesk

Hello everyone out there reading my blog.
I am a computer programmer and a very ambitious one. Apart of working on my daily job I was trying to find a job online, not only because of money, but I want to expand my options. Working for someone else, doing everything they told to do, hoping that they would notice my hard work and grant me bonus or other benefits, haaaah. Not my thing. I tried for nearly six months on odesk and finally got one job.