program 1
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
int main() {
// Create a child process
pid_t child_pid = fork();
if (child_pid < 0) {
perror("Fork failed");
exit(1);
}
if (child_pid == 0) {
// This code will be executed by the child process
// Execute a different program using exec
char *args[] = {"ls", "-l", NULL};
execvp("ls", args);
// If execvp fails, this code will be executed
perror("Exec failed");
exit(1);
} else {
// This code will be executed by the parent process
// Wait for the child process to finish
int status;
wait(&status);
if (WIFEXITED(status)) {
printf("Child process exited with status: %d\n", WEXITSTATUS(status));
} else {
perror("Child process didn't exit normally");
}
}
return 0;
}
Comments
Post a Comment