Computer Fundamentals with C and Unix
(Hardware and Software)
Lecture 01: Dec. 15, 2022 Prof. K.R. Chowdhary : Professor of CS

Disclaimer: These notes have not been subjected to the usual scrutiny reserved for formal publications. They may be distributed outside this class only with the permission of the Instructor.

1.1 Introduction

A modern computer consists two major parts: Hardware and Software. The first consists of various physical components made of semiconductor devices and that carries current. This makes the physical structure of computer, in which major parts are: CPU (central processing Unit), Memory, and Input/output devices. The CPU is called brain of computer and performs all the computations or calculations, Memory is place which stores the program and data at run time. Input/output (I/O) devices are those things to which CPU and memory interacts. The I/O devices are the things we see in the physical computer: these are:

Various devices and their relation to CPU and memory are shown in the Fig. 1.1.


PIC

Figure 1.1: Computer Organization


1.2 Software

The Hardware (HW) is what is directly visible thing in the computer. Truly speaking, the HW is the electronic circuitry in the computer using which various things are built, like, CPU, memory, IO, and devices. The other, in fact more important thing is, software. The software is name of programs, either written by us or those which come along with the computer when we buy it.

This software is made of small-small millions of instructions, which tell the computer as what to do, and it is written in programming languages, like C, C++, Java, Python, and many more, or written in less friendly languages, called machine language. The C, C++, etc are called HLL (high level languages) while the other languages are called machine languages. There are no names for machine languages, and they are machine specific, i.e., CPU specific.

The software is again two types: for example, software for doing the job of editing, computations, drawing presentations, called respectively as MS word, MS Excel, and MS power point. The MS stands for Microsoft corporation, the name of a company which makes these software. These software are called application software. The other application software are for applications of, e.g., payroll, reservation of tickets of train and airlines, etc.

Other software type is called system software, these are for managing other software when those are running as programs in computers. Example of this software is operating system, say Windows, Linux, Ubuntu, IOS, Android (for mobiles), etc.

Software like Chrome, Firefox, etc are applications software and they are used for web browsing, i.e, for displaying pages of different web sites, called hypertext.

Other example of application software is Google search engine, which searches information on websites.

1.3 Computer Languages

The computer languages are basically, two types: 1) HLL (high level languages), and 2) LLL (low level languages). The HLLs are more like mathematical expressions and make use of words of English and decimal numbers. For example, the following section of a program is equally good for C, C++, and Java language:

basic= 25000;

da = basic * 0.50;

hra = basic * 0.20;

pay = basic+da+hra;

This program computes net salary, given the basic salary. The first line “basic=25000” stores value 25000 in a memory location, say 100 in the computer’s memory.

The line “da = basic * 0.50”, takes the value 25000 from the memory location 100 and performs the multiplication in CPU, and stores the result in a memory location, say 101. Similarly, hra and pay may correspond to some memory locations, say 102 and 103. The names basic, da, hra, and pay are tied to these memory locations during the time this program is translated into machine language, by the compiler. Note that only the machine language program runs (executes) in the computer. The HLL language program like listed above does not run, and it is in this natural format only for the convenience of the programmar. The names (called variables), like basic, da, hra are mapped to numeric memory locations, like 100, 101, 102, etc., by the compiler, which translates HLL program into machine language program.

The values 25000, 0.50, 0.20, etc are called constants, i.e., their values shall remain unchanged.

The value of a variable can change, for example, basic = 25000, makes the value of basic as 25000. However, at later instant, if we execute the instruction: basic = basic * 1.2 or basic = 30000, the value of variable basic changes to 30000 in both the cases.

The machine language instructions are in binary format, of 0s and 1s strings, which for the machine it is easy to perform, but for a programmar, it is difficult to write a program using these instructions, difficult to debug, and difficult to understand by other programmar.

1.4 Translators

The translators are software that translate one language to other language. The most common translator is compiler, which translate High language to machine language, e.g., a C language translators, C++ translator, Java translator, and so on. In other languages, there were translators for Fortran, Cobol, Pascal, etc.

A C-language translator for Pentium machine (i.e., CPU) is different from a C-language translator for DEC 11 CPU, or for apple CPU, because the machine language for these two machines are different. Similarly, for every language-CPU combination, there is a different translator (a compiler).

Other translators have various names, like pre-processor, source-to-source translator, i.e, HLL to other HLL translator, etc.

These translator programs have various names, e.g., in Unix/Linux environment, C-language translator is called gcc, for java it is javac.

Other types of translators are not truly translators, they are called Interpreters, and run the program directly, without producing a translated copy of the program.

A HLL program is called source program, while a machine language program is called object program.

A compiler is system software.

1.5 Operating Systems

An operating system is a system software, and works like manager in a computer. Its concept is simple. Like, a manager in a office, or company, assigns tasks to persons, monitors when they are doing the assigned tasks and allots new tasks when they have completed the previous task, etc. When a person commits some error or mistake while doing the job, it is manager’s duty to handle that mistake, either asking to redo or assign that task to other person, etc.

In a computer, the operating system allows the programs to run, allots the memory where they will be stored, allots a CPU to run on that (when there are more than one CPUs else allots the CPU to be shared), allows the printer to be used in a given time slot, allows which data are to be used by which program, makes sure that no two programs fight for the same memory locations, like some times we find passengers fight for a seat (a location) in train.

If two passengers are allotted same seat, there will be a serious problem, similarly, if two programs are allotted same memory by the operating system (i.e., manager) there will be a serious problem. You cannot fit two programs in the same memory locations, though two thin persons can manage with a single seat, at least a mother and the child accompanying her! But, in computer this can never happen!

So, we find that operating system has very serious responsibilities, and very logical !!

All the operating systems, like, Unix, Linux, Ubuntu, Windows, Android, etc., are doing the duty of managers in our computers, laptops, tablets, smartphones, and smart watches !!

1.6 Editing and Compiling a C program

The Fig. 1.2 shows editing and saving a program through vi editor of Linux/Unix/ubuntu.


PIC

Figure 1.2: Editing a C-program to print “Hello World” 10 times


An editor is a program to feed a program into computer through keyboard and save it in a file. Here, we are saving in the file by name “hello.c”. We start the vi editor by command,

$ vi hello.c

The $ sign is prompt for operating system Unix and Linux. For Windows prompt is C and editor name can be vi or nano, both these are also used in Unix also.

We can also use other editors like “nano” and “gedit”, both of them are simpler to use than “vi” editor, however, the vi editor is more powerful and counted as best editor in the software industry. We will be using vi most often, but will also use nano and gedit editors some times.

The editor is like MS-word, but you can only enter the text, i.e., keyboard characters, and no any fonts or symbols, or font sizes.

You compile a C program using gcc compiler, which generates an object file, and it has been linked. Linking here means, different library routines have been linked/joined or added into the object program. We do compilation by command:

$ gcc hello.c

which generates a default name file, i.e., a.out. You can generate a file of different name by,

$ gcc -o myhello hello.c

where, myhello is an executable file, i.e., object/machine language file, which can be run by command:

$ ./myhello

1.7 Running a C program

The Fig. 1.3 shows compiling, and running of the hello.c program on Linux/Unix terminal.


PIC

Figure 1.3: Running the C-program, and listing files


Before we run a C-program, we need to compile it, using a compiler, called gcc (for GNU Compiler), using the command,

$ gcc  hello.c

which produces the compiled and executable file a.out (for all output), this file can be executed by command,

$ ./a.out

where “./” stands for current directory.

The ls (list) command list the files and directories, and ls - l provides long list, that is, with all the details of files and folders, their sizes, owner and groups, and time and date of creation. At the begin there are right of the file.