Presentation is loading. Please wait.

Presentation is loading. Please wait.

Unit 2 Resource Management in Linux

Similar presentations

Presentation on theme: "Unit 2 Resource Management in Linux"— Presentation transcript:

1 Unit 2 Resource 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: 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 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 chmod u+x filename chmod u+x, g+w filename chmod ug+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) 8 8

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 : mygroup empdata chgrp Change group. chgrp <group1> <filename> : Change group of a file to group1. 9 9

10 Linux File Management and Viewing
find Find 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 10 10

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>) rmdir Remove 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>) whereis Locate the binary and man page files for a command. (whereis <program/command>) which Show full path of commands where given commands reside. (which <command>) 11 11

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>) 12 12

13 Linux File Management and Viewing
File compression, backing up and restoring compress Compress 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. 13 13

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. 14 14

15 Linux File Management and Viewing
grep List all files with the specified expression. (grep pattern <filename/directorypath>) Ex: ls –l |grep sidbi : 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. uniq Remove duplicate lines from a sorted file. wc Count lines, words, characters in a file. (wc –c/w/l <filename>). 15 15

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 To view such files use : Ls -l | grep ^l
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
Code Often referred to as the text segment. this is the area in which the executable instructions reside. Only one copy of the instructions of the same program resides in memory at any time. (This is transparent to the running programs). The portion of the executable file containing the text segment is the text section. It is usually swapped out of disk when memory gets too tight.

22 Linux/Unix Address Space
Initialized data Statically allocated and global data that are initialized with nonzero values live in the data segment. The portion of the executable file containing the data segment is the data section.

23 Linux/Unix Address Space
Zero-initialized data Global and statically allocated data that are initialized to zero by default are kept in what is called the BSS area of the process. When running, the BSS data are placed in the data segment. The format of a Linux/Unix executable is such that only variables that are initialized to a nonzero value occupy space in the disk. Thus, a large array declared 'static char somebuf[2048];', which is automatically zero-filled, does not take up 2 KB worth of disk space.

24 Linux/Unix Address Space
Heap The heap is where dynamic memory (obtained by malloc() and friends) comes from. As memory is allocated on the heap, the process's address space grows. It is possible to give memory back to the system and shrink a process's address space. It is typical for the heap to "grow upward." This means that successive items that are added to the heap are added at addresses that are numerically greater than previous items.

25 Linux/Unix Address Space
Stack The stack segment is where local variables are allocated. It is the use of a stack for function parameters and return values that makes it convenient to write recursive functions (functions that call themselves). Variables stored on the stack "disappear" when the function containing them returns. The space on the stack is reused for subsequent function calls. On most modern architectures, the stack "grows downward," meaning that items deeper in the call chain are at numerically lower addresses.

26 Process States signal creation signal termination scheduling
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_t pid; 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. ps Show 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 pts/1 00:00:00 bash pts/1 00:00:00 ps

30 Top command The top 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 and cpu. 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 - [d delay] [p pid] [q] [i] [n iter] [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 Pipe Pipe: For communication between related processes on a system P1
UNIX/Linux System Pipe 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 Example : Pipe #include <unistd.h> int pipe (int filedes[2]);
fork parent child Write end Read end Pipe #include <unistd.h> int pipe (int filedes[2]); 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: int pipe 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 FIFO Named pipe (FIFO): For communication between unrelated processes on a system. P1 P2 UNIX/Linux System FIFO

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+D in 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 the mkfifo() 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 UNIX/Linux FIFOs 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

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 (int pid, 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 System V IPC System V IPC is the label used to refer to three different mechanisms for interprocess communication: 1. Message queues can be used to pass messages between processes. Message queues are somewhat like pipes, but differ in two important respects. First, message boundaries are preserved, so that readers and writers communicate in units of messages, rather than via an undelimited byte stream. Second, each message includes an integer type field, and it is possible to select messages by type, rather than reading them in the order in which they were written.

49 System V IPC .. 2. Semaphores permit multiple processes to synchronize their actions. A semaphore is a kernel-maintained integer value that is visible to all processes that have the necessary permissions. A process indicates to its peers that it is performing some action by making an appropriate modification to the value of the semaphore. 3. Shared memory enables multiple processes to share the same region (called a segment) of memory (i.e., the same page frames are mapped into the virtual memory of multiple processes). Since access to user-space memory is a fast operation, shared memory is one of the quickest methods of IPC: once one process has updated the shared memory, the change is immediately visible to other processes sharing the same.

50 Message Queues Communication via message queues is message-oriented; that is, the reader receives whole messages, as written by the writer. It is not possible to read part of a message, leaving the remainder in the queue, or to read multiple messages at a time. Linux maintains a list of message queues, the msgque vector; each element of which points to a msgid_ds data structure which fully describes the message queue. When message queues are created a new msgid_ds is allocated and inserted into the vector.

51 Message Queues Each msgid_ds contains : ipc_perm
Pointers to messages (first and the last message) Queue creation time Queue modification time Maximum no. of bytes on the queue Pid of last msg sent Pid of last msg received Two wait queues Reader process that wants to read from the queue Writer process that wants to write something into the queue

52 Message Queues System calls associated with message queues are :
msgsnd(int msqid, void *msqp, int msgsz, int msgflag); Msgrcv(int msqid, void *msqp, long mtype, int msgflag);

53 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).

54 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.

55 Memory Management in Linux

56 Memory Management in Linux
A basic unit of memory under Linux is page, a non-overlapping region of contiguous memory. All available physical memory is organized into pages towards the end of the kernel’s boot process. Size of page depends on processor architecture. The traditional page size used by Linux is 4096 bytes. In Linux, subsystem of allocating and releasing memory is split into three layers. These layers are: The Slab Allocator The Zone Allocator The Buddy Allocator

57 The Buddy Allocator The Buddy Allocator splits the memory into pairs of 2^n pages and stores information about the free blocks of pages in the array of lists. Each list consists of free physically contiguous blocks of 2i memory pages, where i is the list number. This is called the order of the block. When the allocator is asked to allocate a block of size 2^i, it first tries to satisfy the request from the list with index i. If the request cannot be satisfied, the buddy allocator will try to allocate and split a larger block from the list with index i+1. On the other hand, every two blocks of memory of the same size, which have common border, may be united into the single block of the bigger size. Such neighboring blocks are called Buddies. When allocation is returned to the Buddy allocator, it checks if buddy of the allocation is free, and if it is so, Buddy allocator unites them into the bigger block. This operation is repeated until no more block buddies are found.

58 The Buddy Allocator Advantages Fast and simple
Avoid external fragmentation Disadvantages - Internal fragmentation

59 The Zone Allocator Different ranges of physical pages may have different properties, for the purpose of the kernel. For handling such situations in a hardware, independent way the Zone Allocator was created. The Zone Allocator is used to allocate pages in the specified zone. The Linux kernel supports 3 different types of memory zones: DMA NORMAL HIGHMEM

60 The Zone Allocator Each zone contains information about
the allocated and unallocated physical memory Base address of the zone Number of frames contained in it. Array of frame structures. Each zone is equipped with a buddy system that facilitates effective allocation of power of two sized blocks of frame.

61 The Zone Allocator System calls : Frame_alloc() Frame_free()

62 The Slab Allocator Since we often need to allocate objects that have size less than the size of a page, we need something to deal with the pages and allocate lesser chunks of memory. We know the sizes of the most objects that are often allocated in the kernel space , so we can create allocator that will receive pages of memory from the zone allocator and allocates small objects in these memory pages. This subsystem is named the Slab Allocator (An Object-Caching Kernel memory Allocator). This means that the constructor of the object is used only for newly allocated slabs and one should initialize object before releasing it to the slab allocator.

63 The Slab Allocator The slab alocator organizes memory in caches, one cache for each object type. Each cache consists of many slabs, and each slab contains multiple initialized objects. Each object begins its life in slab.

64 Memory Allocation Library Calls System Calls

65 Library Calls malloc() calloc() realloc() free()
Dynamic memory is allocated by either the malloc() or calloc() functions. These functions return pointers to the allocated memory.

66 Library Calls Once you have a block of memory of a certain initial size, you can change its size with the realloc() function. Dynamic memory is released with the free() function.

67 Library Calls void *calloc(size_t nmemb, size_t size)
Allocate and zero fill void *malloc(size_t size) Allocate raw memory void free(void *ptr) Release memory void *realloc(void *ptr, size_t size) Change size of existing allocation

68 Initially Allocating Memory
void *malloc(size_t size) Memory is allocated initially with malloc(). The value passed in is the total number of bytes requested. The return value is a pointer to the newly allocated memory or NULL if memory could not be allocated. The memory returned by malloc() is not initialized. It can contain any random garbage. You should immediately initialize the memory with valid data or at least with zeros.

69 Releasing Memory void free(void *ptr)
When you're done using the memory, you "give it back" by using the free() function. The single argument is a pointer previously obtained from one of the other allocation routines. It is safe (although useless) to pass a null pointer to free().

70 Changing Size void *realloc(void *ptr, size_t size)
It is possible to change the size of a dynamically allocated memory area. Although it's possible to shrink a block of memory, more typically, the block is grown. Changing the size is handled with realloc().

71 Allocating and Zero-filling
void *calloc(size_t nmemb, size_t size) The calloc() function is a straightforward wrapper around malloc(). Its primary advantage is that it zeros the dynamically allocated memory. It also performs the size calculation for you by taking as parameters the number of items and the size of each.

72 System Calls brk() sbrk()

73 System Calls int brk(void *end_data_segment)
The brk() system call actually changes the process's address space. The address is a pointer representing the end of the data segment. Its argument is an absolute logical address representing the new end of the address space. It returns 0 on success or -1 on failure.

74 System Calls void *sbrk(ptrdiff_t increment)
The sbrk() function is easier to use. Its argument is the increment in bytes by which to change the address space. It also changes the location of the program break, which defines the end of the process’s data segment.

75 Short Questions Q.1: Write some system calls related to process management Q.2: Write some system calls related to Memory Management and also give their use Q.3: Write some system calls related to File and directory management. Q.4: What is message queue? how we can create it. Q.5: Explain different type of Inter process communication? Q.6: What is System V IPC?

76 Long Questions Write a Program to fork a process and comunicate using pipes.

Download ppt "Unit 2 Resource Management in Linux"

Similar presentations

Ads by Google