PCDP All Program



PROG 1

calculater
(defvar a)
(defvar b)
(defvar c)
(write-line "Enter the value of a and b")
(setf a(read))
(setf b(read))
(sb-thread:make-thread(lambda()
                  (progn
                  (sleep 0)
                  (setf c(+ a b))
                  (print "Addition in binary")
                  (format t "~b" c)
                  (print "addition in decimal")
                  (print c))))


(sb-thread:make-thread(lambda()
                  (progn
                  (sleep 0)
                  (setf c(- a b))
                  (print "substraction in binary")
                  (format t "~b" c)
                  (print "substraction in decimal")
                  (print c))))

(sb-thread:make-thread(lambda()
                  (progn
                  (sleep 0)
                  (setf c(* a b))
                  (print "multiplication in binary")
                  (format t "~b" c)
                  (print "multiplication in decimal")
                  (print c))))

(sb-thread:make-thread(lambda()
                  (progn
                  (sleep 0)
                  (setf c(/ a b))
                  (print "division in binary")
                  (format t "~b" c)
                  (print "divsion in decimal")
                  (print c))))

(sb-thread:make-thread(lambda()
                  (progn
                  (sleep 0)
                  (setf c(* a a))
                  (print "square in binary")
                  (format t "~b" c)
                  (print "square in decimal")
                  (print c))))

(sb-thread:make-thread(lambda()
                  (progn
                  (sleep 0)
                  (setf c(* b b))
                  (print "square in binary")
                  (format t "~b" c)
                  (print "square in decimal")
                  (print c))))

(sb-thread:make-thread(lambda()
                  (progn
                  (sleep 0)
                  (setf c(min a b))
                  (print "min in binary")
                  (format t "~b" c)
                  (print "min in decimal")
                  (print c))))

(sb-thread:make-thread(lambda()
                  (progn
                  (sleep 0)
                  (setf c(max a b))
                  (print "max in binary")
                  (format t "~b" c)
                  (print "max in decimal")
                  (print c))))

(sb-thread:make-thread(lambda()
                  (progn
                  (sleep 0)
                  (setf c(sin a ))
                  (print "sin in binary")
                  (format t "~b" c)
                  (print "sin in decimal")
                  (print c))))

(sb-thread:make-thread(lambda()
                  (progn
                  (sleep 0)
                  (setf c(cos a))
                  (print "cos in binary")
                  (format t "~b" c)
                  (print "cos in decimal")
                  (print c))))
*****************************output******************************************************
[admin@localhost ~]$ sbcl
* (load"pgm1.lisp")

 
Narray

#include<stdio.h>
#include<omp.h>
int a[65536],s,test;
int global_size,global_x,n=4;
void nary_search(int,int);
main()
{
int i,size=65536,x=0;
for(i=0;i<size;i++)
a[i]=i*1;
printf("\n no to be searched");
scanf("%d",&s);
 nary_search(size,x);
}
void nary_search(int size,int x)
{
printf("size=%d",size);
if(size<=4)
{
test=0;
#pragma omp parallel
{
int tid=omp_get_thread_num();
if(a[global_x+tid]==s)
{
printf("found at %d\n",global_x+tid);
test=1;
}
}
if(test==0)
{
printf("Not found\n");
}
}

else
{
test=0;
#pragma omp parallel
{
int tid=omp_get_thread_num();
printf("checking(%d...%d)with thred%d on cpu%d\n",a[tid*size/n+x],a[tid*size/n+size/n+x],tid,sched_getcpu());
if(s>=a[tid*size/n+x]&&s<=a[tid*size/n+size/n-1+x])
{
printf("may be here%d..%d size =(%d)\n",a[tid*size/n+x],a[tid*size/n+size/n+size/n-1+x]);
global_size=size/n;
global_x=tid*global_size+x;
test=1;
}
}
if(test==1)
nary_search(global_size,global_x);
else
printf("not found");
}
}

*******************output***********************
[admin@localhost Documents]$ gcc -fopenmp nary.c
[admin@localhost Documents]$ ./a.out
enter number to be searched
4
size = 65536
checking (0--16383)with thread 0 on cpu 2
may be here 0--16383 size=( 16384 )
checking (32768--49151)with thread 2 on cpu 3
checking (16384--32767)with thread 1 on cpu 1
checking (49152--65535)with thread 3 on cpu 0
size = 16384
checking (12288--16383)with thread 3 on cpu 0
checking (0--4095)with thread 0 on cpu 2
may be here 0--4095 size=( 4096 )
checking (8192--12287)with thread 2 on cpu 3
checking (4096--8191)with thread 1 on cpu 1
size = 4096
checking (3072--4095)with thread 3 on cpu 0
checking (0--1023)with thread 0 on cpu 2
may be here 0--1023 size=( 1024 )
checking (1024--2047)with thread 1 on cpu 3
checking (2048--3071)with thread 2 on cpu 1
size = 1024
checking (768--1023)with thread 3 on cpu 0
checking (0--255)with thread 0 on cpu 2
may be here 0--255 size=( 256 )
checking (512--767)with thread 2 on cpu 3
checking (256--511)with thread 1 on cpu 1
size = 256
checking (0--63)with thread 0 on cpu 2
may be here 0--63 size=( 64 )
checking (128--191)with thread 2 on cpu 1
checking (64--127)with thread 1 on cpu 3
checking (192--255)with thread 3 on cpu 0
size = 64
checking (0--15)with thread 0 on cpu 2
may be here 0--15 size=( 16 )
checking (48--63)with thread 3 on cpu 3
checking (32--47)with thread 2 on cpu 1
checking (16--31)with thread 1 on cpu 0
size = 16
checking (4--7)with thread 1 on cpu 0
may be here 4--7 size=( 4 )
checking (8--11)with thread 2 on cpu 1
checking (12--15)with thread 3 on cpu 3
checking (0--3)with thread 0 on cpu 2
size = 4
found at 4




Reader writer
#include<stdio.h>
#include<time.h>
#include<unistd.h>
#include<omp.h>

void main()
{
    int s=0;
    #pragma omp parallel num_threads(2)
    {
        int tid = omp_get_thread_num();
        int x;
        while(1)
        {
            if(tid == 0)
                printf("tid -> %d \t%d) read\t%d)write \n ",tid ,2*tid , 2*tid +1);
            if(tid == 1)
                printf("tid-> %d \t%d) read\t%d)write \n ",tid ,2*tid , 2*tid+1);

            scanf("%d",&s);
            switch(x)
            {
                case 0: while(s==1)
                    printf("Shared memory is in used\n");
                    printf("Thread 0 is reading\n");
                    sleep(5);
                    printf("Thread 0 complete reading\n");
                    break;

                case 1:while(s==1)
                    printf("Shared memory is in used\n");
                    printf("Thread 0 is Writing\n");
                    sleep(5);
                    printf("Thread 0 complete Writing\n");
                    break;
              
                case 2:while(s==1)
                    printf("Shared memory is in used\n");
                    printf("Thread 1 is reading\n");
                    sleep(5);
                    printf("Thread 1 complete reading\n");
                    break;
                case 3:while(s==1)
                    printf("Shared memory is in used\n");
                    printf("Thread 1 is Writing\n");
                    sleep(5);
                    printf("Thread 1 is writing compliting\n");                  
                    break;
                  
            }
        }
    }
}

OUTPUT:

[admin@localhost ~]$ su
Password:
[root@localhost admin]# cd Desktop
[root@localhost Desktop]# gcc -fopenmp red.c
[root@localhost Desktop]# ./a.out

 tid==> 0    0)read      1)write
enter ur choice
 tid ==> 1 2)read       3)write
0
enter ur choice
 thread 0 is reading

thread 0 is complete reading
 tid==> 0    0)read      1)write
1
enter ur choice
 thread 0 is writing

 thread 0 is complete writing
 tid ==> 1 2)read       3)write
2
enter ur choice
 thread 1 is reading

 thread 1 is complete reading
 tid==> 0    0)read      1)write
3
enter ur choice
 thread 1 is writing

 thread 1 is complete writing
 tid ==> 1 2)read       3)write



PROG 4
yacc

*************************************************************
new.l
***********************************************************************
%{
#include<stdio.h>
#include "y.tab.h"

#define YYSTYPE int
%}
%option reentrant bison-bridge

%%
[0-9]+ {
          *yylval=atoi(yytext);
          return NUMBER;
       }
[\t] ;
[\n] return 0;
. return yytext[0];
%%
int yywrap(yyscan_t yyscanner)
{
return 1;
}

**********************************************************
                                                                new.txt
**********************************************************

2+3*5
5+3-4
5+5-5*5
6+4/2-3
2+3*4
5+3-3
5+5-5*4
6+4/2-1

**********************************************************
                                                                new.y
**********************************************************

%{
    #include<stdio.h>
    #include<stdlib.h>
    #include<omp.h>
    
    #define YYSTYPE int

    int numofthreads,expsperthread;
    char inputfilename[30];
%}
%token NUMBER

%left '+' '-'
%left '*' '/' '%'
%left '(' ')'

%pure-parser
%lex-param {void * scanner}
%parse-param {void * scanner}

%%

ArithmeticExpression: E{
          
         //printf("\nResult=%d\n",$$);
      
         return $$;
        }
E:E'+'E {$$=$1+$3;}
 |E'-'E {$$=$1-$3;}
 |E'*'E {$$=$1*$3;}
 |E'/'E {$$=$1/$3;}
 |E'%'E {$$=$1%$3;}
 |'('E')' {$$=$2;}
 | NUMBER {$$=$1;}
;

%%

void func()
{
void * scanner;
    yylex_init(&scanner);

FILE *fp;
fp=fopen(inputfilename,"r");

int i,j;

int tid;
int res;
 
tid=omp_get_thread_num(); 

char *s=malloc(100);
size_t n=100;

for(i=0;i<(expsperthread*tid);i++)
  {
    getline(&s,&n,fp);
  }

yyset_in(fp,scanner);

for(j=0;j<expsperthread;j++)
  {
    res=yyparse(scanner);

    printf("%dth Expression=%d is given by Thread %d\n",(expsperthread*tid+j+1),res,tid);
  }

    yylex_destroy(scanner);

fclose(fp);
}

void main()
{
printf("Enter Input Text File Name\n");
scanf("%s",inputfilename);
printf("Enter Number of threads to be created\n");
scanf("%d",&numofthreads);
printf("Enter Number of Arithmetic Expressions to be evaluated by each thread\n");
scanf("%d",&expsperthread);

#pragma omp parallel num_threads(numofthreads)
  {
    func();
  }
}

int yyerror()
{
   printf("\nArithmetic expression is Invalid\n\n");
} 



***************************************************************

                                                                                OutPut
***************************************************************




/*

[root@localhost prac4]# byacc -d new.y
[root@localhost prac4]# flex new.l
[root@localhost prac4]# gcc -fopenmp y.tab.c lex.yy.c
[root@localhost prac4]# ./a.out
Enter Input Text File Name
new.txt
Enter Number of threads to be created
4
Enter Number of Arithmetic Expressions to be evaluated by each thread
2
7th Expression=-10 is given by Thread 3
5th Expression=14 is given by Thread 2
1th Expression=17 is given by Thread 0
3th Expression=-15 is given by Thread 1
4th Expression=5 is given by Thread 1
6th Expression=5 is given by Thread 2
2th Expression=4 is given by Thread 0
8th Expression=7 is given by Thread 3
[root@localhost prac4]#

*/ 
Prog 5


Program :-Socket Programing

*********************Server Program :*******************

import java.io.*;
import java.net.*;

public class Server2
{
                public static void main(String args[])
                {
                                int port = 6789;
                                Server2 server = new Server2( port );
                                server.startServer();
                }

                ServerSocket echoServer = null;
                Socket clientSocket = null;
                int numConnections = 0;
                int port;
  
                public Server2( int port )
                {
                                this.port = port;
                }

                public void stopServer()
                {
                                System.out.println( "Server cleaning up." );
                                System.exit(0);
                }

                public void startServer()   
                {

                                try
                                {
                                                echoServer = new ServerSocket(port);
                }
                catch (IOException e)  
                                {
                                                System.out.println(e);
                } 
                                System.out.println( "Server is started and is waiting for connections." );
                                System.out.println( "With multi-threading, multiple connections are allowed." );
                                System.out.println( "Any client can send -1 to stop the server." );

                                while ( true )
                                {
                                                try
                                                {
                                                                clientSocket = echoServer.accept();
                                                                numConnections ++;
                                                                Server2Connection oneconnection = new Server2Connection(clientSocket, numConnections, this);
                                                                new Thread(oneconnection).start();
                                                } 
                                                catch (IOException e)
                                                {
                                                                System.out.println(e);
                                                }
                                }
                }
}

class Server2Connection implements Runnable
{
                BufferedReader is;
                PrintStream os;
                Socket clientSocket;
                int id;
                Server2 server;

                public Server2Connection(Socket clientSocket, int id, Server2 server)
                {
                                this.clientSocket = clientSocket;
                                this.id = id;
                                this.server = server;
                                System.out.println( "Connection " + id + " established with: " + clientSocket );
                                try
                                {
                                                is = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
                                                os = new PrintStream(clientSocket.getOutputStream());
                                }
                                catch (IOException e)
                                {
                                                System.out.println(e);
                                }
                }
                public void run()
                {
                String line;
                                try
                                {
                                                boolean serverStop = false;
                                while (true)
                                {
                                                line = is.readLine();
                                                                System.out.println( "Received " + line + " from Connection " + id + "." );
                                                int n = Integer.parseInt(line);
                                                                if ( n == -1 )
                                                                {
                                                                                serverStop = true;
                                                                                break;
                                                                }
                                                                if ( n == 0 )
                                                                                break;
                                                os.println("" + n*n );
                                }

                                                System.out.println( "Connection " + id + " closed." );
                                is.close();
                                os.close();
                                clientSocket.close();

                                                if ( serverStop ) server.stopServer();
                                }
                                catch (IOException e)
                                {
                                                System.out.println(e);
                                }
                }
}

*****************Client Program :*****************

import java.io.*;
import java.net.*;
public class Client2
{
            public static void main(String [] args)
            {
                        String hostname="127.0.01";
                        int port=6789;
                        Socket clientSocket=null;
                        DataOutputStream os=null;
                        BufferedReader is=null;
                       
                        try
                        {
                                    clientSocket=new Socket(hostname,port);
                                    os=new DataOutputStream(clientSocket.getOutputStream());
                                    is=new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
                        }
                        catch(UnknownHostException e)
                        {
                                    System.err.println("Don't Know About Host :"+hostname);
                        }
                        catch(IOException e)
                        {
                                    System.err.println("Couldn't get I/O for The Connection To :"+hostname);
                        }
                        if(clientSocket == null || os == null || is== null)
                        {
                                    System.err.println("Something is Wrong. One Variable is Null.");
                                    return;
                        }
                        try
                        {
                                    while(true)
                                    {
                                                System.out.println("Enter an Integer (0 for stopClient & -1 for stopServer):");
                                                BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
                                                String KeyboardInput=br.readLine();
                                                os.writeBytes(KeyboardInput + "\n");
                                               
                                                int n=Integer.parseInt(KeyboardInput);
                                                if(n==0 ||n== -1)
                                                            break;
                                                String responseLine=is.readLine();
                                                System.out.println("Server Returns it's square as: "+responseLine);
                                    }
                                    os.close();
                                    is.close();
                                    clientSocket.close();
                                   
                        }
                        catch(UnknownHostException e)
                        {
                                    System.err.println("Trying To Connect Unknown Host :"+e);
                        }
                        catch(IOException e)
                        {
                                    System.err.println("IOException :"+e);
                        }
            }
}

*****************Output :*******************

/*
[root@localhost prac5]# javac Server2.java
[root@localhost prac5]# java Server2
Server is started and is waiting for connections.
With multi-threading, multiple connections are allowed.
Any client can send -1 to stop the server.
Connection 1 established with: Socket[addr=/127.0.0.1,port=40560,localport=6789]
Received 2 from Connection 1.
Received 5 from Connection 1.
Received 9 from Connection 1.
Received 10 from Connection 1.
Received 0 from Connection 1.
Connection 1 closed.
Connection 2 established with: Socket[addr=/127.0.0.1,port=40564,localport=6789]
Received 0 from Connection 2.
Connection 2 closed.
*/
/*
[root@localhost prac5]# javac Client2.java
[root@localhost prac5]# java Client2
Enter an Integer (0 for stopClient & -1 for stopServer):
2
Server Returns it's square as: 4
Enter an Integer (0 for stopClient & -1 for stopServer):
5
Server Returns it's square as: 25
Enter an Integer (0 for stopClient & -1 for stopServer):
9
Server Returns it's square as: 81
Enter an Integer (0 for stopClient & -1 for stopServer):
10
Server Returns it's square as: 100
Enter an Integer (0 for stopClient & -1 for stopServer):
0
[root@localhost prac5]# javac Client2.java
[root@localhost prac5]# java Client2
Enter an Integer (0 for stopClient & -1 for stopServer):
0
[root@localhost prac5]#

*/



************new program of client server******************
*******************server*********************
import java.net.*;
import java.io.*;

public class Server1 extends Thread
{
    protected Socket clntSock;
    public static void main(String args[])throws IOException
    {
        ServerSocket serSock=new ServerSocket(9898);
        while (true)
                {
                    System.out.println ("Waiting for Connection");
                    new Server1 (serSock.accept());
                }

    //System.out.println("connecting to client");
    }
    private Server1 (Socket clientSoc)
    {
            clntSock = clientSoc;
            start();
    }
    public void run()
    {
            System.out.println ("New Communication Thread Started");

        try
        {
            BufferedReader in = new BufferedReader(new InputStreamReader(clntSock.getInputStream())) ;
            String m;
            PrintWriter sout=new PrintWriter(clntSock.getOutputStream(),true);
            while((m=in.readLine())!=null)
            {
                System.out.println("\nServer:"+m);
                sout.println(m);
                if (m.equalsIgnoreCase("Bye"))
                            break;
            }
            sout.close();
            in.close();
            clntSock.close();
        }
        catch (IOException e)
            {
                 System.err.println("Problem with Communication Server");
                 System.exit(1);
              }
    }
}

***********************client*********************************
import java.net.*;
import java.io.*;
public class Client1
{
    public static void main(String args[])throws IOException
    {
        Socket clientsocket=new Socket("localhost",9898);
        System.out.println("connected to server");
        PrintWriter out = new PrintWriter(clientsocket.getOutputStream(),true) ;
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        String response;
        BufferedReader br1=new BufferedReader(new InputStreamReader(clientsocket.getInputStream()));
        while((response=br.readLine())!=null)
        {

            out.println(response);

            response=br1.readLine();
             if (response == null || response.equals(""))
            {
                          System.exit(0);
            }

            System.out.println("echo:"+response);
        }
        out.close();
        br1.close();
        clientsocket.close();
    }
}

Comments

Popular Posts