1 / 72

Unit 2 Resource Management in Linux

Unit 2 Resource Management in Linux. This unit covers File and Directory management Process management IPC ( Interprocess Communication) Memory management. File Permissions. Files are owned by both a user and a group Each file has 3 sets of permissions for

mireya
Download Presentation

Unit 2 Resource Management in Linux

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Unit 2Resource Management in Linux

  2. This unit covers • File and Directory management • Process management • IPC (Interprocess Communication) • Memory management

  3. File Permissions • Files are owned by both a user and a group • Each file has 3 sets of permissions for • Permissions for the user who owns it (user permissions) • Permissions for the group that owns it (group permissions) • Permissions for everyone else (‘other’ or ‘world’ permissions) • There are 3 types of permissions • Read (r) -- controls ability to read a file • Write (w) -- controls ability to write or change a file • Executable (x) -- controls whether or not a file can be executed as a program

  4. Permissions on directories • Directories can also be thought of as a file that lists all the files it contains. • They also belong to a user and a group • They have the same 3 sets of permissions • Interpretation of rwx for directories • Read -- You can read the list of files • Write -- You can change the list of files • Executable -- You can use the directory list to let the operating system “find” the file. This means, you have access to the file. All directories generally have execute permission.

  5. Assigning Permissions • Permissions on files and directories can be assigned in two possible ways: • Numeric • Symbolic • Numeric: • Use numeric codes for permissions: • Symbolic • Use textual symbols

  6. Numeric Codes • chmod Change the file permissions as chmod <set of permissions> <filename> • Add the required set of numerals to get an octal number that represents permissions for a particular user category. • E.g. chmod 755 empdata 755 means 111 101 101 • The three octal number represent permissions for user, group and others respectively

  7. Symbolic Codes: Examples u - User who owns the file.g - Group that owns the file.o - Other.a - All.r - Read the file.w - Write or edit the file.x - Execute or run the file as a program • chmodu+x filename • chmodu+x, g+w filename • chmodug+x filename • chmod a-w filename • chmod u=rw filename

  8. Linux File Management and Viewing • Ex: chmod 751 myfile • change the file permissions to rwx for owner, rx for group and x for others • Ex: chmod go=+r myfile • Add read permission for the group and others (character meanings u-user, g-group, o-other, + add permission,-remove, r-read,w-write,x-exe)

  9. Linux File Management and Viewing • chown Change owner. • chown<owner1> <filename> : Change ownership of a file to owner1. • Eg : chown smith empdata • Chown can also be used to change group of a file as • chown : mygroupempdata • chgrp Change group. • chgrp<group1> <filename> : Change group of a file to group1.

  10. Linux File Management and Viewing • findFind files (find <start directory> -name <file name> -print Ex: find /home –name readme -print (Search for readme starting at home and output full path.)“/home" = Search starting at the home directory and proceed through all its subdirectories "-name readme" = Search for a file named readme "-print" = Output the full path to that file • locate File locating program that uses the slocate database. Ex: locate –u to create the database, locate <file/directory> to find file/directory

  11. Linux File Management and Viewing • pwd Print or list the present working directory with full path. • rm Delete files (Remove files). (rm –rf <directory/file>) • rmdirRemove a directory. The directory must be empty. (rmdir <directory>) • touch Change file timestamps to the current time. Make the file if it doesn't exist. (touch <filename>) • whereisLocate the binary and man page files for a command. (whereis <program/command>) • which Show full path of commands where given commands reside. (which <command>)

  12. Linux File Management and Viewing • File viewing and editing • emacs Full screen editor. • pico Simple text editor. • vi Editor with a command mode and text mode. Starts in command mode. • gedit GUI Text Editor • tail Look at the last 10 lines of a file. • Ex: tail –f <filename> , • Ex: tail -100 <filename> • head Look at the first 10 lines of a file. (head <filename>)

  13. Linux File Management and Viewing • File compression, backing up and restoring • compressCompress data. • uncompress Expand data. • cpio Can store files on tapes. to/from archives. • gzip - zip a file to a gz file. • gunzip - unzip a gz file. • tar Archives files and directories. Can store files and directories on tapes. • Ex: tar -zcvf <destination> <files/directories> - Archive copy groups of files. tar –zxvf <compressed file> to uncompress • zip – Compresses a file to a .zip file. • unzip – Uncompresses a file with .zip extension.

  14. Linux File Management and Viewing • cat View a file Ex: cat filename • cmp Compare two files. • cut Remove sections from each line of files. • diff Show the differences between files. Ex: diff file1 file2 : Find differences between file1 & file2. • echo Display a line of text.

  15. Linux File Management and Viewing • grep List all files with the specified expression. (grep pattern <filename/directorypath>)Ex: ls –l |grepsidbi : List all lines with a sidbi in them. • Ex: grep " R " : Search for R with a space on each side • sleep Delay for a specified amount of time. • sort Sort a file alphabetically. • uniqRemove duplicate lines from a sorted file. • wc Count lines, words, characters in a file. (wc –c/w/l <filename>).

  16. Different file types • a) Ordinary files: All files created by user are called ordinary/regular files. It includes data file, program file, object file, image file, compressed file and executable file. • b) Directory files: These type of files contains regular files/folders/special files stored on a physical device. To view such files use : • Ls -l | grep ^d • c) Special files : There are 5 types of files under this category

  17. Special files 1. Symbolic Link : A symbolic link is a reference to another file ( a shortcut to any file ). Symbol : l                         Color : Cyan To view such files use : Ls -l | grep ^l 2. Socket : A socket file is used to pass information between applications for communication purpose Symbol : s                         Color : Purple To view such files use : Ls -l | grep ^s 3. Named Pipe : A special type of file that is used for interprocess communication without using network socket semantics. Symbol : p                       Color : Red To view such files use : Ls -l | grep ^p 4. Character devices provide only a serial stream of input or output.Your terminals are clasic example for this type of files. To view such files use : Ls -l | grep ^c 5. Block devices These files are hardware files most of them are present in /dev To view such files use : Ls -l | grep ^b

  18. Process Management What is a Process? • A program in execution. • A process is associated with program's instructions and data, program counter and all CPU's registers, process stacks containing temporary data. • Each individual process runs in its own virtual address space and is not capable of interacting with another process except through secure, kernel managed mechanisms. • Each process has some information, like process ID, owner, priority, etc.

  19. Linux Process • Each process is represented by a task_struct data structure, containing: • Process State • Scheduling Information • Identifiers (process id, user id etc) • Times and Timers (The kernel keeps track of a processes creation time as well as the CPU time that it consumes during its lifetime) • Inter-Process Communication • File system (Current directory and a list of open files) • Virtual memory • Processor Specific Context (processor’s register’s and stacks)

  20. Linux/Unix Address Space • A process has five conceptually different areas of memory allocated to it: • Code • Initialized data • Zero-initialized data • Heap • Stack

  21. Linux/Unix Address Space

  22. Linux/Unix Address Space

  23. Linux/Unix Address Space

  24. Linux/Unix Address Space

  25. Linux/Unix Address Space

  26. Process States stopped signal creation signal termination executing zombie ready scheduling input / output suspended end of input / output

  27. Process Creation Phases There are 3 different phases in the creation of a process that uses three important system calls: • Fork : Creates a process by creating a copy of the existing process. The new process has the different PID and the process that created it becomes its parent. • Exec :After the forking process, the address space of the child process is overwritten with the new process data. This is done through an exec call to the system • Wait :Blocks calling process until the child process terminates. If child process has already terminated, the wait() call returns immediately. It picks up the exit status of the child process.

  28. An example of fork #include<stdio.h> #include<type.h> int main() { pid_tpid; pid=fork(); if(pid>0) output printf(“Parent pid %d, Child pid %d”,getpid(), pid); parent pid : 1555 else if (pid == 0) child pid : 1556 printf(“child pid %d, Parent pid %d”, getpid(), getppid()); child pid : 1556 else parent pid : 1555 printf(“fork error”); }

  29. Process Management Commands • kill - Sends specified signal to specified process. This process is specified by process ID. • Killall - Stop a program. The program is specified by command name. • psShow process status • Eg : ps ps –u roger (process run by user ‘roger’) ps –e (list every process that is running) ps -G gpname (process run by specific groups) ps T (processes on current terminal) Ps –A (select all processes) Ps –a (select processes on a terminal, but doesn’t display system processes) Ps –f (detailed listing that includes the ppid of every process) $ ps PID TTY TIME CMD 3511 pts/1 00:00:00 bash 3514 pts/1 00:00:00 ps

  30. Top command • Thetop program provides a dynamic real-time view of a running system. It can displaysystem summary information as well as a list of tasks currently being managed by the Linux kernel. • it shows information like tasks, memory andcpu. Press ‘q‘ to quit window. • It can sort the tasks by CPU usage, memory usage and runtime. • The display is updated every 5 seconds by default, but you can change that with the d command-line option • Switches used by top command are as follows: top-[ddelay] [ppid] [q] [i] [niter] [U user] • Field Descriptors are as follows: • PID The process ID of each task • .PPID The parent process ID each task. • UID The user ID of the task's owner. • USER The user name of the task's owner. • PRI The priority of the task. • Memory usage • Percentage of CPU time • Total number of processes

  31. Types of Process • Interactive processes : that are associated with terminals. • Automatic processes : Automatic or batch processes are not connected to a terminal. Rather, these are tasks that can be queued into a spooler area, where they wait to be executed on a FIFO (first-in, first-out) basis. • Daemons : Daemons are server processes that run continuously. Most of the time, they are initialized at system startup and then wait in the background until their service is required. After the system is booted, the network daemon just sits and waits until a client program, such as an FTP client, needs to connect.

  32. Interprocess Communication Mechanisms Processes communicate with each other and with the kernel to coordinate their activitiesThe Linux IPC facilities provide a method for multiple processes to communicate with one another. Linux supports a number of different IPC mechanisms such as Pipes FIFO Signals System V IPC Message Queues Semaphores Shared Memory

  33. Types of Interprocess Communication: Shared memory permits processes to communicate by simply reading and writing to a specified memory location. Mapped memory is similar to shared memory, except that it is associated with a file in the filesystem. Pipes permit sequential communication from one process to a related process. FIFOs are similar to pipes, except that unrelated processes can communicate because the pipe is given a name in the file system. Sockets support communication between unrelated processes even on different computers.

  34. P1 P2 Pipe UNIX/Linux System Pipe Pipe: For communication between related processes on a system Read End Write End

  35. IPC: Pipes • A pipe is a communication channel that permits unidirectional communication. • Data written to the “write end” of the pipe is read back from the “read end.” • Pipes are serial devices; the data is always read from the pipe in the same order it was written. • Typically, a pipe is used to communicate between two threads in a single process or between parent and child processes. • In a shell, the symbol | creates a pipe. • For example, this shell command causes the shell to produce two child processes, one for ls and one for less: • Eg 1: $ls|less • Eg 2: $ cat file1 | wc -l

  36. child parent fork P P Read end Write end Example : Pipe Pipe pipe System Call

  37. Creating Pipes To create a pipe, invoke the pipe command. Supply an integer array of size 2. The call to pipe stores the reading file descriptor in array position 0 and the writing file descriptor in position 1. For example, consider this code: intpipe fd[2]; if (pipe(fd)<0) printf(“Pipe failure”); switch(fork()) { case -1: printf(“fork error”); case 0 : close(fd[0]); //closes the read end of child … … default : close(fd[1]); //closes the write end of parent ….. } fd[0] : is used for reading purpose fd[1] : is used for writing purpose.

  38. Pipes System calls associated with pipes are: pipe() – to create a pipe. Read() – to read from pipe. Write() – to write into the pipe.

  39. P1 P2 FIFO UNIX/Linux System FIFO Named pipe (FIFO): For communication between unrelated processes on a system.

  40. First-in, First-out (FIFO) A first-in, first-out (FIFO) file is a pipe that has a name in the file system. Any process can open or close the FIFO; the processes on either end of the pipe need not be related to each other. FIFOs are also called named pipes. You can make a FIFO using the mkfifo command. Specify the path to the FIFO on the command line. For example, create a FIFO in /tmp/fifo by invoking this: % mkfifo/tmp/fifo % ls -l /tmp/fifo prw-rw-rw- 1 samuel users 0 Jan 16 14:04 /tmp/fifo There are 3 system calls associated with FIFOs: open() read() write()

  41. First-in, First-out (FIFO) read from the FIFO by invoking the following: % cat < /tmp/fifo write to the FIFO by invoking this: % cat > /tmp/fifo Then type in some lines of text. Each time you press Enter, the line of text is sent through the FIFO and appears in the first window. Close the FIFO by pressing Ctrl+Din the second window. Remove the FIFO with this line: % rm /tmp/fifo In pgms, unlink() system call is used to remove a fifo file.

  42. Creating a FIFO • Create a FIFO programmatically using themkfifo() function. • The first argument is the path at which to create the FIFO; the second parameter specifies the pipe’s owner, group, and world permissions. • Because a pipe must have a reader and a writer, the permissions must include both read and write permissions. • If the pipe cannot be created (for instance, if a file with that name already exists), mkfifo() returns –1. include <sys/types.h> and <sys/stat.h> if you call mkfifo().

  43. Two common uses of FIFOs • In client-server applications, FIFOs are used to pass data between a server process and client processes • Used by shell commands to pass data from one shell pipeline to another, without creating temporary files UNIX/Linux FIFOs

  44. Signals • Signals are software generated interrupts that are sent to a process when an event happens. • Examples of events are : program attempting to access a non-existent location in its virtual memory, a user requesting to kill a process or an error condition. • Signals are also used by shells to signal job control commands to their child processes. • Signals can also come directly from the OS (kernel) when a hardware event occurs such as bus error or an illegal instruction is encountered.

  45. Signals • There are a set of defined signals that the kernel can generate or that can be generated by other processes in the system. • You can list a system’s set of signals using kill command as : kill –l • Signals have no inherent priorities. If two signals are generated for a process at the same time then they may be presented to the process or handled in any ordered. • Each signal defined by the system falls into one of the five classes: • Hardware conditions • Software conditions • I/O conditions • Process Control • Resource Control

  46. Signals • Each signal has a default action which is one of the following: • The signal is discarded after being received. • The process is terminated after the signal is received. • A core file is written then the process is terminated. A process can choose just how it wants to handle the various signals: * Processes can block the signals or they can choose to handle themselves. * If kernel handles the signals then it performs the default actions. Linux implements signals using information stored in the task_struct for the process. The currently pending signals are kept in the signal field. It also handles the information about how each process handles signal. This is held in the array of sigaction data structure pointed by task_struct for each process.

  47. How to send Signals • The process may have specified its own signal handler. This is a routine which will be called whenever the signal is generated and the sigaction structure holds the address of this routine. • Signal handler returns the control to the instruction where the execution was interrupted. • A process can send signals only to other processes with same Uid or Gid in 2 ways: • Kill (intpid, int Sig) //it returns 0 on success and -1 on error • Raise (int sig) //sends the signal to executing program by actually using kill()

  48. Message Queues • System calls associated with message queues are : • msgsnd(intmsqid, void *msqp, intmsgsz, intmsgflag); • Msgrcv(intmsqid, void *msqp, long mtype, intmsgflag);

  49. Memory Management in Linux • The term “memory management” refers to the mechanisms implemented by an operating system to provide applications with memory-related services. These services include usage of virtual memory (utilizing of a hard disk or other non-RAM storage media to provide additional program memory), protected memory (exclusive access to a region of memory by a process), and shared memory (cooperative access to a region of memory by multiple processes).

  50. Memory Management in Linux • The system never allocates physical memory directly • Each process has a virtual view of memory. • A process thinks it is the only task running on the system • The virtual memory is then mapped to physical memory using page tables.

More Related