0% found this document useful (0 votes)
4 views

OS-ass

Uploaded by

devmaqib
Copyright
© © All Rights Reserved
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

OS-ass

Uploaded by

devmaqib
Copyright
© © All Rights Reserved
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
You are on page 1/ 11

FORK() System Call

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

printf("Welcome to child process %d", getpid());

else {

printf("Welcome to parent process %d", getpid());

}
}

Output:

PIPE() System Call


Intro:
pipe() is a system call for inter-process communication. one end is for writing data and the other
end is it's recipient. We can communicate between child and parent process through pipe.

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;

printf("Enter value of x: ");

scanf("%d", &x);

write(fd[1], &x, sizeof(int));

close(fd[1]);

else {

close(fd[1]);

int y;

read(fd[0], &y, sizeof(int));

close(fd[0]);

printf("Got from child process: %d", y);

}
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 pid[5], WT[5], BT[5], TAT[5];

};

int main() {

struct PCB P[5];


for(int i = 0; i<5; i++)

P[i].pid[i] = i;

printf("Enter Burst Time of process %d: ", P[i].pid[i]+1);

scanf("%d", &P[i].BT[i]);

P[0].WT[0] = 0;

for(int j = 1; j<5; j++){

P[j].WT[j] = P[j-1].BT[j-1] + P[j-1].WT[j-1];

for(int l = 0; l<5; l++){

P[l].TAT[l] = P[l].BT[l] + P[l].WT[l];

for(int k = 0; k<5; k++){

printf("Waiting Time of process %d: %d \n",P[k].pid[k]+1, P[k].WT[k]);

for(int m = 0; m<5; m++){


printf("Turaround Time of process %d: %d \n",P[m].pid[m]+1, P[m].TAT[m]);

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 pid[5], WT[5], BT[5], TAT[5];

};

int main() {

int pos, temp, total;

struct PCB P[5];

for(int i = 0; i<5; i++)

P[i].pid[i] = i;

printf("Enter Burst Time of process %d: ", P[i].pid[i]+1);

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

for(int l = 0; l<5; l++){

P[l].TAT[l] = P[l].BT[l] + P[l].WT[l];

//Printing waiting time

for(int o = 0; o<5; o++){

printf("Waiting Time of process %d: %d \n",P[o].pid[o]+1, P[o].WT[o]);

//printing tat

for(int m = 0; m<5; m++){

printf("Turaround Time of process %d: %d \n",P[m].pid[m]+1, P[m].TAT[m]);

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>

void *helloThread(void *add);

int main() {
pthread_t th1;

int x = 1;

pthread_create(&th1, NULL, helloThread, (void*) &x);

pthread_join(th1, NULL);

return 0;

void *helloThread(void *add){

int *add_num = (int *) (add);

printf("Num is: %d", *add_num);

return NULL;

Output:
Num is: 1

You might also like