Skip to content

Commit 3ee30c4

Browse files
committed
Pipe Command Usage
1 parent 28d7c4d commit 3ee30c4

9 files changed

+367
-0
lines changed

Lab5/COE18B061.pdf

347 KB
Binary file not shown.

Lab5/Q1_StringReversal

12.6 KB
Binary file not shown.

Lab5/Q1_StringReversal.c

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
#include<stdio.h>
2+
#include<stdlib.h>
3+
#include<string.h>
4+
#include<unistd.h>
5+
#include<sys/types.h>
6+
#include<sys/wait.h>
7+
8+
void ReverseString(char *string, int begin, int end);
9+
10+
int main()
11+
{
12+
int pipefd1[2], pipefd2[2]; //0 -> read, 1 -> write
13+
14+
char input_str[100];
15+
char reverse_str[100];
16+
pid_t pid;
17+
18+
if(pipe(pipefd1) == -1)
19+
{
20+
printf("Unable to create pipe 1..\n");
21+
exit(EXIT_FAILURE);
22+
}
23+
24+
if(pipe(pipefd2) == -1)
25+
{
26+
printf("Unable to create pipe 2..\n");
27+
exit(EXIT_FAILURE);
28+
}
29+
30+
pid = fork();
31+
32+
if(pid == 0) //child block
33+
{
34+
close(pipefd1[1]); //close writing end of parent pipe
35+
36+
//Read a string from parent pipe
37+
read(pipefd1[0], reverse_str, 100);
38+
39+
printf("\nIn Child :- String read from parent pipe: %s\n", reverse_str);
40+
41+
//Reverse the string given by parent
42+
ReverseString(reverse_str, 0, strlen(reverse_str) - 1);
43+
44+
//close both reading ends
45+
close(pipefd1[0]);
46+
close(pipefd2[0]);
47+
48+
//Write reversed string and close the writing end
49+
write(pipefd2[1], reverse_str, 100);
50+
close(pipefd2[1]);
51+
52+
}
53+
54+
else //parent block
55+
{
56+
close(pipefd1[0]); // Close reading end of parent pipe
57+
58+
printf("\nIn Parent :- Enter a string: ");
59+
gets(input_str);
60+
61+
//write input string and close writing end of parent pipe
62+
write(pipefd1[1], input_str, strlen(input_str) + 1);
63+
close(pipefd1[1]);
64+
65+
//wait for child to send the reverse string
66+
wait(NULL);
67+
68+
close(pipefd2[1]); //close writing end of second pipe
69+
70+
//read string from child, print it and close the reading end
71+
read(pipefd2[0], reverse_str, 100);
72+
printf("\nIn Parent :- Reversed string from child side: %s\n\n", reverse_str);
73+
close(pipefd2[0]);
74+
}
75+
76+
return 0;
77+
}
78+
79+
void ReverseString(char *string, int begin, int end)
80+
{
81+
char c;
82+
83+
if(begin >= end)
84+
return;
85+
86+
c = *(string + begin);
87+
*(string + begin) = *(string + end);
88+
*(string + end) = c;
89+
90+
ReverseString(string, ++begin , --end);
91+
}

Lab5/Q2_StringConcatenation

12.6 KB
Binary file not shown.

Lab5/Q2_StringConcatenation.c

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#include<stdio.h>
2+
#include<stdlib.h>
3+
#include<string.h>
4+
#include<unistd.h>
5+
#include<sys/types.h>
6+
#include<sys/wait.h>
7+
8+
int main()
9+
{
10+
int pipefd1[2], pipefd2[2]; //0 -> read, 1 -> write
11+
12+
char input_str1[100];
13+
char input_str2[100];
14+
char concat_str[100];
15+
pid_t pid;
16+
17+
if(pipe(pipefd1) == -1)
18+
{
19+
printf("Unable to create pipe 1..\n");
20+
exit(EXIT_FAILURE);
21+
}
22+
23+
if(pipe(pipefd2) == -1)
24+
{
25+
printf("Unable to create pipe 2..\n");
26+
exit(EXIT_FAILURE);
27+
}
28+
29+
pid = fork();
30+
31+
if(pid == 0) //child block
32+
{
33+
sleep(5);
34+
close(pipefd2[0]); //close reading end of child pipe
35+
close(pipefd1[1]); //close writing end of parent pipe
36+
37+
printf("\nIn Child :- Enter string 2: ");
38+
gets(input_str2);
39+
40+
//write the input string to child pipe and close it
41+
write(pipefd2[1], input_str2, strlen(input_str2) + 1);
42+
close(pipefd2[1]);
43+
44+
//read string from parent, print it and close the reading end
45+
read(pipefd1[0], concat_str, 100);
46+
printf("\nIn Child :- Concatenated string from parent side: %s\n\n", concat_str);
47+
close(pipefd1[0]);
48+
}
49+
50+
else //parent block
51+
{
52+
close(pipefd1[0]); //close reading end of parent pipe
53+
close(pipefd2[1]); //close writing end of child pipe
54+
55+
printf("\nIn Parent :- Enter string 1: ");
56+
gets(input_str1);
57+
58+
//read the input string by child from the child pipe
59+
read(pipefd2[0], input_str2, 100);
60+
printf("\nIn Parent :- Strings to be concatenated are %s and %s\n", input_str1, input_str2);
61+
62+
//Concat the child string to the parent string
63+
strcat(input_str1, input_str2);
64+
65+
close(pipefd2[0]); //close reading end of second pipe
66+
67+
//Write concatinated string and close the writing end of parent pipe
68+
write(pipefd1[1], input_str1, 100);
69+
close(pipefd1[1]);
70+
}
71+
72+
return 0;
73+
}

Lab5/Q3_SubstringGeneration

12.7 KB
Binary file not shown.

Lab5/Q3_SubstringGeneration.c

+95
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
#include<stdio.h>
2+
#include<stdlib.h>
3+
#include<string.h>
4+
#include<unistd.h>
5+
#include<sys/types.h>
6+
#include<sys/wait.h>
7+
8+
void GenerateSubstr(char *input_str, char *substr, int start, int end);
9+
10+
int main()
11+
{
12+
int pipefd1[2], pipefd2[2]; //0 -> read, 1 -> write
13+
14+
char input_str[100];
15+
char substr[100];
16+
int start, end;
17+
pid_t pid;
18+
19+
if(pipe(pipefd1) == -1)
20+
{
21+
printf("Unable to create pipe 1..\n");
22+
exit(EXIT_FAILURE);
23+
}
24+
25+
if(pipe(pipefd2) == -1)
26+
{
27+
printf("Unable to create pipe 2..\n");
28+
exit(EXIT_FAILURE);
29+
}
30+
31+
pid = fork();
32+
33+
if(pid == 0) //child block
34+
{
35+
close(pipefd1[1]); //close writing end of parent pipe
36+
37+
//Read a string from parent pipe
38+
read(pipefd1[0], input_str, 100);
39+
40+
printf("\nIn Child :- String read from parent pipe: %s\n", input_str);
41+
42+
printf("\nIn Child :- Enter the start index for substring: ");
43+
scanf("%d", &start);
44+
printf("In Child :- Enter the end index for substring: ");
45+
scanf("%d", &end);
46+
47+
//Generate the substring for the string given by parent
48+
GenerateSubstr(input_str, substr, start, end);
49+
50+
//close both reading ends
51+
close(pipefd1[0]);
52+
close(pipefd2[0]);
53+
54+
//Write substring and close the writing end
55+
write(pipefd2[1], substr, 100);
56+
close(pipefd2[1]);
57+
58+
}
59+
60+
else //parent block
61+
{
62+
close(pipefd1[0]); // Close reading end of parent pipe
63+
64+
printf("\nIn Parent :- Enter a string: ");
65+
gets(input_str);
66+
67+
//write input string and close writing end of parent pipe
68+
write(pipefd1[1], input_str, strlen(input_str) + 1);
69+
close(pipefd1[1]);
70+
71+
//wait for child to send the substring
72+
wait(NULL);
73+
74+
close(pipefd2[1]); //close writing end of second pipe
75+
76+
//read substring from child, print it and close the reading end
77+
read(pipefd2[0], substr, 100);
78+
printf("\nIn Parent :- Substring generated from child side: %s\n\n", substr);
79+
close(pipefd2[0]);
80+
}
81+
82+
return 0;
83+
}
84+
85+
void GenerateSubstr(char *input_str, char *substr, int start, int end)
86+
{
87+
int count = 0;
88+
89+
while(count < (end - start) + 1)
90+
{
91+
substr[count] = input_str[start+count];
92+
count ++;
93+
}
94+
substr[count] = '\0';
95+
}

Lab5/Q4_PalindromeCheck

12.7 KB
Binary file not shown.

Lab5/Q4_PalindromeCheck.c

+108
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
#include<stdio.h>
2+
#include<stdlib.h>
3+
#include<string.h>
4+
#include<unistd.h>
5+
#include<sys/types.h>
6+
#include<sys/wait.h>
7+
8+
void ReverseString(char *string, int begin, int end);
9+
int palindrome(char *string1, char *string2);
10+
11+
int main()
12+
{
13+
int pipefd1[2], pipefd2[2]; //0 -> read, 1 -> write
14+
int result;
15+
char input_str[100];
16+
char reverse_str[100];
17+
pid_t pid;
18+
19+
if(pipe(pipefd1) == -1)
20+
{
21+
printf("Unable to create pipe 1..\n");
22+
exit(EXIT_FAILURE);
23+
}
24+
25+
if(pipe(pipefd2) == -1)
26+
{
27+
printf("Unable to create pipe 2..\n");
28+
exit(EXIT_FAILURE);
29+
}
30+
31+
pid = fork();
32+
33+
if(pid == 0) //child block
34+
{
35+
close(pipefd1[1]); //close writing end of parent pipe
36+
37+
//Read a string from parent pipe
38+
read(pipefd1[0], reverse_str, 100);
39+
40+
printf("\nIn Child :- String read from parent pipe: %s\n", reverse_str);
41+
42+
//Reverse the string given by parent
43+
ReverseString(reverse_str, 0, strlen(reverse_str) - 1);
44+
45+
//close both reading ends
46+
close(pipefd1[0]);
47+
close(pipefd2[0]);
48+
49+
//Write reversed string and close the writing end
50+
write(pipefd2[1], reverse_str, 100);
51+
close(pipefd2[1]);
52+
53+
}
54+
55+
else //parent block
56+
{
57+
close(pipefd1[0]); // Close reading end of parent pipe
58+
59+
printf("\nIn Parent :- Enter a string: ");
60+
gets(input_str);
61+
62+
//write input string and close writing end of parent pipe
63+
write(pipefd1[1], input_str, strlen(input_str) + 1);
64+
close(pipefd1[1]);
65+
66+
//wait for child to send the reverse string
67+
wait(NULL);
68+
69+
close(pipefd2[1]); //close writing end of second pipe
70+
71+
//read string from child, print it
72+
read(pipefd2[0], reverse_str, 100);
73+
printf("\nIn Parent :- Reversed string from child side: %s\n", reverse_str);
74+
75+
printf("\nIn Parent :- Palindrome Check..\n");
76+
77+
//check given string is palindrome or not
78+
result = palindrome(input_str, reverse_str);
79+
if(result)
80+
printf("\t Given string %s, is a palindrome\n\n", input_str);
81+
else
82+
printf("\t Given string %s, is not a palindrome\n\n", input_str);
83+
84+
//close the reading end of child
85+
close(pipefd2[0]);
86+
}
87+
88+
return 0;
89+
}
90+
91+
void ReverseString(char *string, int begin, int end)
92+
{
93+
char c;
94+
95+
if(begin >= end)
96+
return;
97+
98+
c = *(string + begin);
99+
*(string + begin) = *(string + end);
100+
*(string + end) = c;
101+
102+
ReverseString(string, ++begin , --end);
103+
}
104+
105+
int palindrome(char *string1, char *string2)
106+
{
107+
return strcmp(string1, string2) == 0;
108+
}

0 commit comments

Comments
 (0)