22F-3672 Lab6 OS

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 14

Question no 1:

#include<studio.h>

#include<stdlib.h>

#include<unistd.h>

#include<sys/type.h>

int main()

pit_t pid1,pid2;

pid1=fork();

if(pid1<0){

perror("Failed to fork P1");

exit(EXIT_FAILURE);

}else if(pid1==0){

printf("I am Child P1 with PID:%d, my parent's PID:%d\n",getpid(),getppid());

exit(0);

}else{

pid2=fork();

if(pid<0){

perror("Failed to fork P2");

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{
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);

printf("Both Childern have Finished Execution, Parent P exiting.\n");

return 0;

Question no 2:
#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) {

perror("Failed to fork P1");

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) {

perror("Failed to fork P2");

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);

printf("Child P2 has finished execution. P1 is exiting.\n");

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) {

printf("C) Child is having ID %d\n", getpid());

printf("D) My Parent ID is %d\n", getppid());

exit(0);

} else {

printf("A) Parent (P) is having ID %d\n", getpid());

wait(NULL);

printf("B) ID of P’s Child is %d\n", pid);

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) {

FILE *file = fopen("Relation.txt", "w");

if (file == NULL) {

printf("Error creating file\n");

return 1;

fclose(file);

} else {

wait(NULL);

FILE *file = fopen("Relation.txt", "a");

if (file == NULL) {

printf("Error opening file\n");

return 1;

char content[100];

printf("Enter content to write into Relation.txt: ");

fgets(content, 100, stdin);

fprintf(file, "%s", content);

fclose(file);

printf("Content written to Relation.txt\n");

return 0;

}
Question no 5:
#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) {

perror("Failed to fork first child");

exit(EXIT_FAILURE);

} else if (pid1 == 0) {

printf("I am the first child (PID: %d)\n", getpid());

sleep(2);

printf("First child (PID: %d) is done.\n", getpid());

exit(0);

} else {

pid2 = fork();
if (pid2 < 0) {

perror("Failed to fork second child");

exit(EXIT_FAILURE);

} else if (pid2 == 0) {

printf("I am the second child (PID: %d)\n", getpid());

sleep(3);

printf("Second child (PID: %d) is done.\n", getpid());

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() {

pid_t pid1, pid2;

pid1 = fork();

if (pid1 < 0) {

perror("Failed to fork C1");

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) {

perror("Failed to fork C2");

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);

printf("Child C2 is done. My parent's PID was: %d\n", getppid());

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 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) {

perror("Failed to fork C1");

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) {

perror("Failed to fork C2");

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);

printf("Child C2 is done. My parent's PID was: %d\n", getppid());

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>

unsigned long long factorial(int n) {

if (n == 0) return 1;

unsigned long long result = 1;

for (int i = 1; i <= n; i++) {

result *= i;

return result;

}
void fibonacci(int steps) {

int t1 = 0, t2 = 1, nextTerm;

printf("Fibonacci Series up to %d steps: ", steps);

for (int i = 1; i <= steps; ++i) {

printf("%d ", t1);

nextTerm = t1 + t2;

t1 = t2;

t2 = nextTerm;

printf("\n");

int main() {

pid_t pid;

int number, steps;

printf("Enter a Number to Calculate its Factorial: ");

scanf("%d", &number);

printf("Enter the Number of Steps for Fibonacci Series: ");

scanf("%d", &steps);

pid = fork();

if (pid < 0) {

perror("Fork failed");

exit(EXIT_FAILURE);

} else if (pid == 0) {

unsigned long long result = factorial(number);


printf("Child Process: Factorial of %d is %llu\n", number, result);

exit(0);

} else {

fibonacci(steps);

wait(NULL);

printf("Parent Process: Child has finished execution.\n");

return 0;

You might also like