Month: October 2014

DBMS Deadlock

Posted on Updated on

DBMS Deadlock

In a multi-process system, deadlock is a situation, which arises in shared resource environment where a process indefinitely waits for a resource, which is held by some other process, which in turn waiting for a resource held by some other process.

For example, assume a set of transactions {T0, T1, T2, …,Tn}. T0 needs a resource X to complete its task. Resource X is held by T1 and T1 is waiting for a resource Y, which is held by T2. T2 is waiting for resource Z, which is held by T0. Thus, all processes wait for each other to release resources. In this situation, none of processes can finish their task. This situation is known as ‘deadlock’.

Deadlock is not a good phenomenon for a healthy system. To keep system deadlock free few methods can be used. In case the system is stuck because of deadlock, either the transactions involved in deadlock are rolled back and restarted.

Deadlock Prevention

To prevent any deadlock situation in the system, the DBMS aggressively inspects all the operations which transactions are about to execute. DBMS inspects operations and analyze if they can create a deadlock situation. If it finds that a deadlock situation might occur then that transaction is never allowed to be executed.

There are a deadlock prevention scheme, which uses time-stamp ordering mechanism of transactions in order to pre-decide a deadlock situation.

WAIT-DIE SCHEME:

In this scheme, if a transaction request to lock a resource (data item), which is already held with conflicting lock by some other transaction, one of the two possibilities may occur:

  • If TS(Ti) < TS(Tj), that is Ti, which is requesting a conflicting lock, is older than Tj, Tiis allowed to wait until the data-item is available.
  • If TS(Ti) > TS(tj), that is Tiis younger than Tj, Ti  Ti is restarted later with random delay but with same timestamp.

This scheme allows the older transaction to wait but kills the younger one.

WOUND-WAIT SCHEME:

In this scheme, if a transaction request to lock a resource (data item), which is already held with conflicting lock by some other transaction, one of the two possibilities may occur:

  • If TS(Ti) < TS(Tj), that is Ti, which is requesting a conflicting lock, is older than Tj, Tiforces Tj to be rolled back, that is Ti wounds Tj. Tj is restarted later with random delay but with same timestamp.
  • If TS(Ti) > TS(Tj), that is Tiis younger than Tj, Ti is forced to wait until the resource is available.

This scheme, allows the younger transaction to wait but when an older transaction request an item held by younger one, the older transaction forces the younger one to abort and release the item.

In both cases, transaction, which enters late in the system, is aborted.

Deadlock Avoidance

Aborting a transaction is not always a practical approach. Instead deadlock avoidance mechanisms can be used to detect any deadlock situation in advance. Methods like “wait-for graph” are available but for the system where transactions are light in weight and have hold on fewer instances of resource. In a bulky system deadlock prevention techniques may work well.

WAIT-FOR GRAPH

This is a simple method available to track if any deadlock situation may arise. For each transaction entering in the system, a node is created. When transaction Ti requests for a lock on item, say X, which is held by some other transaction Tj, a directed edge is created from Ti to Tj. If Tj releases item X, the edge between them is dropped and Ti locks the data item.

The system maintains this wait-for graph for every transaction waiting for some data items held by others. System keeps checking if there’s any cycle in the graph.

1

Two approaches can be used, first not to allow any request for an item, which is already locked by some other transaction. This is not always feasible and may cause starvation, where a transaction indefinitely waits for data item and can never acquire it. Second option is to roll back one of the transactions.

It is not feasible to always roll back the younger transaction, as it may be important than the older one. With help of some relative algorithm a transaction is chosen, which is to be aborted, this transaction is called victim and the process is known as victim selection.

Posted By-: Vissicomp Technology Pvt. Ltd.

Website -: http://www.vissicomp.com

Advertisements

Comments in C++

Posted on Updated on

Comments in C++

Program comments are explanatory statements that you can include in the C++ code that you write and helps anyone reading it’s source code. All programming languages allow for some form of comments.

C++ supports single-line and multi-line comments. All characters available inside any comment are ignored by C++ compiler.

C++ comments start with /* and end with */. For example:

/* This is a comment */ 

/* C++ comments can  also

* span multiple lines

*/

A comment can also start with //, extending to the end of the line. For example:

#include <iostream>

Void main()

{  

cout << “Hello World”; // prints Hello World 

}

When the above code is compiled, it will ignore // prints Hello World and final executable will produce the following result:

Hello World

Within a /* and */ comment, // characters have no special meaning. Within a // comment, /* and */ have no special meaning. Thus, you can “nest” one kind of comment within the other kind. For example:

/* Comment out printing of Hello World: 

cout << “Hello World”; // prints Hello World 

*/

Posted By-: Vissicomp Technology Pvt. Ltd.

Website -: http://www.vissicomp.com

 

Linux Administration

Posted on Updated on

The User Database Files, shadow password

The User Database Files

Format of the user database files, /etc/passwd and /etc/shadow. Each line in both files consists of colon-separated fields, one line per user. The format of the password file, /etc/passwd, is:

username:password:uid:gid:gecos:directory:shell

2

The actual password is stored in /etc/shadow, indicated by an x in the password field in /etc/passwd. Because /etc/passwd is readable by all users, storing even encrypted passwords in it makes password guessing easier.

/etc/shadow is more secure because it is readable only by programs that run with root privileges, such as loginand passwd. The sidebar “The Shadow Password System” discusses shadow passwords in more detail.

An entry in /etc/passwd should resemble the following:

marysue:x:502:502:Mary Sue:/home/marysue:/bin/bash

In this entry, username is marysue; password is x, meaning it is stored in /etc/shadow;  uid is  502;  gid is  502;  gecosis  Mary Sue;  homeis /home/marysue; and shell is /bin/bash.

In addition to storing the encrypted password, /etc/shadow stores password expiration information. As in /etc/passwd, fields are separated by colons. It contains the following fields, listed in the order in which they appear in /etc/shadow:

■■ The account name

■■ The account’s encrypted password

■■ The number of days since 1 January 1970 that the password was last Changed

The number of days permitted before the password can be changed

■■ The number of days after which the password must be changed

■■ The number of days before the password expires that the user is warned the account will expire

■■ The number of days after the password expires before the account is disabled

■■ The number of days since 1 January 1970 after which the account is disabled

■■ Reserved for future use

The entry from /etc/shadow that corresponds to the entry from /etc/passwd shown earlier is:

marysue:$1$TJp8osKa$GDUrFMa9Twac/1sjV3mA90:12877:0:99999:7:::

1

Posted By-: Vissicomp Technology Pvt. Ltd.

Website -: http://www.vissicomp.com

Linux Administration

Posted on Updated on

Installing and Upgrading Software Packages …

Using the Red Hat Package Manager

RPM is a powerful software configuration manager and the preferred tool for installing, removing, verifying,and updating software packages on Fedora Core and RHEL systems.

RPM consists of two components: a set of databases that store information about installed software and the programs that interface with the databases.

RPM can work with binary and source packages. Binary packages, referred to simply as RPMs, contain compiled software ready for installation.

They use the file extension .rpm.

following sections each explore one of RPM’s modes:

■■ General options

■■ Querying

■■ Package maintenance

■■ Package verification

■■ Administrative options

■■ Miscellaneous options

The general options control the overall behavior of the rpm command line tool, such as displaying basic usage information. The query functions can be used to obtain a considerable amount of information about installed software.

Package maintenance enables package installation, removal, and upgrading.

Package verification gives system administrators the ability to compare the present state of files installed by an RPM against information taken from the original package.

  1. General Options

# rpm –test -e jwhois

# rpmquery jwhois

  1. Query Mode

The general form of an RPM query is:

rpmquery [query_opts]

rpmquery [query_opts] package[…]

packagenames the RPM to query. Query multiple RPMs using a space separated list of package names.

Querying Package Dependencies

The –provides, –requires, –whatrequires, and –whatprovides options allow you to identify dependencies between packages.

What’s in That RPM?

The simplest query option, rpm –q or rpmquery, shows only an RPM’s name and version number, as the following command illustrates:

$ rpmquery jwhois

jwhois-3.2.2-1.1

$ rpm -qi jwhois

To list all of the files in an installed RPM, use the –i option:

$ rpmquery –i  jwhois

Formatting Query Output

Inveterate tweakers and hard-core tinkerers appreciate the –qf option because it allows custom formatting of RPM query output.

rpmquery –qf ‘format_str’ [query_opts]

The next command uses two tags, NAME and VERSION, to specify the output fields:

$ rpmquery –qf ‘%{NAME}%{VERSION}’ setup bc hdparm popt

  1. Package Installation and Removal

Installing RPMs

The basic syntax for installing an RPM is:

rpm -i [options] package[…]

package is the complete name of the RPM to install and options refines the installation process.

The following command demonstrates installing an RPM:

# rpm -ivh fortune-mod-1.2.1-1.i386.rpm

Preparing… ########################################### [100%]

1:fortune-mod ########################################### [100%]

Upgrading RPMs

The options for upgrading existing RPMs come in two flavors, -U, for upgrade, and -F, for freshen.

Upgrading a package, using -U, installs it even if an earlier version is not currently installed, but freshening a package, using -F, installs it only if an earlier version is currently installed.

The following sequence of commands illustrates how to upgrade an RPM and the difference between the –U and –F options:

# rpm -Fvh fortune-mod-1.0-13.i386.rpm

# rpm -q fortune-mod

Removing RPMs

rpm -e package[…]

  1. Verifying RPMs

Verifying an RPM compares the current status of files installed by an RPM to the file information recorded at the time the RPM was installed, such as file sizes and MD5 checksum values, and reports any discrepancies.

rpm -V package[…]

Building Packages Using Source RPMs:

rpmbuild -b[stage] spec_file[…]

Any of the values listed in Table is a valid value of stage.

# rpmbuild -bb util-linux.spec

5 and 6 ) Checking Software Versions

$ rpmquery emacs

emacs-21.4-3

The output indicates that the Emacs version is 21.4-3.

$ rpmquery -a | grep emacs

emacs-common-21.4-3

emacspeak-21.0-2

Posted By-: Vissicomp Technology Pvt. Ltd.

Website -: http://www.vissicomp.com

Linux Administration

Posted on Updated on

Administering User Accounts

commands for adding, modifying, and deleting user accounts.

■■ useradd— Create user login accounts

■■ userdel— Delete user login accounts

■■ usermod— Modify user login accounts

■■ passwd— Set or change account passwords

■■ chsh— Set or change a user’s default shell

■■ chage— Modify password expiration information

  • user-add— Create user log in accounts

The useradd command creates new user accounts and, when invoked with the -Doption, modifies the default values applied to new accounts.

As a result, it can be invoked in two ways. The syntax of the first form is:

useradd [-c comment] [-d dir] [-e date]

[-f time] [-g initial] [-G group[,…]]

[-m [-k Dir] | -M]  

[-p password] [-s shell] [-u uid[-o]]

[-n] [-r] username

Table : user add Options and Arguments

The second way to invoke useradd uses the –D option. Invoked with only -D, useradd displays its current default settings.

Using –D with any of the options listed in Table modifies the default value for the corresponding field. Here is the syntax for the second form:

useradd -D [-g group] [-b home_dir]

[-f inactive_time] [-e expire_date]

[-s shell]

useradd’s default values are stored in /etc/default/useradd.

  • User del:

The userdel command deletes a user account and, optionally, related files.

Its syntax is:

userdel [-r] username

username identifies the user account to delete. Using –r deletes the corresponding home directory and mail spool. Without -r, userdel removes only the account references in the user and group database files. You cannot delete the account of a logged in user, so userdel fails if username is logged in.

  • user mod command :

The usermod command modifies an existing user account. Its syntax is:

usermod [-c comment] [-d dir[-m]] [-e date]

[-f inactive] [-g group] [-G group[,…]]

[-l new_username] [-p passwd]

[-s shell] [-u uid[-o]] [-L|-U] username

Usermod accepts the options and arguments listed in Table for useradd and adds three new ones, -l new_username, -Land -U. -l new_username changes the account name from usernameto new_username. –Ldisables (locks) username’s account by placing a !in front of the user’s encrypted password in /etc/shadow. -Uenables (unlocks) the account by removing the !.

  • Passwd command

The passwd command, generally regarded as “the password changing utility,” actually has more capabilities than merely changing passwords. In general, it updates all of a user’s authentication tokens, of which the login password is only one. Its syntax is:

passwd [-dkluf] [-S] username

-d removes the password for username, disabling the account. –k causes passwd to update only expired authentication tokens (passwords, in this case).

  • chsh command

The chsh command changes a user’s login shell. Its syntax is:

chsh [-s shell] [-l] [username]

-s shell sets username’s login shell to shell. Unless configured otherwise, shell can be the full pathname of any executable file on the system.

One common way to take advantage of this feature is to disable an account by setting shellto /bin/falseor another command that does not display a login prompt to the user. Using the -l option displays the shells listed in /etc/shells.

  • chage command

The chage command changes the expiration policy for a user’s password.

Its syntax is:

chage [-l] [-m min days] [-M max days] [-d last day] [-I inactive]

[-E expire date] [-W warn days] username

Table lists the valid options that chage accepts.

If no options are used, chageexecutes in interactive mode, prompting the user for each item of information. The chage command’s –l option to obtain a friendlier display:

# chage -l marysue

Minimum: 0

Maximum: 99999

Warning: 7

Inactive: -1

Last Change: Apr 04, 2005

Password Expires: Never

Password Inactive: Never

Account Expires: Never

Chage does not display the fields in the order in which they appear in /etc/shadow.

Posted By-: Vissicomp Technology Pvt. Ltd.

Website -: http://www.vissicomp.com

Linux Administration

Posted on Updated on

Apache web server,How Web Servers Work      

Why the name“Apache”?

The name “Apache” was chosen from respect for the Native American Indian tribe of Apache (Ind?), well-known for their superior skills in warfare strategy and their inexhaustible endurance.

Secondarily,and more popularly (though incorrectly) accepted,it’s a considered cute name which stuck. Apache is “A Patch y server.”It was based on some existing code and a series of “patch files.”

■■ More eyeballs means fewer bugs. Apache’s source code is freely available,so hundreds of developers have looked at it,found bugs, fixed them,and submitted their fixes for inclusion in the code base.

Apache is also both easy to configure and easy to reconfigure. Its configuration information resides in plain-text files and uses simple English-language directives.Re-configuring a running server is a simple matter of changing the appropriate configuration directive and reloading the configuration file.

How Web Servers Work       

Figure shows the general process that takes place when a Web browser requests a page and the Apache Web server responds.

This simplified illustration disregards the browser cache,content accelerators such Inktomi and Akamai, and the existence of proxy servers between the user’s browser and the Apache Web server.

The Web client (a browser in this case) first performs a DNS lookup on the server name specified in the URL,obtains the IP address of the server,and then connects to port 80 at that IP address (or another port if the server is not using the default HTTP port).

If you specify an IP address directly, the DNS look-up doesn’t occur.When the connection is established, the client sends an HTTP GET request for the document in the URL, which could be, among other possibilities, a specific HTML document, an image,or a script.

After the server receives the request,it translates the document URL into a file name on the local system.For example,the document URL

http://www.example.com/news.html might become /var/www/html/news.html.Next, Apache evaluates whether the requested document is subject to some sort of access control.

If access control is in effect, Apache requests a username and password from the client or rejects the request outright, depending on the type of access control in place.

If the requested URL specifies a directory (that is, the URL ends in /)rather than a specific document, Apache looks for the directory index page,index.html by default,and returns that document to the client.

If the directory index page does not exist,Apache might send a directory listing in HTML format back to the client or send an error message,depending on how the server is configured.

The document can also be a specially written script,a Common Gateway Interface (CGI) script.In this case,Apache executes the script,if permitted to do so,and sends the results back to the client.

Finally,after Apache has transmitted the requested document and the client receives it,the client closes the connection and Apache writes an entry in one or more log files describing the request in varying levels of detail.

1

Fig: Apache transmits a document according to a client’s request

Depending on how the page is written and what it contains,additional processing takes place during the transfer.

For example, embedded scripts or Java applets are transferred to and execute on the client side of the connection;server side includes (discussed in the section titled “Implementing SSI”), however,are processed on the server side, as are CG I scripts, database access, and so forth.

Posted By-: Vissicomp Technology Pvt. Ltd.

Website -: http://www.vissicomp.com

Control & Loop Statement

Posted on Updated on

JavaScript if…else Statements

While writing a program, there may be a situation when you need to adopt one path out of the given two paths. So you need to make use of conditional statements that allow your program to make correct decisions and perform right actions.

JavaScript supports conditional statements which are used to perform different actions based on different conditions. Here we will explain if..else statement.

JavaScript supports following forms of if..else statement:

  • if statement
  • ..else statement
  • ..else if… statement.

if statement:

The if statement is the fundamental control statement that allows JavaScript to make decisions and execute statements conditionally.

Syntax:

if (expression){   Statement(s) to be executed if expression is true}

Example:

<script type=”text/javascript”>

var age = 20;

if( age > 18 )

{

document.write(“<b>Qualifies for driving</b>”);

}

</script>

This will produce following result:

Qualifies for driving

if…else statement:

The if…else statement is the next form of control statement that allows JavaScript to execute statements in more controlled way.

Syntax:

if (expression){

Statement(s) to be executed if expression is true

}else{

Statement(s) to be executed if expression is false

}

Example:

<script type=”text/javascript”>

var age = 15;

if( age > 18 )

{

document.write(“<b>Qualifies for driving</b>”);

}else{

document.write(“<b>Does not qualify for driving</b>”);

}

</script>

This will produce following result:

Does not qualify for driving

if…else if… statement:

The if…else if… statement is the one level advance form of control statement that allows JavaScript to make correct decision out of several conditions.

Syntax:

if (expression 1){

Statement(s) to be executed if expression 1 is true

}else if (expression 2){

Statement(s) to be executed if expression 2 is true

}else if (expression 3){

Statement(s) to be executed if expression 3 is true

}else{

Statement(s) to be executed if no expression is true

}

Example:

var book = “maths”;

if( book == “history” ){
document.write(“<b>History Book</b>”);

}else if( book == “maths” ){

document.write(“<b>Maths Book</b>”);

}else if( book == “economics” ){

document.write(“<b>Economics Book</b>”);

}else{

document.write(“<b>Unknown Book</b>”);

}

</script>

This will produce following result:

Maths Book

JavaScript Switch Case

You can use multiple if…else if statements, to perform a multiway branch. However, this is not always the best solution, especially when all of the branches depend on the value of a single variable.

Syntax:

The basic syntax of the switch statement is to give an expression to evaluate and several different statements to execute based on the value of the expression. The interpreter checks each case against the value of the expression until a match is found. If nothing matches, adefault condition will be used.

switch (expression)

{

case condition 1: statement(s)

break;

case condition 2: statement(s)

break;

case condition n: statement(s)

break;

default: statement(s)

}

The break statements indicate to the interpreter the end of that particular case.

<script type=”text/javascript”>

<!–

var grade=’A’;

document.write(“Entering switch block<br />”);

switch (grade)

{

case ‘A’: document.write(“Good job<br />”);

break;

case ‘B’: document.write(“Pretty good<br />”);

break;

case ‘C’: document.write(“Passed<br />”);

break;

case ‘D’: document.write(“Not so good<br />”);

break;

case ‘F’: document.write(“Failed<br />”);

break;

default:  document.write(“Unknown grade<br />”)

}

document.write(“Exiting switch block”);

//–>

</script>

This will produce following result:

Entering switch block

Good job

Exiting switch block

Example:

Consider a case if you do not use break statement:

<script type=”text/javascript”>

<!–

var grade=’A’;

document.write(“Entering switch block<br />”);

switch (grade)

{

case ‘A’: document.write(“Good job<br />”);

case ‘B’: document.write(“Pretty good<br />”);

case ‘C’: document.write(“Passed<br />”);

case ‘D’: document.write(“Not so good<br />”);

case ‘F’: document.write(“Failed<br />”);

default:  document.write(“Unknown grade<br />”)

}

document.write(“Exiting switch block”);

//–>

</script>

This will produce following result:

Entering switch block

Good job

Pretty good

Passed

Not so good

Failed

Unknown grade

Exiting switch block

JavaScript while Loops

While writing a program, there may be a situation when you need to perform some action over and over again. In such situation you would need to write loop statements to reduce the number of lines.

JavaScript supports all the necessary loops to help you on all steps of programming.

The while Loop

The most basic loop in JavaScript is the while loop which would be discussed in this tutorial.

Syntax:

while (expression){

Statement(s) to be executed if expression is true }

The purpose of a while loop is to execute a statement or code block repeatedly as long as expression is true. Once expression becomes false, the loop will be exited.

Example:

Following example illustrates a basic while loop:

<script type=”text/javascript”>

<!–

var count = 0;

document.write(“Starting Loop” + “<br />”);

while (count < 10){

document.write(“Current Count : ” + count + “<br />”);

count++;

}

document.write(“Loop stopped!”);

//–>

</script>

This will produce following result:

Starting Loop

Current Count : 0

Current Count : 1

Current Count : 2

Current Count : 3

Current Count : 4

Current Count : 5

Current Count : 6

Current Count : 7

Current Count : 8

Current Count : 9

Loop stopped!

The do…while Loop:

The do…while loop is similar to the while loop except that the condition check happens at the end of the loop. This means that the loop will always be executed at least once, even if the condition is false.

Syntax:

do{

Statement(s) to be executed;

} while (expression);

Note the semicolon used at the end of the do…while loop.

Example:

Let us write above example in terms of do…while loop.

<script type=”text/javascript”>

<!–

var count = 0;

document.write(“Starting Loop” + “<br />”);

do{

document.write(“Current Count : ” + count + “<br />”);

count++;

}while (count < 0);

document.write(“Loop stopped!”);

//–>

</script>

This will produce following result:

Starting Loop

Current Count :0

Loop stopped!

JavaScript for Loops

We have seen different variants of while loop. This chapter will explain another popular loop called for loop.

The for Loop

The for loop is the most compact form of looping and includes the following three important parts:

  • The loop initialization where we initialize our counter to a starting value. The initialization statement is executed before the loop begins.
  • The test statement which will test if the given condition is true or not. If condition is true then code given inside the loop will be executed otherwise loop will come out.
  • The iteration statement where you can increase or decrease your counter.

You can put all the three parts in a single line separated by a semicolon.

Syntax:

for (initialization; test condition; iteration statement){

Statement(s) to be executed if test condition is true

}

Example:

Following example illustrates a basic for loop:

<script type=”text/javascript”>

<!–

var count;

document.write(“Starting Loop” + “<br />”);

for(count = 0; count < 10; count++){

document.write(“Current Count : ” + count );

document.write(“<br />”);

}

document.write(“Loop stopped!”);

//–>

</script>

This will produce following result which is similar to while loop:

Starting Loop

Current Count : 0

Current Count : 1

Current Count : 2

Current Count : 3

Current Count : 4

Current Count : 5

Current Count : 6

Current Count : 7

Current Count : 8

Current Count : 9

Loop stopped!

JavaScript Loop Control

JavaScript provides you full control to handle your loops and switch statement. There may be a situation when you need to come out of a loop without reaching at its bottom. There may also be a situation when you want to skip a part of your code block and want to start next iteration of the look.

To handle all such situations, JavaScript provides break and continue statements. These statements are used to immediately come out of any loop or to start the next iteration of any loop respectively.

The break Statement:

The break statement, which was briefly introduced with the switch statement, is used to exit a loop early, breaking out of the enclosing curly braces.

Example:

This example illustrates the use of a break statement with a while loop. Notice how the loop breaks out early once x reaches 5 and reaches to document.write(..) statement just below to closing curly brace:

<script type=”text/javascript”><!–var x = 1;document.write(“Entering the loop<br /> “);while (x < 20){  if (x == 5){      break;  // breaks out of loop completely  }  x = x + 1;  document.write( x + “<br />”);}document.write(“Exiting the loop!<br /> “);//–></script>

This will produce following result:

Entering the loop2345Exiting the loop!

The continue Statement:

The continue statement tells the interpreter to immediately start the next iteration of the loop and skip remaining code block.

When a continue statement is encountered, program flow will move to the loop check expression immediately and if condition remain true then it start next iteration otherwise control comes out of the loop.

Example:

This example illustrates the use of a continue statement with a while loop. Notice how thecontinue statement is used to skip printing when the index held in variable x reaches 5:

<script type=”text/javascript”>

<!–

var x = 1;

document.write(“Entering the loop<br /> “);

while (x < 10)

{

x = x + 1;  if (x == 5){

continue;  // skill rest of the loop body

}

document.write( x + “<br />”);

}

document.write(“Exiting the loop!<br /> “);

//–>

</script>

This will produce following result:

Entering the loop

2

3

4

6

7

8

9

10

Exiting the loop!

Posted By-: Vissicomp Technology Pvt. Ltd.

Website -: http://www.vissicomp.com