Wednesday, 2 May 2012

IMPLEMENTATION OF CPU SCHEDULING ALGORITHMS


//IMPLEMENTATION OF FCFS SCHEDULING
#include<stdio.h>
#include<malloc.h>
struct node
{
char name[15];
int btime,atime;
struct node *next;
};
int main()
{
struct node *head,*temp,*tp,*p;
int d,ch,n=0;
head=NULL;
do
{
temp=(struct node *)malloc(sizeof(struct node));
printf("\nEnter the name of the process:");
scanf("%s",&temp->name);
printf("\nEnter the arrival time:");
scanf("%d",&temp->atime);
printf("\nEnter the burst time:");
scanf("%d",&temp->btime);
temp->next=NULL;
if(temp==NULL)
printf("\nNode is not created..");
if(head==NULL)
head=temp;
else
{
if(temp->atime<head->atime)
{
temp->next=head;//swapping head and temp
head=temp;
}
else if(temp->atime>head->atime)
{
tp=head;
while((tp!=NULL) && (temp->atime>tp->atime))
{

p=tp;
tp=tp->next;
}
p->next=temp;
temp->next=tp;
}
}
printf("\nPress '1' to continue:");
scanf("%d",&ch);
}while(ch==1);
printf("\nGantt chart:");
if(head==NULL)
printf("\nList is empty...! ");
else
{
tp=head;
printf("\n");
printf("\t%s",tp->name);
tp=tp->next;
while(tp!=NULL)
{
printf("\t%s",tp->name);
tp=tp->next;
}
printf("\t%s",tp->name);
printf("\n");
tp=head;
printf("\n");
printf("0");
while(tp->next!=NULL)
{
n=n+tp->btime;
printf("\t%d",n);
tp=tp->next;
}
n=n+tp->btime;
printf("\t%d",n);
}
}

OUTPUT:
Enter the name of the process:P1
Enter the arrival time:0
Enter the burst time:4
Press '1' to continue:1
Enter the name of the process:P2
Enter the arrival time:3
Enter the burst time:1
Press '1' to continue:1
Enter the name of the process:P3
Enter the arrival time:4
Enter the burst time:2
Press '1' to continue:1
Enter the name of the process:P4
Enter the arrival time:6
Enter the burst time:3
Press '1' to continue:1
Enter the name of the process:P5
Enter the arrival time:8
Enter the burst time:2
Press '1' to continue:0

Gantt chart:
P1 P2
0
4
5

P3
7

P4
10

P5
12

(null)

// IMPLEMENTATION OF SJF SCHEDULING
#include<stdio.h>
#include<malloc.h>
struct node
{
char name[15];
int btime,atime;
struct node *next;
};
int main()
{
struct node *head,*temp,*tp,*p;
int d,ch,n=0;
head=NULL;
do
{
temp=(struct node *)malloc(sizeof(struct node));
printf("\nEnter the name of the process:");
scanf("%s",&temp->name);
printf("\nEnter the burst time:");
scanf("%d",&temp->btime);
temp->next=NULL;
if(temp==NULL)
printf("\nNode is not created..");
if(head==NULL)
head=temp;
else
{
if(temp->btime<head->btime)
{
temp->next=head;//swapping head and temp
head=temp;
}
}
if(temp->btime>head->btime)
{
tp=head;
while((tp!=NULL) && (temp->btime>tp->btime))
{
p=tp;

tp=tp->next;
}
p->next=temp;
temp->next=tp;
}
printf("\nPress '1' to continue:");
scanf("%d",&ch);
}while(ch==1);
printf("\nGantt chart:");
if(head==NULL)
printf("\nList is empty...! ");
else
{
tp=head;
printf("\n");
printf("\t%s",tp->name);
tp=tp->next;
while(tp!=NULL)
{
printf("\t%s",tp->name);
tp=tp->next;
}
printf("\t%s",tp->name);
printf("\n");
tp=head;
printf("\n");
printf("0");
while(tp->next!=NULL)
{
n=n+tp->btime;
printf("\t%d",n);
tp=tp->next;
}
n=n+tp->btime;
printf("\t%d",n);
}
}

OUTPUT:
Enter the name of the process:P1
Enter the burst time:5
Press '1' to continue:1
Enter the name of the process:P2
Enter the burst time:9
Press '1' to continue:1
Enter the name of the process:P3
Enter the burst time:2
Press '1' to continue:1
Enter the name of the process:P4
Enter the burst time:6
Press '1' to continue:1
Enter the name of the process:P5
Enter the burst time:12
Press '1' to continue:0

Gantt chart:
P3 P1
0
2
7

P4
13

P2
22

P5
34

(null)

//IMPLEMENTATION OF PRIORITY SCHEDULING
#include<stdio.h>
#include<malloc.h>
struct node
{
char name[15];
int btime,priority;
struct node *next;
};
int main()
{
struct node *head,*temp,*tp,*p;
int d,ch,n=0;
head=NULL;
do
{
temp=(struct node *)malloc(sizeof(struct node));
printf("\nEnter the name of the process:");
scanf("%s",&temp->name);
printf("\nEnter the priority:");
scanf("%d",&temp-> priority);
printf("\nEnter the burst time:");
scanf("%d",&temp->btime);
temp->next=NULL;
if(temp==NULL)
printf("\nNode is not created..");
if(head==NULL)
head=temp;
else
{
if(temp-> priority <head-> priority)
{
temp->next=head;//swapping head and temp
head=temp;
}
else if(temp-> priority >head-> priority)
{
tp=head;
while((tp!=NULL) && (temp-> priority >tp-> priority))
{

p=tp;
tp=tp->next;
}
p->next=temp;
temp->next=tp;
}
}
printf("\nPress '1' to continue:");
scanf("%d",&ch);
}while(ch==1);
printf("\nGantt chart:");
if(head==NULL)
printf("\nList is empty...! ");
else
{
tp=head;
printf("\n");
printf("\t%s",tp->name);
tp=tp->next;
while(tp!=NULL)
{
printf("\t%s",tp->name);
tp=tp->next;
}
printf("\t%s",tp->name);
printf("\n");
tp=head;
printf("\n");
printf("0");
while(tp->next!=NULL)
{
n=n+tp->btime;
printf("\t%d",n);
tp=tp->next;
}
n=n+tp->btime;
printf("\t%d",n);
}
}

OUTPUT:
Enter the name of the process:P1
Enter the priority:7
Enter the burst time:5
Press '1' to continue:1
Enter the name of the process:P2
Enter the priority:4
Enter the burst time:2
Press '1' to continue:1
Enter the name of the process:P3
Enter the priority:0
Enter the burst time:10
Press '1' to continue:1

Gantt chart:
P3
P2
0
10
12

P1
17

(null)

IMPLEMENTATION OF SYSTEM CALLS IIN LINUX


//IMPLEMENTATION OF ls SYSTEM CALL
#include<stdio.h>
#include<dirent.h>
int main(int argc,char *argv[])
{
struct dirent *dp;
DIR *d;
if ( argc!=2 )
printf("No directory name found...");
else
{
if (( d=opendir(argv[1])) == NULL )
printf("Directory cannot be opened");
else
{
while (dp=readdir(d))
printf("\n%s",dp->d_name);0

closedir(d);
exit(0);
}
}
}

OUTPUT:
[student@localhost ex5]$ ./a.out ice
ice
process.c
shan
process2.c
file2.txt

//IMPLEMENTATION OF cat SYSTEM CALL
#include<stdio.h>
#include<sys/stat.h>
#include<string.h>
int main()
{
char fname[21],data[0];
int fd;

printf("Enter filename to be created:");
scanf("%s",fname);
if (( fd=creat(fname,S_IRUSR|S_IWUSR|S_IXUSR ))<0)
printf("\nFile Creation Error...!");
else
printf("\nEnter your data:");
while(fgets(data,50,stdin))
{
data[strlen(data)-1]='\0';
printf("FD=%d",fd);
printf("\n Data=%s\n",data);
if(strcmp(data,"END")==0)
break;
if((write(fd,data,strlen(data)))!=strlen(data))
{
//printf("\n Data1=%s",data);
printf("\nWriteError1...!");
}
}
if((write(fd,"\n",1))!=1)
printf("\nWrite Error2...!");
close(fd);
}

OUTPUT
Enter filename to be created:abinaya
Enter your datatype END to exit:operating system
END

AFTER CREATING FILE
operating system


//FILE MANIPULATIONS
#include<stdio.h>
#include<sys/stat.h>
#include<fcntl.h>
int main()
{
struct stat stbuf;
char file[10],old[20],new[20],filename[20];
int amode,ch;
printf("\n1.Rename 2.Remove 3.Change access 4.Display access mode");
do
{
printf("\nEnter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:printf("\nEnter file to rename:");
scanf("%s",old);
printf("\nEnter the new name:");
scanf("%s",new);
if(rename(old,new)==0)
printf("\n%s is renamed as %s",old,new);
else
printf("\nError in renaming %s",old); break;
case 2:printf("Enter filename to be removed:");
scanf("%s",file);
if(remove(file)==0)
printf("\n%s is removed",file);
else
printf("\nError in removing %s",file); break;
case 3:printf("\nEnter the name of the file to change its access mode:");
scanf("%s",filename);
if((stat(filename,&stbuf))!=0)
{
printf("\nError in moving status info of %s to the strct variable
stbuf..",filename); break;
}
if(stbuf.st_mode & S_IWRITE)
{
printf("\n%s is Changed to READ ACCESS ONLY",filename);
amode=S_IREAD;
}
else
{
printf("\n%s is Changed to WRITE ACCESS ONLY",filename);
amode=S_IWRITE;

}
if(chmod(filename,amode)!=0)
{
printf("\nError in changing the access mode...!");
break;
} break;
case 4:printf("\nCheck the access mode:READ/WRITE mode:");
printf("\nEnter name of a file:");
scanf("%s",filename);
if((access(filename,R_OK))==0)
printf("\n%s has READ permission only..",filename);
if((access(filename,W_OK))==0)
printf("\n%s has WRITE permission only..",filename);
else
printf("\n%s does not have read/write permission");
break;
default:printf("\nInvalid choice...!"); break;
}
}while(ch<=4);
}

OUTPUT

1.Rename 2.Remove 3.Change access 4.Display access mode
Enter your choice:1
Enter file to rename:sum.sh
Enter the new name:add.sh
sum.sh is renamed as add.sh
Enter your choice:2
Enter filename to be removed:add.sh
add.sh is removed
Enter your choice:3
Enter the name of the file to change its access mode:first.c
first.c is Changed to READ ACCESS ONLY
Enter your choice:4
Check the access mode:READ/WRITE mode:
Enter name of a file:first.c
first.c has READ permission only..
Enter your choice:5
Invalid choice…!

Tuesday, 6 March 2012

Random color balls(Multithreaded GUI)


CODING:
import java.awt.*;
import java.awt.event.*;
import javax.swing.JFrame;
class sample extends Canvas implements Runnable
{
Thread t;int i;
int a=50,b=250,interval=0;
Color color=Color.yellow;
public sample(int interval,Color color)
{
this.interval=interval;
this.color=color;
setSize(250,250);
setVisible(true);
t=new Thread(this);
t.start();
}
public void run()
{
while(true)
{
for(i=0;i<=40;i++)
{
try
{
t.sleep(interval);
}
catch(InterruptedException e)
{
}
b=b-5;a=a+2;
repaint();
}
for(i=0;i<=40;i++)
{
try
{
t.sleep(interval);
}
catch(InterruptedException e)
{
}
b=b+5;a=a+2;
repaint();
}
for(i=0;i<=80;i++)
{
try
{
t.sleep(interval);
}
catch(InterruptedException e)
{
}
a=a-2;
repaint();
}
}
}
public void paint(Graphics g)
{
int u,y,z;
Color c;
u=(int)(Math.random()*255);
z=(int)(Math.random()*255);
y=(int)(Math.random()*255);
c=new Color(u,y,z);
g.setColor(c);
g.fillOval(a,b,50,50);
}
}
class ThreadGUI extends Frame
{
ThreadGUI()
{
setSize(900,350);
setVisible(true);
Panel pan=new Panel();
pan.setLayout(new GridLayout(1,3));
sample sam1=new sample(5,Color.red);
sample sam2=new sample(6,Color.blue);
sample sam3=new sample(4,Color.green);
pan.add(sam1);
pan.add(sam2);
pan.add(sam3);
setLayout(new GridLayout(1,1));
add(pan);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent el)
{
System.exit(0);
}
}
);
}
public static void main(String args[])
{
new ThreadGUI();
}
}

Natural Scenery


Wednesday, 21 December 2011

GCD of 2 numbers


import java.io.*;
class gcd
{
public static void main(String args[])
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
try
{
int a,b,c;
System.out.printf("Enter two nos:");
a=Integer.parseInt(br.readLine());
b=Integer.parseInt(br.readLine());
while(b!=0)
{
c=a%b;
a=b;
b=c;
}
System.out.printf("GCD :" + a);
}
catch(Exception e)
{
System.out.println("Error.....");
}
}
}