0% found this document useful (0 votes)
7 views5 pages

Pipe 2

Uploaded by

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

Pipe 2

Uploaded by

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

2. Write a program to create a thread.

The thread prints numbers from zero to n,


where value of n is passed from the main process to the thread.
The main process also waits for the thread to finish first and
then print multiples of 5.

from threading import *


import threading

def func(n):
for i in range(1,n+1):
print(i,end=" ")
print()
n = int(input("Enter n: "))
t1 = Thread(target=func, args=(n,))
t1.start()
t1.join()
for i in range(1,n+1):
print(i,"x5=",i*5)

a. Sum of n numbers
b. Compute Arithmetic Progression
c. Square root of the sum of the first 20 natural numbers

echo "Sum of n Numbers!"


echo "Enter the value of n:"
read n
t=1
total=0
while [ $t -le $n ]
do
echo "Enter the number: "
read x
total=$[total+x]
t=$[t+1]
done
echo "Sum of entered n numbers: $total "

echo "Arithmetic Progression!"


echo "Enter the first term:"
read a
echo "Enter the common difference:"
read d
echo "Enter the number of terms:"
read n
sum=0
echo "The Arithmetic Progression is:"
for ((i=1;i<=n;i++))
do
echo $a
sum=$[sum+a]
a=$[a+d]
done
echo "Sum of the arithmetic progression: $sum "
echo "Square root of the sum of the first 20 natural numbers!"
sum=0
for ((i=1;i<=20;i++))
do
sum=$[sum+i]
done
echo "Sum of first 20 natural numbers: $sum "
echo "The square root of the sum is:"
echo $sum | awk '{ print sqrt($sum) }'

Write a program to implement a program using unnamed pipes in which one process
sends a string message to a second process, and the second process reverses the
case of
each character in the message and sends it back to the first process. For example,
if the
first process sends the message Hi There, the second process will return hI tHERE.
This will
require using two pipes, one for sending the original message from the first to the
second
process and the other for sending the modified message from the second to the first

process.

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/types.h>
#include<string.h>
#include<sys/wait.h>
int main()
{ int fd1[2], fd2[2];
char userString[100];
pid_t p;
if (pipe(fd1)==-1||pipe(fd2)==-1)
{ fprintf(stderr, "Pipe Failed" );
return 1;
}
p = fork();
if (p < 0)
{ fprintf(stderr, "fork Failed" ); return 1;
}
else if (p > 0)
{ char strFromChild[100];
close(fd1[0]);
printf("Enter the string: ");
scanf("%[^\n]s",userString);
write(fd1[1], userString, strlen(userString)+1);
close(fd1[1]);
close(fd2[1]);
read(fd2[0], strFromChild, 100);
printf("Reversed string: %s\n", strFromChild);
close(fd2[0]);
}
Output:
Result: The c program using unnamed pipes to pass a string from one process to
another where
the case of its characters are reversed and passed back to the parent process has
been written
and verified.
------ X ------
//prog2-cntd
else
{ close(fd1[1]);
char str[100];
read(fd1[0], str, 100);
close(fd1[0]);
close(fd2[0]);
int ln = strlen(str);
for (int i = 0; i < ln; i++)
{ if (str[i] >= 'a' && str[i] <= 'z')
str[i] = str[i] - 32;
else if (str[i] >= 'A' && str[i] <= 'Z')
str[i] = str[i] + 32;
}
write(fd2[1], str, strlen(str)+1);
close(fd2[1]);
exit(0);
}
}

Write a shell program to compute the following by accepting user input


a. Basic arithmetic operations(+,-,*,/)
b. Basic Logical operations (Not,AND,OR)

echo "Welcome to my program"


echo "Enter your First Number"
read a
echo "Enter your Second Number"
read b
echo" Arithmetic Operations"
c=`expr $a + $b`
echo "The addition of $a + $b = $c"
d=`expr $a - $b`
echo "The subtraction of $a - $b= $d "
e=`expr $a \* $b`
echo "The multiplication of $a * $b = $e"
f=`expr $a / $b `
echo "The Division of $a / $b= $f "
echo"Welcome to Logical Unit"
if(($a ==$b & $b == $a ))
then
echo "both are equal (IN logical And)"
else
echo "both are not equal (In Logical And)"
fi
if(( ! $a == "true" ))
then
echo "$a is false (In logical Not)"
else
echo "$a is true (In logical Not)"
fi
2. Write a multithreaded program that calculates various statistical values
for a list of numbers. This program will be passed a series of numbers on
the command line and will then create three separate threads. One thread
will determine the average of the numbers, the second will determine the
maximum value, and the third will determine the minimum value. The
variables representing the average, minimum, and maximum values will
be stored globally. The threads will set these values, and the parent thread
will output the values once the workers have exited.

#include<stdio.h>
#include<pthread.h>
int arr[100],n,i;
float average;
int max;
int min;
void *Avg()
{
int sum=0;

printf("enter your number :=");


scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
for(i=0;i<n;i++)
{
sum=sum+arr[i];
}
average=sum/n;
printf("The average value is:%f",average);
}
void *Maximum()
{
max=arr[0];
for(int i=1;i<n;i++)
{
if(max<arr[i])
{
max=arr[i];
}
}
printf("\nThe Maximum value is:=%d",max);
}
void *Minimum()
{
min=arr[0];
for(int i=1;i<n;i++)
{
if(min>arr[i])
{
min=arr[i];
}
}
printf("\nThe Minimum value is:=%d",min);
}
int main()
{
int n,i;
pthread_t p1;
pthread_t p2;
pthread_t p3;
n=pthread_create(&p1,NULL,&Avg,NULL);
pthread_join(p1,NULL);
n=pthread_create(&p2,NULL,&Maximum,NULL);
pthread_join(p2,NULL);
n=pthread_create(&p3,NULL,&Minimum,NULL);
pthread_join(p3,NULL);
}

You might also like