0% found this document useful (0 votes)
5 views6 pages

Java and C Threading & IPC Examples

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views6 pages

Java and C Threading & IPC Examples

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Name:SRI KRISHNA ADITHYA KATRAGADDA

Slot: L45 + 46

Reg. ID: 23BCE9831

Course Code: CSE2008

1) Write a java program to create 3 threads. one thread should display “good morning” for
every 1sec and second thread display “good evening” for every 2 seconds and Third thread should
display “good night” for every 3 seconds.

Code:

class MessageThread extends Thread {

private String message;


private int interval;

public MessageThread(String message, int interval) {


[Link] = message;
[Link] = interval;
}

public void run() {


try {
while (true) {
[Link](message);
[Link](interval * 1000); // convert seconds to milliseconds
}
} catch (InterruptedException e) {
[Link](message + " thread interrupted.");
}
}
}

public class ThreadsDemo {


public static void main(String[] args) {
MessageThread t1 = new MessageThread("Good Morning", 1);
MessageThread t2 = new MessageThread("Good Evening", 2);
MessageThread t3 = new MessageThread("Good Night", 3);

[Link]();
[Link]();
[Link]();
}
}

Output:

2) Write a java program to create 3 threads. One thread should display “Multiplication Table” and
second thread display “Factorial of given number” and Third thread should display “Sum of three
numbers” .

Code:
import [Link];

class MultiplicationTableThread extends Thread {


private int number;

public MultiplicationTableThread(int number) {


[Link] = number;
}

public void run() {


[Link]("Multiplication Table of " + number + ":");
for (int i = 1; i <= 10; i++) {
[Link](number + " x " + i + " = " + (number * i));
}
[Link]();
}
}

class FactorialThread extends Thread {


private int number;

public FactorialThread(int number) {


[Link] = number;
}

public void run() {


long fact = 1;
for (int i = 1; i <= number; i++) {
fact *= i;
}
[Link]("Factorial of " + number + " = " + fact);
[Link]();
}
}

class SumThread extends Thread {


private int n;

public SumThread(int n) {
this.n = n;
}

public void run() {


int sum = 0;
for (int i = 1; i <= n; i++) {
sum += i;
}
[Link]("Sum of first " + n + " numbers = " + sum);
[Link]();
}
}

public class MulThread {


public static void main(String[] args) {

Scanner sc = new Scanner([Link]);

[Link]("Enter a number for multiplication table: ");


int numberForTable = [Link]();

[Link]("Enter a number to calculate factorial: ");


int numberForFactorial = [Link]();

[Link]("Enter n for sum of first n numbers: ");


int nForSum = [Link]();

MultiplicationTableThread t1 = new MultiplicationTableThread(numberForTable);


FactorialThread t2 = new FactorialThread(numberForFactorial);
SumThread t3 = new SumThread(nForSum);

try {
[Link]();
[Link](); // ensures output order
[Link]();
[Link]();
[Link]();
[Link]();
} catch (InterruptedException e) {
[Link]("Thread interrupted.");
}

[Link]();
}
}

Output:

IPC

3) Write a c program to provide IPC using pipes.

Code:

#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>

int main()
{
char str[20];
pid_t pid;
int fd[2],n;
pipe(fd);
pid = fork();
if(pid > 0)
{
close(fd[0]);
write(fd[1],"IPC using pipes",15);
}
else
{
close(fd[1]);
n = read(fd[0],str,15);
write(STDOUT_FILENO,str,n);
}
}

Output:

4) Write a c program that illustrates the inter process communication using shared memory.

Code:

Process A:

#include <stdio.h>
#include <sys/shm.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <string.h>

int main()
{

int shmid,flag;
key_t key = 1234;
char *msg;
shmid = shmget(key,10,IPC_CREAT|0666);

if(shmid < 0)
{
printf("error");
}

printf("Shared memeory Id = %d\n",shmid);


msg = shmat(shmid,0,0);
strcpy(msg,"Example On shared memory");
}

Process B:

#include <stdio.h>
#include <sys/shm.h>
#include <sys/types.h>
#include <sys/ipc.h>

int main()
{
int shmid;
key_t key = 1234;
char *msg;
shmid = shmget(key,10,IPC_CREAT|0666);

if(shmid < 0)
{
printf("error");
}

printf("Id: %d\n",shmid);
msg = shmat(shmid,0,0);
printf("%s",msg);
}

OUTPUT:

You might also like