The digital computer is made of hardware part and software part. The hardware part is made of semi-conductor chips, and they manipulate anything which is made of binary digits or 0s and 1s. The software part is call as operating system and it treats everything [such as file, data, information or attached hardware etc.] as file and process. And operating system too only understands binary digits or bits 0 and 1.
Language Evaluation
1111 10110
1101 10011
Writing instructions to such digital computer using binary digits is call as machine level language programming. It is hard to document, understand, and debug such programs as code lines increases.
mov R,10
mov A,R
JNZ
So, mnemonics or short word like programs were invented, and called as assembly level language programming. It needs assembler to convert assembly language code into binary form code or executable code. For big projects it is not advised as hard to understand the code, its documentation and debugging and maintaining it is not so easy.
if (device == mobile)
printf(“Dip in the water!”);
else
printf(“you are saved from my fury!”);
Finally human readable programming languages were devised for small and large projects alike. Based on coding approach method three types are listed- pop or procedural oriented programming, OOP or Object Oriented Programming and LOP or Logic Oriented Programming. Usually these languages need compiler to compile the code and get binary or executable code.
Structure of Operating System
The architecture layer of OS is divided into three in number: core is kernel or OS itself, middle layer is shell and outer layer is application program/utility one.
The OS/Kernel tasks- Schedules tasks, Manages resources, Controls security
The shells tasks- it acts as the user interface and interprets user commands, starts applications.
The Utilities tasks- the useful application programs which user or os needs.
When any application program is start by clicking its desktop icon or so, signal or action passes through shell/command prompt to OS/kernel. The action asks permission to start its session and needs proper memory and privileges. If O.S finds sufficient memory and requesting application is of its kind, allocates memory, allow it to run and add it to its tasks list.
Shell or Terminal
Operating System is core element of any AI system and it interacts with outer world through a tool called shell. A shell is a command-line interpreter that accepts typed commands from a user and executes the resulting request. In addition to executing programs, shells usually support advanced features, such as the ability to recall recent commands and a built-in scripting language for writing programs.
Shell Commands
The commands are nothing but pre-built programs comes with O.S. User can use commands to do simple tasks such as listing current directory, copy/move files, create files/directory etc.
O.S handles these single line commands in run time, as it is an interpreter. Source program runs in one-step using an interpreter. Interpreter is use in Operating System, as run-time input is not known beforehand; it may any kind of application program, which wants to run on its domain.
Shell Scripting
To do multiple tasks repetitively, issuing commands on terminal prompt is not the best method. Writing all commands in a file, and executing that file on command prompt is called Shell Scripting.
But problem with above two methods is they need built-in commands to do the task, and are interpreted at run time which is a known as a slow process. What if the task involves many databases, files, resources and managing complex things? There come the programming languages in picture.
Programming Languages
The programming languages comes with their own syntax, data types, code, instruction/command sets, which are used to write application program and converted that program into binary form in two steps.
By multistage solutions using program generation a compiler, which generates a target (= object) program in some target language from a source program in a source language.
Even though compiled codes are generated in two stages, they are ready-to run code, hence no interpretation is needed. So run time is speed.
Interpreter vs Compiled programs
In interpreter live planning is done, whereas in compiled programs planned programs are executed.
The development process for an interpreted language is simpler than that for a compiled language. All that is required is an editor to create the source code, either a stand-alone editor (if the code is in ASCII form) or within the interpreter.
Creating code that will be compile to produce a stand-alone application is usually a more complex process, involving combinations of editors, compilers and linkers.
How Interpreter works? a small session
Open a Cygwin shell or terminal and issue following commands. echo command echoes the input that it receives.
$ echo Hello World!
Hello World!
$
One of the primary features of a shell is to perform a command line scan. When you enter a command at the shell's command prompt and press the enter key, then the shell will start scanning that line, cutting it up in arguments. While scanning the line, the shell may make many changes to the arguments you typed. This process is called shell expansion. When the shell has finished scanning and modifying that line, then it will be executed.
In the above example echo is a command and Hello World! are two arguments. You can prevent the removal of white spaces by quoting the spaces. The contents of the quoted string are considered as one argument. In the screenshot below the echo receives only one argument.
$ echo “Hello World!”
Hello World!
$
Quoted lines can include special escaped characters recognised by the echo command (when using echo -e). The screenshot below shows how to use \n for a newline and \t for a tab (usually eight white spaces).
$ echo -e “Hello World! \n Hello Universe!”
Hello World!
Hello Universe!
$ echo -e “A line with a\t tab!”
A line with a tab!
$
You can put two or more commands on the same line separated by a semicolon ; . The shell will scan the line until it reaches the semicolon. All the arguments before this semicolon will be considered a separate command from all the arguments after the semicolon. Both series will be executed sequentially with the shell waiting for each command to finish before starting the next one.
$ echo Hello World! ; echo Hello Universe!
Hello World!
Hello Universe!
$
Lines ending in a backslash are continued on the next line. The shell does not interpret the newline character and will wait on shell expansion and execution of the command line until a newline without backslash is encountered.
$ echo The single command line \
> argument split in \
> two lines
The single command line argument split in two lines
$
Exit Status:
By default in Linux if particular command is executed, it return two type of values, (Values are used to see whether command is successful or not) if return value is zero (0), command is successful, if return value is nonzero (>0), command is not successful or some sort of error executing command/shell script. This value is known as Exit Status of that command. To determine this exit Status we use $? variable of shell.
The exit code of the previous command is stored in the shell variable $?. Suppose we create new file called file1 and check exit code it gives zero or success exit. Now we delete file1 or rm file1 exit code gives zero as command remove it successfully. If we second time issue delete file1 command, command exit code becomes 1 or command not exit successfully code or error code 1 or it will print nonzero value(>0) to indicate error.
$ touch file1
$ echo $?
0
$ rm file1
$ echo $?
0
$ rm file1
$ echo $?
1
$
How compiled program works? a old-traditional “hello world!”
C program must be created in two stages:
1. First, create source code by writing instructions in it and save it as .c file.
2. Second, compile this source file and generate binary file or executable file.
The compiler
When you compile a program, the compiler usually operates in an orderly sequence of phases called passes. The sequence happens approximately like this:
1. First, the compiler reads the source code, perhaps generating an intermediate code (such as pseudo-code) that simplifies the source code for subsequent processing.
2. Next, the compiler converts the intermediate code (if there is any) or the original source code into an object code file, which contains machine language but is not yet executable. The compiler builds a separate object file for each source file. These are only temporary and are deleted by the compiler after compilation.
3. Finally, the compiler runs a linker. The linker merges the newly-created object code with some standard, built-in object code to produce an executable file that can stand alone.
GNU environments use a simple command to invoke the C compiler: gcc, which stands for \GNU Compiler Collection". (It used to stand for \GNU C Compiler", but now GCC can compile many more languages than just C.) Thus, to compile a small program, you will usually type something like the following command:
gcc file_name
On GNU systems, this results in the creation of an executable program with the default name `a.out'. To tell the compiler you would like the executable program to be called something else, use the `-o' option for setting the name of the object code:
gcc -o program_name file_name
For example, to create a program called `myprog' from a file called `myprog.c', write
gcc -o myprog myprog.c
To launch the resulting program `myprog' from the same directory, type
./myprog
File names
GCC uses the following file name conventions:
Source code file program name.c
Object file program name.o
Executable file program name (no ending, in Cygwin it ends with .exe)
Header file name.h
Library file libname.a or libname.so
Write-compile-run a C program
We will write one toy or prototype program to learn about Software Development Life Cycle (SDLC) in C language, which is actually made of three steps:
Edit
Compile
Execute
Decide the program type : console application or GUI one
Before coding any program, developer has to decide whether he wants graphical interfaced program or text interfaced program. For learners, in C, console applications are advised as they help learn all components of the language easily.
Naming source files: main.c is your driver program
C language is used to produce many files involved programs. So naming the source files wisely is advised as it will be helpful to track of code flow in future for many reasons such as for documentation, feature enhancement, code-reuse, debugging etc.
While naming the source codes remember that the driver program, where execution starts is to be name as main.c for easy identification purpose. As program enlarges, more source and include files will be added and it will be real-headache to find this driver file from those many program files.
Creating the Source Code
The source code to be coded is as below; it is called as time-honored tradition code. It greets the user outputting with Hello World! string.
#include <stdio.h>
main()
{
printf(“ Hello World!”);
}
The source code of C language can be created using any simple text editor, like Notepad in Windows O.S. In Cygwin, user can type name of the text editor along with source code file name on its shell prompt to invoke it. The ampersand sign makes text editor available for editing in background while using it on shell prompt simultaneously.
The advanced user can use Cygwin’s terminal prompt to create simple c source codes. The command is cat. Type the cat > main.c command then type one or more lines of code, finishing each line with the enter key. After the last line, type and hold the Control key and press d or Ctrl^d. This sends an End of File or EOF to the running process ending the cat command.
Compile the program
Now issue compile command on shell prompt to get final binary file or executable file. Here gcc means compiler name, main.c is the source name user created and –o means create output file with name main.
$gcc main.c –o main
After compilation user can issue list command ls to see whether executable file is created or not.
Run the program
It is time execute the program main.exe. Just call the program on shell prompt like ./main.exe. In Cygwin it means: call in the current directory file called main.exe. This evokes main.exe file and hence prints Hello World! on screen and goes back to shell prompt again. Now check whether execution is success or not by issuing echo $? command to get 0 for EXIT_SUCESS return value.
Programming Process and its tools
Above method is for coder, what about programmer or developer! They have to follow four steps of the programming process, which are as follows:
1. Specify the task
2. Devise the algorithm for its solution
3. Code the algorithm in C
4. Test the code for data validation and run time error.
In C, while planning the process of writing program, following three tools are used: pseudo codes, algorithm and flow chart.
The pseudo codes and algorithms do not follow the language rules. They use common English like keywords for writing concept. The flow chart gives pictorial form of program flow steps with compulsory start and stop signals.
Pseudo Code Example:
Write an algorithm to print the sum of two numbers.
/* x & y will store the input and sum will store in the result */
Sumof2Num()
Begin
Read x, y;
Set sum = x + y ;
Print: sum
End
Algorithm Example:
Algorithm: find biggest of 3 positive integers
Input: 3 integers
Output: biggest among inputs
Step 1: Read a, b, c.
Step 2: if a > b goto step 4, else continue.
Step 3: if b > c goto step 6, else goto step 7.
Step 4: if a > c goto step 5, else goto step 7.
Step 5: display a goto step 8.
Step 6: display b goto step 8.
Step 7: display c.
Step 8: stop.
Flow Chart Example:
Previous Page | Main Contents | Next Page |
No comments:
Post a Comment