Saturday, May 30, 2015

Moving Files To Disc


     This project is done for log files which make crowd to move  when they fill the space of disc.Did this project while working as intern.

     They complained about logs which filled the disc space and crash the system.This project which i did, automatially sorted logs by creation date. Then they fill them in files and zipped. Then move this zipped files to desired location.Thanks to this, you never lost log files, and can reach any of them by looking creation time.

    Also you can specify source and target disc name, and amount of files which will move. Developed with C# Windows Desktop . 

In Visual Studio when you want to pass parameters to application from outside you can right click the project and  add General -> Settings File.

Add Settings File

     After adding Settings File, enter parameter which use in projects.




SourceFolder: Location of Moving File from it
TargetFolder: Location of Moving File to it
WarningAmountOfFileSize: how much disc size fill the disch and  trigger to move
AmountOfPercentForMovingFull : Amount of percent of files which will move

In Type specify the type of variables and for Scope choose the Application.


In Program.cs in Main method you can reach the parameter:

  string directoryName = Settings.Default.TargetFolder;


In main method specify below.




 
public static void Main(string[] args)
        {
            Program myProgram= new Program();

            //prevent multiple instance of C# application
            
            if (Process.GetProcessesByName(Process.GetCurrentProcess().ProcessName).Length > 1)
            {
                Console.WriteLine("Only one instance allowed");
                Environment.Exit(0);
            }
            else
            {


                /*create target directory to store log files*/

                DateTime dailyDate = DateTime.Now;
               

               String directoryTargetName =  myProgram.GetTargetDirectoryName(dailyDate);
               
                int count = 0;
                //listing all files in disk
                string[] a1 = Directory.GetFiles(@Settings.Default.SourceFolder);


                //get free disk space
                string sourceDriverName = Settings.Default.SourceFolder.Split(new char[] { System.IO.Path.DirectorySeparatorChar }, StringSplitOptions.RemoveEmptyEntries).Last();

                string targetDriverName = Settings.Default.TargetFolder.Split(new char[] { System.IO.Path.DirectorySeparatorChar }, StringSplitOptions.RemoveEmptyEntries).Last();

                /*Specify Target Directory and check its availability*/


                string _directoryNameS = Settings.Default.TargetFolder + "\\" + directoryTargetName;

                myProgram.CheckExistDirectory(_directoryNameS);


                Boolean bFlag= myProgram.ControlOfCapacitySourceAndTargetDisc(sourceDriverName,targetDriverName);
              


                //Display all files
                Console.WriteLine("-----Files -------");
                foreach (string name in a1)
                {
                    Console.WriteLine(name);

                }

                Console.WriteLine("----Sorted Array by creation date------");

                //display sorting array
                //sort file by creation date

                IComparer fileComparer = new CompareFileByDate();
                Array.Sort(a1, fileComparer);

                foreach (string f in a1) // count file numbers
                {
                    count++;
                    Console.WriteLine(f);
                }

                //Move directories to another disc
                string destinationFile = @Settings.Default.TargetFolder; // destination disc
                Console.WriteLine("**********************************************");


                //calculate amount of moving from source to target disc
                decimal AmountOfMoving = Convert.ToDecimal(count) * Settings.Default.AmountOfPercentForMovingFull;
               
                int countSon = Convert.ToInt32(AmountOfMoving);


                 // control of target disc's free space
                decimal division = myProgram.GetDivision(sourceDriverName); // get division
                   
                if (bFlag && division <= Settings.Default.WarningAmountOfFullSize) // what percent of disc is moving
                    {
                        Console.WriteLine("Warning,80 percent of disk is full");
                        Console.WriteLine("you must carry move files another free disk");
                        Console.WriteLine("%d of files are moving " + countSon);

                        foreach (string f in a1)
                        {

                            Console.WriteLine(f);
                            string lastDirectory = f.Split(new char[] { System.IO.Path.DirectorySeparatorChar }, StringSplitOptions.RemoveEmptyEntries).Last();

                            File.Move(f, _directoryNameS + "\\" + lastDirectory); // store in dateTime directory

                            countSon--;
                            if (countSon == 0)
                            {
                                break;
                            }

                        }
                    }
                    else
                    {
                        Console.WriteLine("You have enough space for saving files..");
                    }
               
                  
               

            }

        }



//Check the existence of directory
 

  public void CheckExistDirectory(String directoryName)
        {

            //check folder exists

            if (Directory.Exists(directoryName))
            {
                Console.WriteLine("Warning !!! This directory is exists ..");
            }

            else
            {

                Console.WriteLine("Created Directory successfully ..");
                Directory.CreateDirectory(directoryName);
            }
        }





    
        /// Calculate Capacity of Discs
                public Boolean ControlOfCapacitySourceAndTargetDisc(String sourceDriverName, String targetDriverName)
        {

            /*Calculate amount of logs in Source Driver*/
            DriveInfo dS = new DriveInfo(sourceDriverName);
            Console.WriteLine("Total size of Source Driver : " + dS.TotalSize);
            Console.WriteLine("Total free size of Source Driver: " + dS.TotalFreeSpace);
            decimal totalSizeS = dS.TotalSize;
            decimal totalFreeSizeS = dS.TotalFreeSpace;
            decimal amountOfUsing = totalSizeS - totalFreeSizeS;

            Console.WriteLine("önceki gidecek kısım : " + amountOfUsing);
            amountOfUsing = amountOfUsing * Settings.Default.AmountOfPercentForMovingFull; // taşınacak miktarın büyüklüğü
            Console.WriteLine("tasinacak kısım : " + amountOfUsing);

            /*Control of Target Driver Capacity*/
            DriveInfo dD = new DriveInfo(targetDriverName);

            Console.WriteLine("Total free space of Target Disc: " + dD.TotalFreeSpace);
            decimal totalFreeSizeD = dD.TotalFreeSpace;

            Console.WriteLine("gidecek kısım : " + amountOfUsing + "yer var var mı : " + totalFreeSizeD);
           

            if (amountOfUsing <= totalFreeSizeD)
            {
                return true;
            }
            else 
            {
                Console.WriteLine("Warninng !!!  You do not have enough target space for moving ");
                return false;

            }
        
        }




  public class CompareFileByDate : IComparer
        {
            /// 
            /// Metod açıklaması
            /// 
            /// param1
            /// param2
            /// 
            public int Compare(Object a, Object b)
            {
                FileInfo fia = new FileInfo((string)a);
                FileInfo fib = new FileInfo((string)b);

                DateTime cta = fia.CreationTime;
                DateTime ctb = fib.CreationTime;

                return DateTime.Compare(cta, ctb);
            }
        }


Wednesday, May 27, 2015

In WebSite Find Online and Total Users Count

In websites we see online users and total user who visit the website. We may want to use this information in our websites. In Asp.net we can do it by using Global.asax to display it. To add it follow this way. File -> New-> Project-> Asp.net Web Application and add Global.asax.
Application State can save user counts.More information click link and get more info.



<%@ Application Language="C#" %>

<script runat="server">
    
    
    public static int count = 0;
    void Application_Start(object sender, EventArgs e) 
    {
        Application["TotalUsers"] = count;
        Application["OnlineUsers"] = count;
    }
    
    void Application_End(object sender, EventArgs e) 
    {

       // Application["users"] = -1;

    }
        
       void Application_Error(object sender, EventArgs e)

    {

    Exception exception = Server.GetLastError();

    string message = "Error occurred in - " + Request.Url.ToString() + "and error message is - "

    + exception.Message + " Stack Trace - " + exception.StackTrace.ToString();
    }


    void Session_Start(object sender, EventArgs e) 
    {
        Application.Lock();
        count++;
        Application["OnlineUsers"] = count;
        Application["TotalUsers"] = count ;
       
        Application.UnLock(); 

       

    }

    void Session_End(object sender, EventArgs e) 
    {
       // count--;
        Application.Lock();
        Application["OnlineUsers"] = count - 1;
        Application.UnLock(); 
        

    }

       
</script>


Saturday, May 23, 2015

Parallel Sorting With Multithread


First we must discuss what is thread and why we use it. It means small works and can work multiple with other threads and make paralel working.


The important point when using thread is decide to when it should stop. You can use detach or join for it stop.Detached thread has continued to run in the backround. Join is a thread waiting to be completed before starting another job.


In this project you are asked to implement a parallel program using multi-threading: You are given an array A of N unsorted integers , and you are to sort these numbers in parallel.



In this project you required to solve this problem with 8 threads. Each thread would be responsible for sorting exactly integers of the array as shown in following figure. For example, the first thread T1 would sort the integers in the second thread T2 would sort the integers in and so on. You would then have 8 sorted sub-arrays and you compute the overall sorted array.


Main Thread:

(1) Check command line arguments for validity. Exit in error.
(2) Allocate space for N integers
(3) Read the integers from the file and close the file
(4) Create 8 worker threads to sort the 8 sub-arrays using qsort.
(5) Start timer (use gettimeofday)
(6) Wait for the termination of the worker threads
(7) Stop the timer (use gettimeofday)
(8) Write the sorted integers to file “sorted.dat”.
(9) Print the execution time, and free up the allocated spaces




  • Parallel Sorting

  • 
    #include <stdio.h>
    #include <stdlib.h>
    #include <pthread.h>
    #include <time.h>
    #include <sys/time.h>
    #define NUM_THREADS 8
    
    
    void *worker_thread1(int *);
    void *worker_thread2(int *);
    void *worker_thread3(int *);
    void *worker_thread4(int *);
    void *worker_thread5(int *);
    void *worker_thread6(int *);
    void *worker_thread7(int *);
    void *worker_thread8(int *);
    void merge(int , int , int[] , int[], int);
    int *A; // N büyüklüğünde bir char array
    int base;
    int *C;//big array
    int k;
    int i1=0, i2=0, i3=0, i4=0, i5=0, i6=0, i7=0, i8=0;
    pthread_t thread1, thread2, thread3, thread4, thread5, thread6, thread7, thread8;
    int compare (const void * , const void * );
    int compare (const void * a, const void * b)// compare method to sort of an array
    {
      return ( *(int*)a - *(int*)b );
    }
    void* worker_thread1(int * p)// p:start  q= end of array
     {  //printf("**%d\n",*p);
       
    int i=0;  
      qsort (&A[0], base/8, sizeof(int), compare); // make qsort the array
      
                  //thread2 nin bitmesini bekle
                    while(i2==0);
      pthread_join(thread2, NULL);
    
      merge(base/8,base/8,&A[0],&A[base/8],0);
      
      int g;
      for(g=0;g<2*base/8;g++)
      {
       A[g]=C[g]; // A da T1 ve T2 var
      }
       
                  //thread3 nin bitmesini bekle
                    while(i3==0);
      pthread_join(thread3, NULL);
      merge(base/4,base/4,&A[0],&A[(2*base)/8],0);
      int y;
      for(y=0;y<base/2;y++)
      {
       
       A[y]=C[y]; // Artk A da T1 T2 T3 T4 var
      }
      
                   //thread5 nin bitmesini bekle
                    while(i5==0);
      pthread_join(thread5, NULL);
      
      merge(base/2,base/2,&A[0],&A[(4*base)/8],0);// Artık Cde hepsi sıralı
      
      
      
                    i1=1;
      return 0; 
    }
    void* worker_thread2(int * p)// p:start  q= end of array
     {  
        
      qsort (&A[base/8], base/8, sizeof(int), compare); // make qsort the array
                    i2=1;
      return 0;
    }
    void* worker_thread3(int * p)// p:start  q= end of array
     {  
      int i=(2*base)/8;  
      qsort (p, base/8, sizeof(int), compare); // make qsort the array
    
             //thread4 nin bitmesini bekle
                    while(i4==0);
      pthread_join(thread4, NULL);
      merge(base/8,base/8,&A[(2*base)/8],&A[(3*base)/8],i);
      int f;
      for(f=2*base/8;f<4*base/8;f++)
      {
       A[f]=C[f];// artk a da T3 ve T4 sıralı array var
       
      }
      i3=1;
      return 0;
      
    }
    void* worker_thread4(int * p)// p:start  q= end of array
     {  
        
      qsort (&A[(3*base)/8], base/8, sizeof(int), compare); // make qsort the array
                    i4=1;
      return 0;
    } 
    void* worker_thread5(int * p)// p:start  q= end of array
     { 
      int i=(4*base)/8;  
      qsort (p, base/8, sizeof(int), compare); // make qsort the array
    
               //thread6 nin bitmesini bekle
                    while(i6==0);
      pthread_join(thread6, NULL);
      merge(base/8,base/8,&A[(4*base)/8],&A[(5*base)/8],i);
      int h;
      for(h=(4*base)/8;h<(6*base)/8;h++)
      {
       A[h]=C[h]; // A da T5 ve T6 var
      }
      
      
                   //thread7 nin bitmesini bekle
                    while(i7==0);
      pthread_join(thread7, NULL);
      merge(base/4,base/4,&A[(4*base)/8],&A[(6*base)/8],(4*base)/8);
      
      for(h=4*base/8;h<base;h++)
      {
       A[h]=C[h]; // A da T5 T6 T7 T8 var
      }
      i5=1;
      return 0;
      
    }
    void* worker_thread6(int * p)// p:start  q= end of array
     {
        
      qsort (&A[(5*base)/8], base/8, sizeof(int), compare); // make qsort the array
                    i6=1;
      return 0;
    }
    void* worker_thread7(int * p)// p:start  q= end of array
     {  
      int i=(6*base)/8;  
      qsort (&A[(6*base)/8], base/8, sizeof(int), compare); // make qsort the array
    
    
                    //thread8 nin bitmesini bekle
                    while(i8==0);
      pthread_join(thread8, NULL);
      merge(base/8,base/8,&A[(6*base)/8],&A[(7*base)/8],i);
      int h;
      for(h=(6*base)/8;h<base;h++) // burada 7 ve 8 i birlestircez
      {
       A[h]=C[h]; // burada T7 ve T8 var
      }
                    i7=1;
      return 0;
      
    }
    void* worker_thread8(int * p)// p:start  q= end of array
     {  
        
      qsort (&A[(7*base)/8], base/8, sizeof(int), compare); // make qsort the array
                    i8=1;
      return 0;
    }   
    
    // main method
    
    
    int main(int argc, char *argv[])
    {   
     struct timeval start_time,end_time;
       base = (argc > 2) ? atoi(argv[1]) : -1; // convert to string to 
     if(base == -1){
     printf("Yanlis deger girdiniz!!");
     exit(0);}
    
     
      A = (int *)malloc(sizeof(int)*base); // allocate space for a 100 character string
      C = (int *)malloc(sizeof(int)*base); 
         if (A == NULL) {fputs ("Memory error",stderr); exit (2);}
    
     FILE *fp = fopen(argv[2], "rb");
     fread(A, sizeof(int), base, fp);
     
     fclose(fp);
     
    
     int r1=pthread_create(&thread1, NULL,(void *) &worker_thread1,(int *)&A[0]);
     int r2=pthread_create(&thread2, NULL,(void *) &worker_thread2,(int *)&A[base/8]);
     int r3=pthread_create(&thread3, NULL,(void *) &worker_thread3,(int *)&A[(2*base)/8]);
     int r4=pthread_create(&thread4, NULL,(void *) &worker_thread4,(int *)&A[(3*base)/8]);
     int r5=pthread_create(&thread5, NULL,(void *) &worker_thread5,(int *)&A[(4*base)/8]);
     int r6=pthread_create(&thread6, NULL,(void *) &worker_thread6,(int *)&A[(5*base)/8]);
     int r7=pthread_create(&thread7, NULL,(void *) &worker_thread7,(int *)&A[(6*base)/8]);
     int r8=pthread_create(&thread8, NULL,(void *) &worker_thread8,(int *)&A[(7*base)/8]);
     
     int x;
            gettimeofday(&start_time,NULL); // baslangıc zamanı
     
     //thread1 nin bitmesini bekle  
            while(i1==0);
     pthread_join( thread1, NULL);
    
     gettimeofday(&end_time,NULL); //bitis zamanı
    
     FILE *fx = fopen("sorted.dat", "wb"); //  sıralı diziyi dosyaya yazdır
     fwrite(&C[0], sizeof(int), base, fx);
     fclose(fx);
    
     //print execution time
     printf ("Execution time: %ld microseconds \n",((end_time.tv_sec *1000000 + end_time.tv_usec)-(start_time.tv_sec * 1000000 + start_time.tv_usec)));
    
     
    
     free(A);    // arraylari serbest bırak
     free(C);
    return 0;
     
    
    }
    void merge(int m, int n, int A[], int B[],int ptr) {  // sıralı A ve B arrayini merge yapıp global C arrayine at
    
          int i, j;
          
    
          i = 0;
    
          j = 0;
    
       
    
          while (i < m && j < n) {
    
                if (A[i] <= B[j]) {
    
                      C[ptr] = A[i];
                  
    
                      i++;
        ptr++;
    
                } else {
    
                      C[ptr] = B[j];
     
                      j++;
        ptr++;
    
                }
    
                
    
          }
    
          while(i<m && j>=n){
                      C[ptr] = A[i];
                      i++;
                      ptr++;
                      
          } 
    
    
          while(j<n && i>=m){
                      C[ptr] = B[j];
                      j++;
                      ptr++;
                     
          }
      
     
    
    
    }