Mohamed Kheider University of Biskra Computer Science PDF
Document Details
Uploaded by UndamagedSanJose
Université Mohamed Khider de Biskra
2025
Dr. D. Boukhlouf
Tags
Summary
This document is course material for Introduction to Operating Systems I, Chapter 2: UNIX Operating System & File Management, at the Mohamed Kheider University of Biskra, Algeria, academic year 2024/2025. It covers topics such as the definition and architecture of Unix, its features, file management, and file systems.
Full Transcript
Mohamed Kheider University of Biskra Faculty of Natural and Life Sciences Department of Computer Science Course material Module: Introduction to Operating Systems I Chapter 2: UNIX Operating System & File Man...
Mohamed Kheider University of Biskra Faculty of Natural and Life Sciences Department of Computer Science Course material Module: Introduction to Operating Systems I Chapter 2: UNIX Operating System & File Management Level: 1st year engineer Module leader: Dr. D. Boukhlouf Academic year 2024/2025 Chapter II: Unix OS & File management 1. Definition of UNIX The Unix operating system is a set of programs that act as a link between the computer and the user. First developed in 1970s, it is a multitasking OS that supports simultaneous use by multiple users. 2. Unix Architecture Here is a basic block diagram of a Unix system: The main concept that unites all the versions of Unix is the following four basics : Kernel: The kernel is the heart of the operating system. It interacts with the hardware and most of the tasks like memory management, task scheduling and file management. Shell: The shell is the utility that processes your requests. When you type in a command at your terminal, the shell interprets the command and calls the program that you want. The shell uses standard syntax for all commands. C Shell, Bourne Shell and Korn Shell are the most famous shells which are available with most of the Unix variants. Commands and Utilities: There are various commands and utilities which you can make use of in your day to day activities. cp, mv, cat and grep, etc. are few examples of commands and utilities. There are over 250 standard commands plus numerous others provided through 3rd party software. All the commands come along with various options. Files and Directories: All the data of Unix is organized into files. All files are then organized into directories. These directories are further organized into a tree-like structure called the filesystem. 3. Features of unix operating system 3.1 Multi- tasking: Unix is a multi-tasking operating system. It has the ability to support concurrent execution of 2 or more active process (instance of program in execution is called process). 3.2 Multi-user operating system: Unix is multi-user operating system. It has ability to support more than one user to login into the system simultaneously and execute programs. Difference: the diiference between multi –tasking and multi-user system is: In multi-tasking, different tasks the process running concurrently belongs to one user. In multi-user environment different tasks belong to diff users. 3.3 Portable IOS1 course: Dr D. Boukhlouf 2 Chapter II: Unix OS & File management Unix operating system is highly portable. Compared to other os, it is very easy to port unix on to different hardware platforms with minimal or no modifications at all, because it was developed in c language which is high level language 3.4 Inter machine communication: The development of communication protocols like tcp/ip has made possible by unix operating system to a user to exchange information in the form of email and shared data. 3.5 Security: As Unix is multi-user system, there is every chance that a user may intrude into a another users area either unintentionally. But, Unix offers solid security at various levels beginning from the system startup level to accessing files as well as saving data in an encrypted form. 6. Library of utility and commands: Unix has good library of utilities and commands that have been used to develop newer applications. 7. File and directory system: One of the very important key feature of any unix system is that allows users to organize and maintain these files/directories easily and efficiency. 4. Unix Systems Here are some variants of unix system: 5. File management 5.1 Definitions: File: is a memory block of secondary storage device like disk, magnetic tape that logically stores related records permanently. Operating system provides various components to manage the data on a disk system permanently. Such as File manager: It manages the allocation of disk-space for the storage of user files. Buffer manager: It is responsible for fetching data from disk storage into main memory buffers for processing, and then writing the updated data back onto the disk. Disk Space manager: It is responsible for managing disk space on disk. File system: provides a powerful and flexible way to organize and manage user information. File system is a Group of files and relevant information are organized in a well defined order and stored on a Hard Disk. Operating System defines a File System on Devices which is usually Hierarchical file system including UNIX. Directory: It is a File that keeps a list of other files and sub directories on the File System. UNIX has single File system. Devices are mounted into this File System. The disk space allotted to a linux/unix file system is made up of “blocks”, each of which are 512 bytes. To find out block size on your file system, use the cmchk command. $cmchk BSIZE=2048 IOS1 course: Dr D. Boukhlouf 3 Chapter II: Unix OS & File management 5.2 Types of File Blocks on a disk: All blocks belonging to the file system are logically divided into : 1) Boot block: (Master Boot Record): numbered 0, It represents the beginning of the file system. It contains a program called “Boost Strap Loader”. This program is executed during ‘booting’ process to load the file system into the main memory. 2) Super block:It is the second block of every file system and is numbered 1. It describes the entire file system and its state. It is used to control the allocation of blocks. It contents of super block are: a) The size of file and status of file name b) Details of free blocks and their addresses. A file occupies a minimum of 1 Block, even it has one character in it. A block size can be of 512 bytes or multiple of 512 bytes. c) Size of inode section of the file system. d) It specifies “how large the file system is, how many maximum files it can accommodate, how many more files it can accommodate, how many more files can create etc.” 3) Inode block: It contains most information regarding the files. Every file will have an entry in this area, identified by a 64-bit structure referred to as I-node. The information related to all the files (not the contents) is stored in an inode table on the disk. For each file, there is an inode entry in the table. Each entry is made up of 64 bytes in the table. These details are: 1) Owner of the file 2) Group to which the owner belongs 3) Type of a file 4) File access permission 5) Date and time of last access 6) Date and time of last modifications 7) No of links to the file 8) Size of the file 9) Address of blocks where the file is physically present. 4) Data blocks: Contains the actual file contents. An allocated block can belong to only one file in the file system. This block can’t be used for storing any other file’s contents unless the file which it originally belonged is deleted. 5.3 Types of files: A file in a UNIX system having the following types: Regular file / Ordinary file (-) Directory file (d) Device or Special file (c or b) Hidden files or Dot files (.) FIFO file (f) Link file (l) Socket file (s) Ordinary files: An ordinary file is a file on the system that contains data, text, or program instructions. Used to store your information, such as some text you have written or an image you have drawn. This is the type of file that you usually work with. Directories: Directories store both special and ordinary files. UNIX directories are equivalent to folders. A directory file contains an entry for every file and subdirectory that it houses. If you have 10 files in a directory, there will be 10 entries in the directory. Each entry has two components. i) The Filename ii) A unique identification number for the file or directory (called the inode number) IOS1 course: Dr D. Boukhlouf 4 Chapter II: Unix OS & File management Device or Special Files: Some special files provide access to hardware such as hard drives, CD-ROM drives, modems, and Ethernet adapters. Other special files are similar to aliases or shortcuts and enable you to access a single file using different names. On UNIX systems there are two types of special files for each device, character special files and block special files. Character special file: A file related to I/O and used to model serial I/O devices like Terminals ,Printers & Networks. Block Special file: A file related to memory and used to model devices like Disk drives and Magnetic tapes. Hidden files or Dot files : A dot(.) character also used to construct a file system. Any file name beginning with a.(dot) character is called a hidden file a dot file. These are used to store some specific information regarding configuration of system. FIFO file : used for making communication between two or more application programs. 5.4 The UNIX File Attributes: These are, 1) File type - specifies what type of file it is. 2) Access permission - the file access permission for owner, group and others. 3) Hard link count - number of hard link of the file 4) UID - The file owner/ User ID. 5) GID -The file Group ID. 6) File size - The file size in bytes. 7) I-node no - The system I-node (Information Node) number of the file. 8) File system id - The file system id where the file is stored. 9) Last access time - the time, the file was last accessed. 10) Last modified time - the time, the file was last modified. 11) Last change time - the time, the file was last changed. 5.5 Filenames Filenames under Unix may contain any character other than / ("slash") which is reserved as the directory separator. Typically, filenames only use alphabetic and numeric characters, underscores, hyphens and dots, as other characters (such as dollar signs, spaces, etc) have a special meaning to the shell and can be annoying to work with. Unix supports long filenames, usually up to 255 characters in length (this varies from system to system). You do not need to give Unix files an extension like.doc or.exe - the operating system can figure out what type a file is by other means. Filenames which begin with a dot are hidden and do not appear in directory listings unless you specifically ask for them. Multiple filenames can be specified using special pattern-matching characters. The rules are: '?' matches any single character in that position in the filename. '*' matches zero or more characters in the filename. A '*' on its own will match all files. '*.*' matches all files with containing a '.'. ('[' and ']') match any filename that has one of those characters in that position. ("{" and "}") A list of comma separated strings enclosed in curly braces expanded as a Cartesian product with the surrounding characters. For example: 1. ??? matches all three-character filenames. 2. ?ell? matches any five-character filenames with 'ell' in the middle. 3. he* matches any filename beginning with 'he'. 4. [m-z]*[a-l] matches any filename that begins with a letter from 'm' to 'z' and ends in a letter from 'a' to 'l'. 5. {/usr,}{/bin,/lib}/file expands to /usr/bin/file /usr/lib/file /bin/file and /lib/file. IOS1 course: Dr D. Boukhlouf 5 Chapter II: Unix OS & File management Note that the UNIX shell performs these expansions (including any filename matching) on a command's arguments before the command is executed. 5.6 The Hierarchical File Structure Unix systems have a tree-like file structure, similar to DOS or Windows systems (which were, in a roundabout way, based on Unix). The base of this tree is the "root directory" which is referred to as / ("slash"). Other directories branch out of the root directory, and each level of branching is separated by a slash, in a similar way to how DOS/Windows directories are separated by a backslash (\). Typically, the following directories can be found under the root directory of a Unix system: 5.7 Directory and file handling commands Command Description ls Lists files in current directory cd Change directory mkdir Make a directory rmdir Remove directory (must be empty) cp Copy file from a source to a destination rm Remove or delete file mv Move or rename files more Look at file, one page at a time IOS1 course: Dr D. Boukhlouf 6 Chapter II: Unix OS & File management Man or help Online manual (help) about command who Lists who is logged on your machine history Lists commands you've done recently cal Print calendar date Print out current date cat for creating and displaying short files. chmod change permissions echo echo argument. grep search file. head display first part of file tail display last part of file 5.8 File permissions To see more detailed information, use the -l option (long listing), which can be combined with the -a option as follows: $ ls -a -l (or, equivalently,) $ ls -al Each line of the output looks like this: where: o type is a single character which is either 'd' (directory), '-' (ordinary file), 'l' (symbolic link), 'b' (block-oriented device) or 'c' (character-oriented device). o permissions is a set of characters describing access rights. There are 9 permission characters, describing 3 access types given to 3 user categories. The three access types are read ('r'), write ('w') and execute ('x'), and the three users categories are the user who owns the file, users in the group that the file belongs to and other users (the general public). An 'r', 'w' or 'x' character means the corresponding permission is present; a '-' means it is absent. o links refers to the number of file system links pointing to the file/directory. o owner is usually the user who created the file or directory. o group denotes a collection of users who are allowed to access the file according to the group access rights specified in the permissions field. o size is the length of a file, or the number of bytes used by the operating system to store the list of files in a directory. o date is the date when the file or directory was last modified (written to). The -u option display the time when the file was last accessed (read). o name is the name of the file or directory. 5.8.1 File Ownership Every file and directory on Unix/Linux system is assigned 3 types of owner, given below. IOS1 course: Dr D. Boukhlouf 7 Chapter II: Unix OS & File management User A user is the owner of the file. By default, the person who created a file becomes its owner. Hence, a user is also sometimes called an owner. Group A user- group can contain multiple users. All users belonging to a group will have the same Linux group permissions access to the file. Suppose you have a project where a number of people require access to a file. Instead of manually assigning permissions to each user, you could add all users to a group, and assign group permission to file such that only this group members and no one else can read or modify the files. Other Any other user who has access to a file. This person has neither created the file, nor he belongs to a usergroup who could own the file. Practically, it means everybody else. Hence, when you set the permission for others, it is also referred as set permissions for the world. 5.8.2 Users, groups and permissions Every file and directory in UNIX/Linux system has following 3 permissions( defined for all the 3 owners discussed above. read (r) reading, opening, viewing, and copying the file is allowed. write (w) writing, changing, deleting, and saving the file is allowed. execute (x) executing and invoking the file is allowed. This is required for directories to allow searching and access. Abbreviations: r = read permission u= User w = write permission g=Group x = execute permission o=Other – = no permission a=All 5.8.3 Changing a file’s permissions To change file and directory permissions, use the command chmod (change mode). The owner of a file can change the permissions for user (u), group (g), or others (o) by adding (+) or subtracting (-) the read, write, and execute permissions. For example, to add permission for everyone to read a file in the current directory named myfile, at the Unix prompt, enter: chmod a+r myfile The a stands for "all", the + for "add", and the r for "read". For example, to remove read and write permission for group and other users (leaving only yourself with read and write permission) chmod go-rw myfile 5.8.4 Symbolic and Absolute method The first and probably easiest way is the relative (or symbolic) method, which lets you specify permissions IOS1 course: Dr D. Boukhlouf 8 Chapter II: Unix OS & File management with single letter abbreviations (r,w,x,-). The other way to use the chmod command is the absolute form, in which you specify a set of three numbers that together determine all the access classes and types. Rather than being able to change only particular attributes, you must specify the entire state of the file's permissions. The three numbers are specified in the order: user (or owner), group, and other. Each number is the sum of values that specify read, write, and execute access: For file myfile, to grant read, write, and execute permissions to yourself (4+2+1=7), read and execute permissions to users in your group (4+0+1=5), and only execute permission to others (0+0+1=1), you would use: chmod 751 myfile To grant read, write, and execute permissions on the current directory to yourself only, you would use: chmod 700 5.9 Editing in Unix system The vi editor The most common editor on Unix systems is vi. Although its commands and syntax are quite difficult to learn, it is a very powerful text editor which will allow you to write or program very efficiently. Also, the fact that vi is often the only editor on many Unix systems means it is essential to know at least the basics. Syntax: vi filename To enter "insert mode", type i. This allows you to enter text. Movement commands IOS1 course: Dr D. Boukhlouf 9 Chapter II: Unix OS & File management Simple text editing commands Saving and quitting 5.10 Variants of vi There are variants of vi such as vim, nvi and elvis, all of which provide additional functionality on top of the basic vi commands. Other editors : emacs pico joe jed Running % emacs % pico -w filename %joe filename % jed using Complica Cursor keys should work to Cursor keys to Defaults to the move the cursor. move the cursor. emacs emulation ted; Type to insert mode. Type to insert text under the Cursor keys to move text under the cursor. the cursor. cursor. The menu bar has ^X commands CTRL-K then S Type to insert text listed. This means hold down under the cursor. to save. CTRL-X then S CTRL and press the letter involved, eg CTRL-Wto search to save. for text. CTRL-O to save. Exiting CTRL-X Use CTRL-X from the main CTRL-C to exit CTRL-X then without save. CTRL-C to exit. then menu CTRL-K then X CTRL-C. to save and exit. IOS1 course: Dr D. Boukhlouf 10