Question: can some one help me im getting Memcheck, a memory error detector in my code when i run in my shell #include #include #include #include

can some one help me im getting "Memcheck, a memory error detector" in my code when i run in my shell #include
#include
#include
#include
#include
#include
#define MAX_INPUT_SIZE 177
#define MAX_ARGS 5
void execute_command(char *args[]){
pid_t pid;
int status;
pid = fork();
if (pid ==0){
if (execvp(args[0], args)==-1){
perror("Error executing command");
exit(EXIT_FAILURE);
}
} else if (pid <0){
perror("Error forking process");
} else {
waitpid(pid, &status, 0);
printf("Child PID: %d, Exit Status: %d
", pid, WEXITSTATUS(status));
}
}
void execute_piped_commands(char *args1[], char *args2[]){
int pipe_fd[2];
pid_t pid1, pid2;
int status1, status2;
if (pipe(pipe_fd)==-1){
perror("Pipe creation failed");
exit(EXIT_FAILURE);
}
pid1= fork();
if (pid1==0){
close(pipe_fd[0]);
dup2(pipe_fd[1], STDOUT_FILENO);
close(pipe_fd[1]);
if (execvp(args1[0], args1)==-1){
perror("Error executing command");
exit(EXIT_FAILURE);
}
} else if (pid1<0){
perror("Error forking process");
} else {
pid2= fork();
if (pid2==0){
close(pipe_fd[1]);
dup2(pipe_fd[0], STDIN_FILENO);
close(pipe_fd[0]);
if (execvp(args2[0], args2)==-1){
perror("Error executing command");
exit(EXIT_FAILURE);
}
} else if (pid2<0){
perror("Error forking process");
} else {
// Parent process
close(pipe_fd[0]);
close(pipe_fd[1]);
waitpid(pid1, &status1,0);
waitpid(pid2, &status2,0);
printf("Child PID: %d, Exit Status: %d
", pid1, WEXITSTATUS(status1));
printf("Child PID: %d, Exit Status: %d
", pid2, WEXITSTATUS(status2));
}
}
}
int main(int argc, char *argv[]){
char input[MAX_INPUT_SIZE];
char *args[MAX_ARGS];
char *token;
const char delim[]="";
const char pipe_delim[]="|";
char *args1[MAX_ARGS]={NULL};
char *args2[MAX_ARGS]={NULL};
if (argc >1){
printf("%s ", argv[1]);
} else {
printf(">");
}
while (fgets(input, sizeof(input), stdin)!= NULL){
if (input[0]=='
'){
fprintf(stderr, "Error: Empty command
");
} else {
input[strcspn(input,"
")]=0;
if (strstr(input, pipe_delim)!= NULL){
token = strtok(input, pipe_delim);
int i =0;
while (token != NULL && i < MAX_ARGS -1){
args1[i]= token;
token = strtok(NULL, delim);
i++;
}
args1[i]= NULL;
token = strtok(NULL, pipe_delim);
int j =0;
while (token != NULL && j < MAX_ARGS -1){
args2[j]= token;
token = strtok(NULL, delim);
j++;
}
args2[j]= NULL;
execute_piped_commands(args1, args2);
} else {
int i =0;
token = strtok(input, delim);
while (token != NULL && i < MAX_ARGS -1){
args[i]= token;
token = strtok(NULL, delim);
i++;
}
args[i]= NULL;
if (strcmp(args[0], "exit")==0){
break;
}
execute_command(args);
}
} c
if (argc >1){
printf("%s ", argv[1]);
} else {
printf(">");
}
}
return 0;
} its a C language

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!