-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
df609c4
commit 1a752f3
Showing
4 changed files
with
379 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <unistd.h> | ||
#include <ctype.h> | ||
#include <string.h> | ||
#include <sys/wait.h> | ||
|
||
void reverse_case(char *str) | ||
{ | ||
for (int i = 0; str[i] != '\0'; i++) | ||
{ | ||
if (islower(str[i])) | ||
{ | ||
str[i] = toupper(str[i]); | ||
} | ||
else if (isupper(str[i])) | ||
{ | ||
str[i] = tolower(str[i]); | ||
} | ||
} | ||
} | ||
|
||
int main() | ||
{ | ||
int pipefd[2]; | ||
|
||
if (pipe(pipefd) == -1) | ||
{ | ||
printf("Error: Pipe creation failed\n"); | ||
exit(1); | ||
} | ||
int pid; | ||
pid = fork(); | ||
if (pid == -1) | ||
{ | ||
printf("Error: Fork failed\n"); | ||
exit(1); | ||
} | ||
|
||
if (pid > 0) | ||
{ | ||
char message[100]; | ||
printf("Enter a message: "); | ||
fgets(message, sizeof(message), stdin); | ||
int len = strlen(message); | ||
if (len > 0 && message[len - 1] == '\n') | ||
{ | ||
message[len - 1] = '\0'; | ||
} | ||
close(pipefd[0]); | ||
write(pipefd[1], &len, sizeof(len)); | ||
write(pipefd[1], message, strlen(message) + 1); | ||
close(pipefd[1]); | ||
wait(NULL); | ||
} | ||
else | ||
{ | ||
int size = 0; | ||
close(pipefd[1]); | ||
read(pipefd[0], &size, sizeof(size)); | ||
char *str = (char *)malloc(size); | ||
read(pipefd[0], str, size); | ||
close(pipefd[0]); | ||
reverse_case(str); | ||
printf("Reversed case message: %s\n", str); | ||
free(str); | ||
exit(0); | ||
} | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
#include <stdio.h> | ||
#include <unistd.h> | ||
#include <sys/wait.h> | ||
#include <stdlib.h> | ||
|
||
int main() | ||
{ | ||
int pipe1[2], pipe2[2]; | ||
|
||
if (pipe(pipe1) == -1 || pipe(pipe2) == -1) | ||
{ | ||
printf("Error: Pipe creation failed\n"); | ||
return 1; | ||
} | ||
|
||
int pid; | ||
pid = fork(); | ||
|
||
if (pid < 0) | ||
{ | ||
printf("Error: Fork failed\n"); | ||
return 1; | ||
} | ||
|
||
if (pid > 0) | ||
{ | ||
int sum = 0; | ||
int num_elements; | ||
printf("Enter the number of elements: "); | ||
scanf("%d", &num_elements); | ||
|
||
int arr[num_elements]; | ||
printf("Enter the elements of the array:\n"); | ||
for (int i = 0; i < num_elements; i++) | ||
{ | ||
scanf("%d", &arr[i]); | ||
} | ||
int size = sizeof(int) * num_elements; | ||
close(pipe1[0]); | ||
close(pipe2[1]); | ||
|
||
write(pipe1[1], &num_elements, sizeof(int)); | ||
write(pipe1[1], arr, size); | ||
close(pipe1[1]); | ||
|
||
read(pipe2[0], &sum, sizeof(int)); | ||
close(pipe2[0]); | ||
|
||
wait(NULL); | ||
|
||
printf("Sum of array: %d\n", sum); | ||
} | ||
else | ||
{ | ||
int sum = 0; | ||
int num; | ||
close(pipe1[1]); | ||
close(pipe2[0]); | ||
|
||
read(pipe1[0], &num, sizeof(int)); | ||
|
||
int arr[num]; | ||
int size = sizeof(int) * num; | ||
|
||
read(pipe1[0], arr, size); | ||
close(pipe1[0]); | ||
|
||
for (int i = 0; i < num; i++) | ||
{ | ||
sum += arr[i]; | ||
} | ||
|
||
write(pipe2[1], &sum, sizeof(int)); | ||
close(pipe2[1]); | ||
|
||
exit(0); | ||
} | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <unistd.h> | ||
#include <fcntl.h> | ||
#include <sys/wait.h> | ||
|
||
int main(int argc, char *argv[]) | ||
{ | ||
if (argc != 3) | ||
{ | ||
printf("Error: No file names entered\n"); | ||
return 1; | ||
} | ||
|
||
int pipe_fd[2]; | ||
if (pipe(pipe_fd) == -1) | ||
{ | ||
printf("Pipe creation failed\n"); | ||
return 1; | ||
} | ||
|
||
int pid = fork(); | ||
if (pid < 0) | ||
{ | ||
printf("Fork failed\n"); | ||
return 1; | ||
} | ||
|
||
if (pid > 0) | ||
{ | ||
int src_fd = open(argv[1], O_RDONLY); | ||
if (src_fd < 0) | ||
{ | ||
printf("Error opening source file\n"); | ||
return 1; | ||
} | ||
|
||
close(pipe_fd[0]); | ||
|
||
char buffer[1024]; | ||
int bytes_read; | ||
while ((bytes_read = read(src_fd, buffer, sizeof(buffer))) > 0) | ||
{ | ||
write(pipe_fd[1], buffer, bytes_read); | ||
} | ||
|
||
close(src_fd); | ||
close(pipe_fd[1]); | ||
|
||
wait(NULL); | ||
} | ||
else | ||
{ | ||
close(pipe_fd[1]); | ||
|
||
int dest_fd = open(argv[2], O_WRONLY | O_CREAT | O_TRUNC, 0644); | ||
if (dest_fd < 0) | ||
{ | ||
printf("Error opening destination file\n"); | ||
return 1; | ||
} | ||
|
||
dup2(pipe_fd[0], 0); | ||
close(pipe_fd[0]); | ||
|
||
dup2(dest_fd, 1); | ||
close(dest_fd); | ||
|
||
char buffer[1024]; | ||
int bytes_read; | ||
while ((bytes_read = read(0, buffer, sizeof(buffer))) > 0) | ||
{ | ||
write(1, buffer, bytes_read); | ||
} | ||
|
||
exit(0); | ||
} | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <unistd.h> | ||
#include <sys/wait.h> | ||
#include <stdbool.h> | ||
#include <string.h> | ||
|
||
int gcd(int a, int b) | ||
{ | ||
while (b != 0) | ||
{ | ||
int temp = b; | ||
b = a % b; | ||
a = temp; | ||
} | ||
return a; | ||
} | ||
|
||
bool is_prime(int num) | ||
{ | ||
if (num <= 1) | ||
return false; | ||
for (int i = 2; i * i <= num; i++) | ||
{ | ||
if (num % i == 0) | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
int main() | ||
{ | ||
int pipe1[2], pipe2[2]; | ||
if (pipe(pipe1) == -1 || pipe(pipe2) == -1) | ||
{ | ||
printf("Error: Pipe creation failed"); | ||
return 1; | ||
} | ||
|
||
int pid1 = fork(); | ||
if (pid1 < 0) | ||
{ | ||
printf("Error: Fork failed"); | ||
return 1; | ||
} | ||
|
||
if (pid1 > 0) | ||
{ | ||
close(pipe1[0]); | ||
close(pipe2[1]); | ||
|
||
int num_elements; | ||
printf("Enter the number of elements: "); | ||
scanf("%d", &num_elements); | ||
|
||
while (num_elements <= 0) | ||
{ | ||
printf("Error: Number of elements should be greater than 0\n"); | ||
printf("Enter the number of elements: "); | ||
scanf("%d", &num_elements); | ||
} | ||
|
||
int arr[num_elements]; | ||
printf("Enter the elements: "); | ||
for (int i = 0; i < num_elements; i++) | ||
{ | ||
scanf("%d", &arr[i]); | ||
} | ||
|
||
write(pipe1[1], &num_elements, sizeof(int)); | ||
write(pipe1[1], arr, sizeof(arr)); | ||
close(pipe1[1]); | ||
|
||
char result_msg[50]; | ||
int bytes_read = read(pipe2[0], result_msg, sizeof(result_msg)); | ||
close(pipe2[0]); | ||
|
||
result_msg[bytes_read] = '\0'; | ||
printf("%s\n", result_msg); | ||
wait(NULL); | ||
} | ||
else | ||
{ | ||
int pid2 = fork(); | ||
if (pid2 < 0) | ||
{ | ||
printf("Error: Fork failed"); | ||
return 1; | ||
} | ||
|
||
if (pid2 > 0) // Child process 1: Bob | ||
{ | ||
close(pipe1[1]); | ||
close(pipe2[0]); | ||
|
||
dup2(pipe1[0], 0); | ||
close(pipe1[0]); | ||
dup2(pipe2[1], 1); | ||
close(pipe2[1]); | ||
|
||
int num_elements; | ||
read(0, &num_elements, sizeof(int)); | ||
int arr[num_elements]; | ||
read(0, arr, sizeof(arr)); | ||
|
||
int result_gcd = arr[0]; | ||
for (int i = 1; i < num_elements; i++) | ||
{ | ||
result_gcd = gcd(result_gcd, arr[i]); | ||
} | ||
|
||
// Send the GCD to Charlie via stdout | ||
write(1, &result_gcd, sizeof(int)); | ||
|
||
exit(0); | ||
} | ||
else // Child process 2: Charlie | ||
{ | ||
close(pipe1[0]); | ||
close(pipe1[1]); | ||
close(pipe2[1]); | ||
|
||
dup2(pipe2[0], 0); | ||
close(pipe2[0]); | ||
|
||
// Read the GCD from Bob via stdin | ||
int result_gcd; | ||
read(0, &result_gcd, sizeof(int)); | ||
|
||
// Check if the GCD is prime and send the result to Alice | ||
char message[50]; | ||
if (is_prime(result_gcd)) | ||
{ | ||
sprintf(message, "The GCD is prime"); | ||
} | ||
else | ||
{ | ||
sprintf(message, "The GCD is not prime"); | ||
} | ||
|
||
write(1, message, strlen(message) + 1); // Send the actual message length | ||
|
||
exit(0); | ||
} | ||
} | ||
|
||
return 0; | ||
} |