22F-3672 Lab6 OS
22F-3672 Lab6 OS
22F-3672 Lab6 OS
#include<studio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/type.h>
int main()
pit_t pid1,pid2;
pid1=fork();
if(pid1<0){
exit(EXIT_FAILURE);
}else if(pid1==0){
exit(0);
}else{
pid2=fork();
if(pid<0){
exit(EXIT_FAILURE);
}else if(pid2==0){
exit(0);
}else{
printf("I am PArent P with PID:%d,created Children P1(PID:%d) and P2(PID:%d)\n",getpid(),pid1,pid2);
wait(NULL);
wait(NULL);
return 0;
Question no 2:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
int main() {
pid1 = fork();
if (pid1 < 0) {
exit(EXIT_FAILURE);
} else if (pid1 == 0) {
printf("I am Child P1 with PID: %d, my parent's PID: %d\n", getpid(), getppid());
pid2 = fork();
if (pid2 < 0) {
exit(EXIT_FAILURE);
} else if (pid2 == 0) {
printf("I am Child P2 with PID: %d, my parent's PID: %d\n", getpid(), getppid());
exit(0);
} else {
wait(NULL);
exit(0);
} else {
printf("I am Parent P with PID: %d, created Child P1 (PID: %d)\n", getpid(), pid1);
wait(NULL);
printf("Child P1 has finished execution. Parent P is exiting.\n");
return 0;
Question no 3:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
int main() {
pid_t pid;
pid = fork();
if (pid < 0) {
perror("Failed to fork");
exit(EXIT_FAILURE);
} else if (pid == 0) {
exit(0);
} else {
wait(NULL);
return 0;
Question no 4:
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <sys/wait.h>
int main() {
pid_t pid;
pid = fork();
if (pid == 0) {
if (file == NULL) {
return 1;
fclose(file);
} else {
wait(NULL);
if (file == NULL) {
return 1;
char content[100];
fclose(file);
return 0;
}
Question no 5:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
int main() {
pid1 = fork();
if (pid1 < 0) {
exit(EXIT_FAILURE);
} else if (pid1 == 0) {
sleep(2);
exit(0);
} else {
pid2 = fork();
if (pid2 < 0) {
exit(EXIT_FAILURE);
} else if (pid2 == 0) {
sleep(3);
exit(0);
} else {
printf("I am the parent (PID: %d), created children (PID: %d and PID: %d)\n", getpid(), pid1, pid2);
wait(NULL);
wait(NULL);
printf("Both children have finished execution. Parent (PID: %d) is exiting.\n", getpid());
return 0;
}
Question no 6:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
int main() {
pid1 = fork();
if (pid1 < 0) {
exit(EXIT_FAILURE);
} else if (pid1 == 0) {
printf("I am child C1 with PID: %d, my parent's PID: %d\n", getpid(), getppid());
exit(0);
} else {
pid2 = fork();
if (pid2 < 0) {
exit(EXIT_FAILURE);
} else if (pid2 == 0) {
printf("I am child C2 with PID: %d, my parent's PID: %d\n", getpid(), getppid());
sleep(5);
exit(0);
} else {
printf("I am parent with PID: %d, created children C1 (PID: %d) and C2 (PID: %d)\n", getpid(), pid1,
pid2);
sleep(2);
exit(0);
return 0;
Question no 7:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
int main() {
pid_t pid1, pid2;
pid1 = fork();
if (pid1 < 0) {
exit(EXIT_FAILURE);
} else if (pid1 == 0) {
printf("I am child C1 with PID: %d, my parent's PID: %d\n", getpid(), getppid());
exit(0);
} else {
pid2 = fork();
if (pid2 < 0) {
exit(EXIT_FAILURE);
} else if (pid2 == 0) {
printf("I am child C2 with PID: %d, my parent's PID: %d\n", getpid(), getppid());
sleep(5);
exit(0);
} else {
printf("I am parent with PID: %d, created children C1 (PID: %d) and C2 (PID: %d)\n", getpid(), pid1,
pid2);
sleep(2);
printf("Parent is exiting now.\n");
exit(0);
return 0;
Question no 8:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
if (n == 0) return 1;
result *= i;
return result;
}
void fibonacci(int steps) {
int t1 = 0, t2 = 1, nextTerm;
nextTerm = t1 + t2;
t1 = t2;
t2 = nextTerm;
printf("\n");
int main() {
pid_t pid;
scanf("%d", &number);
scanf("%d", &steps);
pid = fork();
if (pid < 0) {
perror("Fork failed");
exit(EXIT_FAILURE);
} else if (pid == 0) {
exit(0);
} else {
fibonacci(steps);
wait(NULL);
return 0;