I Have these 2 programs:
nicecmp.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
int main() {
int pipe_to_loop[2]; // Pipe for sending data to loopcmp.c
int pipe_from_loop[2]; // Pipe for receiving data from loopcmp.c
// Create the pipes
if (pipe(pipe_to_loop) == -1 || pipe(pipe_from_loop) == -1) {
perror("Pipe creation failed");
return 1;
}
// Fork to create a child process for loopcmp.c
pid_t pid = fork();
if (pid < 0) {
perror("Fork failed");
return 1;
} else if (pid == 0) {
// Child process - loopcmp.c
// Close unused pipe ends
close(pipe_to_loop[1]);
close(pipe_from_loop[0]);
// Duplicate the read end of pipe_to_loop to stdin (file descriptor 0)
dup2(pipe_to_loop[0], 0);
// Duplicate the write end of pipe_from_loop to stdout (file descriptor 1)
dup2(pipe_from_loop[1], 1);
// Execute loopcmp.c
execlp("./loopcmp", "./loopcmp", NULL);
perror("Exec failed");
return 1;
} else {
// Parent process - nicecmp.c
// Close unused pipe ends
close(pipe_to_loop[0]);
close(pipe_from_loop[1]);
// Data to be sent to loopcmp.c
const char *str1 = "Hello";
const char *str2 = "World";
int number = 42;
// Send data to loopcmp.c through the pipe
write(pipe_to_loop[1], str1, strlen(str1) + 1);
write(pipe_to_loop[1], str2, strlen(str2) + 1);
write(pipe_to_loop[1], &number, sizeof(int));
// Read data back from loopcmp.c
int result;
read(pipe_from_loop[0], &result, sizeof(int));
// Print the result received from loopcmp.c
printf("Received result from loopcmp.c: %d\n", result);
// Close pipe ends
close(pipe_to_loop[1]);
close(pipe_from_loop[0]);
}
return 0;
}
loopcmp.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
int main() {
// Data buffers for receiving strings and integer
char str1[100], str2[100];
int number;
// Read data from nicecmp.c through the pipe
read(0, str1, sizeof(str1));
read(0, str2, sizeof(str2));
read(0, &number, sizeof(int));
// Print the received data
printf("Received strings from nicecmp.c: %s, %s\n", str1, str2);
printf("Received integer from nicecmp.c: %d\n", number);
// Send the number 20 back to nicecmp.c through the pipe
int result = 20;
write(1, &result, sizeof(int));
return 0;
}
when Im trying to run ./nicecmp that's the result i get: enter image description here it's just going blank and doesn't show that the program is running.
What can I Do in order to solve the problem?
Thanks in advance :)
I Tried to do it with multiple pipes, 2 pipes for the strings and 1 pipe to the integer but couldn't solve it either.