OS
OS
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.
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(){
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;
if(child_pid == 0){
if(i % 2 != 0){
sum_odd += i;
if( i % 2 == 0){
sum_even += i;
Else perror("Folk");
return 0;
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);
return 0;
OUTPUT :
CODE :
#include <stdio.h>
#include <unistd.h>
int main()
sleep(1);
return 0;
OUTPUT:
CODE :
#include <stdio.h>
struct Process {
int pid;
int arrival_time;
int burst_time;
};
calculateWaitingTime(processes, n, waiting_time);
double total_waiting_time = 0;
double total_turnaround_time = 0;
total_waiting_time += waiting_time[i];
total_turnaround_time += turnaround_time[i];
int main() {
int n;
scanf("%d", &n);
processes[i].pid = i + 1;
printf("Enter arrival time and burst time for Process %d: ", i + 1);
calculateAverageTimes(processes, n);
return 0;
Output :
Code:
#include <stdio.h>
struct Process {
};
int waiting_time[n];
int turnaround_time[n];
processes[j + 1] = temp;
waiting_time[0] = 0;
turnaround_time[0] = processes[0].burst_time;
}
}
int main() {
int n;
scanf("%d", &n);
processes[i].pid = i + 1;
scanf("%d", &processes[i].burst_time);
sjfScheduling(processes, n);
return 0;
Output :