sobota 11. června 2011

VUT–FIT: IFY (laboratorní protokoly)

Snad to někomu ulehčí práci. ;-)
Odkaz na dokumenty...







VUT–FIT: IOS projekt 2 (problém spícího holiče)

Druhý projekt do operačních systémů. Jednalo se o problém spícího holiče.
Získal jsem za to 13 bodů z 15, takže pro inspiraci se může hodit.


Zdrojový kód nalezte ZDE.
Makefile:
barber: main.c
gcc -std=gnu99 -Wall -Wextra -Werror -pedantic -lrt main.c -o barbers




Script na úklid:
#!/bin/bash ids=$(ipcs | grep $(whoami) | awk "{print \$2}") for id in $ids; do ipcrm -m $id 2>/dev/null ipcrm -q $id 2>/dev/null ipcrm -s $id 2>/dev/null done


   1: /**
   2:     - Soubor:  main.c
   3:     - Datum:   2011/04/26
   4:     - Autor:   Matej Marecek
   5:     - Kontakt: xmarec12@stud.fit.vutbr.cz
   6:     - Projekt: Semafory IOS.
   7:     - Popis:   Program implementuje problem spiciho holice pomoci semaforu.
   8:     - Dalsi popis: Napoveda-viz. stranky se zadanim.
   9:     - Verze:   017_stableD
  10:  **/
 

Foto - 22.5.2011





čtvrtek 9. června 2011

Smile Generator

I have created simple smile generator. It is my first non-console application. I am using .NET programming platform and code itself is written in C# and (G)UI is based on (WPF (Windows Presentation Foundation).

String versus StringBuilder:
In first prototype of my application I used algorithms which were using simple .NET strings and this solution proved to be ineffective. Problems occurred when I wanted to generate more lines of smiles. The disadvantage of using strings is, that when you want to append another characters, operating system has to reallocate memory. So when you appending in each cycle another data to string, it is very slow.

Try to write algorithm where is one cycle inside another and in each pass computer has to reallocate memory. If you put high numbers to cycle’s conditions, you can wait forever.

One solution is to use array of chars, like in C/C++ and then create intelligent function, which can double allocated memory if it is needed (that is just example). But in that case, you have to write everything yourself (C# is awesome because you can use lot of libraries). It would be better to write application in C/C++ because it these apps are usually faster than .NET/Java apps.

Fortunately C# has StringBuider Class (System.Text.StringBuilder) and it is very useful when you need to work with long strings.

There is example form Microsoft website:

using System;
using System.Text;

public sealed class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      // Create a StringBuilder that expects to hold 50 characters.
      // Initialize the StringBuilder with "ABC".
      StringBuilder sb = new StringBuilder("ABC", 50);

      // Append three characters (D, E, and F) to the end of the StringBuilder.
      sb.Append(new char[] { 'D', 'E', 'F' });

      // Exampleend a format string to the end of the StringBuilder.
      sb.AppendFormat("GHI{0}{1}", 'J', 'k');

      // Display the number of characters in the StringBuilder and its string.
      outputBlock.Text += String.Format("{0} chars: {1}", sb.Length, sb.ToString()) + "\n";

      // Insert a string at the beginning of the StringBuilder.
      sb.Insert(0, "Alphabet: ");

      // Replace all lowercase k's with uppercase K's.
      sb.Replace('k', 'K');

      // Display the number of characters in the StringBuilder and its string.
      outputBlock.Text += String.Format("{0} chars: {1}", sb.Length, sb.ToString()) + "\n";
   }
}

// This code produces the following output.
//
// 11 chars: ABCDEFGHIJk
// 21 chars: Alphabet: ABCDEFGHIJK

Optimization:
Optimization is very important aspect in programming. Many orthodox programmers say that apps written in .NET/Java etc. are slow. In some cases it is true. Especially some Java apps are incredible slow. But if you write bad code, your app in assembly language/C/C++ can be much slower than Java. There are many things, which can cause problem with speed. In my Smile Generator I had to solve problems with access to RAM memory.

Processors are very complex and fast devices. In the past decade, processor performance increased rapidly (and RAM not so much). If we compare CPU and RAM, we can see that CPU is much faster and every single access to RAM is like ages for processor. And this is the reason why we should avoid read/write to RAM unreasonably often.

Code examples:

string vystup = "";
              for (int i = 0; i < _lines; i++)
              {
                     for (int j = 0; j <= i; j++)
                     {
                           vystup = vystup + _type + " ";
                     }
                     vystup = vystup + "\n";
              }
              return vystup.ToString();


If we rewrite this code with StringBuilder we will achieve better result with long strings.

StringBuilder vystup = new StringBuilder();
           
              for (int i = 0; i < _lines; i++)
              {
                     for (int j = 0; j <= i; j++)
                     {
                           vystup.Append(_type + " ");
                     }
                     vystup.Append("\n");
              }
              return vystup.ToString() ;



And if we optimize this algorithm, your app becomes even much faster.

StringBuilder vystup = new StringBuilder();
              StringBuilder tmp = new StringBuilder();
              for (int i = 0; i < _lines; i++)
              {
                     tmp.Append(_type + " ");
                     vystup.Append(tmp.ToString() + "\n");
              }
              return vystup.ToString();


The result is this algorithm is:

:-)

:-) :-)

:-) :-) :-)

:-) :-) :-) :-)

:-) :-) :-) :-) :-)

If you want, the app is HERE. :-) (it is still beta version)