Sunday, 28 September 2014

Deadlock Detection Algorithm Implementation In C


Deadlock Detection Algorithm   Implementation In C
 
#include <stdio.h>
#include <conio.h>   
int main()
{
    int alloc[10][10],req[10][10],ins[10],avail[10],tp,tr,i,j;
    int tmp[10]={0},count=0;
    bool finish[10]={false},flag;
    printf("Enter total no of processes:");
    scanf("%d",&tp);
    printf("Enter total no of resources:");
    scanf("%d",&tr);
    printf("Randomly Generated Allocation Matrix:\n");     //itz tp x tr order matrix
    for(i=0;i
    {
        for(j=0;j
        {
            alloc[i][j]=rand()%5;
            printf("\t%d",alloc[i][j]);
        }
        printf("\n");
    }
    printf("Randomly Generated Request Matrix:\n");
    for(i=0;i
    {
        for(j=0;j
        {
            req[i][j]=rand()%5;
            printf("\t%d",req[i][j]);
        }
        printf("\n");
    }
    printf("Randomly Generated Resource Instance Vetctor:\n");
    for(i=0;i
    {
        ins[i]=rand()%10+tr+tp;       //Just to increase resource instances
        printf("\t%d",ins[i]);
    }

    //To calculate resource availability vector

    for(i=0;i
    {
        for(j=0;j
        {
            tmp[i]+=alloc[j][i];     //sum calculated columnwise for allocation matrix
        }
    }
    printf("\nCalculated Availability Vector:\n");
    for(i=0;i
    {
        avail[i]=ins[i]-tmp[i];
        printf("\t%d",avail[i]);
    }
    //main logic starts:P
    while(count
    {                              //if finish array has all true's(all processes to running state)
                                   //deadlock not detected and loop stops!
    for(i=0;i
    {
        count=0;
        //To check whether resources can be allocated any to blocked process 
        if(finish[i]==false)
        {
            for(j=0;j
            {
                    if(req[i][j]<=avail[j])
                    {
                        count++;
                    }
            }
            flag=false;
            if(count==tr)
            {
                for(j=0;j
                {
                    avail[j]+=alloc[i][j];        //allocated reources are released and added to available!
                }
                finish[i]=true;
                printf("\nProcess %d is transferred to running state and assumed finished",i+1);
            }
            else
                flag=true;
        }
    }
    count=0;
    for(j=0;j
    {
        if(finish[j]==true)
        {
            count++;
        }
    }
    }
    for(i=0;i
    {
        if(finish[i]==false)
        {
            printf("\n Oops! Deadlock detected and causing process is:process(%d)\n",i+1);
            break;
        }
    }
    i=i-1;
    if(finish[i]==true)
    printf("\nHurray! Deadlock not detected:-)\n");
    return 0;
}
/*
Sample Output:
Enter total no of processes:7
Enter total no of resources:3
Randomly Generated Allocation Matrix:
    3    1    2
    0    3    0
    1    2    4
    1    2    2
    0    4    3
    1    0    1
    2    1    1
Randomly Generated Request Matrix:
    3    2    4
    2    0    2
    3    2    0
    4    2    2
    3    4    2
    3    1    1
    2    4    3
Randomly Generated Resource Instance Vetctor:
    11    19    14
Calculated Availability Vector:
    3    6    1
Process 3 is transferred to running state and assumed finished
Process 4 is transferred to running state and assumed finished
Process 5 is transferred to running state and assumed finished
Process 6 is transferred to running state and assumed finished
Process 7 is transferred to running state and assumed finished
Process 1 is transferred to running state and assumed finished
Process 2 is transferred to running state and assumed finished
Hurray! Deadlock not detected:-)*/

Thursday, 18 September 2014

JSP program to validate a login page

JSP program to validate a login page


<%@ page import="java.sql.*" %>
<%@ page contentType="text/html" pageEncoding="UTF-8"%>
<%
Connection conn;
int i;
PreparedStatement psmt;
Statement stmt;
ResultSet rs;
%>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>
JSP Page
</title>
</head>
<body>
<%
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","tiger");
stmt=conn.createStatement();
rs=stmt.executeQuery("select * from usertab where userid='"+request.getParameter("loginid")+'"
and password='"+request.getParameter("password")+'"");
if(rs.next())
{
resopnse.sendRedirect("homepage.jsp");
}
else
{
out.println("invalid user id and password");
}
}
catch(Exception e)
{
out.println("" +e.toString());
}
%>
</body>
</html>

Simple JSP program to connect to a database

Simple JSP program to connect to a database


    <%@ page language ="java" %>
    <%@ page import="java.sql.*" %>
    <%@ page import="java.io.*" %>
    <%@ page import="java.util.*" %>
    <html>
    <head>
    <title>sample</title>
    </head>
    <form method="post">
    UserName:
    <input type="text" fname="t1" >
    <br>
    <input type="text" lname="t2" >
    <br>
    <input type="submit" name="sub">
    <%
    String fname=request.getParameter("t1");
    String lname=request.getParameter("t2");
    %>
    <%
    try{
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    Connection con=DriverManager.getConnection("jdbc:odbc:mydatasource","system","tiger");
    PreparedStatement st;
    st = con.prepareStatement("insert into student values (1,2)");
    st.setString(1,fname);
    st.setString(2,lname);
    st.executeUpdate();
    %>
    <%   
    }
    catch(Exception e1)
    {
    out.println("cannot display the records"+e1);
    }
    %>
    </form>
    </body>
    </html>

Friday, 5 September 2014

write a java program to print all real solutions to the qudratic eq ax2+b+c=0 Read a,b,c values and use the formula (–b+sqrt(b2-4ac))/2a.



AIM:write a java program to print all real solutions to the qudratic eq ax2+b+c=0 Read a,b,c values and use the formula (–b+sqrt(b2-4ac))/2a.

SOURCE CODE:


import java.io.*;
         import java.lang.Math; class QuadraticEquation 

{

public static void main(String[] args) throws IOException

{

String s;
int a,b,c,d;
double r1,r2;
DataInputStream in=new DataInputStream(System.in);
System.out.print("Enter a value");
s=in.readLine();
a=Integer.parseInt(s);
System.out.print("Enter b value");
s=in.readLine();

b=Integer.parseInt(s); System.out.print("Enter c value?"); s=in.readLine(); c=Integer.parseInt(s); d=((b*b)-(4*a*c));

if(d<0)

{

System.err.println("No REAL solution...");

}

else

{

r1=((-b)+Math.sqrt(d))/(2*a); r2=((-b)-Math.sqrt(d))/(2*a);

System.out.println("Root 1 = "+r1);

System.out.println("Root 2 = "+r2);

}

}

}

Saturday, 30 August 2014

C PROGRAM ON STRUCTURES

C PROGRAM ON STRUCTURES

#include<stdio.h>
#include<conio.h>
void main()
{
struct notebook
{
char name[20];
int pages;
float price;
};
struct notebook nb={"long note book",192,12.50};
clrscr();
printf("name=%s/n",nb.name);
printf("page=%d/n",nb.pages);
printf("price=%f/n",nb.price);
getch();
}

C PROGRAM TO FIND AREA OF TRIANGLE

C PROGRAM TO FIND AREA OF TRIANGLE

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
 float a,b,c,s,area;
 clrscr();
 printf("enter the values of a,b,c");
 scanf("%f%f%f",&a,&b,&c);
  s=(a+b+c)/2;
  area=((s*(s-a)*(s-b)*(s-c)));
 printf("the area of numbers is %f%f%f is %f",a,b,c,area);
 getch();
}

C PROGRAM TO FIND BIGGEST OF GIVEN THREE NUMBERS

C PROGRAM TO FIND BIGGEST OF GIVEN THREE NUMBERS

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
 int a,b,c,s;
 clrscr();
 printf("enter the values a,b,c");
 scanf("%d%d%d",&a,&b,&c);
  s=a>b?(a>c?a:c):(b>c?b:c);
 printf("the largest three numbers is %d%d%d %d",a,b,c,s);
 getch();
}

C PROGRAM ON FIBONACCI SERIES

C PROGRAM ON FIBONACCI SERIES


#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c,i,n;
clrscr();
printf("<=fibonacie series to find the numbers between 1 to n series=>");
printf("\n\n\n\t\t\t enter the size(n):",n);
scanf("%d",&n);
printf("\n\n\t\t\t<........fibonacie series.........>",n);
printf("\n\n %d%d",n);
for(i=0;i<=n-2;i++)
{
c=a+b;
printf("  ");
a=b;
b=c;
}
getch();
}

C PROGRAM ON BUBBLE SORT

C PROGRAM ON BUBBLE SORT


#include<stdio.h>
#include<conio.h>
#define max 10
void main()
{
int a[max],i,n;
printf("Bubble sort:");
printf("======\n");
printf("enter no.of integers:");
scanf("%d",&a[i]);
printf("\n enter %d integer:",n);
for(i=0;i<n;i++)
scanf("%d",a[i]);
bubble(a,n);
printf("\n\n elements before sorting:");
scanf("%d",&n);
printf("\n\n elements after sorting:");
scanf("%d",&n);
{
int i,j,temp;
int x[1];
{
for(i=0;i<n-1;i++)
{
for(j=0;j<n-1;j++)
{
if(x[i]>x[j+1]);
}
temp=x[j];
x[j]=x[i];
x[i]=temp;
}
}
}
}

C Program to read a file and display its contents along with line numbers before each line.

C Program to read a file and display its contents along with line numbers before each line.


#include<stdio.h>
void main()
{
FILE *fp;
char ch;
char source[67];
int count = 1;
clrscr();
puts("enter the file name:");
gets(source);
fp=fopen(source,"r");// read only mode for the source file
if(fp==NULL)
{
puts("unable to open the file:");
getch();
exit();
}
clrscr();
printf("file name:%s",source);
printf("\n line:-%d\t",count);
while((ch=getc(fp))!=EOF)
{
if(ch=='\n')
{
count++;
printf("\nline:-%d\t",count);
}
else
{
printf("%c",ch);
}
}
printf("\n press any key...");
getch();
fclose(fp);
}

c program by using structure to pointers


c program by using structure to pointers


#include<stdio.h>
#include<string.h>
struct student
{
    int id;
  char name[30];
float percentage;
};
  void main()
{
  int I;
clrscr();
struct student record1={1,’xxxx”,36.5}
struct student record2, *record3, *ptr1, record4;

printf(“Records of STUDENT1-record structure \n);
printf(“Id:%d\n Name:%s\n Percentage:%f\n”, record1.id, record1.name, record1.percentage);

record2=record1;

printf(“Records of STUDENT1-Direct copy from record1  \n);
printf(“Id:%d\n Name:%s\n Percentage:%f\n”, record2.id, record2.name, record2.percentage);

ptr1=&record1;
memcpy(record3, ptr1, sizeof(record1));

printf(“Records of STUDENT1- copied from record1 using memcpy  \n);
printf(“Id:%d\n Name:%s\n Percentage:%f\n”, record3.id, record3.name, record3.percentage);


ptr1=&record1;
memcpy(record3, ptr1, sizeof(record1));




printf(“Records of STUDENT1- copied individual members from  record1  \n);
printf(“Id:%d\n Name:%s\n Percentage:%f\n”, record4.id, record4.name, record4.percentage);
strcpy(record4.name,record1.name);

    record4.percentage = record1.percentage;

    printf("  Id : %d \n  Name : %s\n  Percentage : %f\n", record4.id, record4.name, record4.percentage);

     getch();
}