Unit 2 Resource Management in Linux
Unit 2 Resource Management in Linux
Unit 2 Resource Management in Linux
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. By Narinder Kaur, Asstt. Prof.
Bharati Vidyapeeths 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 Vidyapeeths 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 Vidyapeeths 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 Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. By Narinder Kaur, Asstt. Prof.
Numeric Codes
Add the required set of numerals to get an octal number that represents
permissions for a particular user category.
111
101
101
The three octal number represent permissions for user, group and others
respectively
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. By Narinder Kaur, Asstt. Prof.
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. By Narinder Kaur, Asstt. Prof.
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. By Narinder Kaur, Asstt. Prof.
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. By Narinder Kaur, Asstt. Prof.
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. By Narinder Kaur, Asstt. Prof.
Ex: grep " R " : Search for R with a space on each side
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. By Narinder Kaur, Asstt. Prof.
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. By Narinder Kaur, Asstt. Prof.
Special files
1. Symbolic Link : A symbolic link is a reference to another file ( a shortcut to any file ).
Symbol : l
To view such files use :
Color : Cyan
Ls -l | grep ^l
2. Socket : A socket file is used to pass information between applications for communication
purpose
Symbol : s
To view such files use :
Color : Purple
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
To view such files use :
Color : Red
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
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. By Narinder Kaur, Asstt. Prof.
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.
Bharati Vidyapeeths 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 (processors registers and
stacks)
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. By Narinder Kaur, Asstt. Prof.
Code
Initialized data
Zero-initialized data
Heap
Stack
20
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. By Narinder Kaur, Asstt. Prof.
21
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. By Narinder Kaur, Asstt. Prof.
22
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. By Narinder Kaur, Asstt. Prof.
24
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. By Narinder Kaur, Asstt. Prof.
Process States
creation
stopped
signal
signal
termination
ready
end of
input / output
scheduling
executing
zombie
input / output
suspended
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. By Narinder Kaur, Asstt. Prof.
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.
Bharati Vidyapeeths 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)
output
else if (pid == 0)
printf(child pid %d, Parent pid %d, getpid(), getppid()); child pid : 1556
else
printf(fork error);
}
Bharati Vidyapeeths 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.
Eg : ps
$ ps
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. By Narinder Kaur, Asstt. Prof.
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
Memory usage
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. By Narinder Kaur, Asstt. Prof.
Types of Process
1.
2.
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.
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. By Narinder Kaur, Asstt. Prof.
1.
2.
3.
4.
Pipes
FIFO
Signals
System V IPC
Message Queues
Semaphores
Shared Memory
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. By Narinder Kaur, Asstt. Prof.
2.
3.
4.
5.
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. By Narinder Kaur, Asstt. Prof.
Pipe
Pipe: For communication between related
processes on a system
P1
P2
Read End
Write End
Pipe
UNIX/Linux System
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. By Narinder Kaur, Asstt. Prof.
IPC: Pipes
Example : Pipe
child
parent
fork
P
Read
end
Pipe
Write end
#include <unistd.h>
int pipe (int filedes[2]);
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. By Narinder Kaur, Asstt. Prof.
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.
Bharati Vidyapeeths 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 Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. By Narinder Kaur, Asstt. Prof.
FIFO
Named pipe (FIFO): For communication between
unrelated processes on a system.
P1
P2
FIFO
UNIX/Linux System
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. By Narinder Kaur, Asstt. Prof.
/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:
1. open()
2. read()
3. write()
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. By Narinder Kaur, Asstt. Prof.
Bharati Vidyapeeths 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 pipes 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 Vidyapeeths 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 Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. By Narinder Kaur, Asstt. Prof.
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 nonexistent 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.
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. By Narinder Kaur, Asstt. Prof.
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 systems 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
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. By Narinder Kaur, Asstt. Prof.
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.
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. By Narinder Kaur, Asstt. Prof.
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. By Narinder Kaur, Asstt. Prof.
Message Queues
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);
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. By Narinder Kaur, Asstt. Prof.
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. By Narinder Kaur, Asstt. Prof.
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. By Narinder Kaur, Asstt. Prof.
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. By Narinder Kaur, Asstt. Prof.
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. By Narinder Kaur, Asstt. Prof.
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 Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. By Narinder Kaur, Asstt. Prof.
Bharati Vidyapeeths 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
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. By Narinder Kaur, Asstt. Prof.
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. By Narinder Kaur, Asstt. Prof.
Bharati Vidyapeeths 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.
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. By Narinder Kaur, Asstt. Prof.
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. By Narinder Kaur, Asstt. Prof.
Memory Allocation
Library Calls
System Calls
64
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. By Narinder Kaur, Asstt. Prof.
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.
65
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. By Narinder Kaur, Asstt. Prof.
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.
66
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. By Narinder Kaur, Asstt. Prof.
Library Calls
void *calloc(size_t nmemb, size_t size)
Allocate and zero fill
67
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. By Narinder Kaur, Asstt. Prof.
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().
69
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. By Narinder Kaur, Asstt. Prof.
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().
70
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. By Narinder Kaur, Asstt. Prof.
71
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. By Narinder Kaur, Asstt. Prof.
System Calls
brk()
sbrk()
72
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. By Narinder Kaur, Asstt. Prof.
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.
73
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. By Narinder Kaur, Asstt. Prof.
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 processs data segment.
74
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. By Narinder Kaur, Asstt. Prof.
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?
Bharati Vidyapeeths 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.
Bharati Vidyapeeths Institute of Computer Applications and Management, New Delhi-63. By Narinder Kaur, Asstt. Prof.