Notebook On Introduction To Programming in Unix
Notebook On Introduction To Programming in Unix
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
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.
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
Controladora
ROM
RS232 Mfromme
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.
There is a single flow or thread of execution that will advance linearly unless it is interrupted.
we indicate something else.
• 8.640.000.000.000 days.
3,155,760,000,000,000 per year.
Programs that easily and quickly solve problems/tasks that would take
to solve by hand.
4
A.− − 1, 1.5
B .− − ...2
C .− −
1.− −
Aanalysis 2.− −
Specification
3.− −
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.
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.
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.
Get to know and use the system. Know how to make the most of it.
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.
6
Chapter 2
Structured programming
Quality software
It can be defined as one who is:
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.
Robust. It should tolerate as much as possible the errors made by users or other programs.
But, and I will not make a mistake.
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:
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:
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.
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.
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
10
Co entario] − − − − [Comienzo de block
Hace r before
Hace r d e pu és
− − [Fiinthe block
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.
Selection
Cand i ion Condition
FALSO FALSO TRUE
TRUE CASO 1 CASO 2 CASO N
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
SELECT switch C
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
WHILE while C
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.
14
1 this is zero
factorial(n) = n! = ( n
i=1isiin >0
Q
factorial.c main.c
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.
√
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
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:
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.
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
MAL GOOD
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
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
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
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
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.
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
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).
’´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.
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.
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
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:
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++).
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.
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
What is UNIX?
It is undoubtedly one of the most extensive and powerful operating systems there is.
His main virtues are:
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.
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.
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:
...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...
£
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.
6 Games.
7 Miscellanea.
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:
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.
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).
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?
-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.
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.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.
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).
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 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.
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.
√
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.
[abc] House with a character from the specified set between the brackets.
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.
? Have you consulted the rewriters? Has the file been modified? T 4.75
√
Try the same operations using option -i. T 4.76
45
!! Note that it has indeed been removed. T 4.79
√
I tried to delete the file /etc/passwd. T 4.80
√
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.
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:
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
£
¢
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.
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.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.
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
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:
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. 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.
Compile only the compilation step of the indicated file, but not the monitoring step.
take.
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.
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.
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.
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.
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:
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.
55
-Add (or replace if it exists) to the library the specified files. If it does not exist
the library is created
-vVerbose
5.5. Constructor
56
Makefile
This is a comment
CC=gcc These are macros
-g
OBJS2=test.o prim.o
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:
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.
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