0% found this document useful (0 votes)
31 views62 pages

Notebook On Introduction To Programming in Unix

This document presents an introduction to programming on UNIX. It explains what programming is, how a computer works at an internal level, what a computer can do, and what it is used for. It describes the software life cycle and the programming cycle, including the stages of specification, analysis, design, programming, validation, and implementation. It also introduces key concepts such as users, groups, sessions, processes, file trees, and development tools such as editors, compilers, and debuggers.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views62 pages

Notebook On Introduction To Programming in Unix

This document presents an introduction to programming on UNIX. It explains what programming is, how a computer works at an internal level, what a computer can do, and what it is used for. It describes the software life cycle and the programming cycle, including the stages of specification, analysis, design, programming, validation, and implementation. It also introduces key concepts such as users, groups, sessions, processes, file trees, and development tools such as editors, compilers, and debuggers.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 62

Notebook of

Introduction to Programming on UNIX

Francisco Rosales García

Department of Architecture and Technology of Computer Systems


Faculty of Computer Science
Polytechnic University of Madrid

February 5, 2008
´General index

What is programming? 3
1.1. What is a computer like on the inside? 3
1.2. What can a computer do? 4
1.3. What are computers used for? 4
1.4. Software life cycle 4
1.5. Programming Cycle 5
1.6. What knowledge does a programmer have? 6
1.7. Summary 6

2. Structured programming 7
2.1. Concepts 7
2.2. Programming techniques 9
2.3. Programming structures . . . . . . . . . . . . . . . . . . . . . . . . . . 10
2.4. Coding style . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17

3. Types and data structures 21

4. The UNIX Environment 31


4.1. Users and Groups ...................... 32
4.2. Session . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
4.3. Mandates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
4.4. Processes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
4.5. ´
File Tree . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
4.6. File descriptors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
4.7. Interpreter of mandates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
4.8. Configuration 47
4.9. Xwindows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48

5. Development Tools 49
5.1. Editor 49
5.2. Compiler . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
5.3. Debugger .......................................................... 53
5.4. Libraries ................................................. 55
5.5. Constructor
5.6. Other tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57

i
ii
Structured Programming

1
Chapter 1

What is programming?

In this session, the aim is for the student to understand what a computer can do.
Nador, and how the programmer will be able to instruct it to do what he wants.
wish.

1.1. What is a computer like on the inside?


Today's computers are built on the basis of the architecture conceived
by John Von Neumann (Fig. 1.1).

Memoria Ccontrollers
Pr cipal of Peripherals
RAM
Mapas of
mem ori a de Controladora
GráficaAGP
prog ama
µP
PAL
Withtroladora
ALU of Disco IDE

YES Bus de datos Bus of Withtroladora


YES Records
YESTOS Entrada/Exit USB
TE
TE
TEXTO

Controladora
ROM
RS232 Mfromme

Figure 1.1: Von Neumann Architecture.

Around a unit with computing capacity, called a microprocessor,


They organize on one side the peripheral devices, and on the other a storage area.
of information, the main memory.
The microprocessor hides within a unit capable of performing operations on
a set of records. The nature and order of these operations is dictated by the

3
instructions that are stored in memory. There are instructions that govern
the data transfer between memory and registers. There are also instructions
intended to operate on the devices connected to the so-called input/output bus.

1.2. What can a computer do?


Very little. Very fast.

Execute instructions step by step.

There is a single flow or thread of execution that will advance linearly unless it is interrupted.
we indicate something else.

It only understands very, very basic instructions.

Arithmetic (addition, subtraction, multiplication, division).


Logical (equal, greater, lesser).
•Jump (conditional, unconditional, subroutine call).
He executes them very, very quickly, at an incredible speed.

Currently over 100 MIPS and rising.


Count every inhabitant of the earth in 1 minute!
Without pause.

• 8.640.000.000.000 days.
3,155,760,000,000,000 per year.

1.3. What are computers used for?


The computer will only do what someone has told it how to do.

They run programs/applications that are useful for someone.

Programs that easily and quickly solve problems/tasks that would take
to solve by hand.

Only those problems for which there is a solution 'at hand'.

1.4. Software life cycle


Software development involves numerous stages that can overlap in time,
subdivide in turn, and that in numerous cases will involve the need to return to a
previous stage (Fig.1.4).

Specification A specific program or application is proposed. It is defined


with precision that is requested and that is not, for this the end user must be consulted.

4
A.− − 1, 1.5
B .− − ...2
C .− −
1.− −
Aanalysis 2.− −
Specification
3.− −

Ciclthe due Diseno

del software main(){}


Uuser
√A.
√B . Programming
√C .

Implementation Validacion

Analysis It is investigated how to solve the problem by breaking it down into smaller subproblems.
mitigated and easy to address.

Design outlines each part of the application (module) and its interconnection.

ProgrammingThis stage corresponds to the programmer.

Integration The quality of each module and its integration with the rest is monitored.

Validation: The correct functioning of the application and compliance are checked.
from the original specifications.

Implementation is monitored to ensure the startup and proper functioning of the program.
but in its final environment and the acceptance by the end user.

1.5. Programming cycle


The programmer's work will also be cyclical. On many occasions, they will have to return.
in previous stages, when correcting errors or to modify or add functionalities.

CodeWrite code that implements each module precisely.

Comment sufficiently on the source code to assist in its reading and understanding.
Sion.

5
Compile this code into instructions that the computer knows how to execute. For this
makes use of the compiler.

Verify its correct operation against all possible cases. For this
create test programs that subject each module to all test cases
possible and makes use of code coverage tools.

Debug. Locate and correct the errors you find. To do this, use the debugger.

Document helps generate the documentation that the end user will need to use.
correctly launch the program.
This work is normally undervalued, but it is vital. No matter how good a
A program or application will be useless if the user does not know how to use it.

1.6. What knowledge does a programmer have?


The programmer is also an 'advanced' user of the system on which
develop.

Get to know and use the system. Know how to make the most of it.

Read manuals incessantly.


He/She knows and uses many tools that help him/her in his/her task.

•Editors, compilers-interpreters, debuggers, libraries.


Consult the help, read the manuals.
Know and use various programming techniques and languages.

Structured programming, C.
Object-oriented programming, C++.

Summary
The computer is a very fast, precise, and tireless fool.

The programmer is intelligent and knows how to instruct them precisely. Their job is
creative and rewarding, but requires a lot of knowledge.

The user is... unpredictable and demanding.

6
Chapter 2

Structured programming

Initially, software development was artisanal but as time went on...


projects became larger, more ambitious, and more crowded, problems were observed
of low productivity, very difficult maintenance, code redundancy, etc., etc. To this
it was called 'the software crisis'.
In response, methodologies for design and programming techniques were created.
that allow for a more organized and systematic approach to software development
and obtain higher quality software.

Quality software
It can be defined as one who is:

Correct. It does exactly what it says, precisely.

Efficient. If there are several ways to do something, the least expensive will be chosen.

Reusable. Avoid reinventing the wheel. Use existing libraries. Program with g
reality in order to reuse the code.

Transportable. Prevent its code from depending on a specific architecture, system,


etc.

Standard. It makes your program behave as people expect, avoiding originality.


unnecessary data.

Robust. It should tolerate as much as possible the errors made by users or other programs.
But, and I will not make a mistake.

Legible. The code must be written to facilitate understanding by other programmers.

Sustainable. The software is intended to evolve, to be corrected and improved. Its


Design and structure must facilitate its maintenance.

2.1. Concepts
Algorithm + Data = Program

7
Algorithm
It is a method to solve a problem without ambiguities and in a finite number of
steps.
The algorithm must be the general solution to all problems of the same type.
All possible situations that may arise should be considered.
It is possible that there are several algorithms for the same problem. It will be necessary to know
choose the most suitable, efficient, elegant, etc.
The following example applies:

Bubble sort algorithm:


Sort N elements (A,B,C,D,...) if and only if
for any two elements X and Y there exists:
a numerical distance criterion(X,Y)
a mechanism for exchanging
(...X...Y...) in (...Y...X...)

Sorting 2 elements X and Y is:


If distance(X,Y) is negative
then swap X and Y.

Sorting N elements is:


Sort each element X with respect to each of the
other elements Y.

Data
It is the information that the program receives, handles, or returns.
An autistic program, which would neither take nor produce data, would be of no use.
The data will be managed by grouping related information sets.
data streams that facilitate their use.
We will address the 'data structures' in a separate section, but for now, let it suffice.
as an example:

height is: a real number


It is: a table of heights
NTA is: the number of elements in TA
TA[i]: it is the number i element of TA

Program
It is the result of expressing an algorithm in a language, as a set of instructions.
actions whose execution sequence applied to the input data will solve the problem
proposed producing the desired result.
The program includes the description of the data structures that will be manipulated, and
the language instructions that manipulate said data.

8
In the case of the example, the program would implement the bubble sort algorithm, applied
to the ordering of the NTA heights contained in a table named TA. Additionally defined
a comparison criterion of heights and a mechanism for exchanging elements
from the table.

2.2. Programming Techniques


We are going to look at a few techniques whose application will make it easier for us to tackle development.
of programs of a certain entity.

Abstract terms
The natural solution, not computer-based, of a problem is independent of the tool.
to be used for its execution.
Thus, we must conceive the logic of the program in terms or vocabulary.
natural aspects of the problem, forgetting until the last moment the fact that finally it
You will use a computer to solve it.
In the first instance, it is essential to identify what these terms are and what they signify.
relationships are established between them.


Enumere y describa brevemente al menos 10 t´erminos en los que expresar´ıa a un T 2.1
Friend, how do you play mus?

Imagine a program capable of simulating car collisions against a wall, T 2.2
intended to study occupant safety.
Enumere al menos 20 t´erminos del vocabulario natural de este problema y organicelos
in a graph that expresses their interrelation.

? Do you think that terms related to representation should appear in the graph? T 2.3
collision graph?

With this very basic technique, we are identifying the data that should be handled.
our program. Moreover, this way of reasoning is fundamental in object-oriented programming.
object-oriented.

Top-Down reasoning
Many times, the nature or magnitude of the problem does not allow us to visualize it in
its entire extent. In these cases, it is very likely that we will be able to identify
stages or phases in it.
It is about subdividing the original problem into smaller subproblems.
size, going from the general to the specific, always thinking in abstract terms. A
As we advance in the decomposition of the original problem, we will refine one.
solution.
The goal is to evolve towards a decomposition with a degree of detail that
allow its expression with the basic structures of structured programming (that
we will see later.

9

Break down the problem 'development of a game of mus' into a hierarchy T 2.4
guide that contains all the phases of the game.

T 2.5 Break down hierarchically the statement 'project and construction of 150'
VPO housing

Top-down reasoning is vital for addressing problems of some significance. With it,
We are discovering what the 'storyline' of our program will be.

Modularization
As the fundamental components of a program take shape, we should...
think about whether they are sufficiently 'generic' and/or sufficiently 'delimited' as
to deserve to be treated as an independent, possibly reusable piece. To this
we will call the piece module.
A module will be defined by the set of functionalities it offers.
they will also be conceived based on the interfaces of other more primitive ones, forming
finally a hierarchy.
The concept of module is essential for the development of medium and large
projects as it can be: analyzed, coded and verified independently.
Each functionality of each module must be programmed in a structured manner.

2.3. Programming structures


Structured programming defines a limited series of basic structures for
grammar to use and some coding style rules. This minimizes the probability
of human error and makes programs more reliable, efficient, and adaptable to others
languages or systems.

Sequence
Successive steps or actions that will be executed in strict order.
Figure 2.1 shows a graphical representation of this type of structure.
called an organogram. We will use this type of representation when we want
Create a graphical representation of our algorithm before programming it.
Below are two equivalent examples, written in pseudocode and in
C language. We will use pseudocode to express the algorithm or program textually.
what we have in mind before programming it.
Sequence of pseudocode C code sequence

This is a comment. /* This is a comment. */


Comments do not execute. /* Comments do not execute. */
PRINT "Enter a number" Enter a number >= 0
value scanf("%u", &value);
PRINT "You entered the ", value, NL printf("You entered %u\n", value);

10
Co entario] − − − − [Comienzo de block

Hace r before

− − [En estricta sequence

Hace r d e pu és

− − [Fiinthe block

Figure 2.1: Sequence structure.

The pseudocode can be as free as we want, as long as we are sis-


thematic in their use. On the contrary, as can be seen in the figure, the languages of
programming (in this case C) requires us to adhere to a very specific syntax.

Selection

They allow directing the flow of execution to one among several alternatives based on
certain condition or conditions established on the data.
Each condition is evaluated as a logical expression, that is, it will ultimately take a
a TRUE value or a FALSE value.

IF_THEN IF_THEN_ELSE SELECT

Selection
Cand i ion Condition
FALSO FALSO TRUE
TRUE CASO 1 CASO 2 CASO N

Hace r Hace r To make Hace r Hace r Hacer

Figure 2.2: Selection structures.

Figure 2.2 shows the flowchart of the different types of selection. Next
Examples of each case are shown in pseudocode and in C.

11
IF THEN ifen C

Conditional action if (num != 0)


IF (num is not equal to 0) {
duplicate num num = num * 2;
END_IF }

IF THEN ELSE if else in C

Alternative actions if (num == 0)


IF (num is equal to 0) {
increment num num = num + 1;
ELSE } else {
decrement number num = num - 1;
END_IF }

SELECT switch C

Multiple selection switch(num + 5)


depending on the value of expr. {
SELECT (expr) case -1:
CASE -1 num = num * num;
Raise a number to the square break;
CASE 0 case 0:
do nothing break;
ELSE default
divide the number by 2 num = num / 2;
END_SELECT }

Iteration

They are the programming structures called loops, which allow executing none-
Either once or several times the set of actions indicated in the body of the loop. The iteration
The loop will be controlled by a specific condition set on the data.
this condition must be able to change its state (from TRUE to FALSE or vice versa) in
the body of the loop so that it can terminate. Otherwise, it would be a loop
infinite (or that which never happened), which would be symptomatic (most of the time) of a
error in our program.
Figure 2.3 presents the flowchart of the three most classic loops. Next
Examples of each case are shown in pseudocode and in C.
The body of the WHILE loop executes while the condition evaluates to true, then
it may not be executed even once.

12
WHISLAND DO_WHISLE FOR

Desof

Mwhile Hacer
FALSO
Mie tras
FALSO
TRUE TRUE
CERTAIN
Hacer
Hace r M i e n t ra s
Step
FALSO

Figure 2.3: Iteration structures.

WHILE while C

WHILE (num is negative) while (num < 0)


If num is greater than or equal to {
that 0 will not execute. num = num + 1;
increment number }
DO

The body of the DO WHILE loop is executed at least once and will iterate when the
condition is evaluated to TRUE.
DO WHILE do while in C

DO do
decrement num {
WHILE (num greater than 0) num = num - 1;
} while (num > 0);

If to iterate the condition must resolve to FALSE, we will be talking about the loop.
REPEAT UNTIL.
REPEAT UNTIL
In C, this loop does not exist but
REPEAT it can always be done by denying the
decrement num condition:
UNTIL (num less than or equal to 0)
do{...}while(!(cond));
As can be seen in figure 2.3, the FORe loop is basically a loop.
WHILE adding an initialization step for the loop control variable
and another stage of increase (or decrease) of this control variable.

13
FOR foren C

Iterate for the values for (num = 1; num <= MAX; num++)
indicators of the variable {
FOR num=1 up to MAX in 1 sum = sum + num;
sum of numbers }
DO

Function-Procedure
Programming languages support a concept similar to that of a function.
thematic: that operation or method that, for some fixed invocation arguments, de-
it returns to its exit a unique possible value.

f ( x ) = a x 2+bx+c

This definition talks about important concepts such as: invocation, arguments and
returned value. We will always consider a single entry point to the function reached
When invoking it, there is a single exit point to where the resulting value is returned.
In structured programming we should stick to this definition as much as possible.
of function, but reality will lead us to use the term function to refer to the
more generic, functionality, that in many cases will return more than one value, modifying
the arguments with which we invoke it. We will call procedure to the function such that it does not
returns value, although it will modify the arguments with which it is invoked.
We will use functions to group under the name of the same, each well defined.
operation unit, that is, each functionality or concrete method of each module of
our program.

Main function
In fact, from the point of view of their execution, programs will be nothing more than a
enormous collection of functions that call one another, with a specific one being the first
when invoked. This is the main function or entry point of the program, in C it is
always the main function.
minimo.c
This is the 'correct' C program
int main(void) smaller than can be written.
{
A main function that ends
return 0;
} returning a 0.

Arguments, parameters, and local variables


Below is the mathematical definition of the factorial function(n), being
a natural number (non-negative). It also shows an implementation in C.
same and of a main function that uses it.

14
1 this is zero
factorial(n) = n! = ( n
i=1isiin >0
Q

factorial.c main.c

unsigned factorial(unsigned n) int main(void)


{ {
unsigned value = 1; printf("3! = %d\n", factorial(3));
/* Goes from n to 2 */ 7! = 5040
for (; n > 1; n--) return 0;
value = value * n; }
return value;
}

Note that we did not carry out an iteration of 1, but rather of 2. Given that the result
what is finally obtained will be identical to that of the mathematical definition, this type of improvements
I would clarify, but they would demand some explanation.
In this example, the important details are appreciated:

The functions declare that they receive certain parameters (called parameters.
formals), which will determine their behavior as they take the indicated value in
the invocation to the function.

unsigned value = 1; Functions can declare local variables that serve as


leads to the implementation of the algorithm. These variables should be conveniently-
variables initialized in each invocation before being used, otherwise their
the value will be indeterminate (rubbish).

factorial(3) When a function is invoked, the declared formal parameters


they mark the value indicated in such invocation.
In this case, the parameter will take the value 3 during the first invocation and
during the second it will take the value 7 (factorial(7)).

Each invocation of a function or procedure is independent of the others, in


the fact that both formal parameters and local variables are "others"
"different" in each invocation. Its "state" is not saved from one invocation to another.


Program in pseudocode or in C, the main function of a program, that shows T 2.6
el factorial de los nu´meros del 0 al 20 inclusives. Use un bucle.

Program the binomial function(m, n) defined over natural numbers m and n as: T 2.7

m m!
binomial(m, n) = Ã ! =
n n! (m−n)!

15

Program the following Taylor series development that approximates the function x , T 2.8 e
defined for real values (float in C).

10
xi
ex=
X
i=1
i!

Recursion
It is the faculty of functions (or procedures) to invoke themselves, directly, or
indirectly through others.
It is an elegant and natural way to express the solution to certain problems that are
"self-contained". The typical example is the mathematical function factorial(n) that we have already
seen, but that can also be defined recursively in the following way.

1 here es0
factorial(n) = n! = (
n*(n-1)! if n > 0

factorial.c main.c

unsigned factorial(unsigned n) int main(void)


{ {
if (n == 0) unsigned num = 5;
n = 1; unsigned res;
else res = factorial(num);
n = n * factorial(n - 1); %d! = %d
return n; return 0;
} }

Note that in this case the implementation does not contain any iterative loop.
However, this implementation is perfectly equivalent to the previous one, although it is without
the most difficult doubt to understand.
In this example, it should be noted that:

n = 1; In the implemented solution, it has been decided to do without using a variable.


auxiliary and the parameter has been reused by changing its value as if it were a variable
it will be local.

In the example, the variable num, local to the main program, is 5 in the
moment to invoke the factorial function.

The passing of parameters is by value. The value of the variable is passed to the function.
not the variable itself. This will always be so in C.
Since the parameters and local variables are exclusive to each invocation,
Disabling its value does not imply modification of the argument with which it was invoked.

printf ... the variable numLa changes value even though it is done by the parameter.
which is worth. It will still be worth 5 when the main program prints it.

16
A recursive function must precisely control its termination condition. If not
If I did it like this, the recursion would be infinite and this would inevitably lead to termination.
program anomaly.
The extensive use of recursion is discouraged, as it makes the code
more difficult to understand. In fact, any recursive solution can always be transformed
in an iterative.

2.4. Coding style

The style rules are intended to improve the readability of the code and make it more
understandable and independent of the author. Its application is very important. As we will see
subsequently, its use does not imply any change to the structure of our programs
however, they mark the difference between a good and a bad programmer.

Named

It is very important that in the implementation phase the...


abstract terms that have been used in the decomposition of the problem,
so that it is clearly reflected in the code.
The variable and/or function names we use should be sufficiently clear and
specific. We will avoid using names that are difficult to identify or that are ambiguous.

MAL GOOD

void bar(m R) void shuffle(remaining_deck_of_cards)


{ {
int c = R.c; int cuantos = restantes.cantidad;
int nc = 10; int num_cortes = 10;
int pc = rand() % c; int first_cut = rand() % how_many;
... ...
} }

Indentation

It is equally important that the "drawing" of the lines of our code reflects the
its structure, so that its visualization helps its understanding.
We will achieve this by making good use of 'whitespace', tabbing.
correctly the lines indicating their 'depth' and placing the block delimiters
({y}) uniformly in the code we wrote.

17
MAL GOOD

if (n == 0) if (n == 0)
{ {
n = 1; n = 1;
if (n == 1) { if (n == 1)
n = 2; {
} n = 2;
else { }
n = 3; else
}n = n + 1;} {
n = 3;
}
n = n + 1;
}

Comments
Regardless of the two previous points, the fact of 'coding the idea' that
having in mind necessarily implies a loss of information. The programmer
He knows what he wants to achieve, but when coding, he strictly indicates how to achieve it.
Continuously while programming, after a non-trivial reasoning, we make decisions.
about how and why to code things. Even if the code were perfect in its purpose,
named and styled, someone who read it would have to make a great effort to understand
why the author made the decisions he made.
A good programmer should adequately comment the code they write, for
to illuminate its readers about the reasons that determined the decisions made. If it
He made a good appointment; many useless comments can be avoided.
MAL GOOD

Restaurant 1 desfase_horario = -1;


H = H - 1; time = time + time_offset;
... ...
I raise to the second power const float PI = 3.141592654
r = r * r; It is a circle.
I multiply by PI superf = r * r * PI;
s = r * 3.14159;

Restrictions
In many 'structured' programming languages, there are clauses that allow
"breaking" the structure of the program, as they involve abrupt jumps in the execution thread.
Its abuse may tempt, but we must avoid it at all costs, except in certain cases where
on the contrary, it is appropriate.
We cite below those that appear in the C language, indicating the situations in
that can be used.
goto should directly go to another point in the code. It should never be used again.

18
breakSale abruptly out of the inner oswitchm´as loop. Its only usage is in
the switch statements of C.
MAL GOOD

do { switch (response) {
... case 'y':
... case 'Y':
if (exit) salir = 1;
break; break;
... default
... salir = 0;
while (1); /* Always */ }

jumps abruptly to the next iteration of the innermost loop. Its only use
Theloopislikeabodyofloopsthatshouldotherwisebeempty.
MAL GOOD

while (time() < limit); while (time() < limit)


continue;

Return abruptly from a function returning a value (or from a procedure)


only one unique return should exist as the last statement.
each function of our program. At the end of each procedure there is a return
implicit.
MAL GOOD

int F(...) int F(...


if (...) int ret = -1;
if (...) if (...)
for (... if (...
return 0; for (...
} else { ret = 0;
switch(...) } else {
default: return 2; switch(...)
} default: ret = 2;
} }
return -1; }
} return ret;
}

exit ends the program. We should only use one exit per program, and
it should appear as the last statement of the main function of it.

19
20
Chapter 3

Data types and structures

Every program uses variables and constants to represent quantities and/or qualities.
from the problem it implements. Before being able to use a variable in our program
We should have declared it, so that the compiler (or interpreter) of the language knows how to
what to expect. The declaration of a variable informs the compiler of the name with which
we will refer to her, her living environment and visibility, and the type of data associated with it
same.

Named

The language may restrict the naming of variables which, on the other hand, and as
we have already seen, it must be sufficiently explicit and reflect the abstract terms of the
problem. Specifically, compilers will not allow declaring variables whose name
matches one of the reserved words of the language.

´
Scope of life

The scope of life or of a variable determines how long it exists. A


A variable declared as global to a module or to the entire program will exist throughout.
the execution time of it. A local variable to (or a formal parameter of) a
function exists only during the execution of that function.

Visibility

The visibility of a variable determines from which points in our code we can
make reference to her.
Global variables in the program can be referred to from anywhere. They va-
Global variables in a program module can only be referred to from that module.
Local variables a (or formal parameters of) a function can only be referred to
from the code of said function.
If we declare a local variable with the same name as a global one, the global one will
will be hidden during the execution of said function.

21
Type
The programmer will choose the type of data for the variable based on its nature.
concept that represents and the set of possible values that we want the variable
may take. The type of a variable can be one of the basic types that the language
know in advance, or of a type derived from these.
Basic numeric types determine possible values within a range. For example-
Thus, an integer variable cannot contain (and therefore cannot take) the decimal value.
3.5. A variable declared as always positive cannot contain a negative value.

Value and format


It is essential to clarify the difference between the value that a variable has (or contains)
and the format or manner in which such value can be represented.
A kilogram of gold has a certain value,
regardless of its form: ingot, powder,...
Always remember.
During the following explanation, we will see examples of the same numerical value represented.
sitting in different formats (representation bases).

Basic types
In this section, we will briefly present the basic types of data in order to
better understand the so-called dynamic data structures that we will see next
nuance
As part of the practical exercises that we will do later in the course, we
they will handle these types of data and dynamic data structures, so here not
We will go into the topic in great detail.

Remember that the type of a variable or constant determines its nature and amount of
distinct values it can take.
Normally, for each basic type, the language provides a way to express constants.
of that type.

Without type

If we want to declare a function that does not return anything, that is, a procedure,
we will use the pseudotype void.
Otherwise, variables of this pseudotype cannot be declared.
GOOD MAL

void wait(unsigned seconds); void null_variable;

The logical
In many languages, there is a data type called logical (boolean) capable of representing the
Logical values TRUE or FALSE. The result of logical operations (comparison)

22
and its combination through logical operators (AND, OR, NOT, etc.) is a value of
logical type.
In C and C++, there is no logical data type, but any expression can
to be evaluated as a logical expression. The numeric value zero is equivalent to the logical value
FALSE, and as a negation, any numerical value other than zero is equivalent to a
logical value TRUE.

Character
They represent ASCII characters or extended ASCII (Latin1).

ASCII:’\0’, ’a’, ’B’, ’?’, ’\n’

’´a’, ’U’ ¨
§ ¤
char letter;
¦ ¥

Enter
They are positive or negative numbers or only positive, of whole nature, that is,
they cannot be divided into pieces (without decimals or with decimal part implicitly 0). They serve
to number things or count quantities.
Integer values can be represented in different bases or formats.

In decimal (base 10): 0, -1, 4095 65184

In octal (base 8): 00, -01, 07777, 0177240

0
§ ¤
int bank balance; /* No the blacks or reds */
¦ ¥
§ ¤
unsigned number of fingers; /* Playing Chinese */
¦ ¥

Real
They are positive or negative numbers with or without decimals. They are called 'comma'.
floating.

Notation point: 3.14159265358979323846

Exponential Notation: 42E-11 which equals 42 * 10−11


§ ¤
float distance, angle;
¦ ¥
§ ¤
double PI;
¦ ¥

23
Enumerated
For variables that can take any of the symbolic values listed.
Internally they will be represented as an integer value.
' $
enum {
thumb
index
heart,
to annul
pinky
} fingers;
enum {male, female} gender;
ring finger
female
& %

Pointer
They are variables that, instead of containing a value, contain the address of another variable.
of the indicated type.
They are declared with the character *. With the operator & you can obtain the address of a
variable. To refer to a pointer and thus access the value of the variable it points to.
point is used the operator *.
' $
int variable = 7;
int * pointer_to_integer;
pointer_to_integer = & variable;
*pointer_to_integer = 8;
/* Now the variable is worth 8 */
& %
As we will see shortly, pointer type variables are the basis for creation.
of dynamic data structures.
The value of address 0 (zero) is treated as a special marker. The NULL symbol
assigned to a pointer type variable, assigns it a zero value, which as an address is
invalid (cannot be dereferenced), but it helps us know that a pointer
it doesn't point anywhere at the moment.

Data groupings
Vectors
They are groups of homogeneous information. They are declared by indicating the type of each
component, its name and, in brackets ([]), its dimension or size. They can be of a
dimension or of several, in which case we talk about matrices.
The contents of a vector or matrix are stored contiguously in memory. Their
Contents are accessed by indexing (with [index]) with another integer value as the index.
In C, indexing starts at zero.

24
' $
char buffer[80];
float table[100];
unsigned char screen[1024][768];
unsigned char icono[16][16];
buffer[0] = 'a';
tabla[100] = 1.0; /* FUERA */
& %
Character strings are also vectors. A constant character string is expressed
in double quotes. Its last character is implicitly a null character ('\0').
Hello World!
M
a
Hello[11] == '\0'; /* End */

Structures
They group together a heterogeneous set of related fields into a single entity that
they form a concrete information.
Fields are accessed with the notation.field name.
In C, new data types can be defined with the typedef statement.
student.h student.c

typedef struct Student { #include "student.h"


char * Name, * LastName; Student_t me;
struct Date { Student_t Class[22], *current;
int Year, Month, Day; yo.Edad = 33;
Birth; actual = &Class[0];
float Age; *actual.Name = "Ramón";
} Student_t; actual->Apellido = "Ram´ırez";

Dynamic data structures


When our programs need to handle homogeneous groupings of data whose
unknown quantity in advance, we will organize these as a dynamic structure
of data.

Example: Imagine a program to sort the words contained in an


file. We do not know a priori the number of words that may be: 100, 1000,
one hundred thousand or a million. The program should be able to adapt in each case, and
only occupy the necessary memory.

A dynamic data structure is a data structure (something capable of holding


data) that can grow and shrink at runtime. To achieve this, they are built using
other data structures that incorporate 'pointer' fields to interconnect with each other.
The null value of these pointers will serve to mark the ends of the structure.

25
Dynamic structures, as their name indicates, are located dynamically in a
special memory space called, likewise, dynamic memory. This memory
is managed through basic primitives, whose name changes depending on the language, but that
they coincide in their functionality, namely:

Location of a number of bytes: Reserve the amount of dynamic memory


requested. They return the address of that memory. malloc(in C) new(in C++).

Liberating space located: They free the indicated dynamic memory area,
that must have been previously returned by the location primitive.free, (in
C) delete (in C++).

If we want to use a dynamic data structure in a program, we must dis-


put on (or program ourselves), a set of utility functions that are capable
to perform the basic operations on that type: insert and extract an element, search
by content, ordering, etc., etc.
Going into details about dynamic data structures would warrant an entire book.
In the later chapters, there will be an opportunity to make use of them and get to know them better.
in depth. In this chapter, we will limit ourselves to commenting on certain basic characteristics of the
more common, which are:

Lists are a linear structure of elements. Like the links of a chain.


Depending on how each element is linked to the previous and/or the next one, we will speak.

Singly linked list: Each element points to the next one. The pun-
The "next" of the last element will be null. It only allows the traversal of the
list in one direction and limit the operations of insertion and extraction.
As a reference to the list, we will keep a pointer to the first of its elements.
It will be empty if this pointer is null.
Doubly linked list: Each element points to both the next and the previous one.
my previous one. The 'next' pointer of the last element and the 'previous' of
First, they will be null. It allows for the traversal of the list in both directions and op-
insertion/extraction ratios of any position/element of the same.
To manage the list, we will keep a pointer to the first of its elements.
or another structure that holds pointers to both ends. It will be empty if
this pointer (or both) is null.

According to the order of insertion/extraction of elements, we will talk about:

FIFO Queue: The first to enter will be the first to exit.


LIFO Stack: The last one to enter will be the first to leave.

T́rees are a tree-like structure of nodes. Every tree will have a root node, and
The nodes that have no descendants are called leaves.
According to the number of children of each node, we will talk about:
´
Binary tree: Each node has two children.

26
Ń-ary Tree: Multiple children per node.

Hash Tables are mixed structures that usually consist of a vector (of
fixed size of pointers to a dynamic structure. Its fundamental use is the opti-
prioritization of data search when there are too many.
Every hash table uses a hashing function (hash function) that, depending on
a key field of each element determines the entry of the vector in which it should be
to find said element.

27
28
UNIX Environment and Tools

29
Chapter 4

The UNIX Environment

What is UNIX?
It is undoubtedly one of the most extensive and powerful operating systems there is.
His main virtues are:

Multiuser On a UNIX machine, many can be working simultaneously.


users. Each user will have the impression that the system is exclusively theirs.
Multiprocessing Each user can be running many processes simultaneously.
Each process runs independently of the others as if it were unique.
in the system. All processes of all users are executed concurrently
fearful about the same machine, so that its executions progress in a way
independent. But they will also be able to communicate with each other.

MultiplatformThe core of the UNIX operating system is written in more than 95%
in C language. Thanks to this, it has been ported to machines of all sizes
and architectures. Programs developed for a UNIX machine can be
easily taken anywhere else.

How it is UNIX!
Be mindful of the font. Distinguish between uppercase and lowercase. It's not the same. !!
unixqueUnixqueUNIX.

For NOT clumsy users. Other Operating Systems subject every 'dangerous' action
(e.g. rewriting a file) to the user's consent.
In UNIX, it is understood by default that the user knows what they want, and what the
User requests, it is done, whether it is dangerous or not.

The deleted is irretrievable. It is an example of the previous point. One must be careful-
As we will see later, there is a possibility for the user to protect themselves from
eveninthistypeofmandates.

How is Linux!
POSIX. It is a version of UNIX that complies with the Standard Portable Operating System.
Interface.

31
Free. It is distributed under the GNU license, which means it must be distributed with everything.
your source code, so that if someone wishes, they can modify it as they please or
need.

Evolve. To be free, it is in constant development and improvement, by programmers


volunteers from around the world.

Free. At the incredible price of 0 thousand pesetas.

Contents
In this chapter, the UNIX environment is presented from the perspective of a user.
of the same. The goal is for the user to feel minimally comfortable in UNIX.

4.1. Users and Groups


For a person to be able to use a particular UNIX system, they must have been
previously registered as a user of it, that is, they must have an open account
in the system.

User
Every registered user in the system identifies themselves with a username.
(login name) that is unique. Each account is protected by a password (password) that
the user must enter to access their account. Internally, the system identifies the
each user with a unique number called UID (User Identifier).

Group
For user management to be more convenient, these are organized.
in groups. Just like users, groups are identified in the system by a
name and by a unique number (GID). Each user belongs to at least one group.

Privileges
The operations that a user will be able to perform in the system will be limited to
function of its identity, this is due to the UID-GID pair, as well as the permissions of
access to the file or resource you wish to access.

Superuser
Among all users, there is one called 'superuser' or root, which is the
in charge of managing and configuring the system.
It is the one that has the UID number 0 (zero). The superuser has no restrictions.
Of no kind. He can do and undo whatever he wants, see, modify, or delete at will.
Being a superuser is a delicate and responsible task.
The administration of a UNIX system is complex and requires extensive knowledge of
same.

32
4.2. Session
Session durations the period of time that a user is using the system.
We know that each user has an account that will be protected by a password or
password
Now follow the steps below to enter and exit the system.
§ ¤
myname T 4.1
¦ ¥
To enter the system, please enter your username or login name.
§ ¤
passwd:******* T 4.2
¦ ¥
If you have already set a password, you must enter it now, so that the system
verify that you are who you say you are.
§ ¤
T 4.3
¦ ¥
Upon entering the system, a program is launched that will serve as our interpreter of
mandates. It presents us with an urgent message.
§ ¤
logout T 4.4
¦ ¥ £
To end a session, you must use this command or exit.
¢ ¡

4.3. Mandates
All operating systems have commands that facilitate the use of the system. Their
the general form is:

mandate [options] [arguments...]

The fields are separated by one or more spaces. Something in brackets [ ] is


optional. The notation...indicates one or more. The options typically start with
un-(less)

Throughout this presentation, you will be asked to perform numerous tasks.


practices, observe the behavior of the system or answer questions...

Now you must log into the system as indicated in the previous section. T 4.5

!! Note that UNIX distinguishes between uppercase and lowercase. T 4.6

? Answer, would you know how to end the session? T 4.7

...in many cases it will be suggested that you execute certain mandates...
£
¢
who am i ¡ T 4.8
Get information about yourself.
£
¢
date ¡ T 4.9
Know the date and time.
£
¢
cal ¡ T 4.10
Visualize a calendar of the current month.

33
...more explanations will be given to expand their knowledge...

The majority of mandates recognize help-options as a request for


help about it.

...and it will be requested that you do more practices...

£
T 4.11 date -h
¢ ¡
Learn what options this mandate accepts.

T 4.12 ? What day of the week was the day you were born?
...likewise, during this presentation we will be introducing a summary of
some commands that are considered more important.

If you want complete information about something, you should use the mandate.

man [what] Manual Pages


Visualize an electronic copy of the system manuals. It is certain that we will take...
There will often be the doubt of how to correctly use some command or anything else.
We should consult the manual.
The manuals are divided into sections:

1 Commands (e.g. sh, man, cat).

2 System calls (e.g. open, umask).

3 Library functions (e.g. printf, fopen).

4 devices (e.g. null).

5 File format (e.g. passwd).

6 Games.

7 Miscellanea.

8 System Administration Mandates.


£
T 4.13 man -h
¢ ¡
Seek brief help on the mandate.
£
T 4.14 man man
¢ ¡
Read the complete documentation of the mandate.
§ ¤
T 4.15 man [1-8] intro
¦ ¥
In case of conflict, if an example exists in different sections of the same sheet
manual, must specify the section to search in.
The introductory page of each section informs about its contents.

34
4.4. Processes
When you invoke a command, the executable file of the same name is executed.
Every program in execution is a process. Every process is identified by a number.
D
P
I.ruoym
e,tsysehtni euqinu

Concurrency
At all times, each active process of every user in the system is competing.
with the others to be executed. The operating system is the one that determines whose turn it is.
at every moment.
All this happens very, very quickly, resulting in each process advancing in its
execution without noticing the presence of others, as if it were the only process of the system,
but in reality everyone is advancing simultaneously. This idea is called
concurrence.
£
ps T 4.16
¢ ¡ It allows you to see a snapshot of the processes in the system. It supports a multitude of op-

Hierarchy
All system processes are organized in a parent-child hierarchy that has
as it culminated the process called init whose PID is 1.
Every process also has an associated number that identifies its parent process, it is
elPPID.
§ ¤
pstree T 4.17
¦ ¥
It allows you to see a snapshot of the process hierarchy in the system. It supports mul-
attitude of options.

4.5. ´
File Tree
There is a unique hierarchical structure of file names, that is, there is no
concept of 'unity'. In UNIX, devices are 'mounted'.

Root directory
The root of the hierarchical naming is the so-called root directory and is denoted by
the character/(divided by) not by the \(backslash).

DirectoryHOME
As soon as you enter the system, the user is placed in the so-called directory.
HOME of your account.
Each account has its own HOME, which is created by the system administrator.
when opening the user's account.
This directory belongs to the corresponding user. Below it you can create
how many subdirectories or files you want.

35
Types of object
To find out what is or what is contained under a file name, you can use the
mandatofile
There are (basically) 3 types of 'objects', namely:

Directories: It is a container whose entries refer to other files and/or directories.


The use of directories leads to the hierarchy of names. Every directory contains
always the following two entries:

It refers to the same directory that contains it.


It refers to the parent directory of this, that is, to its parent in the
hierarchy.

Normal files: They are contained in directories. They contain sequences of bytes.
they could be program codes, data, text, a source in C or anything else
what.

Special files: They are like normal files, but they do not contain data; rather, they are
the interface for access to peripheral devices such as printers, disks, the
rat, etc, etc.
Each device, existing or possible, is represented by a special file.
that is located under the /dev directory.
UNIX extracts these special files (and therefore the devices) exactly.
just as it deals with normal files, in such a way that for programs,
the ones and the others are indistinguishable.

pwd Print Working Directory


It allows to find out which directory we are in at every moment, to
which we call the current working directory.
§ ¤
T 4.18 pwd
¦ ¥
Visualize your current working directory.

T 4.19 !! Note that the absolute path (from the root directory) of your directory is shown.
HOME.

T 4.20 !! Note that the components of a path are separated among themselves with the character/
(divided by) and not by the (backslash).

cd [dir] Change Directory


With this command, we change from one directory to another. It accepts as an argument the
name of the directory we want to change.
There are three special cases that are useful.
£
¢
cd ¡
Invoked without argument, it returns us directly to our HOME.

36
£
¢
cd . ¡
The directory named (dot) always refers to the current directory.
work. It really is not going to change our directory.
§ ¤
pwd T 4.21
¦ ¥
Note that we remain in the same directory.

As we will see later, the directory is useful for referring to files that
they are right here.
£
¢
cd .. ¡
Every directory always contains an entry named..(dot dot) that does
always refer to the parent directory of the current directory.
§ ¤
pwd T 4.22
¦ ¥
Note that we ascend to the parent directory.
£
¢
cd .. ¡
T 4.23
Repeat the operation until reaching the root.
§ ¤
pwd T 4.24
¦ ¥
Note that you cannot go beyond the root directory.
£
cd
¢ ¡
T 4.25
Which directory have we gone to?

ls [-opt] [dirs...] List Directory Contents


This mandate presents us with information about the files and directories contained in
the specified directory(ies).
If no directory is specified, it reports on the current working directory.
This mandate allows for a number of options that configure the information obtained.
Among them, the most used are, in this order:

-Long format. One line per file or directory with the fields: permissions, owner-
tario, group, size, date and name.

It also informs about names that begin with (dot), which normally
they do not show.

It recursively descends through each found subdirectory, reporting on its


contents.

If you need to explore more options, refer to the manual.


£
ls
¢ ¡
T 4.26
Note that your HOME directory appears to be empty.
£
¢
ls -a ¡
T 4.27
But it is not. At least it will always show the entries.

37
£
!! It is possible that other names may appear that he/she did not see. ¢ ¡
T 4.28

This command prevents showing entries that start with the dot character.
We could say that the files or directories whose name begins with a dot
they are "hidden", but they can be seen if we use option-a.
They are usually configuration files (or directories).
£
T 4.29 ls -la
¢ ¡
If we want to know the details of the 'object' associated with each entry of a live broadcast-
river, we will use option-l.

T 4.30 ? Whose files are in your HOME?


What size do they have?
When were they last modified?
§ ¤
T 4.31 ls /home
¦ ¥
Observe the HOME of other users on your system.
Inspect the contents of your directories.
§ ¤
T 4.32 The provided text is a command line instruction and not a translatable text.
¦ ¥
Show the "trunk" of the name tree.
£
T 4.33 man 7 here
¢ ¡
Read a complete description of the hierarchy of the UNIX file system.
is using.
§ ¤
T 4.34 ls -???/bin
¦ ¥
Get the contents of the /bin directory ordered from smallest to largest size
the files.

T 4.35 !! Note that a file named 'ls' appears. That is the file you are executing.
he cuts every time he uses the mandate.
Those files are only a part of what you could run, there are many more.

T 4.36 ? Who owns the /bin/ls file?


T 4.37 !! Your permissions allow you to execute it.

4.6. File descriptors


Processes handle files through what are called file descriptors.
For example, the result of opening a file is a descriptor. Subsequent operations of
Management (reading, writing, etc.) of this file receives the descriptor as a parameter.
From the point of view of our program, a descriptor is nothing more than a number.
positive integer (0, 1, 2, ...).

38
Standard descriptors
The first three file descriptors are referred to as 'standard', and it is considered
that are always available to be used.
They will normally be associated with the terminal but could be associated with a file.
or any other object of the file system, but this is a detail that should not
to worry when programming.
Programs that behave in a 'standard' way will use them appropriately.

Standard input. It is the main entry point to the program. The programs that are composed-
in standard form, they receive the data to be processed by reading from their input
standard (file descriptor number 0).

Standard output. It is the main output of the program. Programs that behave
they standardly emit their results by writing to their standard output (descriptor
of file number 1).

Standard error. It is the output for the error messages of the program. If a program
The one that behaves in a standard way must emit an error message; it should do so.
through its standard error (file descriptor number 2).

4.7. Interpreter of mandates


The user's interaction with the system could be graphical, but in UNIX it's more
as common as it is textual, this will be through dialogue with a command interpreter.
In UNIX, it is called shell. There is not a single shell but many.
§ ¤
ls -l /bin/*sh T 4.38
¦ ¥
The files that appear are the different available shells. Perhaps some may appear.
another file. Perhaps symbolic links appear (another type of object in the system of
files).

They are fundamentally distinguished by the syntax they recognize and the facilities that
they provide for interactive use and to use them as a programming language
scripts of commands.
The most common for interactive use are eltctsh and bash.
We have already been using the shell, in fact you are running one of them in !!
this moment.
We are going to see some basic characteristics of the shell that will be very useful to us.

Interactive use
When the user logs into their account, a prompt eventually appears on their terminal.
the urgent message that informs you that unshell is at your service.
This document displays the dialogues with the shell in the following way:
¨
$ The prompt will be the symbols.
©

39
A good shell for interactive use offers services that make tasks easier for the user.
This means that there are multiple key combinations associated with some function.
special feature. To fully understand them you should consult the manual.

Online edition
We can move the cursor ( ) over the command line we are typing with
← → , delete before and over the cursor with Backspace and Del, go to the beginning and the end wit
Start and End, etc.

T 4.39 !! Observe what happens if you try to edit a line longer than the screen.

Historical of mandates
If we have recently typed a command, it is not necessary to type it again. It is enough to
search it using the up and down arrow keys.
The internal mandate history informs us of all the mandates we have used.
.yletal
§ ¤
T 4.40 history
¦ ¥
Visualize the commands that you have executed so far in your shell.

T 4.41 !! Note that history stores the previous mandates of one session to another.
£
T 4.42 !!
¢ ¡
Repeat the last command.
£
T 4.43 !5
¢ ¡
Repeat the command that occupies position 5 in the story.

End of data
The key combination Ctrl on an empty line indicates 'end of data'.
If we press it and there is a program reading from the terminal, it will understand that there is no more.
data and will normally finish.
£
T 4.44 cat
¢ ¡
It is a mandate that what you read from your input you write in your output. Write several.
data lines to observe their behavior.
As you are reading from the terminal, to finish normally, I must indicate to you
end of data
§ ¤
T 4.45 $Ctrl D
¦ ¥
Elshell also is a program that reads from its standard input. Indicate "end of
data
T 4.46 !! It will understand that there are no more data, it will end normally and therefore
It will take us off the account.
If this has been the case and this behavior is not to your liking, there is a way to configure the
to prevent it from happening. We will see it later.

40
Work control
In UNIX we can run commands to be executed in the background.
ground) placing the character at the end of the line.
Also, from its shell you can suspend the work that is in the foreground.
ground), restart suspended jobs, leave them in the background, kill them, etc.

Ctrl C Pressing this key combination when we have a program running in


close-up, we will send him a signal that "will kill him."

Ctrl Z The foreground program will be suspended and we will see the prompt again
shell.
§ ¤
This internal mandate informs us of the ongoing jobs and their status. We will be able to
¦ ¥
referring to a specific one by the position it occupies in this list.
§ ¤
Executing it, the last suspended process will return to the foreground. To refer to
¦ ¥ § ¤
for the second job on the job list we will use fg %2.
¦ ¥
§ ¤
By executing it, the last suspended process will be executed again, but in the second.
¦ ¥
Plan! It's as if we had ripped it off from behind.
£
This internal command allows you to send a signal to a process, which, normal-
¢ ¡
I swear, we will kill him.

√ § ¤
I started several processes in the background, for example several sleep 60 & , and toT 4.47
¦ ¥
continue to check the active jobs.
√ £
Start a process in the foreground, for example cat, suspend it. Check the T 4.48
¢ ¡
active jobs. Bring it back to the forefront and then send it a signal that it
friend.

? How would I kill it if it's in the background? T 4.49

Complete file names


If we have a partially written filename and we press the tab key (→), the
you will try to complete that name. If what we press is the combination Ctrl D,
will show us the list of names that start like him.
It also works for names of mandate.
§ ¤
√ £
Write .cs→then try ls .cs Ctrl D, observe the difference. T 4.50
¢ ¡ ¦ ¥

Control descrollde terminal


We call terminal the screen/keyboard set or the window through which
we talked with elshell.

41
Ctrl S freezes the terminal. It will not display anything that our programs write, nor
will attend to what we type.
If by mistake we press Ctrl we could think that the system has frozen.
hanging, but it is not like that.

Ctrl Q Unfreezes the terminal. What the programs would have written while the screen
it was frozen, just as what we would have typed will appear at this moment.
The common use of the pair Ctrl S Ctrl Q is to control the output of programs.
that emit many lines.
§ ¤
T 4.51 Ctrl S is -la
¦ ¥
Note that, for the moment, none of what we have typed appears in the terminal.
¨
T 4.52 Ctrl Q
©
You will see how the command we typed appears and is executed normally.
§ ¤
T 4.53 This command is not translatable as it is a terminal command for listing files.
¦ ¥
The result of this command is too many lines of text. Play with Ctrl S
Ctrl Q to stop and let the screen run.
§ ¤
T 4.54 Ctrl C
¦ ¥
When you get tired, kill the work.

Redirect
In UNIX we can redirect standard input, standard output, and/or standard error.
of the mandates.
This is a very very useful feature, since it does not require any modification at all.
a mandate can be used to take its input from a file instead of having to
type it.
Similarly, instead of the command output appearing on the screen, we could
send it to a file to use it later.
The correct syntax for performing redirects is:
§ ¤
file mandate
¦ ¥
In its execution, the command will use a file as input, it will read from it and not from the keyboard.
§ ¤
mandate>file
¦ In its execution, the¥ output of the command will be sent to a file and not to the screen. If
if the file existed beforehand, it will be created automatically, but if it exists,
it will be previously truncated to zero size.
§ ¤
mandate>>file
¦ ¥
Append the command output to the end of the specified file.

The error messages that a command may generate can also be redirected to
a file, but we usually prefer that they are displayed on the screen.

42

T 4.55 Obtain from a file called list all the files in long format
of everything walking through the file tree recursively from the root directory.

If you started it in the foreground, suspend it and move it to the background. While being in T 4.56
The background keeps running, but in the meantime we can continue doing other things.
£
¢
ls -l all files ¡
T 4.57
Check that the file is growing in size.
£
¢
tail -f all files ¡
T 4.58
Examine how the content of the file is growing. To exit, use Ctrl C.

!! Only the error messages that the command produces will appear on the screen when it does not T 4.59
to be able to access certain directories.
§ ¤
grep login name <all files> my files T 4.60
¦ ¥
Use the command to locate the lines of the input file that contain
indicated text, in this case your username. Leave the result in a file.
of my file names.

Sequence
In UNIX we can redirect the standard output of a command directly to the
standard entry of another. With this we build sequences of commands that process the
result of the previous one and emit its result to be processed by the following mandate
from the sequence.
We achieved this by separating the commands with the character '|' (pipe).
This is a very powerful feature of UNIX, since it does not require files.
Large intermediaries can process large amounts of information.

§ ¤
ls -la /home | greplogin name T 4.61
¦ ¥
Obtain a long-format list of all account directories and filter those that
lines that contain our username.

!! No intermediate file is created. T 4.62


Get the list of 'all your files' from the previous section, connecting the re- T 4.63
result to the mandate to stay only with its files. Send the result to
a new file named MY files. Put it like this, in uppercase, to not
crush the file named that way but in lowercase.
Remember to invoke the command so you can continue doing other things.

When they have finished executing the mandates, check the differences between T 4.64
the files named my files and MY files. To do this, use the command
diff, consulting the manual if necessary.

43
less [files...] On Screen File Browser
Filter for the display on screen of files. It is an improved version of the classic.
more mandate.
If we use it by indicating a set of files, we will be able to go to the next one and the previous one.
con:ny:prospectively.
With this command we will only be able to see the files, but not modify them, later on
we will see some editor.
£
T 4.65 less * files
¢ ¡
Visualize the content of the files that already exist in your HOME that end with
files
£
T 4.66 ls *
¢ ¡
Note that the metacharacter does not refer to files whose name starts with
point.

Metacharacters
Metacharacters (wildcards) are wildcard characters. With their use, we can make
reference to sets of files and/or directories, indicating the regular expression that matches
with their names.

Individual character wildcard.

Wildcard of zero or more characters.

~Abbreviation of the home directory.

Abbreviation of the user home directory.

[abc] House with a character from the specified set between the brackets.

House with a character in the specified range between the brackets.

Deny the selection indicated in the brackets.

Grouping. House successively each strip.



T 4.67 Obtain a long format listing of the files in the /biny directories
/usr/bin/that contain the text in their name.

T 4.68 Obtain a long format listing of the files in the /dev directory that co-
Miencen port termination end digit.

password Change User Password


UNIX protects access to user accounts with a password.
The system accounts, their passwords and other information are registered in
/etc/passwd file.

44
§ ¤
T 4.69 less /etc/passwd
¦ ¥
View the content of the password file.
This file contains information about the user accounts that exist in your
system.
Check that there is an account with your username.
§ ¤
man 5 passwd T 4.70
¦ ¥
I studied what the contents of the password file are.

A user can change their password using the mandatopasswd.


It is very important that the password is not trivial, nor a date, nor a car license plate.
telephone, proper name, etc.
§ ¤
password T 4.71
¦ ¥
If you don't have a good password, change it right now!

copy orig dest Copy Files


Obtain a copy of a file or copy a set of files to the directory
indicated.
It allows for a variety of options, which we will not describe in this document (if it has
If you have any doubts, consult the manual.
It is important to highlight that this is a dangerous mandate, given that (by default) it makes
what we ask for without consulting, which may imply that we crush files that already
There is. To consult us, we must use it with the -i option.
§ ¤
copy /etc/passwd ~ T 4.72
¦ ¥
Copy the file /etc/passwd to your HOME.

!! Visualize that your content is really a copy of the original. T 4.73



Copy the .cshrc file that you now have in your account with the name passwd.T 4.74

? Have you consulted the rewriters? Has the file been modified? T 4.75

Try the same operations using option -i. T 4.76

rm files... Remove Files


Delete files. To see your options consult the manual.
You also have a dangerous mandate. In this case, I only do what you asked me to.
not by consulting, but rather in UNIX there is no way to recover a file (or
directory) that has been deleted. Therefore, it is essential to use it with the option
-i.
√ § ¤
Try to delete the passwd file of your account with rm -i passwd. Answer that T 4.77
¦ ¥
no.

Delete the passwd file that you now have in your account. T 4.78

45
!! Note that it has indeed been removed. T 4.79

I tried to delete the file /etc/passwd. T 4.80

T 4.81 ? Can you do it? Why?

move original destination Move (Rename) Files


Change the name of a file or move a file or set of files to a
directory.
It is also a dangerous mandate, because it could "crush" the file(s) of
destination without asking us for confirmation. It should be used with the -i option.


T 4.82 Try to move the file /etc/passwd to your HOME.

T 4.83 Try to move a file from your account (one that is not useful to you) to the directory /etc.

T 4.84 ? Can you do these operations? Why?



T 4.85 I renamed the file to my files.

T 4.86 ? Could he do it? Did he consult you?

mkdir dir Make Directory


Create a new directory. Of course, if a file or directory with that name already exists
name will give an error.
The directories that you think will be yours. You will only be able to create files or directories in
those directories where you have write permission.

T 4.87 Create a subdirectory named namesubarbol in your account.

T 4.88 Visualize its content with ls -la.

T 4.89 Create other deeper subdirectories without using cd.
£
T 4.90 The provided text is a command and does not require translation.
¢ ¡
Visualize the current content of your home recursively.

remove directory Remove Directory


Delete a directory. Of course, the directory must be empty.

T 4.91 I tried to delete an empty directory, e.g. subdirectory.

T 4.92 ? Can you do it?



T 4.93 Visualize the content of the subtree.

T 4.94 Delete the deepest directories and ascend until you manage to delete.
the directory of the tree.

46
4.8. Configuration
Each user can configure the behavior of their account by editing.
certain files present in your HOME that depend on which shell you use.
The configuration file that is of interest to us at this moment is:

.bashrc runs every time the user starts a new bash.

The .cshrc is executed every time the user starts a new csh.

Since these files are only read when the shell starts, for the changes to
let's make it happen, we should either exit the account and log back in or
make use of the internal source mandate so that your shell can read and interpret the content of the
file.

Dangerous mandates
In UNIX, if a file or directory is deleted, there is no way to recover it.
Moreover, the commands blindly obey whoever uses them, and do not consult. To avoid
Making a mistake is advisable to use certain mandates in a way that consults before acting.
To do this, we will use the internal command alias, to protect ourselves from the commands.
problematic.

End of data
We will prevent the shell from terminating on the Ctrl D key press by asking it to ignore it.

£
¢
alias ¡
T 4.95
By executing this command, you will observe that aliases are in operation.
The aliases modify "on the fly" the commands we want to execute, just before
to be finally executed.
§ ¤
picofile T 4.96
¦ ¥
It is a very simple-to-use editor. Use it to make the following modification in the
configuration file for your shell.
Choose the version on the left if you use eltcsho elcsh, or the one on the right.
If you use elbash. Add the following at the end of the indicated configuration file.
lines:
.cshrc .bashrc

alias cp 'cp -i' alias cp='cp -i'


alias mv 'mv -i' alias mv='mv -i'
alias rm 'rm -i' alias rm='rm -i'
set ignoreeof IGNORE EOF=10

£
¢
source ¡ T 4.97
Remember to ask your shell to read and interpret the content of the file.
configuration, so that it has a record of the modification it just made.

47
£
¢
alias ¡
T 4.98
You will observe if the aliases you have established are functioning.

T 4.99 Ctrl D
Check if this sequence makes your shell terminate.

4.9. Xwindows
So far we have experienced the use of UNIX from a textual interface, that is,
from a terminal. There is a very powerful graphical environment called X windows simple-
menteX
£
To start it we can use the command startx.
¢ ¡

48
Chapter 5

Development tools

We will approach the use of various useful tools for the programmer.
There are integrated environments for software development, but they are not commonly used.
Traditionally in UNIX, various tools are used that fulfill the various
stages of development.
This part of the course is primarily practical. The student must consult the help.
available. As a quick reference method, brochures with summaries will be available.
tools of various instruments.

5.1. Editor
We are going to see three types of editors. The student must choose the option that best suits them.
suitable for their needs.
Initially, it is recommended that you use elpico.

little Simple Text Editor


It is the editor that the tool uses internally for email.
simple to use but not very powerful.
As an introduction to its use, I carried out the following tasks:
§ ¤
peak T 5.1
¦ ¥
Start the editor.

Ctrl G T 5.2
Access the help to learn the few controls it has.

Ctrl X T 5.3
Leave the editor.

vi Visual Editor
Elvies is the most classic editor and we will find it in any UNIX machine. It is
That's why, despite being less friendly, there are many people who prefer him to others.
sophisticated. There is a more modern and powerful version called vim.

49
£
¢
vim ¡ T 5.4
Start the editor.

T 5.5 Esc
Change to "command mode".

T 5.6:help
Access the navigable help.

T 5.7 No text provided for translation.


Abandon the current edition.
£
T 5.8 vimtutor
¢ ¡
The editor starts working on creating a tutorial for it.

emacs GNU Project Editor


Elemacs, developed by FSF under GNU license, is much more than just a simple editor.
It is an environment on which multiple applications, email, browser are integrated.
web, news, etc., etc., etc. It is written about a delisp interpreter.
There is a version called Xemacsmás adapted to the X graphical environment, and totally
compatible with the previous one.

£
T 5.9 emacs
¢ ¡
Start the editor.

T 5.10 Ctrl G
Abort the action or mandate to be specified.

T 5.11 Ctrl H T
Access the creation of a tutorial.

T 5.12 Ctrl X Ctrl C


Abandon the current edition.

Exercises
For the editor of your choice, or for each of them, carry out the following exercises.
practicals:

T 5.13 Start the editor to create a new file.

T 5.14 Read the sample file 'estilos.c' from the editor.
√ Without modifying its functionality, format the text in the style you consider most appropriate.
T 5.15
legible.

T 5.16 Save the edited text with the name "my style.c".

T 5.17 I left the editor.

50
5.2. Compiler
Compiling a high-level language, such as C, is translating the source file or files into
object files, which contain an approximation of machine language, plus a list of the
externalsymbolsreferencedbutnotresolved.
The assembly stage is capable of linking object files with each other, and in turn
with the necessary libraries, resolving all external references and symbols. This
stage finally produces an executable file.
An executable file has an internal format that the operating system is capable of
understand, which describes how to position the process in memory for its execution.

Legend
F ichers
Car
Mmodules estandard
de de Fich Herram Product
uuser calphabet
ra Ccompilation between each other
Dependent

F ichors
stdio.h Oobject
Llibraries
estandard
stdlib.h
Mmontage
libc.a
main.c
Eexecutable
gcc -c main.o
prim.h ld primes
gcc -c prim.o
prim.c
libm.a

math.h

Figure 5.1: Compilation and assembly phases.

In figure 5.1, the separate compilation of two modules main.cyprim.c is observed.


and the assembly of the two resulting object files for obtaining the executable file
primes.

Extensions
Certain suffixes or extensions of file names indicate to the compiler the
contents of it, and therefore the type of treatment that must be carried out:

.C source code. It must be preprocessed, compiled, and assembled.

C header file. It contains declarations of data types and prototypes of


functions. It must be preprocessed.
.Object file. It is the result of compiling a source file. It must be mon-
tado.

51
The object file library. It is used to resolve the symbols of the functions
standard that we have used in our program and that therefore still remain without
resolve in the object files.

Errors and Warnings


As you may have noticed, programming languages, including C, present
a quite strict syntax. One of the basic missions of the compiler is to indicate to us
the mistakes we have made while coding, where they are and what they consist of.
The warnings that a compiler is capable of notifying are of two types:

Errors. They are serious problems, so much so that they prevent the compilation process from being able to
conclude. However, the compiler will continue processing the file or files, in a
attempt to detect all the errors that may exist.
It is essential to correct these errors so that the compiler can finish
do your job.

Warnings are minor issues or alerts about potential problems in our code.
Although the compiler may finish doing its job, it is essential to
fix the problems that cause these warnings, because they will surely be
source of more serious future problems.

The compilation of a correctly written program should not yield any type
of message, and finish cleanly.

gcc GNU C and C++ Compiler


The name commonly given to the C language compiler in UNIX is cc.
we will use the gcc which is a C and C++ compiler, developed by FSF under license
GNU.
It is a very good, fast, and efficient compiler. It covers the stages of compilation and
assembly. It involves one or more sources in C, objects and/or libraries and finally generates a
autonomous executable.
It accepts a multitude of options, among which the following stand out:

Compile only the compilation step of the indicated file, but not the monitoring step.
take.

request that the compilation result be a file with this name.

Activate the detection of all types of possible errors. The compiler becomes
more strict.

Information related to the object file or executable that (as we will see later)
it will allow the symbolic purification of it.

-Activate optimization mechanisms that will achieve a faster executable, at the cost of
from a longer compilation.

Request the assembly against the specified library.

52
-Ldirectory Indicate a directory where to search for the libraries specified with the -l option.
It has many more options. Consult the manual if you need it.

minimum.c T 5.18
Write the file using your preferred editor and the indicated contents in the
previous chapter 2.3.
§ ¤
gcc minimo.c T 5.19
¦ ¥
We use the compiler to obtain the executable resulting from this source.
£
ls
¢ ¡
T 5.20
Note that if we do not tell the compiler otherwise, the resulting executable will
Maria, the default name.
§ ¤
./a.out T 5.21
¦ ¥
We execute the file, indicating that it is in the current directory. This will be ne-
necessary if we do not have the directory '.' in the PATH environment variable.

Now we are going to work on a more complex program.

factorial.c T 5.22
Write the file with the editor of your choice and the indicated contents in the
previous chapter. The objective will be to show the factorial of the numbers from 0 to 20
using a loop.
§ ¤
gcc -g -c factorial.c T 5.23
¦ ¥
With this step, we will compile to obtain the file objectfactorial.o. This file
it will contain symbolic information.
§ ¤
gcc factorial.o -o factorial T 5.24
¦ ¥
When we compile correctly, we will mount to obtain the executable.
§ ¤
./factorial T 5.25
¦ ¥
Execute the command and verify its proper functioning.

5.3. Debugger
The compilation of a program without errors does not imply that it is correct. It is very possible
that contain errors that will only become visible at the moment of their execution. To
To correct these errors, we need a tool that allows us to supervise the execution.
from our program and corner the error until locating it in the code.
There are low-level and high-level debuggers. Low-level ones allow us to inspect
the machine code or assembly language, and its execution step by step. The high level, also
called symbolic debuggers, allow to visualize and control the online execution
of the source code that we have written, as well as to inspect the value of the variables of
our program..
In order for a program to be properly debugged, its executable must contain
There must be information that associates it with the source code from which it derived. For this, it must have been
compiled with the -g option.
Some of the functions of the debugger are:

53
Set breakpoints in the execution of the program.

Examine the value of variables

Run the program line by line

We will use the gdb developed by FSF under the GNU license.
We will also see what a graphical frontend for gdb is.

Ficherocore
While working in UNIX, it will often happen that when executing a program it ends.
suddenly and showing some error message such as:
§ ¤
segmentation fault: core dumped.
¦ ¥
This message informs us that our program in execution, that is, the process, has
an illegal access to memory has been made and the operating system has killed it. Likewise, we
It indicates that a file named core has been generated. The core file is a dump of the
memory of the process at the precise moment when I made the mistake, this is a snapshot.
This file will help us to study in detail the situation that gave rise to
error.

gdb prog [core] GNU debugger


We will invoke the indicated program executable that we want to debug and option-
We will finally indicate in the file what generated its anomalous execution.
Once the debugger is started, it provides a series of commands of its own for
control the purification

access a help menu.

runStarts the execution of the program.

Set a breakpoint (a line number or the name of a function).

Print the specified lines of code.

Print the value of a variable.

continue the execution of the program after a breakpoint.

Next, execute the following line. If it is a function call, execute it completely.

execute the following line. If it is a function call, execute only the


call and stop at the beginning of it.

quit Ends the execution of the debugger

For more information about the debugger, please refer to its interactive help, the manual or the
triptych.
Later, we will have the opportunity to make extensive use of the debugger.

54
ddd prog [core] Data Display Debugger
It is a graphical frontend for gdb, that is, it provides us with a fairly graphical interface.
intuitive for debugging, but ultimately we will be using gdb. As
added feature, provides us with facilities for the graphic visualization of structures
data from our programs, which will be very useful. To use them I need to have
X-Windows started.

Libraries
A basic rule for creating correct software is to use code that is already tested.
and not systematically reinventing the wheel. If there is a library function that
We should use it and not rewrite such functionality.
There are many many libraries with facilities to do all kinds of things:
multiple precision arithmetic, graphics, sounds, etc., etc.
In order to correctly use those library functions, we must consult the
manual that will indicate to us the need to include in our source file a certain file
header (e.g. #include <string.h>), which contains the declarations of data types and
the prototypes of the library functions.
It is recommended that you consult the ANSI C brochure.
We will highlight here two fundamental libraries:

libc.a is the standard library of C. It contains functions for: string handling


characters, standard input and output, etc.
It corresponds to the functions defined in the header files:
<assert.h> <ctype.h> <errno.h> <float.h> <limits.h>
<locale.h> <math.h> <setjmp.h> <signal.h> <stdarg.h>
<stddef.h> <stdio.h> <stdlib.h> <string.h> <time.h>
The setup against this library is done automatically. It is not necessary.
indicate it, but we would do it by invoking the compiler with the -lc option.

libm.a is the library that contains functions for mathematical and trigonometric calculations.
sqrt, pow, hypot, cos, atan, etc. To make use of the functions in this library
we would need to include in our file <math.h>.
The assembly against this library is carried out by invoking the compiler with the option
-lm.

ar -opts [member] arch files... Manage Archive Files


When you want to undertake medium or large projects, you can create your own libraries.
tecas using this tool. It is a utility for the creation and maintenance of
file libraries.
To use a library, the library is specified in the compile line.
conventionlibname.a) instead of the objects.
Some frequently used options are:

-Remove the specified files from the library

55
-Add (or replace if it exists) to the library the specified files. If it does not exist
the library is created

-r is the same as -r but only replaces if the file is newer

Show a list of the files contained in the library

-vVerbose

Extract the specified files from the library

Below are some examples of the use of this command.


To obtain the list of objects contained in the standard C library, you would execute:
§ ¤
ar -tv /usr/lib/libc.a
¦ ¥
The following mandate creates a library with objects that handle different structures.
of data:
§ ¤
ar -rv $HOME/lib/libest.a pila.o lista.o
¦§ ¥¤
ar -rv $HOME/lib/libest.a arbol.o hash.o
¦ ¥
Once the library is created, there would be two ways to compile a program that uses it.
library and also mathematics:
§ ¤
cc -o pr pr.c -lm $HOME/lib/libest.a
¦§ ¤¥
cc -o pr pr.c -lm -L$HOME/lib -lest
¦ ¥

5.5. Constructor

If the program or application we are developing is suitably decom-


placed in multiple modules, the global compilation process can be automated
using the make tool.

make Application Maintainer

This utility facilitates the process of generation and updating of a program.


automatically which parts of a program need to be recompiled upon an update
of some modules and recompiles them. To carry out this process, make must know the ...
pending issues between the files: a file must be updated if any of the files it depends on is
newer.
The tool makequery a file (Makefile) that contains the rules that is-
specify the dependencies of each target file and the commands to update it.
continuation, an example is shown:

56
Makefile

This is a comment
CC=gcc These are macros
-g
OBJS2=test.o prim.o

all: primes test This is the first rule

primes: main.o prim.o This is another rule


gcc -g -o primes main.o prim.o -lm
This is the associated mandate

test: $(OBJS2) Here we use the macros


${CC} ${CFLAGS} -o $@ ${OBJS2}

main.o prim.o test.o : prim.h # This is a dependency.

This does not depend on anything, it is mandatory.


rm -f main.o ${OBJS2}

Note that the lines containing the commands must be properly formatted.
buladas using the tab key, not spaces. If not done this way, the tool will
It will indicate that the file format is incorrect.
£ £
We will run make to trigger the first rule, or make clean to be explicit.
¢ ¡ ¢ ¡
the rule we want to trigger. So what will happen is:

The rule corresponding to the indicated objective is located.

Their dependencies are treated as objectives and are triggered recursively.

3. If the target file is less current than any of the files it depends on,
The associated commands are carried out to update the target file.

There are special macros. For example, $@ corresponds to the name of the target.
Likewise, rules can be specified based on the extension of a file. Some
Some of them are predefined (e.g. the one that generates the .o from the .c).

Exercise 5.1.

5.6. Other tools


There are a variety of other tools that may be useful to you when developing.
code in a UNIX system We will cite three:
§ ¤
gprof
¦ ¥
It is a tool that allows us to create a profile of the execution of our
programs, indicating where time is wasted, so that we will have criteria
to optimize if it is convenient these areas of our code.

57
£
gcov
¢ It¡ is similar to the previous one, but its purpose is different. In the verification phase of the
program, allows to cover the code, that is, to ensure that all lines of code
from our program have been sufficiently tested.
£
¢
indent ¡
It allows you to indent the source files in C. It is very customizable, so that the result-
the final draft corresponds to the author's own style.

58

You might also like