Friday, February 27, 2015

Wait and Pulse Method in C# Threading


Wait and Pulse Method in C# Threading

Introduction

The monitor class have two static method Wait() and Pulse(). The purpose of Wait and Pulse is to provide a simple signaling mechanism: Wait blocks until it receives notification from another thread; Pulse provides that notification.


Wait must execute before Pulse in order for the signal to work. If Pulse executes first, its pulse is lost, and the late waiter must wait for a fresh pulse, or remain forever blocked. This differs from the behavior of an AutoResetEvent, where its Set method has a "latching" effect and so is effective if called before WaitOne.

One must specify a synchronizing object when calling Wait or Pulse. If two threads use the same object, then they are able to signal each other. The synchronizing object must be locked prior to calling Wait or Pulse.

When a thread is temporarily blocked from running, it calls Wait() This causes the thread to go to sleep and the lock for that object to be released, allowing another thread to use the object. At a later point, the sleeping thread is awakened when some other thread enters the same lock and calls Pulse() or PulseAll(). A call to Pulse() resumes the first thread in the queue of threads waiting for the lock. A call to PulseAll signals the release of the lock to all waiting threads.


Here are two commonly used forms of Wait():

public static bool Wait(object waitObject)
public static bool Wait(object waitObject, int milliseconds)

The first form waits until notified. The second form waits until notified or until the specified period of milliseconds has expired. For both waitObject specifies the object upon which to wait.

Here are the general forms for Pulse() and PulseAll():
public static void Pulse(object waitObject)
public static void PulseAll(object waitObject)

Here, waitObject is the object being released.
To understand the need for and the application of Wait() and Pulse() we create following program

using System;
using System.Threading;
namespace WaitndPulesMethod
{
class PingPong
    {
        public void ping(bool running)
        {
            lock (this)
            {
                if (!running)
                {
                    //ball halts.
                    Monitor.Pulse(this); // notify any waiting threads
                    return;
                }
                Console.Write("Ping ");
                Monitor.Pulse(this); // let pong() run
                Monitor.Wait(this); // wait for pong() to complete
            }
        }
        public void pong(bool running)
        {
            lock (this)
            {
                if (!running)
                {
                    //ball halts.
                    Monitor.Pulse(this); // notify any waiting threads
                    return;
                }
                Console.WriteLine("Pong ");
                Monitor.Pulse(this); // let ping() run
                Monitor.Wait(this); // wait for ping() to complete
            }
        }
    }
    class MyThread
    {
        public Thread thread;
        PingPong pingpongObject;
        //construct a new thread.
        public MyThread(string name, PingPong pp)
        {
            thread = new Thread(new ThreadStart(this.run));
            pingpongObject = pp;
            thread.Name = name;
            thread.Start();
        }
        //Begin execution of new thread.
        void run()
        {
            if (thread.Name == "Ping")
            {
                for (int i = 0; i < 5; i++)
                    pingpongObject.ping(true);
                pingpongObject.ping(false);
            }
            else
            {
                for (int i = 0; i < 5; i++)
                    pingpongObject.pong(true);
                pingpongObject.pong(false);
            }
        }
    }
    class BouncingBall
    {
        public static void Main()
        {
            PingPong pp = new PingPong();
            Console.WriteLine("The Ball is dropped... \n");
            MyThread mythread1 = new MyThread("Ping", pp);
            MyThread mythread2 = new MyThread("Pong", pp); 
            mythread1.thread.Join();
            mythread2.thread.Join();
            Console.WriteLine("\nThe Ball Stops Bouncing.");
            Console.Read();
        }
    }
}

Saturday, February 21, 2015

Uploading a file to Azure Blob Storage using C#




So I'm in the process of upgrading my blog to be able to upload pictures to the Azure Blob Storage.
Most of the samples I found required alot of XML configuration, or special project types. This post offers a simpler way, that will get you started.
  1. First, you need the NuGet Package "WindowsAzure.Storage".
  2. After that, then ensure that you have a storage account on Azure, and that you created a public container.
  3. Find you account name and primary access key (There is a icon called manage keys at the bottom of the screen of the mangement portal)
  4. Take the code snippet below and add your values (remember to edit the file path to a file that exists):
 
 var credentials = new StorageCredentials("deldysoft", "YOUR PRIMARY KEY FROM. GET IT FROM THE MANGEMENT PORTAL");

var client = new CloudBlobClient(new Uri("http://deldysoft.blob.core.windows.net/"), credentials);

// Retrieve a reference to a container. (You need to create one using the mangement portal, or call container.CreateIfNotExists())
var container = client.GetContainerReference("deldydk");

// Retrieve reference to a blob named "myfile.gif".
var blockBlob = container.GetBlockBlobReference("myfile.gif");

// Create or overwrite the "myblob" blob with contents from a local file.
using (var fileStream = System.IO.File.OpenRead(@"C:\\myfile.gif"))
{
  blockBlob.UploadFromStream(fileStream);
}
 
 

Image result for azure logo
 

Thats it! You should now be able to see your blob at the mangement portal.
If you need to list blobs, and other stuff that is porsible, then look at other tutorials on the web or at the Azure Website. You should be able to use the client you created here to skip over all the configuration stuff that many of them include.
Have fun!
 

Sunday, February 15, 2015

Running Hadoop on Cygwin in Windows



In this document you are going to see how you can setup pseudo-distributed, single-node Hadoop (any stable version 1.0.X) cluster backed by the Hadoop Distributed File System, running on windows. Run your Hadoop cluster through 10 steps
 
Pre-requisite
Software’s to be downloaded before you start these procedures. You can download all the recommended software’s before you get started with the steps listed below
 
 
Single-node Hadoop cluster step by step instruction
 
1. Installing Cygwin
a) Cygwin comes with a normal setup.exe to install in Windows, but there are a few steps you need to pay attention, I would like to walk you through the step by step installation. Click her to download Cygwin setup
b)Once you start installing the first screen which appears this
SSH Installation
c) After 4 steps from the above screen you will be getting a screen to select packages, in this step you can choose OpenSSH installation along with Cygwin
 
d)  Cygwin installer proceeds with including all dependent packages which are required for the installation.

Now you installed Cygwin with OpenSSH2. Set Environment Variable in Window
a)  Find “My Computer” icon either on the desktop, right-click on it and select Properties item from the menu.
b) When you see the Properties dialog box, click on the Environment Variables button which you see under the Advance Tab.
c) When you click Environment Variables dialog shows up, click on the Path variable located in the System Variables box and then click the Edit button.
d) Edit dialog appears append you cygwin path end of the Variable value field
             (I installed Cygwin under C: drive  – c:\cygwin\bin;)

Now you are down with Cygwin environmental setup
 
 
 
3. Setup SSH daemona) Open the Cygwin command prompt.
b) Execute the following command:
                    $ ssh-host-config
 
c)       When asked if privilege separation should be used, answer no.
 
d)       When asked if sshd should be installed as a service, answer yes.
(If it prompts with CYGWIN environment variable, enter ntsec)

4. Start SSH daemon

a) Find My Computer icon either on your desktop, right-click on it and select Manage from the context menu.

b) Open Services and Applications in the left-hand panel then select the Servicesitem.

c) Find the CYGWIN sshd item in the main section and right-click on it.

d) On the property popup you can select “Start up :” Automatic. So that it will start up when windows starts
 
 
5.      Setup authorization keys
a)       Open Cygwin Terminal and exectute the command
             $ ssh-keygen
(Since we are generating keys without password, so press enter. Below is the sequence of text which appears in the terminal prompt)
b)      Once the command completed generating the key
             $ cd ~/.ssh
(.ssh folder will be under $ <user> directory, eg:- please find he screen shot below .ssh is under my user profile installed in my system is “sunder”)

 
c)   Next step is to create an RSA key pair with an empty password. You have to enable SSH access to your local machine with this newly created key.
                $ cat id_rsa.pub >> authorized_keys
 
Now you created RSA key pair
 
To test SSH installed, from a terminal prompt enter:
                                    $ ssh localhost
( You will get a similar notification in the terminal)
 
1
$ ssh localhost
2
Last login: Mon Apr  8 21:36:45 2013 from shankar-pc
 
Now you SSH successfully running with keys generated
 

 
 
6.       JAVA Installation
a)       Installing JAVA in windows system is a easy step up step process
b)       You can download .exe for Windows installation file from the Oracle JDK download page
c)      Choose your JAVA installation folder (eg :- C:\Java\jdk1.6.0_41) and install JAVA
Now you successfully installed JAVA
 
7.       Setting JAVA_HOME in Windows
a)       Set environmental variable for JAVA_HOME, as we already did for Cygwin in the above instruction – the same steps to be followed for setting JAVA_HOME
b)       You may need to create a new variable under the User Variable / System Variable. Please find the reference screen shot below

 
8.    Setting JAVA_HOME in Cygwin
  a. To set JAVA_HOME in Cgwin have to update Java home directory in /etc/bash.bashrc
 
 b. edit $HOME/.bashrc file to set JAVA home directory
 
                         $ vi bashrc
 
c. Set Java home, you can see export JAVA_HOME line in the file been commented using #. Remove # (uncomment it) and you have to key in by giving your Java installed path 
 
 d. export JAVA_HOME= c:\\java\\jdk1.6.0_41
 
(to recognize your windows folder you have give 2 backward slash”\\” for each folder, since I installed java under  c:\java\jdk1.6.0_41 in my windows path) 
 
 
Please Note:
 Since you are using Windows you can also edit file through windows explorer whenever you are editing any files inside Cgwin through Windows either with notepad or wordpad, after saving the files in windows ensure you get into Cgywinterminal and locate the file and execute a UNIX command  “$ dos2unix <filename>”. This is more important in all stages of execution
 
e. Exit any terminal and open a new terminal
f. To check the variable is set, type the command in a terminal
                       $ echo $JAVA_HOME
(The above command will display the java directory path you se or you can also type $ java –version or simply $ java to see execution of java commands in the terminal )
 
Now you set environment variable in Cygwin ie. JAVA_HOME
 
 
9.       Hadoop Installation
Below step by step instruction will help you to setup a single-node Hadoop cluster. Before we move on know about the HDFS (Hadoop Distributed File System) Architecture Guide
 
a) Download a recent stable release from one of the Apache Download Mirrors.
b) Download “hadoop-<version>.tar.gz” to your desired directory
c)    From the terminal type this command where you download your hadoop-< version>.tar.gz file
$ tar -xvf hadoop-<version>.tar.gz
d)  The above command will extract the hadoop files and folder
e)  Once you extracted all the files, you may have to edit few configuration files inside  <Hadoop Home> directory
Feel free to edit any file through windows with wordpad but don’t forget to execute the UNIX command “$ dos2unix <filename>” for all the files you open up in windows.
f)   Now edit <Hadoop Home>/conf/hadoop-env.sh to set Java home as you did it before for environmental variable setup
(Since I already set my JAVA_HOME in .bashrc so I gave JAVA_HOME=$JAVA_HOME)
 
g)   And then update <Hadoop Home>/conf/core-site.xml. with the below xml tag to setup hadoop file system property
<configuration>
<property>
<name>fs.default.name</name>
  <value>hdfs://localhost:50000</value>
</property>
</configuration>
h)    Now update<Hadoop Home>/conf/ mapred -site.xml with the below xml tag
<configuration>
<property>
<name>mapred.job.tracker</name>
  <value>localhost:50001</value>
</property>
</configuration>
i)    Now update<Hadoop Home>/conf/ hdfs -site.xml with the below xml tag
<configuration>
<property>
<name>dfs.data.dir</name>
<value>/home/<user>/hadoop-dir/datadir</value>
</property>
<property>
<name>dfs.name.dir</name>
<value>/home/<user>/hadoop-dir/namedir</value>
</property>
</configuration>
Assume you created your directory in your user profile, so user your user name after /home otherwise you can also check your folder by executing pwd command after you get into your terminal inside your created folder 
Assuming create “data” and “name” directory from your home directory, I created a directory hadoop-dir and inside that I have created 2 directories one for name node and other for data node
ensure your data and name directory created and accessed by hadoop, execute the command to change the directory permission  by
             $ chmod 755 data
as well as
 
             $ chmod 755 name
i.e.  if you $ ls -l your directory you data and name directory should be in this mode “drwxr-xr-x” which means owner has three permissions, and group and other have only read and execute permissions
 
10.   HDFS Format
Before starting your cluster you may need to format your HDFS by running the below command from <Hadoop-Home-Dir>/bin
$ ./hadoop namenode -format
 
11.  Copy File to HDFS
        To copy local file to HDFS execute this command from <Hadoop-Home-Dir>/bin  from the terminal
            $ ./hadoop dfs -copyFromLocal <localsrc> URI
Eg: – If I have a sample.txt file in the path /home/<user>/Example
 
Then I have to executing the command from<Hadoop-Home-Dir>/bin
             $ ./hadoop dfs –copyFromLocal /home/<user>/Example/sample.txt /
 This command will copy the local fin into HDFS home directory
 
12.   Browse HDFS through web interface
Starting hadoop cluster is by executing a command from <Hadoop-Home-Dir>/bin
            $ ./start-all.sh
This will startup a Namenode, Datanode, Jobtracker and a Tasktracker on your machine
 
To stop the cluster
            $ ./stop-all.sh
to stop all the daemons running on your machine.
 
To understand more on Getting Started With Hadoop
Browse the web interface for the NameNode and the JobTracker; by default they are available at:
 
NameNode – http://localhost:50070/
JobTracker – http://localhost:50030/
 

 
 
 
 

 

Uninstalling Cygwin

Here's the rough steps on how to do it, according to the official FAQ.
  1. Stop and remove any cygrunsrv services
  2. Stop all Cygwin processes and unmount any filesystem's mount with the mount command.
  3. Delete the Cygwin root folder and all subfolders.
  4. Delete the Cygwin shortcuts on the Desktop and Start Menu.
  5. Remove cygwin binary path from your Windows search path, and delete the CYGWIN system variable.
  6. Delete Software\Cygwin keys from registry.  

Removing SSHD service

To remove Cygwin sshd in the cleanest possible way:
  • cygrunsrv -E sshd
  • cygrunsrv -R sshd
  • Delete the folder c:\cygwin and all its sub-folders
  • Remove the Environment Variable 'CYGWIN'
  • Edit the Environment path accordingly
  • Start...Run... regedit and delete these two registry trees
  • "HKEY_CURRENT_USER_Software_Cygnus Solutions"
  • "HKEY_LOCAL_MACHINE_Software_Cygnus Solutions"
  • Go to Control Panel, Category View, Performance and Maintenance, Administrative Tools, Computer Management.or click Start...Run...compmgmt.msc delete the sshd user if it exists in the "System Tools" - "Local Users and Groups" - "Users" section.

Monday, February 9, 2015

Install and Enable Telnet in Windows 8 & 8.1 – Use The Telnet Client Utility

Telnet is one of the famous network protocol widely used to manage network and other equipment. If you are a system or network administrator, I’m sure that you know the importance of having telnet client utility on your Windows 8.1 or 8 computer. For some strange security reasons,Windows 8 or 8.1 did not come telnet client installed. By default you can’t use Windows 8 or 8.1 to connect other devices with telnet protocol. But you can enable this feature easily.  Here is a  simple guide about how to install and enable telnet in Windows 8 and 8.1.

By default if you search telnet in apps or type ’telnet’ in command prompt, it will return an error message as ’not recognized as an internal or external command’. Installing the telnet client from control panel in Windows 8.1 or 8 will solve this issue.
Installation medium such as CD/DVD, ISO or any other downloads are not required for this purpose, we can simply enable it from control panel.


To Enable Telnet in Windows 8 & Windows 8.1, Follow the Steps.

1) Access Control Panel from Windows charm bar or pressing Windows key + I   from desktop.


telnet in windows 8

2) In ‘Category’ control panel view, click ‘Programs’.

click on programs

3) Click ‘Turn Windows features on or off’ in next screen as shown below.

turn on windows features

 

Recommended Reading

How to enable ping response in Windows 8
5) Earlier step will bring a dialog box where you can select extra features. Select  ‘Telnet Client’ by ticking the box. A small installation will run once you have pressed OK.

telnet in windows 8.1
That’s it, restarting PC not required here. You have successfully installed telnet on Windows 8 or 8.1.
Now if you try telnet command in command prompt or search in Apps, you will get the correct telnet prompt as below.

working telnet in Windows 8

If you are a professional network admin, Windows telnet client utility will not be suitable for you. You can try some other professional telnet client utilities.
My favorite is, PuTYY which works fine on Windows 8.1 and 8.

putty