0% found this document useful (0 votes)
5 views

Exercise 5

ddddd

Uploaded by

atharvag062
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Exercise 5

ddddd

Uploaded by

atharvag062
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

22

Ex No. 5 IMPLEMENTATION OF MULTITHREADING

A. MULTITHREADING USING JAVA

PROGRAM

import java.lang.*;
import java.util.*;

// Thread A( for arithematic operations)

class A1 extends Thread


{
int i,j;
A1(int x,int y)
{
i=x;
j=y;
}
public void run()
{
System.out.println("THREAD A:: ARITHEMATIC OPERATIONS");
System.out.println("SUM "+(i+j));
System.out.println( "DIFFERENCE "+(i-j));
System.out.println(" PRODUCT "+(i*j));
System.out.println("RATIO "+(i/j));
System.out.println("POWER "+Math.pow(i,j));
System.out.println("END OF A");
}
}

// Thread B (for trignometric operations)

class B1 extends Thread


{
int i;
B1(int x)
{
i=x;
}

public void run()

{
System.out.println("THREAD B:: TRIGNOMETRIC OPERATIONS");
System.out.println("SINE OF "+i+""+Math.sin(i));
System.out.println("COSINE OF "+i+""+Math.cos(i));
23

System.out.println("TAN OF "+i+" "+Math.tan(i));


System.out.println("SQUARE ROOT OF "+i+" "+Math.sqrt(i));
System.out.println("END OF B");

// MAIN CLASS

class Operate
{
public static void main(String args[])
{
Scanner s=new Scanner(System.in);
System.out.println("ENTER TWO VALUES FOR ARITHEMATIC OPERATIONS");
int x=s.nextInt();
int y=s.nextInt();
System.out.println("ENTER A VALUE FOR TRIGNOMETRIC OPERATIONS");
int z=s.nextInt();
A1 a=new A1(x,y);
B1 b=new B1(z);
a.start();
b.start();

OUTPUT

ENTER TWO VALUES FOR ARITHEMATIC OPERATIONS


35
ENTER A VALUE FOR TRIGNOMETRIC OPERATIONS
45
THREAD A:: ARITHEMATIC OPERATIONS
THREAD B:: TRIGNOMETRIC OPERATIONS
SUM 8
DIFFERENCE -2
PRODUCT 15
RATIO 0
POWER 243.0
SINE OF 450.8509035245341184
END OF A
COSINE OF 450.5253219888177297
TAN OF 45 1.6197751905438615
SQUARE ROOT OF 45 6.708203932499369
END OF B
24

B. MULTITHREADING USING PTHREAD

PROGRAM

#include<pthread.h>
#include<stdio.h>
#include<stdlib.h>
int sum;
void *runner(void *param);

int main(int argc,char *argv[])


{
pthread_t tid;
pthread_attr_t attr;

if(argc!=2)
{
fprintf(stderr,"usage : a.out <integer value>\n");
return -1;
}

if(atoi(argv[1])<0)
{
fprintf(stderr,"%d must be >=0\n",atoi(argv[1]));
return -1;
}

pthread_attr_init(&attr);

pthread_create(&tid,&attr,runner,argv[1]);

pthread_join(tid,NULL);
printf("argc=%d\n",argc);
printf("Sum =%d\n", sum);

void *runner(void *param)


{

int i, upper=atoi(param);
sum=0;
for(i=1;i<=upper;i++)
sum+=i;
pthread_exit(0);
}
25

OUTPUT

sudha@sudha:~/os/syscall$ cc pthread.c -lpthread


sudha@sudha:~/os/syscall$ ./a.out 20
argc=2
Sum =210

You might also like