Pointer – The Most Powerful and Painful thing in C

আমরা সবাই জানি, সি একটি অতি powerful ল্যাঙ্গুয়েজ এবং এর পাওয়ার টা পুরোপুরি ই পয়েন্টার কে নিয়ে। পয়েন্টার হচ্ছে অতি মাত্রার পেইনফুল কিন্তু অনেক মজার ফিচার।

প্রথমেই বলে রাখি, পয়েন্টার নিয়ে কথা বলার মত কোন এক্সপার্ট আমি না। আমি মনে করি যে “teach while u learn”. তাই অধমের এই লেখাটা পড়ে যদি একজনের ও উপকার হয় তাহলে লেখা টা স্বার্থক হবে ঃD

পয়েন্টার এর বেসিক টা একটু বুঝে গেলেই এটা দিয়ে অনেক কাজ সহজেই করে ফেলা যায়।

পয়েন্টার এর কিছু ব্যবহারঃ

১। array element access করার জন্য

২। একটা ফাংশন থেকে একাধিক ভ্যালু রিটার্ন করার জন্য

৩। ডাইনামিকালি allocated মেমরী access করার জন্য

৪। ডাটা স্ট্রাকচার [linked list, trees, graphs] ইম্পলিমেন্ট করার জন্য ।

মেমরী কীঃ

1

পয়েন্টার সম্পর্কে জানতে হলে যেই ব্যাপার টা বুঝা দরকার তা হলো আমরা কিভাবে ভেরিয়েবল ইউজ করি এবং কম্পিউটার এর মেমরি কে কিভাবে কাজে লাগানো হয়।

পয়েন্টার আর মেমরী address একে অপরের সাথে সম্পর্কিত। মেমরী অনেক গুলো ছোট ছোট ব্লকে বিভক্ত। প্রত্যেকটি ব্লক বা জায়গা কে একটি বিট বলে। একটা বিট এ ০ / ১ থাকতে পারে। এরকম আটটি বিট নিয়ে একটি বাইট গঠিত হয়।

১ বিট * ৮ = ১ বাইট

১ বাইট * ১০২৪ = ১ কিলো বাইট

১ কিলোবাইট * ১০২৪ = ১ মেগা বাইট

আর একেকটা বাইট একেকটা address দিয়ে চিহ্নিত থাকে।

ধরে নেই ৬৪ মেগাবাইট এর একটা র‌্যাম আছে। তাহলে total address কত হবে আমরা একটু হিসাব করলেই বের করে ফেলতে পারিঃ

৬৪ মেগাবাইট = ৬৪ * ১০২৪ কিলোবাইট

= ৬৪ * ১০২৪ * ১০২৪ বাইট

= ৬৭১০৮৮৬৪ বাইট

তাহলে address হবে ০ থেকে ৬৭১০৮৮৬৩ পর্যন্ত।

আমরা জানি যে,

ভেরিয়েবল ইউজ করতে হলে তা ডিক্লেয়ার করতে হয়। কিন্তু কেন? কি লাভ তাতে? না করলে কি সমস্যা হত?

কারণঃ আমরা যে, ভেরিয়েবল ইউজ করি তার জন্য compiler memory তে জায়গা allocate করে।

ধরি, আমরা age নামে একটা variable declare করবো। টাইপ হবে ইন্টিজার।


int age;

তাহলে কম্পাইলার ২ টা কনজিগিউটিভ(পাশাপাশি) বাইট বরাদ্দ করবে এই age variable এর জন্য।

2

এখন ধরা যাক, এই দুইটি বাইটের address হলো ০০০০০০০১২৩ এবং ০০০০০০০১২৪. তাহলে age variable এর address হবে প্রথম বাইটের address. যেহেতু কম্পাইলার জানে এইটা ইন্টিজার টাইপ। সুতরাং যখনি সে age variable নিয়ে কাজ করবে, তখনি সে ধরে নেবে যে পরের বাইটটি ও age variable এর অংশ এবং সেভাবেই কাজ করার জন্য বাঘা বাঘা কম্পিউটার বিজ্ঞানী গুলো কম্পাইলার এবং ল্যাঙ্গুয়েজ ডিজাইন করে থাকে।

এখন age এ একটি value স্টোর করি,


age = 100;

তাহলে ১০০, age এ স্টোর হবে, এবং অবশ্যই বাইনারী ফরম্যাটে।

তোমরা বলতে পারো? যে ইন্টিজার ত ৪ বাইট জায়গা নেয়। আসলে এটা কম্পিউটার আর্কিটেকচার এবং সিস্টেম এর উপর নির্ভর করে। নিম্নে একটা টেবিল দিলাম কোন কোন ডাটা টাইপের জন্য কি কি এমাউন্টের মেমরী এলোকেট হয় তা নিজেই দেখে নাওঃ

3.PNG

4

Address অপারেটরঃ

‘&’ (Ampersand) অপারেটর দিয়ে আমরা মেমরী address নিয়ে কাজ করতে পারি । নিচে একটি ছোট্ট প্রোগ্রাম দিলামঃ

#include <stdio.h>;
main(){
 char name = 'a';
 int age = 20;
 float salary = 14500.00;

 printf("Value of name = %c, Address of name = %u\n", name, &name);
 printf("Value of age = %d, Address of age = %u\n", age, &age);
 printf("Value of salary = %f, Address of sal = %u\n", sal, &sal);

 return 0;
}

আউটপুটঃ

6

এখানে আমরা %u control sequence use করেছি। তোমরা যারা control sequence সম্পর্কে জানো না তাদের জন্যঃ


printf("%d",age);

// এখানে যে ইন্টিজার প্রিন্ট করার জন্য %d ইউজ করলাম। এগুলোকে কন্ট্রোল সিকুয়েন্স বলে।

এই যে, আমরা address print করার জন্য %u ব্যবহার করলাম তার মানে এই না যে এড্রেস হচ্ছে আনসাইন্ড ইন্টিজারস।

আমরা এইটা এই কারনে ইউজ করলাম যে এড্রেস প্রিন্ট করার জন্য কোন স্পেসিফিক কন্ট্রোল সিকুয়েন্স নাই। এড্রেস গুলো হচ্ছে পূর্ণ সংখ্যা, এই এড্রেস গুলা প্রত্যেকবার প্রোগ্রাম রান করার সময় চেঞ্জ হয়ে যায়। এইটা ডিপেন্ড করে অপারেটিং সিস্টেম মেমরীর কোন অংশটা কম্পাইলার কে ইউজ করতে দেয়।

এখন আমরা এড্রেস অপারেটর (&) ব্যবহারের কিছু রেস্ট্রিকশন দেখবোঃ

১। আমরা এড্রেস অপারেটর কে কোন ভেরিয়েবলের সামনে দিলে সেই ভেরিয়েবলটির মেমরী এড্রেস টা দেখে নিতে পারি।


&j; 

// এইটার মানে হইলো j variable এর address টা আমাকে দাও।

২। আমরা এড্রেস অপারেটর কে কোন array এলিমেন্টের সামনে দিলে সেই element এর address পাবো।


&arr[1]; // arr[1] এর লোকেশন;

৩। কিন্তু আমরা কোন কন্সট্যান্ট ভ্যালু অথবা এক্সপ্রেশনের সামনে এইটা ইউজ করতে পারবো না।


&200;
// ২০০ এর এড্রেস তোমাকে সে কোথা থেকে দিবে। ২০০ এর কোন এড্রেস কি আছে?
&(j+k);
//j এবং k variable  এর যোগফল এর এড্রেস দাও। কিন্তু ওই ভ্যালুর আবার এড্রেস কী? 

পয়েন্টার ভেরিয়েবলঃ

পয়েন্টার হচ্ছে এমন এক্টা ভেরিয়েবল যেটার কাজ কেবল মেমরী এড্রেস স্টোর করা। অন্যান্য ভেরিয়েবল এর ও নাম আছে আর তার জন্য মেমরী তে ২ বাইটের মত জায়গাও লাগে।

পয়েন্টার বলার কারনঃ এটা মেমরীতে একটা পার্টিকুলার পজিশন কে পয়েন্ট করে।

কিভাবে ডিক্লেয়ার করবো ?

data_type *pname;

এই * চিহ্নটাকে ভালো করে খেয়াল করো। এইটা দিয়েই পয়েন্টার আইডেন্টিফাই করা হয়।

নিচের দুইটা লাইন খেয়াল করোঃ

int age;
int *age;

একটা নরমাল ভেরিয়েবল আরেকটা পয়েন্টার ভেরিয়েবল ।

এগুলো দিয়ে কি করা যায় তা নিয়ে আমরা একটু পরে আলোচনা করবো। তার আগে আমরা আরো কিছু উদাহরণ দেখিঃ


int *iptr;
// iptr একটি ইন্টিজার টাইপের পয়েন্টার ভেরিয়েবল
float *fptr;
// fptr একটি ফ্লোট টাইপের পয়েন্টার ভেরিয়েবল
char *cptr, char1, char2;
// cptr একটি ক্যারেক্টার টাইপের পয়েন্টার ভেরিয়েবল বাট char1 এবং char2 হচ্ছে সাধারন ভেরিয়েবল।

এখন একটা প্রশ্ন হতে পারে? যদি সব পয়েন্টার মেমরী এড্রেস ই স্টোর করে তাহলে বিভিন্ন টাইপের দরকার হয় কেন? ইন্টিজার পয়েন্টার , ফ্লোট পয়েন্টার এরকম ডাটা টাইপ ওয়াইজ ভাগ করতেছি কেন? একটা টাইপ হলে  অসুবিধা কি ছিল?

(উত্তরটা একটু পরে দিচ্ছি)

পয়েন্টার এর মধ্যে এড্রেস এসাইন করা?

অনেক ত বকবক হলো, এখন একটা কোড দেখি যে ক্যামনে পয়েন্টার এর মধ্যে এড্রেস এসাইন করবোঃ

যখন আমরা পয়েন্টার ভেরিয়েবল ডিক্লেয়ার করি, তখন তাতে Garbage Value থাকে। যেমনঃ এইটা মেমরীর যেকোন দিকে পয়েন্ট করে থাকতে পারে।

তাই পয়েন্টার ইউজ করার আগে সবসময় এড্রেস এসাইন করে নেয়া লাগবে। যদি আমরা unassigned পয়েন্টার ইউজ করি তাহলে এইটা আমাদের unpredictable result দিবে যার জন্য প্রোগ্রাম ক্রাশ ও হতে পারে।


int *iptr, age = 30;

float *fptr, salary = 400.00;

iptr = &age;

fptr = &salaray;

আমি প্রথমে age নামের ভেরিয়েবল টিতে ৩০ এসাইন করলাম। তারপর age এর এড্রেস টা আমরা iptr এ রেখে দিলাম।

7.pngআমরা একই সাথে পয়েন্টার ডিক্লেয়ার করে ইনিশিয়ালাইজ করতে পারি। যেমনঃ


int distance = 10, *distance_pointer = &distance;

float pro = 3.132, *float_pointer = &pro;

আমরা যেমনঃ এক ভেরিয়েবল এর value আরেক ভেরিয়েবল এ বসাতে পারি। ঠিক তেমনি এক পয়েন্টার এ এসাইন্ড এড্রেস আরেকটাতে সেট করতে পারি। যেমনঃ

আরেকটা পয়েন্টার ডিক্লেয়ার করিঃ


int *p1;

// এখন আমরা p1 এ iptr সেই এড্রেস সেট করবো

p1 = iptr;

যদি দুইটা পয়েন্টার এই same এড্রেস থাকে তাহলে তারা ওই এড্রেস এর দিকে ই পয়েন্ট করে থাকে। যেমন p1, iptr এক্ষেত্রে age এর দিকে পয়েন্ট করে থাকবে।

8.png

আমরা যেকোন টাইপ এর  পয়েন্টার এ কন্সট্যান্ট জিরো এসাইন করতে পারি। সি এর stdio.h এ symbolic constant NULL define করা আছে। ঐটা বলতে বুঝায় “কোন কিছুই না” ।

যদি আমরা নাল এসাইন করি তাহলে অর্থ দাঁড়াবে যে ওই পয়েন্টার টি কোন valid memory location এ পয়েন্ট করে নাই।


ptr = NULL

(চলবে …)

Advertisements

Why I chose to work with Linux?

Now-a-days, I have been working with Linux and decided to be a Linux user from now on. People asks me “why we need Linux?”, when we have so much good options to use Mac or Windows.

Well a lots of you know the answer. But those who do not, this analogy is for you. I have got this while reading a Linux book.

Windows is like a Game Boy. You go to the store and buy one all shiny new
in the box. You take it home, turn it on, and play with it. Pretty graphics, cute
sounds. After a while, though, you get tired of the game that came with it, so
you go back to the store and buy another one. This cycle repeats over and over.
Finally, you go back to the store and say to the person behind the counter, “I
want a game that does this!” only to be told that no such game exists because
there is no “market demand” for it. Then you say, “But I only need to change
this one thing!” The person behind the counter says you can’t change it. The
games are all sealed up in their cartridges. You discover that your toy is limited
to the games that others have decided that you need and no more.

Linux, on the other hand, is like the world’s largest Erector Set. You open
it up, and it’s just a huge collection of parts—a lot of steel struts, screws, nuts,
gears, pulleys, and motors and a few suggestions on what to build. So you start
to play with it. You build one of the suggestions and then another. After a while
you discover that you have your own ideas of what to make. You don’t ever have
to go back to the store, because you already have everything you need. The
Erector Set takes on the shape of your imagination. It does what you want.
Your choice of toys is, of course, a personal thing, so which toy would you
find more satisfying?

As I wish to be a good engineer, and I want to get into the details and customize upto my satisfaction I stick with Linux. What about you? Let me know… 😀

Ref : Linux Command Line – A Complete Reference.

The Definitive C++ Book List

Those whomever are seeking for C++ books. This collection is for you. This has been taken from a stack overflow thread.

Beginner Level Books

Introductory, no previous programming experience

  • Programming: Principles and Practice Using C++ (Bjarne Stroustrup) (updated for C++11/C++14) An introduction to programming using C++ by the creator of the language. A good read, that assumes no previous programming experience, but is not only for beginners.

Introductory, with previous programming experience

  • C++ Primer * (Stanley Lippman, Josée Lajoie, and Barbara E. Moo) (updated for C++11) Coming at 1k pages, this is a very thorough introduction into C++ that covers just about everything in the language in a very accessible format and in great detail. The fifth edition (released August 16, 2012) covers C++11. [Review]
  • A Tour of C++ (Bjarne Stroustrup) (EBOOK) The “tour” is a quick (about 180 pages and 14 chapters) tutorial overview of all of standard C++ (language and standard library, and using C++11) at a moderately high level for people who already know C++ or at least are experienced programmers. This book is an extended version of the material that constitutes Chapters 2-5 of The C++ Programming Language, 4th edition.
  • Accelerated C++ (Andrew Koenig and Barbara Moo) This basically covers the same ground as the C++ Primer, but does so on a fourth of its space. This is largely because it does not attempt to be an introduction to programming, but an introduction to C++ for people who’ve previously programmed in some other language. It has a steeper learning curve, but, for those who can cope with this, it is a very compact introduction into the language. (Historically, it broke new ground by being the first beginner’s book to use a modern approach at teaching the language.) [Review]
  • Thinking in C++ (Bruce Eckel) Two volumes; is a tutorial style free set of intro level books. Downloads: vol 1, vol 2. Unfortunately they’re marred by a number of trivial errors (e.g. maintaining that temporaries are automatically const), with no official errata list. A partial 3rdparty errata list is available at (http://www.computersciencelab.com/Eckel.htm), but it’s apparently not maintained.

* Not to be confused with C++ Primer Plus (Stephen Prata), with a significantly less favorable review.

Best practices

  • Effective C++ (Scott Meyers) This was written with the aim of being the best second book C++ programmers should read, and it succeeded. Earlier editions were aimed at programmers coming from C, the third edition changes this and targets programmers coming from languages like Java. It presents ~50 easy-to-remember rules of thumb along with their rationale in a very accessible (and enjoyable) style. For C++11 and C++14 the examples and a few issues are outdated and Effective Modern C++ should be preferred. [Review]
  • Effective Modern C++ (Scott Meyers) This is basically the new version of Effective C++, aimed at C++ programmers making the transition from C++03 to C++11 and C++14.
  • Effective STL (Scott Meyers) This aims to do the same to the part of the standard library coming from the STL what Effective C++ did to the language as a whole: It presents rules of thumb along with their rationale. [Review]

Intermediate Level Books

  • More Effective C++ (Scott Meyers) Even more rules of thumb than Effective C++. Not as important as the ones in the first book, but still good to know.
  • Exceptional C++ (Herb Sutter) Presented as a set of puzzles, this has one of the best and thorough discussions of the proper resource management and exception safety in C++ through Resource Acquisition is Initialization (RAII) in addition to in-depth coverage of a variety of other topics including the pimpl idiom, name lookup, good class design, and the C++ memory model.[Review]
  • More Exceptional C++ (Herb Sutter) Covers additional exception safety topics not covered inExceptional C++, in addition to discussion of effective object oriented programming in C++ and correct use of the STL. [Review]
  • Exceptional C++ Style (Herb Sutter) Discusses generic programming, optimization, and resource management; this book also has an excellent exposition of how to write modular code in C++ by using nonmember functions and the single responsibility principle. [Review]
  • C++ Coding Standards (Herb Sutter and Andrei Alexandrescu) “Coding standards” here doesn’t mean “how many spaces should I indent my code?” This book contains 101 best practices, idioms, and common pitfalls that can help you to write correct, understandable, and efficient C++ code. [Review]
  • C++ Templates: The Complete Guide (David Vandevoorde and Nicolai M. Josuttis) This is thebook about templates as they existed before C++11. It covers everything from the very basics to some of the most advanced template metaprogramming and explains every detail of how templates work (both conceptually and at how they are implemented) and discusses many common pitfalls. Has excellent summaries of the One Definition Rule (ODR) and overload resolution in the appendices. A second edition is scheduled for 2017. [Review]

Advanced Level Books

  • Modern C++ Design (Andrei Alexandrescu) A groundbreaking book on advanced generic programming techniques. Introduces policy-based design, type lists, and fundamental generic programming idioms then explains how many useful design patterns (including small object allocators, functors, factories, visitors, and multimethods) can be implemented efficiently, modularly, and cleanly using generic programming. [Review]
  • C++ Template Metaprogramming (David Abrahams and Aleksey Gurtovoy)
  • C++ Concurrency In Action (Anthony Williams) A book covering C++11 concurrency support including the thread library, the atomics library, the C++ memory model, locks and mutexes, as well as issues of designing and debugging multithreaded applications.
  • Advanced C++ Metaprogramming (Davide Di Gennaro) A pre-C++11 manual of TMP techniques, focused more on practice than theory. There are a ton of snippets in this book, some of which are made obsolete by typetraits, but the techniques, are nonetheless useful to know. If you can put up with the quirky formatting/editing, it is easier to read than Alexandrescu, and arguably, more rewarding. For more experienced developers, there is a good chance that you may pick up something about a dark corner of C++ (a quirk) that usually only comes about through extensive experience.

Reference Style – All Levels

  • The C++ Programming Language (Bjarne Stroustrup) (updated for C++11) The classic introduction to C++ by its creator. Written to parallel the classic K&R, this indeed reads very much alike it and covers just about everything from the core language to the standard library, to programming paradigms to the language’s philosophy. [Review]
  • C++ Standard Library Tutorial and Reference (Nicolai Josuttis) (updated for C++11) Theintroduction and reference for the C++ Standard Library. The second edition (released on April 9, 2012) covers C++11. [Review]
  • The C++ IO Streams and Locales (Angelika Langer and Klaus Kreft) There’s very little to say about this book except that, if you want to know anything about streams and locales, then this is the one place to find definitive answers. [Review]

C++11/14 References:

  • The C++ Standard (INCITS/ISO/IEC 14882-2011) This, of course, is the final arbiter of all that is or isn’t C++. Be aware, however, that it is intended purely as a reference for experiencedusers willing to devote considerable time and effort to its understanding. As usual, the first release was quite expensive ($300+ US), but it has now been released in electronic form for $60US.
  • The C++14 standard is available, but seemingly not in an economical form – directly from the ISO it costs 198 Swiss Francs (about $200 US). For most people, the final draft before standardization is more than adequate (and free). Many will prefer an even newer draft, documenting new features that are likely to be included in C++17.
  • Overview of the New C++ (C++11/14) (PDF only) (Scott Meyers) (updated for C++1y/C++14) These are the presentation materials (slides and some lecture notes) of a three-day training course offered by Scott Meyers, who’s a highly respected author on C++. Even though the list of items is short, the quality is high.
  • CppCoreGuidelines (C++11/14/17? evolving) (Markdown only) (Bjarne Straustrup, Herb Sutter) This document is a set of guidelines for using C++ well. The aim of this document is to help people to use modern C++ effectively. By “modern C++” we mean C++11 and C++14 (and soon C++17). In other words, what would you like your code to look like in 5 years’ time, given that you can start now? In 10 years’ time? The guidelines are focused on relatively higher-level issues, such as interfaces, resource management, memory management, and concurrency. Such rules affect application architecture and library design. Following the rules will lead to code that is statically type safe, has no resource leaks, and catches many more programming logic errors than is common in code today. And it will run fast – you can afford to do things right.

Classics / Older

Note: Some information contained within these books may not be up-to-date or no longer considered best practice.

  • The Design and Evolution of C++ (Bjarne Stroustrup) If you want to know why the language is the way it is, this book is where you find answers. This covers everything before the standardization of C++.
  • Ruminations on C++ – (Andrew Koenig and Barbara Moo) [Review]
  • Advanced C++ Programming Styles and Idioms (James Coplien) A predecessor of the pattern movement, it describes many C++-specific “idioms”. It’s certainly a very good book and might still be worth a read if you can spare the time, but quite old and not up-to-date with current C++.
  • Large Scale C++ Software Design (John Lakos) Lakos explains techniques to manage very big C++ software projects. Certainly a good read, if it only was up to date. It was written long before C++98, and misses on many features (e.g. namespaces) important for large scale projects. If you need to work in a big C++ software project, you might want to read it, although you need to take more than a grain of salt with it. The first volume of a new edition is expected in 2015.
  • Inside the C++ Object Model (Stanley Lippman) If you want to know how virtual member functions are commonly implemented and how base objects are commonly laid out in memory in a multi-inheritance scenario, and how all this affects performance, this is where you will find thorough discussions of such topics.
  • The Annotated C++ Reference Manual (Bjarne Stroustrup, Margaret A. Ellis) This book is quite outdated in the fact that it explores the 1989 C++ 2.0 version – Templates, exceptions, namespaces and new casts were not yet introduced. Saying that however this is book goes through the entire C++ standard of the time explaining the rationale, the possible implementations and features of the language. This is not a book not learn programming principles and patterns on C++, but to understand every aspect of the C++ language.

Ref : stack overflow thread.

A Deep dive to Linux System | Process, Sessions, Process Groups

Recently, I paid my attention to learn the fundamentals of Computer Science. And One of Aaroon Swartz’s Documentary really touched me. People like them really make their ways to do whatever they think is necessary. I committed myself to be like them. In that journey I decided to get my hands dirty with Linux Shells. And on that way, Sometimes I share I think will be valuable for all of you guys.

Enough talk about me, let’s talk about linux system. Today I learnt about Processes, Sessions and User Groups. I think it would be interesting to share this with all of you guys.

Screenshot from 2016-09-10 12-50-27.png
Relation between Sessions, Processes, process group and Controlling Terminal

When a new user first logs into a machine, the login process creates a new session
that consists of a single process, the user’s login shell. The login shell functions as the
session leader.

The pid of the session leader is used as the session ID. A session is a collection of one or more process groups.

Each process group contains one or more processes. Processes are things like what a program actually does. For Example – you are listening to music while browsing the internet. Meanwhile there is another process – your pdf reader opening a book that you were reading.

Want to learn SQL? Follow Me..

Before diving into the good stuff about SQL, and how to get started, please allow me to give some brain dump and copy-paste data from several sources.

SQL stands for Structured Query Language. It is a language for querying databases.

Originally based upon relational algebra and tuple relational calculus, SQL consists of a data definition language, data manipulation language, and Data Control Language. The scope of SQL includes data insert, query, update and delete, schema creation and modification, and data access control. Although SQL is often described as, and to a great extent is, a declarative language (4GL), it also includes procedural elements.

History Of SQL

SQL was initially developed at IBM by Donald D. Chamberlin and Raymond F. Boyce in the early 1970s.[14] This version, initially called SEQUEL (Structured English Query Language), was designed to manipulate and retrieve data stored in IBM’s original quasi-relational database management system, System R, which a group at IBM San Jose Research Laboratory had developed during the 1970s.[14] The acronym SEQUEL was later changed to SQL because “SEQUEL” was a trademark of the UK-based Hawker Siddeley aircraft company.[15]

In the late 1970s, Relational Software, Inc. (now Oracle Corporation) saw the potential of the concepts described by Codd, Chamberlin, and Boyce, and developed their own SQL-based RDBMS with aspirations of selling it to the U.S. Navy, Central Intelligence Agency, and other U.S. government agencies. In June 1979, Relational Software, Inc. introduced the first commercially available implementation of SQL, Oracle V2 (Version2) for VAX computers.

After testing SQL at customer test sites to determine the usefulness and practicality of the system, IBM began developing commercial products based on their System R prototype including System/38, SQL/DS, and DB2, which were commercially available in 1979, 1981, and 1983, respectively

Design

SQL deviates in several ways from its theoretical foundation, the relational model and its tuple calculus. In that model, a table is a set of tuples, while in SQL, tables and query results are lists of rows: the same row may occur multiple times, and the order of rows can be employed in queries (e.g. in the LIMIT clause).

Critics argue that SQL should be replaced with a language that strictly returns to the original foundation: for example, see The Third Manifesto.

Language Elements

The SQL language is subdivided into several language elements, including01:

  • Clauses, which are constituent components of statements and queries. (In some cases, these are optional.)
  • Expressions, which can produce either scalar values, or tables consisting of columns and rows of data
  • Predicates, which specify conditions that can be evaluated to SQL three-valued logic (3VL)(true/false/unknown) or Boolean truth values and are used to limit the effects of statements and queries, or to change program flow.
  • Queries, which retrieve the data based on specific criteria. This is an important element of SQL.
  • Statements, which may have a persistent effect on schemata and data, or may control transactions, program flow, connections, sessions, or diagnostics.
    • SQL statements also include the semicolon (“;”) statement terminator. Though not required on every platform, it is defined as a standard part of the SQL grammar.
  • Insignificant whitespace is generally ignored in SQL statements and queries, making it easier to format SQL code for readability.

There are some subset of SQL Standard.

  1. DDL – Data Definition Languages
  2. DML – Data Manipulation Language
  3. DCL – Data Control Language

Data Definition language – is used to create tables and constraints. These includes

  1. CREATE
  2. DROP
  3. ALTER

Data Manipulation Language – is used to modify and view data within the Database.

  1. SELECT
  2. INSERT
  3. UPDATE
  4. DELETE

Data Control Language 

  1. GRANT
  2. REVOKE

Many Database Implementations requires the use of SQL, and over the years, vendors have implemented dialects of SQL to provide more  functionality as well as simplify it. Because of these deviations from the standard, SQL is fractured – syntax that works on one implementations does not necessarily work on another.

Most DBMSs have additional languages for writing stored procedures. In Oracle this is PL/SQL (Procedural Language/Structured Query Language), in PostgreSQL it’s PL/pgSQL (Procedural Language/PostgreSQL). Outside of stored procedures or functions, Oracle and PostgreSQL use SQL.

Microsoft SQL Server uses the term T-SQL (Transact-SQL) for both “plain” SQL (queries, DML, ..) and the language used for stored procedures.

Okay, I think that was more than enough about SQL. Here are the following books, resources to learn SQL

 

Free SQL Programming Books

Free SQL/Database Online Courses

SQL/Database Online Tutorial.

Online Tools to practise and improve SQL Skills

  1. SQL Fiddle

Refernces:

  1. Stack Overflow Tag – SQL Wiki

Mathematics For Computer Science – Part 1 | Proof {Notes}

Some interesting propositions i learnt in the chapter – “what is proof?”.

Proposition :  ∀ n ∈ N n2 + n + 41 is a prime number.

This is a special kind of proposition called a predicate, which is a proposition whose truth depends on the value of one or more variables. This predicate is certainly true for many natural numbers n:

01

Experimental data like this can be useful in mathematics, but can also be misleading. In this case, when n = 40, we get n2 + n + 41 = 40 2 + 40 + 41 = 41 · 41, which is not prime. So this Proposition is actually false!

Proposition :a4 + b4 + c4 = d4 has no solution when a, b, c, d ∈ N+ .
Here N+ denotes the positive natural numbers, {1, 2, 3, . . .}.

In 1769, Euler conjectured that this proposition was true. But the it was proven false 218 years later by Noam Elkies at the liberal arts school up Mass Ave. He found the solution a = 95800, b = 217519, c = 414560, d = 422481. We could write his assertion symbolically as follows:
∃ a, b, c, d ∈ N+
a4 + b4 + c4 = d4

The ∃ symbol is read “there exists”. So, in words, the expression above says that there exist positive natural numbers a, b, c, and d such that a4 + b4 + c4 = d4 .

Proposition : 313(x3 + y3 ) = z3 has no solution when x, y, z ∈ N+ .

This proposition is also false, but the smallest counterexample has more than 1000 digits. This counterexample could never have been found by a brute-force computer search!

Proposition : Every even integer greater than 2 is the sum of two primes.
For example, 24 = 11 + 13 and 26 = 13 + 13. This is called the Goldbach Conjecture, after Christian Goldbach who first stated the proposition in 1742. Even today, no one knows whether the conjecture is true or false. Every integer ever checked is a sum of two primes, but just one exception would disprove the proposition.

Example of An Implication :

If pigs fly, you will understand my Nature!

Proof By Contradiction

Theorem : √2 is an irrational number.

This theorem was first proved by the Pythagoreans, a secretive society dating back to about 500 BC that intertwined mysticism and mathematics. The irrationality of 2 and the existence of a twelve-sided regular polyhedron (the dodecahedron) were among their prized secrets.

dodecahedron
dodecahedron – from wiki

Reference:

Mathematics for Computer Science – Eric Lehman and Tom Leighton – 2004

 

Processes – A Deep Jump to the Linux System | Part – 3

Table Of Contents:

  • Processes
    • Threads
    • Process Hierarchy

Processes

Processes are object code in execution: active, alive, running programs. But they’re more htan just object code – processes consist of data, resources, state, and a virtualized computer.

Processes begin life as executable object code, which is machine-runnable code in an executable format that the kernel understands (the format most common in Linux is ELF). The executable format contains metadata, and multiple sections of code and data. Sections are linear chunks of the object code that load into linear chunks of memory. All bytes in a section are treated the same, given the same permissions, and generally used for similar purposes.

The most important and common sections are the

  1. text section,
  2. the data section,
  3. and the bss section.

The text section contains executable code and read-only data, such s constant variables, and is typically marked read-only and executable.

The data section contains initialized data, such as C variables with defined values, and is typically marked readable and writable.

The bss section contains uninitialized global data. Because the C standard dictates default values for C variables that are essentially all zeros, there is no need to store the zeros in the object code on disk.

Instead, the object code can simply list the uninitialized variables in the bss section, and the kernel can map the zero page (a page of all zeros) over the section when it is loaded into memory. The bss section was conceived solely as an optimization for this purpose. The name is a historic relic; it stands for block started by symbol, or block storage segment.

Other common sections in ELF executables are the absolute section[which contains non-relocatable symbols] and the undefined section [a catchall].

A process is also associated with various system resources, which are arbitrated and managed by the kernel. Processes typically request and manipulate resources only through system calls. Resources include timers, pending signals, open files, network connections,  hardware and IPC mechanisms. A Process resources, along with data and statistics related to the process are store inside the kernel in the process’ process descriptor.

A process is a virtualization abstraction. The linux kernel, supporting both pre-emptive multitasking and virtual memory, provides both virtualized processor, and a virtualized view of memory. From the process’ perspective, the view of the system is as though it alone were in control.

That is even though a given process may be scheduled alongside many other processes, it runs as though it has sole control of the system.

The kernel seamlessly and transparently preempts and reschedules processes, sharing the system’s processors among all running processes. Processes never know the difference. Similarly, each process is afforded a single linear address space, as if it alone were in control of all of the memory in the system. Through virtual memory and paging the kernel allows many processes to co-exist on the system, each operating in a different address space. The kernel manages this virtualization through hardware support provided by modern processors, allowing the os to concurrently manage the state of multiple independent processes.

Threads

Each process consists of one or more threads of execution (usually just called threads). A thread is the unit of activity within a process, the abstraction responsible for executing code, and maintaining the process’ running state.

Most processes consist of only a single thread; they are called single-threaded. Processes that contain multiple threads are said to be multithreaded. Traditionally, Unix programs have been single-threaded, owing to Unix’s historic simplicity, fast process creation times, and robust IPC mechanisms, all of which mitigate the desire for
threads.

A thread consists of a stack (which stores its local variables, just as the process stack does on nonthreaded systems), processor state, and a current location in the object code (usually stored in the processor’s instruction pointer). The majority of the remaining parts of a process are shared among all threads.

The name of the current Linux thread implementation, which is part of glibc, is the Native POSIX Threading Library (NPTL).

Process Hierarchy

Each process is identified by a unique positive integer called the process ID (pid). The pid of the first process is 1, and each subsequent process receives a new, unique pid.

In Linux, processes form a strict hierarchy, known as the process tree. The process tree is rooted at the first process, known as the init process, which is typically the init program. New processes are created via the fork( ) system call. This system
call creates a duplicate of the calling process. The original process is called the parent; the new process is called the child. Every process except the first has a parent. If a parent process terminates before its child, the kernel will reparent the child to the init process.

When a process terminates, it is not immediately removed from the system. Instead, the kernel keeps parts of the process resident in memory, to allow the process’ parent to inquire about its status upon terminating. This is known as waiting on the terminated process. Once the parent process has waited on its terminated child, the child is fully destroyed. A process that has terminated, but not yet been waited upon, is called a zombie. The init process routinely waits on all of its children, ensuring that reparented processes do not remain zombies forever.

References: “Linux System Programming by Robert Love. Copyright 2007 O’Reilly Media, Inc., 978-0-596-00958-8.”