Friday, 16 February 2018


Program For identify keywords and Identifiers

#include<stdio.h>
#include<ctype.h>
#include<string.h>
void keyw(char *p);
int i=0,id=0,kw=0,num=0,op=0;
char keys[32][10]={"auto","break","case","char","const","continue","default",
"do","double","else","enum","extern","float","for","goto",
"if","int","long","register","return","short","signed",
"sizeof","static","struct","switch","typedef","union",
"unsigned","void","volatile","while"};
main()
{
        char ch,str[25],seps[15]=" \t\n,;(){}[]#\"<>",oper[]="!%^&*-+=~|.<>/?";
        int j;
        char fname[50];
        FILE *f1;
        //clrscr();
printf("enter file path (drive:\\fold\\filename)\n");
scanf("%s",fname);
f1 = fopen(fname,"r");
//f1 = fopen("Input","r");
        if(f1==NULL)
        {
         printf("file not found");
         exit(0);
        }
        while((ch=fgetc(f1))!=EOF)
        {
for(j=0;j<=14;j++)
{
if(ch==oper[j])
{
printf("%c is an operator\n",ch);
op++;
str[i]='\0';
keyw(str);
}
}
for(j=0;j<=14;j++)
{
if(i==-1)
break;
if(ch==seps[j])
{
if(ch=='#')
{
while(ch!='>')
{
printf("%c",ch);
ch=fgetc(f1);
}
printf("%c is a header file\n",ch);
i=-1;
break;
}
if(ch=='"')
{
do
{
ch=fgetc(f1);
printf("%c",ch);
}while(ch!='"');
printf("\b is an argument\n");
i=-1;
break;
}
str[i]='\0';
keyw(str);
}
}
if(i!=-1)
{
str[i]=ch;
i++;
}
else
i=0;
        }
printf("Keywords: %d\nIdentifiers: %d\nOperators: %d\nNumbers: %d\n",kw,id,op,num);
//getch();
}
void keyw(char *p)
{
int k,flag=0;
for(k=0;k<=31;k++)
{
if(strcmp(keys[k],p)==0)
{
printf("%s is a keyword\n",p);
kw++;
flag=1;
break;
}
}
if(flag==0)
{
if(isdigit(p[0]))
{
printf("%s is a number\n",p);
num++;
}
else
{
//if(p[0]!=13&&p[0]!=10)
if(p[0]!='\0')
{
printf("%s is an identifier\n",p);
id++;
}
}
}
i=-1;
}



Output:








CLIENT SERVER CHAT APPLICATION
EXP of parallel distributed system

SERVER


import java.io.*;
import java.net.*;
public class DatagramServer
{
public static void main(String args[])
{
DatagramSocket sock=null;
try
{
sock=new DatagramSocket(7777);
byte[] buffer= new byte[65536];
DatagramPacket incoming=new DatagramPacket(buffer,buffer.length);
echo("server socket created.waiting for incoming data.....");
while(true)
{
sock.receive(incoming);
byte[] data=incoming.getData();
String s =new String(data,0,incoming.getLength());
echo("Server Says OK....Your message is:"+s);
DatagramPacket dp=new DatagramPacket(s.getBytes(),s.getBytes().length,incoming.getAddress(),incoming.getPort());
sock.send(dp);
}
}
catch(IOException e)
{
System.err.println("IOException" +e);
}
}
public static void echo(String msg)
{
System.out.println(msg);
}
}


CLIENT


import java.io.*;

import java.net.*;

import java.util.*;

public class DatagramClient

{

public static void main(String args[])

{

DatagramSocket sock=null;

int port=7777;

String s;

Scanner cin=new Scanner(System.in);

try

{

sock=new DatagramSocket();

InetAddress host=InetAddress.getByName("localhost");

while(true)

{

echo("enter messsage to send");

s=cin.next();

byte[] b=s.getBytes();

DatagramPacket dp=new DatagramPacket(b,b.length,host,port);

sock.send(dp);

byte[] buffer=new byte[65536];

DatagramPacket reply=new DatagramPacket(buffer,buffer.length);

sock.receive(reply);

byte[] data=reply.getData();

s=new String(data ,0,reply.getLength());

}

}

catch(IOException e)

{

System.err.println("IOException" +e);

}

}

public static void echo(String msg)

{

System.out.println(msg);

}

}


OUTPUT:-


Monday, 26 December 2016

SHELL PROGRAMING

Factorial

#!/bin/bash
echo "Total no of factorial wants"
read fact

ans=1
counter=0
while [ $fact -ne $counter ]
do
        counter=`expr $counter + 1`
        ans=`expr $ans \* $counter`

done
Output:-

SJF-SHORTEST JOB FIRST


SHORTEST JOB FIRST

import java.util.*;
class SJF {
 public static void main(String args[]) {
  Scanner sc = new Scanner(System.in);
  int n, BT[], WT[], TAT[];
  System.out.println("Enter no of process");
  n = sc.nextInt();
  BT = new int[n + 1];
  WT = new int[n + 1];
  TAT = new int[n + 1];
  float AWT = 0;
  System.out.println("Enter Burst time for each process");
  for (int i = 0; i < n; i++) {
   System.out.println("Enter BT for process " + (i + 1));
   BT[i] = sc.nextInt();
  }
  for (int i = 0; i < n; i++) {
   WT[i] = 0;
   TAT[i] = 0;
  }
  int temp;
  for (int i = 0; i < n; i++) {
   for (int j = 0; j < n - 1; j++) {
    if (BT[j] > BT[j + 1]) {
     temp = BT[j];
     BT[j] = BT[j + 1];
     BT[j + 1] = temp;
     temp = WT[j];
     WT[j] = WT[j + 1];
     WT[j + 1] = temp;
    }
   }
  }
  for (int i = 0; i < n; i++) {
   TAT[i] = BT[i] + WT[i];
   WT[i + 1] = TAT[i];
  }
  TAT[n] = WT[n] + BT[n];
  System.out.println(" PROCESS BT WT TAT ");
  for (int i = 0; i < n; i++)
   System.out.println(" " + i + " " + BT[i] + " " + WT[i] + " " + TAT[i]);
  for (int j = 0; j < n; j++)
   AWT += WT[j];
  AWT = AWT / n;
  System.out.println("***********************************************");
  System.out.println("Avg waiting time=" + AWT + "\n***********************************************");
 }
}

OUTPUT:


FCFS- FIRST COME FIRST SERVE



First Come First Serve (FCFS)

  • Jobs are executed on first come, first serve basis.
  • It is a non-preemptive, pre-emptive scheduling algorithm.
  • Easy to understand and implement.
  • Its implementation is based on FIFO queue.
  • Poor in performance as average wait time is high.








FIRST COME
 FIRST SERVE PROGRAM





import java.util.*;
class FCFS
{
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
int p;
float t1=0,t2=0;
System.out.println("Enter number of processes");
p=in.nextInt();
int bt[]=new int[p];
System.out.println("Enter burst time");
for(int i=0;i<p;i++)
{
System.out.println("Burst time for P"+(i+1)+"=");
bt[i]=in.nextInt();
}
int wt[]=new int[p];
wt[0]=0;
for(int i=1;i<p;i++)
{
wt[i]=bt[i-1]+wt[i-1];
t1+=wt[i];
}
int tat[]=new int[p];
for(int i=0;i<p;i++)
{
tat[i]=bt[i]+wt[i];
t2+=tat[i];
}
System.out.println("Process\tBurst time\tWaiting time\tTurn Around time");
for(int i=0;i<p;i++)
{
System.out.println("P"+(i+1)+"\t\t"+bt[i]+"\t\t"+wt[i]+"\t\t"+tat[i]);
}
float awt,atat;
System.out.println("Average Waiting time="+t1/p);
System.out.println("Average Turn Around time="+t2/p);
}

OUTPUT :

IF ANY ERRORS COMMENTS  :)

SHELL SCRIPT AND PROCESS

Prime Number

#!/bin/bash
echo -n "Enter a number: "
read num
i=2
while [ $i -lt $num ]
do
  if [ `expr $num % $i` -eq 0 ]
  then
      echo "$num is not a prime number"
      echo "Since it is divisible by $i"
      exit
  fi
  i=`expr $i + 1`
done

echo "$num is a prime number "
Save your Program in .sh

Basic linux command Operating System MU