Pipe 2
Pipe 2
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
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);
}
}
#include<stdio.h>
#include<pthread.h>
int arr[100],n,i;
float average;
int max;
int min;
void *Avg()
{
int sum=0;