You are on page 1of 4

Question#1

#include <stdio.h>

#include <stdlib.h>

#include <sys/types.h>

#include <unistd.h>

#include <sys/wait.h>

int main() {

int status;

pid_t pid1, pid2, pid3;

pid1 = fork();

if (pid1 == 0) {

printf("Child 1 with PID %d has exit status 20\n", getpid());

exit(20);

pid2 = fork();

if (pid2 == 0) {

printf("Child 2 with PID %d has exit status 25\n", getpid());

exit(25);

pid3 = fork();

if (pid3 == 0) {

printf("Child 3 with PID %d has exit status 30\n", getpid());

exit(30);

}
Question#2

#include <stdio.h>

#include <stdlib.h>

#include <sys/types.h>

#include <unistd.h>

#include <sys/wait.h>

int main() {

pid_t pid;

int status;

pid = fork();

if (pid == 0) {

printf("Child process with PID %d and PPID %d is sleeping\n", getpid(), getppid());

sleep(10);

printf("Child process with PID %d and PPID %d is awake\n", getpid(), getppid());

exit(0);

printf("Parent process with PID %d created child process with PID %d\n", getpid(), pid);

sleep(5);

printf("Parent process with PID %d is executing the ps command to display child process information:\
n", getpid());

char command[100];

sprintf(command, "ps -p %d -o pid,ppid,state,command", pid);

system(command);
waitpid(pid, &status, 0);

printf("Parent process with PID %d waited for child process with PID %d and received exit status %d\
n", getpid(), pid, WEXITSTATUS(status));

return 0;

Question#3

#include <stdio.h>

#include <stdlib.h>

#include <sys/types.h>

#include <sys/wait.h>

#include <unistd.h>

int main() {

pid_t pid = fork();

if (pid == 0) {

// Child process

execl("/bin/ls", "ls", "-l", NULL);

perror("execl");

exit(1);

} else if (pid > 0) {

// Parent process

wait(NULL);

printf("Child process terminated\n");

} else {

// Error

perror("fork");
exit(1);

return 0;

You might also like