OS-ass
OS-ass
Intro:
fork() is a system call for generating or creating process. The process is handled as child and
parent process.
Program:
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
int main(){
pid_t pid;
pid = fork();
if(pid == -1){
printf("Error");
if(pid == 0){
else {
}
}
Output:
Program:
#include <stdio.h>
#include<stdlib.h>
#include<unistd.h>
int main() {
int fd[2];
//f[0] = read
//f[1] = write
if(pipe(fd) == -1){
printf("Error");
pid_t pid;
pid = fork();
if(pid == 0){
close(fd[0]);
int x;
scanf("%d", &x);
close(fd[1]);
else {
close(fd[1]);
int y;
close(fd[0]);
}
return 0;
Output:
FCF Scheduling
Intro:
The first come first serve scheduling algorithm states that the process that requests the CPU
first is allocated the CPU first so it works according to arrival time. It is implemented by using
the FIFO queue.
Program:
#include <stdio.h>
struct PCB {
};
int main() {
P[i].pid[i] = i;
scanf("%d", &P[i].BT[i]);
P[0].WT[0] = 0;
return 0;
Output:
SJF Scheduling
Intro:
It is non-preemptive. It works on the principle that the process having lesser burst time or
execution time will be the first to receive cpu attention.
Program:
#include <stdio.h>
struct PCB {
};
int main() {
P[i].pid[i] = i;
scanf("%d", &P[i].BT[i]);
//sorting acc to BT
for(int j=0;j<5;j++)
pos=j;
for(int k=j+1;k<5;k++)
{
if(P[k].BT[k]<P[pos].BT[pos])
pos=k;
temp=P[j].BT[j];
P[j].BT[j]=P[pos].BT[pos];
P[pos].BT[pos]=temp;
temp=P[j].pid[j];
P[j].pid[j]=P[pos].pid[pos];
P[pos].pid[pos]=temp;
//waiting time
P[0].WT[0]=0;
for(int n=1;n<5;n++)
P[n].WT[n]=0;
for(int p=0;p<n;p++)
P[n].WT[n]+=P[p].BT[p];
total+=P[n].WT[n];
}
//Turnaround time
//printing tat
return 0;
Output:
Threading
Intro:
Thread is used for using same memory block for running multiple processes in parallel manner
to reduce memory overhead.
Program:
#include <stdio.h>
#include<pthread.h>
int main() {
pthread_t th1;
int x = 1;
pthread_join(th1, NULL);
return 0;
return NULL;
Output:
Num is: 1