[go: up one dir, main page]

0% found this document useful (0 votes)
46 views64 pages

RTOS UNIT-IV - Class

PPT

Uploaded by

manforlover7
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)
46 views64 pages

RTOS UNIT-IV - Class

PPT

Uploaded by

manforlover7
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/ 64

UNIT-IV: Introduction to Linux

▪ overview of Unix/Linux,
▪ Features, commands,
▪ File manipulation commands, editors,
▪ directory commands,
▪ input/output redirection,
▪ pipes and filters,
▪ file protection, process commands,
▪ about the shell
➢ Linux and Unix are famous operating systems that allow you to perform
multiple tasks and specific functions.
➢ Let’s find some major differences between Linux and Unix.
What is Linux?
✓ Linux is an open-source operating system.
✓ This OS is supported on several computer platforms and includes multiple
software features that handle computer resources, and allow you to do tasks.
✓ This operating system was launched by Linus Torvalds at the University of
Helsinki in 1991.
What is Unix?
✓ Unix is a powerful multitasking operating system that behaves like a bridge
between the user and the computer.
✓ It allows the user to perform specific functions.
✓ This operating system was launched in 1960 and was released by AT&T Bell
Labs.
Features Linux Unix
Basic Linux is an open-source operating system. This OS is Unix is a powerful and multitasking operating
Definition supported on several computer platforms and includes system that behaves like a bridge between the
multiple software features that handle computer user and the computer.
resources, and allow you to do tasks.
Launched by This operating system was launched by Linus Torvalds This operating system was launched in 1960 and
at the University of Helsinki in 1991. released by AT&T Bell Labs.
OS family It belongs to the Unix-like family. It belongs to the Unix family.
Available in It is available in multiple languages. It is available in English.
Kernel Type It is monolithic. It can be microkernel, monolithic, and hybrid.
Written in C and other programming languages. C and assembly language.
File system It supports more file systems than Unix. It also supports less than Linux.
support
Usage It is used in several systems like desktop, smartphones, Unix is majorly used on workstations and
mainframes and servers. servers.
Examples Some examples of Linux are: Fedora, Debian, Red Hat, Some examples of Unix are IBM AIX, Darwin,
Ubuntu, Android, etc. Solaris, HP-UX, macOS X, etc.
Security Linux provides higher security. Unix is also highly secured.
Price Linux is free and its corporate support is available at a Unix is not totally free. There are some Unix
price. versions that are free, other than that UNIX is
expensive.
• Introduction:
• The Unix operating system has been the darling of system-level programmers for
the past three decades.
• However, in recent years, Linux, a Unix-like open-source software, has
become very popular for both ·application software and embedded software.
• The scaled-down version of the Linux K ernel can be used as a n embedded
operating system.
• In this chapter, we will take a quick voyage into the deep ocean of Linux
Programming.
• We will study Linux architecture, various user commands and shell
programming.
• We will 'also study how Linux manages the processes and the various· system
calls used for process management.
• We will discuss the system. calls• used for inter-process communication.·
Overview of Unix/Linux:
• Unix is not just an operating system, it is a "great
programming- environment" as described by its authors.
• Unix was developed by Ken Thomson and Dennis Ritchie
beginning in 1969.
• It became the most popular operating system in the
academic community during the 1970s.
• It was commercialized in 1981 and since then, its use in
both the academic and commercial world has grown
tremendously.
• The main attractions of UNIX are:
• The main attractions of UNIX are:
•Most of the operating system was developed in a high-level language i.e. C.
• This was revolutionary during those days because, before the advent of
Unix, operating systems were developed in assembly languages.
• As very little code of Unix is written in processor-specific assembly
language, porting the operating system is very easy. As a result, Unix can be
ported to hardware built around different processors.
• The main attraction of Unix is that it provides a large number of utilities
for software development—editors, compilers, debuggers, source code
control system, utilities for string processing(awk), utilities for developing
new compilers (lex and yacc), and a very powerful user interface, the shell.
➢ But then, a good number of programmers are scared of Unix, due to some
popular myths.(Its name comes from the initials of its designers: Aho, Weinberger, and Kernighan. awk features
user-defined functions, multiple input streams, TCP/IP networking access, and a rich set of regular expressions.)
• "It is a complicated operating system": Any operating system that has many
features and utilities will appear complicated.
• If you want to learn Unix completely and only then want to start working
on projects, you may need to spend your whole lifetime learning, without
first contributing to your organization.
• So, learn the basics and then focus on problem-solving.
• "Too many options for each command": Yes, a simple command to list the
contents of a directory has nearly 20 options.
• Do not try to remember every option, it is just a waste of precious memory
of your brain.
• Look up the manual to see when you require a special option.
• Unix OS is the first OS to be developed in a high-level language, C.
• Hence, it is a portable operating system. Only a small portion of the OS
will have processor-dependent code.
• "Too many commands and utilities": It is
important to know the features supported by
Unix, and then learn only those utilities that are
required for your project.
• For example, "awk" programming language is an
excellent tool for string processing and non-time-
critical database development.
• If your project does not require such facilities,
please ignore awk!
• A number of Unix flavors are available such as AT & T Unix, HP UX, Sun
Solaris, IBM AIX, DEC Ultrix and Linux.
• Linux, developed by Linus Torvalds at the University of Helsinki, is a freely
distributed implementation of a UNIX-like kernel.
• Even the source code for the Linux kernel is freely available.
• The Linux community supports the concept of free software, promoted by Free
Software Foundation.
• Programming in a Linux environment is essentially the same as programming
in a Unix environment.
• The GNU Compiler Collection provides a number of compilers for various
languages. (GNU stands for "GNU's Not Unix“ and it is pronounced as “g-noo”. GNU is a
free and open-source operating system that was started in 1984 by Richard Stallman. GNU
is based on the Unix operating system, but it has been greatly modified over the years.)
• However, as the source code is available free of cost to everyone, many
variations of Linux are available
Features of Linux: The important features of Linux are listed below
➢ It is a multi-user operating system. Every user is given a username or login name.
➢ The system administrator logs in as 'root'. A number of users can form a 'group' and each of these groups is
given a name.
➢ The file system is organized hierarchically. At the topmost level is the root directory denoted by / under which
there will be sub-directions. Under each subdirectory, there can be directories or files.
Some important directories are:
❖ /bin contains commonly used commands
❖ /usr/bin contains less commonly used commands
❖ /etc contains the system administration commands
❖ /dev contains device files
❖ /usr/spool/mail contains mail files(Simultaneous Peripheral operations online -a computer document or task
list (or "job") is to read it in and store it)
❖ /usr contains users directories
➢ A variety of user interfaces are available. The command user interfaces are shell (sh,) kshell
(ksh) and C shell (csh). Menu-driven interface is through curses. X-Windows provides the
Graphical User Interface. (shell is the Linux command line interpreter. It provides an interface between the user and the kernel
and executes programs called commands. For example, if a user enters ls then the shell executes the ls command).(korn shell,Bourne)

➢ All devices are treated as files. In /dev directory, there will be one entry for each file. Writing
to a printer or a serial port is no different from writing to a file in the hard disk.
➢ To provide security for user's files, a user will have a password. Only the encrypted password
is stored in the system. The file /etc/passwd contains the list of all authorized users. It also
contains the encrypted passwords, default working directory of the users& their default shell.
➢ Every file is given protections—read, write and execute permissions are given to the owner,
group and others. The owner of a file can give permission to the group members and others to
read, write or execute the files.
Linux Command Summary
➢ When you login to the Unix system using the username and password given by your
system administrator, the "$" sign will be displayed. This is the system prompt.
➢ The system prompt is an indication that the system is ready to take commands from
you.
➢ After typing the command as given in the example column, you have to press enter key.
➢ If you make typing mistakes or give wrong arguments, Linux gives very cryptic error
messages. If the command is correct, the system will execute the command and display
the "$" prompt again.
➢ A command will be in the format
$commandname options arguments.
A sample command is as follows:
$nl —d hello.c
In this command, nl is the commandname, -d is the option and hello.c is the argument
Command Function Example
banner To display a banner banner prasad
cal To display calendar for a month or year cal 2004 cal 1 2004
date To display today's date date
sleep To make the shell sleep for a few seconds Sleep 10
who am i To know about your login details (sometimes you forget
who you are!)) Who am i
who To display the list of users who are
presently logged in who

clear To clear the screen clear

passwd To change the password passwd

echo To echo a string Echo “How are you”


File Manipulation Commands:
• A number of shell commands are available to display the files in a directory; to see the
contents of a file in different formats; to copy, delete, and rename files; to count the number
of lines, words, and characters in a file, etc.
• The most commonly used file manipulation commands are listed in this section.
• You can create a small text file in your directory, using the command: $cat > hello
• After giving this command, type a few lines of text, and then in a new line press^d (Control
and together). (The cat (concatenate) command in Linux displays file contents)
• Note that ^d is called the End Of File (EOF) character.
• Now you will see the $ prompt again. You have created a text file with the name hello.
• Using the same command but using different words in place of hello, create a few more text
files.
File Manipulation Commands:
Command Function Example
ls To see the files In the directory(The ls command is used to list files.) ls
ls -l Displays details of the files lists files and directories in a long format, ls -l
such as permissions, size, date of last update, number of links
ls -a To List hidden files (files with the name starting with or hidden files) ls -a
cat To display contents of ono or more files
cat hello
(The cat (concatenate) command in Linux displays file contents.) cat hello.c
hello2.c
Copy file 1 to file 2
cp cp file 1 file 2
rm To delete a file(remove) rm hello
rm -i To delete a file, ask for confirmation before deletion rm -i
mv To rename(move) a file my hello mytextfile
head To display top 10 lines of the file head hello
tall To display bottom10 lines of the file tail hello
spell To check the spelling of words in a file spell hello
wc To display number of lines, words and characters in a file wc hello
wc -w To display number of words in a file wc -w hello
wc -l To display number of lines in a file wc –l hello
wc -c To display number of characters In a file wc -c hello
nl To display a file with line numbers nl hello
EDITORS: Linux text editors can be used for editing text files, writing codes,
updating user instruction files and more. A Linux system supports multiple text
editors.
➢ To create text files or C/C++/Java programs, you need a text editor.
➢The two most commonly used editors arc "ed" and "vi".
➢ "ed" is a line editor you can edit one line at a time.
➢ "vi" is a screen editor— you can freely move around the screen and edit the
text wherever you want.
➢ vi will appear a bit complicated to start with, but soon you will start liking it
for its rich editing features.
➢ You need to spend a few hours on "vi" editor with the online manual pages to
get a good hang of the screen editor.
DIRECTORY COMMANDS
➢ When you login to the Linux system, by default, you will be given a directory. This is your home
directory, denoted by HOME.
➢ All your files will be stored in this directory. Within your directory, you can create sub-directories—
for example, one sub-directory to store C files, one directory to store Java files, etc.
➢ You can also move around to other directories—in fact, you can freely move to the root directory.
➢ Every directory will have a parent directory to which it is a child.
➢ For example, assume that your default directory is /usr/john. /usr is your parent directory. You can
move to the parent directory by giving the following command: $cd ..
Note: The cd stands for change directory. This is followed by two dots to move to the parent directory.
Alternatively, you can also give the command $cd /usr
• In this command, you specify the absolute path, whereas in the earlier command, you are specifying
the relative path.
The various Directory commands listed as follows as

Command Function Example


pwd To know the present working directory pwd
cd/etc
To change the directory, you can specify either
cd Cd..
the relative path or absolute path. Cd ./cprograms
mkdir hello
mkdir To create a new directory Mkdir /usr/
Prasad/hello
rmdir To delete a directory (only when it is empty) rmdir hello
find To search for a file find / -name hello -print
du To know the directory size in number of blocks(disk du prasad
usage information quickly)

df To know the disk space occupied by the total system df


INPUT/OUTPUT REDIRECTION:
➢ Normally, the input is given through the keyboard which is called the standard input, abbreviated stdin.
➢ The output is displayed on the monitor which is called the standard output, abbreviated stdout.
➢ The error messages are also displayed on the monitor which is called the standard error device, abbreviated
stderr.
➢ In Linux, all devices are considered as files, hence we have three standard files—stdin, stdout and stderr.
➢ Instead of taking the input from the keyboard, you can take the input from a file. Similarly, the output can be
redirected to a file instead of a monitor.
➢ Hence, you can redirect the output to a file instead of stdout, or you can take the input from a file instead of
stdin.
➢ Give the following commands one after the other: $cat filelist $ls -l > filelist
• The first command is to display the contents of the directory. Instead of displaying the contents on the monitor,
the output command is sent to a file named filelist. Using the second command, you can see the contents of the
file, i.e. the result of the command “ls -l. This is output redirection.
• The output is redirected using the symbol ">".
• you can append a file to another file using output redirection through the following command:
$cat file2 >> file 1
• Suppose you have two files with the filenames file1 and file2 in your directory. Now, guess what
happens when you give the following command: $cat file2 > file1
• You have overwritten file 1!
• Instead of taking the input from the stdin, input can be taken from a file as in the following command:
$wc < filename .This is called input indirection and the symbol used is "<". (word &characters-wc)
• You can combine both input indirection and output redirection in one command as in the following
command: $wc < filel > file2
This command will read the contents of filel, do a word/line/character count and the counts are put in
file2.
Note: The standard input, standard output and standard error files will have file descriptors 0, 1 and 2,
respectively. They are abbreviated stdin, stdout and stderr.
PIPES AND FILTERS:

• You can pass the output of one command to another command using pipes.
• When you give the shell command: $cat hello.c
the file hello.c will be displayed in one shot.
• If it is a large file, you will see only the last few lines. If you want to see few lines at a
time, you can give the command: $cat hello.c | more
• Here, the vertical line | is used for the piping operation.
• The output of the cat command is passed to the more command which displays a few
lines at a time.
• You can press the space bar or the return key to see the next set of lines.
• Alternatively, you can give the command:
$more filename
"more" is a filter that processes the input data to display a few lines at a time. In the first command, you are piping
the output of the cat filename to the "more" filter. In the second command, you are invoking the filter directly.
• The important filters that can be used directly as commands or that can be used through the pipe operation are:
sort Sorting utility. To sort a file in an order, such as alphabetical order, numerical order etc.
grep Pattern matching utility. To match patterns in strings such as to check whether a pattern exists in a file or
not.(Grep, short for “global regular expression print”, is a command used for searching and matching text patterns in files contained in the regular expressions.)
tr Translation utility. To translate upper case to lower case, remove punctuation marks, etc
prep File splitting utility, To split the file so that each word is placed in a separate line.(Prep reads each file in sequence
and writes it on the standard output, one `word' to a line. )

• The following command sorts the contents of a directory: $ls | sort


▪ The following command sorts the contents of a directory in reverse alphabetical order: $ls | sort -r
• The following command sorts the numbers in a file: $sort -n filename
• The following command displays all the lines in a file which contain the string"how".
$grep "how" filename
➢ Alternatively, you can give the following command to get the same result:
$ cat filename | grep "how"
➢ The following command takes the input file city1, converts all upper case
letters to lower case and produces a file city2.
$ tr "[:upper:]" "[:lower:]" < city1 > city2
here,[:upper:] and [:lower:) are translation arguments.
➢ You can use a number of translation arguments such as alnum, alpha,
blank, digit, lower, upper, punct, space and xdigit to represent
alphanumeric, alphabetic, blank, digit, lower case, upper case, punctuation
marks, space and hexadecimal characters.
▪ Now, guess what the following command will do: $tr -s "[ ]" "[\012]" < temp > temp0
▪ It will read the file temp, replace each space (blank character) by a new line, and the output
will be kept in the file temp0.
▪ So, if the file "temp" contains a sentence, the file "temp0" will contain one word of that
sentence in each line.
▪ Now you can write a C program to do the same thing, and you will take half an hour to do
that! It is time to appreciate the power Unix/Linux utilities.
➢ Now you can combine the basic shell commands, filters, pipes and input/output redirection
as in the following commands: $who > log $who | wc -l
$who | sort $who | grep prasad $</etc/passwd | wc -l
➢ The following command illustrates how You can give more than one shell command in the
same line:$date; who | wc -l > log
➢ The following command is a powerful command that uses many of the features of the shell.
➢ The tr utility takes file1 as input, and produces one word per line.
➢ The output is passed through the grep which matches with only words with four letters (see the
pattern which contains ^, three dots and $).
➢ The output of grep is sorted. The sorted output will contain multiple entries for the same word,
and such repeated words are removed by uniq.
➢ Here is this powerful command: $tr -s “[]” ”[\012]” < file | grep “^…$” | sort | uniq -c > fours
▪ Now try the following commands:
▪ Str "[:upper:]" "[:lower:]" < text > temp 1 (to translate all upper case letters to lower case)
▪ $tr -s "[ ]" "[\012]" < temp 1> temp 2 (to create a file with one word per line)
▪ $sort temp 2 > temp 3 (to sort in alphabetical order)
▪ $uniq -c temp 3 > output (to find frequency count of words)
➢ The first command translates all upper case letters to lower case letters.
➢ The second command creates a file with one word per line.
➢ The third command sorts the file in alphabetical order and
the fourth command finds the frequency count of words.
➢ Each command creates a temporary file which is given as the input to the next command.
➢ This set of commands can now take a file "text" and produce the frequency count of words.
➢ Here is the command to remove punctuation marks from a file:
$tr-d "[:punct:]" < text > temp0
File Protections
➢ A file will have read, write and execute permissions. These permissions can be given to the owner, group
members and others.
➢ Each file will be owned by a user. The owner of the file can assign read, write and execute permissions to a file.
➢ To avoid accidental deletion of a file, you can assign read-only permission to your file.
➢ You can also assign these permissions to your group members or others.
▪ When you give the command: $ls -l
the first field in the output of each line displays the file permissions in the format: drwxrwxrwx
➢ If the first field is in the format: drw-r— r —
▪ This indicates that the file is a directory, owner has read and write permissions, group members have only read
permission and others also have only read permission.
▪ The read, write and execute bits are set to 1 or 0 depending on whether the permission is granted or not.
▪ For example, let us say, you want to assign read and write permissions to yourself, only read permission to
group members and others.
➢ The command to set the permissions is as follows:
$chmod 644 filename
➢ If you want to give read, write and execute permissions to everyone, the command is
$chmod 777 filename
➢ Alternatively, you can use the command with -r, -w, -x or +r, +w, +x options,
-to indicate denial of permission and + to give permission as below:
$chmod -w filename
$chmod +w filename
➢ To change ownership of a file, the command is
$chown username filename
➢ To change group ownership of file, the command is
$chgrp groupname filename
Process Commands:
❖ When a process is run in the background, the shell prompt returns immediately
without waiting for the child process to complete.
❖ When you login, a process is created which is the shell.
❖ When you give a shell command, the shell creates another process.
❖ The shell process is the parent and the command process is the child process.
❖ Each process is given a process ID denoted by PID.
❖ Each PID will have PPID which is the parent process ID.
❖ You can see the various processes running on your Linux system by giving the
command: $ps -ef
➢ When you observe the output, you will see the PID, PPID, the time of creating the
process and the command given.
➢ When you give a command, it will be executed and then the shell prompt will
reappear. This is called foreground processing.
➢ If you want the command prompt to appear immediately before the command
execution is complete, you can give a command as below. This command
prints a file in the background.
➢ Note that /dev/1p0 is the device file for the printer.
$cat filename > /dev/1p0 &
▪ Using this command, the printing will be done in the 'background' and the
prompt will appear so that you can give another command. This is useful for
executing time consuming programs such as executing a C program.
➢ A running process can be terminated by using the kill command.
➢ The kill command sends a signal to the process for termination.
➢ You can terminate a process running in the background using the kill command.
$kill -9<pid>
➢ You need to replace the <pid> with the process ID obtained using the ps -ef command.
➢ Note that only the system administrator (the super user) can kill any process. You are not
authorized to kill others processes, but only your processes.
➢ you can find out the time taken for a process to execute by giving the command:
$time <command>
$time 1s -l
Three times will be displayed—total time, user time and CPU time.
Process Priorities:
➢ The process scheduler of Linux assigns CPU time using time
slices.
➢ Each process will have a priority. A higher priority process gets
CPU time more frequently.
➢ The following command can he issued by the super user to assign
a high priority to his process:
$nice - - 10 ./a.out > filnename
where 10 is priority to the process.
About the Shell
➢ Shell provides a very powerful interface to the user of the operating
system.
➢ But note that the shell is not a part of the operating system.
➢ It is just a C program to provide the interface between the user and the
operating system.
➢ It interprets each command, and then takes appropriate action.
➢ When a command is given by you, shell will search for the command in
/bin, and then /usr/bin and then your current working directory.
➢ The procedure for command line interpretation is as follows:
The procedure for command line interpretation is as follows:
➢ Shell waits for user to enter a command
➢ When the user enters a command and presses the enter key, shell reads the command, analyzes and checks for
syntax. If there are syntax errors, an error message is displayed. Otherwise, the command is broken into
words.
➢ Any special processing to be done is identified: such as input/output redirection, command and variable
substitution, filename expansion, etc.
➢ First word is taken as the command and rest of the words as arguments.
➢ Shell creates a child process. This operation is known as fork. The child process executes the command and
the parent process, the shell, waits for the child to complete.
➢ The child executes the command using the exec command.
➢ The parent process issues a new prompt for the next command.
➢ If the command is placed in background, the parent does not wait for the child to complete. It displays the
system prompt immediately, ready to take a command from you.

You might also like