© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63. By Narinder Kaur, Asstt. Prof.
Unit 2
Resource Management in Linux
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63. By Narinder Kaur, Asstt. Prof.
This unit covers
• File and Directory management• Process management• IPC (Interprocess Communication)• Memory management
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63. By Narinder Kaur, Asstt. Prof.
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
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63. By Narinder Kaur, Asstt. Prof.
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.
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63. By Narinder Kaur, Asstt. Prof.
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
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63. By Narinder Kaur, Asstt. Prof.
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
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63. By Narinder Kaur, Asstt. Prof.
Symbolic Codes: Examples
• chmod u+x filename• chmod u+x, g+w filename• chmod ug+x filename• chmod a-w filename• chmod u=rw filename
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
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63. By Narinder Kaur, Asstt. Prof.
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)
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63. By Narinder Kaur, Asstt. Prof.
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.
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63. By Narinder Kaur, Asstt. Prof.
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
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63. By Narinder Kaur, Asstt. Prof.
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>)
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63. By Narinder Kaur, Asstt. Prof.
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>)
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63. By Narinder Kaur, Asstt. Prof.
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.
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63. By Narinder Kaur, Asstt. Prof.
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.
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63. By Narinder Kaur, Asstt. Prof.
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>).
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63. By Narinder Kaur, Asstt. Prof.
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
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63. By Narinder Kaur, Asstt. Prof.
Special files1. 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 ^l2. 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 ^s3. 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 ^p4. 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 ^c5. Block devices These files are hardware files most of them are present in /dev
To view such files use : Ls -l | grep ^b
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63. By Narinder Kaur, Asstt. Prof.
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.
Process Management
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63. By Narinder Kaur, Asstt. Prof.
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)
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63. By Narinder Kaur, Asstt. Prof.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
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63. By Narinder Kaur, Asstt. Prof.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.
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63. By Narinder Kaur, Asstt. Prof.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.
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63. By Narinder Kaur, Asstt. Prof.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.
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63. By Narinder Kaur, Asstt. Prof.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.
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63. By Narinder Kaur, Asstt. Prof.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.
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63. By Narinder Kaur, Asstt. Prof.
ready
stopped
suspended
executing zombie
creationsignal
signal
scheduling
input / outputend ofinput / output
termination
Process States
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63. By Narinder Kaur, Asstt. Prof.
There are 3 different phases in the creation of a process that uses three important system calls:1. 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.
2. 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
3. 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.
Process Creation Phases
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63. By Narinder Kaur, Asstt. Prof.
An example of fork#include<stdio.h>#include<type.h>int main(){pid_t pid;
pid=fork();if(pid>0)
outputprintf(“Parent pid %d, Child pid %d”,getpid(), pid); parent pid : 1555else if (pid == 0)
child pid : 1556printf(“child pid %d, Parent pid %d”, getpid(), getppid()); child pid : 1556else
parent pid : 1555printf(“fork error”);}
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63. By Narinder Kaur, Asstt. Prof.
• 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 : psps –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)
Process Management Commands
$ ps
PID TTY TIME CMD 3511 pts/1 00:00:00 bash 3514 pts/1 00:00:00 ps
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63. By Narinder Kaur, Asstt. Prof.
• 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
Top command
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63. By Narinder Kaur, Asstt. Prof.
1. Interactive processes : that are associated with terminals.2. 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.
3. 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.
Types of Process
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63. By Narinder Kaur, Asstt. Prof.
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
1. Pipes2. FIFO3. Signals4. System V IPC
Message QueuesSemaphoresShared Memory
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63. By Narinder Kaur, Asstt. Prof.
1. Shared memory permits processes to communicate by simply reading and writing to a specified memory location.
2. Mapped memory is similar to shared memory, except that it is associated with a file in the filesystem.
3. Pipes permit sequential communication from one process to a related process.
4. FIFOs are similar to pipes, except that unrelated processes can communicate because the pipe is given a name in the file system.
5. Sockets support communication between unrelated processes even on different computers.
Types of Interprocess Communication:
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63. By Narinder Kaur, Asstt. Prof.
PipePipe: For communication between related processes on a system
P1 P2
UNIX/Linux System
Pipe
Write EndRead End
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63. By Narinder Kaur, Asstt. Prof.
• 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
IPC: Pipes
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63. By Narinder Kaur, Asstt. Prof.
Example : Pipe
P Pfork
parent child
Write end
Read end Pipe
#include <unistd.h> int pipe (int filedes[2]);
pipe System Call
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63. By Narinder Kaur, Asstt. Prof.
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 purposefd[1] : is used for writing purpose.
Creating Pipes
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63. By Narinder Kaur, Asstt. Prof.
Pipes
System calls associated with pipes are:
1. pipe() – to create a pipe.2. Read() – to read from pipe.3. Write() – to write into the pipe.
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63. By Narinder Kaur, Asstt. Prof.
FIFONamed pipe (FIFO): For communication between unrelated processes on a system.
P1 P2
UNIX/Linux System
FIFO
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63. By Narinder Kaur, Asstt. Prof.
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/fifoprw-rw-rw- 1 samuel users 0 Jan 16 14:04 /tmp/fifo
There are 3 system calls associated with FIFOs:1. open()2. read()3. write()
First-in, First-out (FIFO)
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63. By Narinder Kaur, Asstt. Prof.
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.
First-in, First-out (FIFO)
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63. By Narinder Kaur, Asstt. Prof.
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().
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63. By Narinder Kaur, Asstt. Prof.
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
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63. By Narinder Kaur, Asstt. Prof.
• 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.
Signals
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63. By Narinder Kaur, Asstt. Prof.
• 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
Signals
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63. By Narinder Kaur, Asstt. Prof.
• 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.
Signals
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63. By Narinder Kaur, Asstt. Prof.
• 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()
How to send Signals
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63. By Narinder Kaur, Asstt. Prof.
• System calls associated with message queues are :
1. msgsnd(int msqid, void *msqp, int msgsz, int msgflag);
2. Msgrcv(int msqid, void *msqp, long mtype, int msgflag);
Message Queues
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63. By Narinder Kaur, Asstt. Prof.
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).
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63. By Narinder Kaur, Asstt. Prof.
• 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.
Memory Management in Linux
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63. By Narinder Kaur, Asstt. Prof.
Memory Management in Linux
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63. By Narinder Kaur, Asstt. Prof.
• 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
Memory Management in Linux
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63. By Narinder Kaur, Asstt. Prof.
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.
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63. By Narinder Kaur, Asstt. Prof.
Advantages– Fast and simple– Avoid external fragmentation
Disadvantages- Internal fragmentation
The Buddy Allocator
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63. By Narinder Kaur, Asstt. Prof.
• 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
The Zone Allocator
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63. By Narinder Kaur, Asstt. Prof.
• 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.
The Zone Allocator
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63. By Narinder Kaur, Asstt. Prof.
• System calls :
– Frame_alloc()– Frame_free()
The Zone Allocator
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63. By Narinder Kaur, Asstt. Prof.
• 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.
The Slab Allocator
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63. By Narinder Kaur, Asstt. Prof.
• 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.
The Slab Allocator
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63. By Narinder Kaur, Asstt. Prof.64
Memory Allocation
Library Calls System Calls
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63. By Narinder Kaur, Asstt. Prof.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.
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63. By Narinder Kaur, Asstt. Prof.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.
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63. By Narinder Kaur, Asstt. Prof.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
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63. By Narinder Kaur, Asstt. Prof.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.
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63. By Narinder Kaur, Asstt. Prof.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().
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63. By Narinder Kaur, Asstt. Prof.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().
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63. By Narinder Kaur, Asstt. Prof.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.
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63. By Narinder Kaur, Asstt. Prof.72
System Calls
brk() sbrk()
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63. By Narinder Kaur, Asstt. Prof.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.
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63. By Narinder Kaur, Asstt. Prof.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.
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63. By Narinder Kaur, Asstt. Prof.
Short QuestionsQ.1: Write some system calls related to process managementQ.2: Write some system calls related to Memory Management and
also give their useQ.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?
© Bharati Vidyapeeth’s Institute of Computer Applications and Management, New Delhi-63. By Narinder Kaur, Asstt. Prof.
Long Questions• Write a Program to fork a process and comunicate
using pipes.