Mail us : hexainclude@gmail.com
Hexainclude

Menu

Skip to content
  • HOME
  • C/C++
    • C
    • C++
  • OS
  • RDBMS
  • JAVA
  • PHP
  • WORDPRESS
  • DOWNLOAD
    • eBOOKS

C

29Oct/16

Else…If ladder

October 29, 2016Celse-if-ladder, if-elseDreamMaker

When we have multiple options available or we need to take multiple decisions based on available condition, we can use another form of if statement called else…if ladder. In else…if ladder eachRead More…

22Oct/16

If…else statement

October 22, 2016Cconditional-statements, if-else, if-statementDreamMaker

If …else statement is an extension of simple if statement. When we have two different path to follow,then simple if statement cannot be useful because if provides a single path to follow.Read More…

20Oct/16

Decision making statements

October 20, 2016Celse-if-ladder, if, if-elseDreamMaker

Normally in c programming language statement are execute in a sequenced manner. Sometime we need to execute some statement based on some specific condition. Depending on the condition either we have toRead More…

05Oct/16

Operators

October 5, 2016CoperatorsDreamMaker

An operator is a special symbol that tells the compiler to perform some task. C- Language provides large number of In-build operators. An operator operates on operands. Operators can be dividing intoRead More…

09Sep/16

Compilation and Linking Of C Program

September 9, 2016Cc-compilaion, linking-of-c-programDreamMaker

Linking of C program includes the following steps: Create a C-Program source file using any standard editor such as Turbo C or Turbo C++. Compile source file using C compiler. Modify programRead More…

31Aug/16

C-Token

August 31, 2016Cc-token, constant, identifier, operatorDreamMaker

Every C-Program statement contains C-token. A token is a smallest unit in C-programing language. They are the building blocks of the program. C-token can be divided into the following categories. [1] Keyword Read More…

08Jun/16

Basic Data Types

June 8, 2016Cc_datatypes, datatypes, identifier-rules, naming-convention-rulesDreamMaker

A data type specify type of the data type Data types are require storing different type of value for operation purpose. C- Language has the following basic data types. [1] int (Integer):Read More…

08Jun/16

Structure of C Program

June 8, 2016Cc-program-structure, structure_of_cDreamMaker

Every C program follows block structure. It is written as a collection of function also known as sub-routine. The basic structure of a C program is as follow: [1] Documentation Section: ThisRead More…

08Jun/16

C- Character sets

June 8, 2016Cc_character_setDreamMaker

C – Language includes the following character set. [1] Alphabets: a to z lower case A to Z upper case. [2] Digits:Digits from 0 to 9 [3] Special characters: Special Characters MeaningRead More…

08Jun/16

History of C

June 8, 2016Cc_history, history_of_cDreamMaker

A computer programming started in 1960 with the language ALGOL. The concept of block structure programming was derived from the ALGOL language. It was developed by International Group in 1960. In 1967Read More…

Posts navigation

  • « Previous
  • 1
  • 2
  • 3
  • 4
  • 5
  • Next »
Download Android App

Recent Posts

  • PHP Functions
  • PHP Arrays
  • Safe Working-Practice
  • COPA Trade Introduction
  • NESTED STRUCTURE
  • If …else statement is an extension of simple if statement.
  • When we have two different path to follow,then simple if statement cannot be useful because if provides a single path to follow.
  • In such situation if…else statement is more suitable.
  • If…else statement helps to perform different actions based on true or false condition.
  • It has the following form:

Syntax:

if (condition)
{ 
   //true block;
}		
else				
{			
   //false block;	
}

if-else Statement

  • If given expression evaluates to true then true block will be executed and if condition is false then else block will be execute.
  • In any situation only one block will be executed that is if condition is true then true block will be executed and else block will be skipped.
  • Similarly, if condition is false then else block will be executed and true block will be skipped.
  • When if and else statement has a single statement then brackets are not required but it is necessary when it contain two or more statements.
  • The above code fragment (part) will display odd or even number depending on the condition.

Example:

<?php 
       // Program to check if number is even or odd.

  $n = 10 ;
        
  if($n % 2 == 0)
  {
     echo "Number is even" ;
  }
  else
  {
     echo "Number is odd" ;
  }
      
?>

Conditional Statement:

  • Conditional statement is written using conditional operator also known as ternary operator.
  • The format of the conditional operator is as follow:
(Condition)? Statement-1 : Statement-2
  • It takes three operands.
  • If condition is true then statement-1 will be executed otherwise statement-2 will be executed.
  • We can also represent the condition for assignment operation like:
variable = (Condition)? Value-1 : Value-2
  • Here if condition is true then variable has assigned the value-1 otherwise it has assigned the value-2
$sales = 70000 ;
$commission = (sales > 50000) ? 2000 : 0; // assign 2000 to commission
  • In above example if sale is > 50000 then commission value will be 2000 otherwise it will be zero.
  • It is similar to if…else statement and we can rewrite conditional statement using if…else statement as follow:
<?php

      if( sales > 50000)
           commission = 2000 ;
      else
           commission = 0 ;
?>
June 12, 2017
  • Normally in PHP programming language statement are execute in a sequenced manner.
  • Sometime we need to execute some statement based on some specific condition.
  • Depending on the condition either we have to execute or skip some statement.
  • In such situation we need to take decision to transfer control flow on the required statement.
  • PHP Programming language provides the following decision making statement:

           (1) If Statement 

          (2) Switch Statement

  1. If Statement:

  • When we want to execute some statement based on a given condition that time we can use if statement.
  • If statement is a two way decision making statement.
  • It provides two alternative paths to be followed according to the condition.
  • The following are different from of if statement.

          (1) Simple if Statement

          (2) if …else Statement

          (3) else…if ladder

          (4) Nested if

(1) Simple if Statement:

  • It is a simple form of if statement in which some statement are either executed or skipped according to the given condition.
  • It has the following form:
  • It first checks the condition or expression given inside if.
  • If condition becomes true, control enters into if block and statements inside the block are executes.
  • If condition becomes false then statements inside if block are skipped and control transfers to the statement following by if statement.
  • When if statement has a single statement then braces are not required but it is necessary when if statement contain two or more statement

flow of if statementSyntax:

if (condition)
{
   Statement(s);
}

Example:

<?php
      if (category =="sport")
      {
          mark = mark + bonus_mark;
      }
?>
  • It first checks the condition or expression given inside if.
  • If condition becomes true, control enters into if block and statements inside the block are executes.
  • If condition becomes false then statements inside if block are skipped and control transfers to the statement following by if statement.
  • When if statement has a single statement then braces are not required but it is necessary when if statement contain two or more statement
  • The following program demonstrates the use of if…else statement to find out maximum number.
<?php
    // program to find out maximum number

  $x = 10, $y = 20 ;   
      
  if ($x > $y)
      echo "X is maximum" ;
  else
      echo "Y is maximum" ;
  
?>
June 12, 2017
  • The following program demonstrates how 2D-array can be used as a function argument.
  • The below program performs basic matrix operations such as matrix addition, multiplication, transpose of a matrix etc.
/* Program to perform operations like addition, multiplicaton, etc. on matrix using function with 2D-array. */

#include "stdio.h" 
#include "conio.h"

#define MAX 3            // constant variable declaration

void create ( int [3][3] ) ;    // 2D-array as argument
void display ( int [3][3] ) ;
void matadd ( int [3][3], int [3][3], int [3][3] ) ;
void matmul ( int [3][3], int [3][3], int [3][3] ) ;
void transpose ( int [3][3], int [3][3] ) ;

void main( )
{
	int mat1[3][3], mat2[3][3], mat3[3][3], mat4[3][3], mat5[3][3] ;

	clrscr () ;

	printf ( "Enter first Matrix :\n\n" ) ;
	create ( mat1 ) ;

	printf ( "Enter elements second Matrix:\n\n" ) ;
	create ( mat2 ) ;

	printf ( "First Matrix:\n" ) ;
	display ( mat1 ) ;
	printf ( "Second Matrix:\n" ) ;
	display ( mat2 ) ;

	matadd ( mat1, mat2, mat3 ) ;
	printf ( "After Addition:\n" ) ;
	display ( mat3 ) ;

	matmul ( mat1, mat2, mat4 ) ;
	printf ( "After Multiplication:\n" ) ;
	display ( mat4 ) ;

	transpose ( mat1, mat5 ) ;
	printf ( "Transpose of first matrix:\n" ) ;
	display ( mat5 ) ;

	getch() ;
}

/* creates matrix mat */

void create ( int mat[3][3] )
{
	int i, j ;

	for ( i = 0 ; i < MAX ; i++ )
	{
		for ( j = 0 ; j < MAX ; j++ )
		{
			printf ( "Enter the element: " ) ;
			scanf ( "%d", &mat[i][j] ) ;
		}
	}
	printf ( "\n" ) ;
}

/* displays the contents of matrix */

void display ( int mat[3][3] )
{
	int i, j ;

	for ( i = 0 ; i < MAX ; i++ )
	{
		for ( j = 0 ; j < MAX ; j++ )
			printf ( "%d\t", mat[i][j] ) ;
		printf ( "\n" ) ;
	}
}

/* adds two matrices m1 and m2 */

void matadd ( int m1[3][3], int m2[3][3], int m3[3][3] )
{
	int i, j ;

	for ( i = 0 ; i < MAX ; i++ )
	{
		for ( j = 0 ; j < MAX ; j++ )
			m3[i][j] = m1[i][j] + m2[i][j] ;
	}
}

/* multiplies two matrices m1 and m2 */

void matmul ( int m1[3][3], int m2[3][3], int m3[3][3] )
{
	int i, j, k ;
	for ( k = 0 ; k < MAX ; k++ )
	{
		for ( i = 0 ; i < MAX ; i++ )
		{
			m3[k][i] = 0 ;
			for ( j = 0 ; j < MAX ; j++ )
				m3[k][i] += m1[k][j] * m2[j][i] ;
		}
	}
}

/* obtains transpose of matrix m1 */
void transpose ( int m1[3][3], int m2[3][3] )
{
	int i, j ;

	for ( i = 0 ; i < MAX ; i++ )
	{
		for ( j = 0 ; j < MAX ; j++ )
			m2[i][j] = m1[j][i] ;
	}
}

Note: While passing a two dimension array into the function we have specified set of two square braces at function declaration and function definition.

May 28, 2017
  • Similar to normal variable, we can also pass an array to the function.
  • There is no major difference between passing a variable and array except that we have to specify the dimension for an array so that compiler can differentiate them.
  • While passing an array as argument we have to specify array dimension by set of square braces (“[]”) as a function argument at the time of function declaration and function definition.
  • There is no difference on calling function with array arguments.
  • If array argument is of one dimension, we have to specify single set of square braces (“[]”) and if it is two dimension array, we have to specify two dimension at the time of function declaration and function definition by set of two square braces (“[][]”).
  • The following example demonstrates passing array as function argument two find out maximum number from it.

Passing Array to Function

Note: By default, Arrays are always passed as pass by reference method

  • Here is another example to perform different operations on matrix using array where we have passed 2D-array as function arguments. Please have a look to make it more clear how an array can be used as function arguments.
May 28, 2017
  • In my previous post titled “What is File?” I explained about the concept of file, file attributes and file types. This post is the supplementary for the same so if you haven’t read that post yet, I strongly recommend to read that first before proceeding to this one. This post deals with the basic operations performed on the file.
  • A file is an abstract data type. To define a file properly, we need to consider the operation performed on the file.
  • Operating System can provide system calls to create, write, read, reposition, delete and truncate files.

Creating a File

  • Creation of a file is the first operation. For creating a file two steps are necessary.
  1. Required space must be allocated.
  2. An entry for new file must be made in the directory.
  • The directory entry records the name of the file and the location in the file system.

Writing a File

  • For file writing operation, we make a system call specifying both the name of a file and the information to be written into the file.
  • System searches the entire directory structure to find the location of the specified file.
  • System keeps a write pointer that keeps track of writing at the location in the file.
  • The system updates write pointer each time whenever a file write operation occurs.

Reading a File

  • To perform file read operation, we use a system call that specifies name of the file and the block of the file to read from it.
  • Again the directory is searched for the specified file.
  • System keeps a read pointer that keeps track of reading location in the file.
  • The system updates read pointer each time whenever a file read operation occurs

Repositioning Within a File

  • Repositioning within a file operation does not involve any actual input output.
  • The directory is searched for the appropriate entry and the current file position is set to a given value.
  • It is also known as files seek operation.

Deleting a File

  • File deletion operation also requires searching of a specified file entry within the directory structure.
  • As soon as the file is deleted, space allocated to that file becomes available for further use.

Truncating a File

  • In some cases the user may want to erase the file contents but keep its attributes as it is. This operation is called truncating a file.
  • Instead of delete a file and recreating it with same attributes, this function allows all attributes to remain unchanged except the file content.
  • File length attribute is reset to a length zero and its file space is released.
May 27, 2017
  • A file is the most important and basic entity for data storage. In this article we will get some basic knowledge about concept of a file, different attributes of file, types of file and how a file is useful for the operating system.

A file is a collection of related information stored on a peripheral device

  • File is a logical storage unit.
  • Computer store information on different storage area such as magnetic disk, tap drives etc.
  • These storage devices are usually of non-volatile type that means contents are stored despite of power failure.
  • It is a named collection of related information that is recorded on secondary storage. It is the smallest allotment of logical secondary storage as per the user’s point of view.
  • File may represent programs or data.
  • Data files may be numeric, alphabetic, alphanumeric or binary.
  • In general a file is a sequence of bits, bytes, lines or records whose meaning is defined by the file creator.
  • A file can store different information such as source program, object program, executable program, numeric data/text, database records, Graphic Image etc.
  • Each file has certain structure according to its type.
  • A text file is represented as a sequence of characters organized into lines.
  • A source file is a sequence of sub routines and functions.
  • An object file is a sequence of bytes organized into block which is understandable by system linker. Usually it has the .obj extension.
  • Executable file is a series of binary code that can be executed by the CPU. It has the .exe extension.

Types of File

  • To perform an operation on the file, the O/S must recognize its types.
  • Normally file type is implemented by including it as a part of file name.
  • The type of file represents the contents of that file.
  • File name is divided into two parts:
  1. Name
  2. Extension
  • By this way user and O/S can easily identify type of the file.
  • In Dos the name can be of 8 characters long followed by a period and extension up to 3 characters.
  • The system uses the extension to identify the file type and allow operations that can be perform on that file.
  • For example only file with .com, .exe or .bat extension are used for executing a file. .com and .exe are two forms of binary executable file whereas .bat file is a batch files.
  • DOS supports only a few extensions but application program also uses extension to indicate file types.
Files Extension Functions
Executable .exe, .com, bin, none. Need to run m/c lang .prg
Object .o, .obj Compile, m/c Lang; non linked
Source code .c, .cc, .java, .asm, .a, .pas Source code in various Lang
Batch file .bat, .sh Commands used for interpreting by o/s
Text .txt, .rtf, .doc Textual data documents
Word file .wp, .tex, .ry, .doc Various word processor formats
Library .lib, .a, .so, .dll, .mpeg, .mov, .rm Libraries routine for programs
Archive .arc, .zip, rar Related files
Multimedia .mpeg, .mov, .rm, mp4, mkv Binary file with audio, video information
  • UNIX uses a magic number to represent file type.
  • This number is stored at the beginning of a file at the time of creation.
  • Not all files have magic numbers so systems features cannot be based on this type of information.
  • It is roughly allocate to indicate some of the file types such as program file and scripting file
  • UNIX also does not record the name of the creative program.

File Attributes

  • File attributes specifies properties of the file
  • It also defines behavior of any file.
  • A file can be referred by its name.
  • A name is usually a string of characters such as example.c
  • Some system treats upper and lower case character names differently while other systems consider them to be equivalent.
  • A file has certain attributes that vary from the o/s to o/s.
  • Some common file attributes are as follow:
  1. Name: Symbolic file name is the only information which is in human readable form.
  2. Type: It indicates the type of the file such as text, binary, object etc.
  3. Location: It is a pointer to the location of the file where a file is stored.
  4. Size: It determines the current size of the file. (bytes, blocks)
  5. Protection: It provides access control information to a user who can read write and execute it.
  6. Date, Time and User Identifier:  It provides the information about the file creation date/time, last modification and last used date/time. It is helpful for security and usage monitoring.
  • All information about each file is kept in the directory structure which resides on secondary storage.
  • Normally the directory entry consists of files name and identifier.
  • The identifier locates all the attributes of the files.
  • Files and directories are non volatile storage they must be store on the device and brought into memory as needed.
May 27, 2017
  • In my last post I had explained about “Deadlock Detection” techniques in which I explained how we can detect if system has deadlock or not.
  • During the deadlock detection step if we found that system has deadlock than we have to take some steps to solve the deadlock by applying different deadlock recovery techniques. So in this post I am going to focus on how a deadlock can be recovered by using some deadlock recovery techniques.
  • When a deadlock detection algorithm detects that system has deadlock then we have two options:
  1. One possibility is to inform the operator that deadlock is created and let the operator to handle it manually.
  2. Second possibility is to recover the system from deadlock automatically.
  • There are two options for breaking a deadlock.
  1. Terminate one or more process to break the circular wait (Recovery by process termination).
  2. Preempt some resources from one or more deadlocked processed
  1. Recovery by Process Termination

  • In this method we terminate one or more deadlocked process for deadlock recovery.
  • We can either terminate all deadlocked process or one process at a time until the recover from deadlock.
  1. Abort all Deadlocked Process
  • In this method, all deadlocked processes are aborted. It guarantees to break the deadlock.
  • But this method has higher cost because deadlocked processes may have done some task so, aborting them will result in loss of data and they may have to recompute later when they restart.
  1. Abort one Process at a time until Deadlock break

  • This method terminates process one by one until deadlock break but this method adds extra overhead on the system because after terminating each process deadlock detection algorithm needs to execute to check if the system has still deadlock.
  • Aborting a process may result in possible data loss if process has done some task.
  • Termination of process should be done with a minimum cost.
  • Selection of process for termination should consider the following factors:
  1. What the priority of the process is?
  2. How much work has been done and how long it would take to complete?
  3. How many and what type of resources are used by the process?
  4. How many additional resources will be required to complete?
  5. Whether the process is interactive or both?
  6. How many processes will be required to terminate?
  1. Recovery by Checkpoint and Rollback (Resource Preemption)

  • Some operating system provides deadlock recovery by using checkpoint and rollback.
  • Checkpointing is saving enough state of a process so that process can restart from that checkpoint state later.
  • Autosaving facility of a file is an example of checkpointing.
  • Cost of checkpointing depends on the implemented algorithms.If deadlock is detected, one or more processes are restarted from their last checkpoint.
  • Restarting a process from a checkpoint is called rollback.
  • Rollback is similar to familiar undo command of the windows based operating system which is used to cancel effect of last performed command.
  • Deadlock recovery is normally used where deadlocks rare and cost of recovery are low.
  • Checkpoint can also be used for reliability and reduce startup costs.
May 20, 2017
  • In my previous post, I had explained Banker’s algorithm as a “Deadlock Avoidance” technique.  We discussed basic concept of bankers’ algorithm as well as features and limitations of it.
  • In this post, I am going to discuss Deadlock Detection techniques that deals with how deadlock in the system can be detected by using different techniques and methods.
  • If a system does not implement a deadlock-prevention or a deadlock avoidance algorithm then system may have deadlock.
  • In this situation the system may use:
  1. Algorithm to check the stat of the system for possible deadlock detection.
  2. An algorithm to recover from the deadlock
  • Deadlock detection and recovery scheme includes run-time costs of maintaining and executing detection algorithm as well as possible loss of data from recovery.
  • The algorithm should be capable of detecting deadlock for both:
  1. Single instance of each resource type and
  2. Several instance of each resource type.
  1. Single Instance of each Resource Type

  • If all resources have only single instance, we can use one of the form of the resource allocation graph called a wait for graph to detect deadlock
  • We can prepare this graph from the resource-allocation graph by removing the resource nodes and collapsing the appropriate edges.
  • A deadlock exists in the system if and only if the wait-for graph contains cycle.
  • An algorithm to detect a cycle in a graph requires an order of n^2 operations, where n is the number of vertices in the graph.
  • the following diagram represents resource allocation graph as well as its corresponding wait-for-graph.

Wait-for-Graph

 

  1. Several Instance of each Resource Type

  • The wait-for graph scheme is not applicable in a system with several instance of each resource type.
  • We have to use deadlock detection algorithms that can be used for such system.
  • Such algorithm uses some time-variant data structures similar to those used in banker’s algorithm.
  1. Request: An n X m matrix to represent the current request of each process.
  2. Allocation: An n X m matrix to represent the number of resources of each type currently allocated to each process.
  3. Available:  A vector of length m to represents the number of available resources of each type.
  • Execution of deadlock detection algorithm depends on two criteria:
  1. How many processes will be affected by the deadlock if deadlock occur?
  2. How often deadlock is likely to occur?
  • Execution of deadlock detection algorithm for every resource request will add extra overhead on the system.
  • In my next post, I will explain Deadlock Recovery Scheme so please stay tuned!
May 15, 2017
  • Hello everyone! In my previous two posts I had explained some basic information regarding “Deadlock” and “Necessary Conditions to occur Deadlock”. if you haven’t read those posts yet, I strongly suggest you to read that first before proceeding to this post.
  • In this post I will explain deadlock avoidance techniques in brief. So lets start!
  • This method uses different algorithms to avoid deadlocks in advance before it could occur.
  • It uses algorithms to check the possibility of deadlock and takes action accordingly.
  • This method is different from deadlock prevention which guarantees that deadlock cannot occur by breaking one of the necessary conditions for deadlock.

Banker’s Algorithm

  • One of the most commonly used deadlock avoidance algorithms is the Banker’s algorithm introduced by Dr. D.W. Dikjistra in 1965.
  • It uses similar concept used by banker to decide if the loan can be granted or not.
  • Banker’s algorithm is based on banking system. A bank never allocates its available can in such a manner that it can no longer satisfy the needs of all its customers.
  • The system must have the advance knowledge of the maximum possible requests for each process which is limited by available resources.
  • During the system run we should keep monitoring the status of the resource allocation to ensure that no circular wait condition become true.
  • If necessary conditions for a deadlock are about to occur, it is still possible to avoid deadlock by taking care when resources are allocated.
  • The following features should be considered to avoid deadlock as per baker’s algorithm:
  1. Each process declares maximum number of each resource type that it may need.
  2. Keep the system in a safe state in which we can allocate resources to each type in some order to avoid deadlock.
  3. Check for the safe state by finding a safe sequence: <P1,P2,….Pn>
  • The resource allocation status is defined by the number of available and allocated resources and the maximum demands of the process.
  • The system can be in one of the following status:

Safe State

A state is safe if the system can allocate resources to each process (up to its maximum need) in some order without creating deadlock.

A system is said to be in a safe state only if there exists a safe sequence.

Unsafe State

If a system does not have any safe sequence to follow for resources allocation and it is in a situation which may result to a deadlock then system is in unsafe state.

Deadlock State

  • If there exists circular wait condition for some processes in the system then it is a deadlock state and we can say that system is in a deadlock state.
  • Now let’s study the following example to understand this concept.

Example

Consider a situation in which 4 processes [P1,P2,P3,P4] can be compared with the customers in a bank, resources such as printers as a cash available in the bank and the operating system as a banker. Let assume that total number of available resources are 10.

Processes Resouces used Max. resource
P1 0 6
P2 0 5
P3 0 4
P4 0 7
  • As we can see that the operating system has only 10 resources available. So all request cannot be granted at once.At some later stage the situation becomes as follow:
Processes Resources used Max. resource
P1 1 6
P2 1 5
P3 2 4
P4 4 7

Now total available resurcesare : 2

Safe State

  • For a system to be in a safe state there must exists at least one way for all process to finish.
  • The state of the above table is safe because with available 2 resources the operating system can allocate both resources to P3 first. After completion of P3 it will release all 4 resources. Now with 4 resources the operating system can either grant request of P4 or P2 and so on.

Unsafe State

  • consider what would happen if a request from P2 for one more unit was granted? We would then have the following situation: 
Processes RESOURCES used Max. resource
P1 1 6
P2 2 5
P3 2 4
P4 4 7

 Now available resource is 1:

  • This is an unsafe state because the operating system could not satisfy any of them and system would have deadlock.

Note: It should be noted that an unsafe state does not means that system is in a deadlock or it will have deadlock. Unsafe state means only that system may have possibility for a deadlock because of unsafe sequence.

  • This way baker’s algorithm is used to consider each request and check that after granting it the system will be in a safe state not.
  • If system it does then request is granted otherwise it is postponed for later.

Limitations of the Banker’s Algorithm:

  • Habeman shown that since algorithm is executed each time a resource request arrives, the overhead is quit high.
  • The main limitations of the banker’s algorithm are as follow:
  1. It is time consuming to execute on every request of each resource.
  2. If the claim information [information about resource] is not accurate, system resources may be underutilized.
  3. When a system is heavily loaded, very few safe sequences remain as so many resources are granted.
  4. Arrival of new process may create problems:
  5. The requested resources by the process must be less than the total number of available resources.
  6. Since the state without the new process is safe, it would be also safe with the new process. A the new process will be added at the end.
  7. To avoid starvation problem among processes it require a little more work but it not much harder.
  8. If resource becomes unavailable [for example damage in tap drive], it can result in an unsafe state.
  • Please read my next post Deadlock Detection fore more information.
May 11, 2017
  • In previous post I explained about what is deadlock and now in this post I will try to explain necessary conditions to occur deadlock. It focuses on what conditions need to be true for the deadlock creation.
  • A system is said to be in a deadlock state if the following four conditions are true simultaneously:
  1. Mutual Exclusion

  • At least one resource must be held by the processes in a non-shareable mode; that means only one process at a time can use the resource.
  • If other process requests that resource then it must wait till that resource is released.
  • Consider the following scenario in which process P1 and P2 both are having at least one resource. (P1 is having tap drive and P2 is having printer) and both resources are non-shareable. If process P1 requires printer to complete its task then it must have to wait till printer is released but printer is allocated to process P2 which is also waiting for tap drive to finish its job so both process are waiting for each other to finish so it will lead to the creation of deadlock.

Deadlock Creation

  1. Hold and Wait

  • A process must have at least one resource and waiting for other resources which are allocated to other waiting processes.
  • In our example P1 has tap drive and is waiting for printer which is held by P2.
  • P1 can only finish if printer released by P2 which is not possible as P2 is waiting for tap drive so here, hold and wait condition becomes true and we can say system is in a deadlock state.
  • If any process holds any resource but does not waiting for any resource than that process can complete its execution and will release its all resources that can be assigned to other waiting processes later and hence all process will be completed and no deadlock will be created.
  • So for the deadlock to occur both hold and wait condition must be true simultaneously.
  1. No preemption

  • Resources cannot be preempted; that means a resource cannot be force fully deallocated and it can be released only by the process voluntarily after completing its task.
  • If resources are preempted then it can be forcefully deal located and given to the process waiting for that and hence no deadlock will exist in the system.
  1. Circular Wait

  • There must exist a set of waiting processes (P0,P1,P2,…Pn) such that P0 is waiting for resource held by P1, p1 is waiting for a resource held by p2,…..Pn-1 is waiting for a resource held by Pn and Pn is waiting for resource held by P0.
  • In our example, we can see that circular wait is exist as P1 is waiting for P2 and P2 is waiting for P1 to complete.
  • All four conditions must be true simultaneously to occur a deadlock.
  • The circular wait condition also implies the hold and wait condition so all four conditions are interdependent.
May 1, 2017

All Posts

  • What is Algorithm?
  • Flowchart
  • Types of programming languages
  • Interpreter and Compiler
  • History of C
  • C- Character sets
  • Structure of C Program
  • Basic Data Types
  • C-Token
  • Compilation and Linking Of C Program
  • Operators
  • Decision making statements
  • If…else statement
  • Else…If ladder
  • Nested if statement
  • Switch Statement
  • Goto Statement
  • Looping Structures
  • Entry Control Loop
  • Exit Control Loop
  • Break, Continue and Exit
  • Introduction to Array
  • Two-Dimension Array
  • Multi-Dimension Array
  • Introduction to String
  • Arrays of String
  • String Functions
  • Searching
  • Sorting
  • Introduction to Function
  • Elements of Functions
  • Types of Function
  • Call by Value v/s Call by Reference
  • Passing Array to Function
  • Matrix Operations
  • Recursion
  • I/O Functions
  • Conversion Functions
  • Math Functions
  • Structure in C
  • Structure Declaration
  • Array Within Structure
  • ARRAY OF STRUCTURE
  • NESTED STRUCTURE
Copyright © Hexainclude
Developed by Hexainclude
  • TIPS &TRICKS
  • QUIZ
  • CONTACT US
  • COPA