0% found this document useful (0 votes)
30 views10 pages

OS

The document discusses the fork() system call in Unix-like operating systems. It explains that fork() duplicates the calling process, creating a child process that is an exact copy of the parent process. It returns different values to the parent and child processes to indicate successful or unsuccessful creation of the child process. An example C program is provided to demonstrate how fork() works.

Uploaded by

Himanshu Mehra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views10 pages

OS

The document discusses the fork() system call in Unix-like operating systems. It explains that fork() duplicates the calling process, creating a child process that is an exact copy of the parent process. It returns different values to the parent and child processes to indicate successful or unsuccessful creation of the child process. An example C program is provided to demonstrate how fork() works.

Uploaded by

Himanshu Mehra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

1. Write a program to implement fork() system call .

The fork() system call is a fundamental operation in Unix like operating system . The, fork() system
call allows the creation of a new process. When a process calls the fork(), it duplicates itself,
resulting two process to run at the same time.

The new process that created is called a child process, and is the copy of parent process.

The child process uses the same pc (program counter), same CPU registers, and same open files
which use in the parent process. It take no parameters and returns integer value.

1. (- ve) values :- Unsuccessful creation of the child class.

2. (zero) :- Returned to newly created child process.

3. (+ve) values :- Returned to parent or caller. The value contains process ID of the newly created
child process.

CODE :-

#include<stdio.h>

#include<unistd.h>

#include<sys/types.h>

void forkexample(){

if(fork() == 0) printf("Hello from child!\n");

else printf("Hello from parent\n");

int main(){

forkexample();

return 0;

OUTPUT :

Himanshu Mehra / 20 / Q
2. Write a program to compute the sum of odd numbers through parent process and sum of the odd
number through child process using fork() system call.

CODE :-

#include<stdio.h>

#include<stdlib.h>

#include<unistd.h>

#include<sys/types.h>

int main(){

int n;

scanf("%d",&n);

int sum_even = 0;

int sum_odd = 0;

pid_t child_pid = fork();

if(child_pid == 0){

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

if(i % 2 != 0){

sum_odd += i;

printf("Child process : sum of odd number %d = %d\n",n,sum_odd);

else if(child_pid >0){

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

if( i % 2 == 0){

sum_even += i;

}printf("Parent process : sum of even number %d = %d\n", n ,sum_even);

Else perror("Folk");

return 0;

} Himanshu Mehra / Q /20


OUTPUT :

Himanshu Mehra / Q /20


3. What is wait() system call in operation system write a c program .

CODE-

#include<stdio.h>

#include<stdlib.h>

#include<sys/wait.h>

#include<unistd.h>

int main()

pid_t cpid;

if (fork()== 0)

exit(0);

else

cpid = wait(NULL);

printf("Parent pid = %d\n", getpid());

printf("Child pid = %d\n", cpid);

return 0;

OUTPUT :

Himanshu Mehra / Q /20


5. Write a c program to demonstrate sleep() system call in linux os.

CODE :

#include <stdio.h>

#include <unistd.h>

int main()

printf("Program will sleep for 1 second.\n");

sleep(1);

printf("This line will be executed after 1 second.");

return 0;

OUTPUT:

Himanshu Mehra / Q /20


6. Write a program to demonstrate FCSF algorithm.

CODE :

#include <stdio.h>

struct Process {

int pid;

int arrival_time;

int burst_time;

};

void calculateWaitingTime(struct Process processes[], int n, int waiting_time[]) {

waiting_time[0] = 0; // The first process has 0 waiting time

// Calculate waiting time for each process

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

waiting_time[i] = waiting_time[i - 1] + processes[i - 1].burst_time;

void calculateTurnaroundTime(struct Process processes[], int n, int waiting_time[], int


turnaround_time[]) {

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

turnaround_time[i] = waiting_time[i] + processes[i].burst_time;

void calculateAverageTimes(struct Process processes[], int n) {

int waiting_time[n], turnaround_time[n];

calculateWaitingTime(processes, n, waiting_time);

calculateTurnaroundTime(processes, n, waiting_time, turnaround_time);

double total_waiting_time = 0;

double total_turnaround_time = 0;

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


Himanshu Mehra / Q /20

total_waiting_time += waiting_time[i];

total_turnaround_time += turnaround_time[i];

double average_waiting_time = total_waiting_time / n;

double average_turnaround_time = total_turnaround_time / n;

printf("Process\tArrival Time\tBurst Time\tWaiting Time\tTurnaround Time\n");

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

printf("%d\t%d\t\t%d\t\t%d\t\t%d\n", processes[i].pid, processes[i].arrival_time,


processes[i].burst_time, waiting_time[i], turnaround_time[i]);

printf("Average Waiting Time: %.2lf\n", average_waiting_time);

printf("Average Turnaround Time: %.2lf\n", average_turnaround_time);

int main() {

int n;

printf("Enter the number of processes: ");

scanf("%d", &n);

struct Process processes[n];

// Input process information

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

processes[i].pid = i + 1;

printf("Enter arrival time and burst time for Process %d: ", i + 1);

scanf("%d %d", &processes[i].arrival_time, &processes[i].burst_time);

calculateAverageTimes(processes, n);
return 0;

Output :

Himanshu Mehra / Q /20


7. Write a program to demonstrate SJF.

Code:

#include <stdio.h>

struct Process {

int pid; // Process ID

int burst_time; // Burst time

};

void sjfScheduling(struct Process processes[], int n) {

int waiting_time[n];

int turnaround_time[n];

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

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

if (processes[j].burst_time > processes[j + 1].burst_time) {

// Swap the processes

struct Process temp = processes[j];

processes[j] = processes[j + 1];

processes[j + 1] = temp;

waiting_time[0] = 0;

turnaround_time[0] = processes[0].burst_time;

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

waiting_time[i] = turnaround_time[i - 1];

turnaround_time[i] = waiting_time[i] + processes[i].burst_time;

printf("Process\tBurst Time\tWaiting Time\tTurnaround Time\n");

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

printf("%d\t%d\t\t%d\t\t%d\n", processes[i].pid, processes[i].burst_time, waiting_time[i],


turnaround_time[i]);

}
}

int main() {

int n;

printf("Enter the number of processes: ");

scanf("%d", &n);

struct Process processes[n];

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

processes[i].pid = i + 1;

printf("Enter burst time for Process %d: ", i + 1);

scanf("%d", &processes[i].burst_time);

sjfScheduling(processes, n);

return 0;

Output :

Himanshu Mehra / Q /20

You might also like