KEMBAR78
OS Practical File | PDF | Information Technology | Concurrent Computing
0% found this document useful (0 votes)
100 views48 pages

OS Practical File

The document contains 6 questions related to operating system concepts like process creation using fork() and exec() system calls, obtaining kernel and CPU information from /proc files, file statistics using stat(), implementation of Round Robin and First Come First Serve scheduling algorithms. Code snippets in C/C++ are provided as answers to illustrate concepts like process scheduling, waiting time, turnaround time calculations.

Uploaded by

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

OS Practical File

The document contains 6 questions related to operating system concepts like process creation using fork() and exec() system calls, obtaining kernel and CPU information from /proc files, file statistics using stat(), implementation of Round Robin and First Come First Serve scheduling algorithms. Code snippets in C/C++ are provided as answers to illustrate concepts like process scheduling, waiting time, turnaround time calculations.

Uploaded by

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

OPERATING SYSTEM

KANCHAN SAGAR
20/CS/21
SEM = 3rd
PRACTICAL FILE
Q1) // C program to illustrate use of fork() &

// exec() system call for process creation

#include <stdio.h>

#include <sys/types.h>

#include <unistd.h>

#include <stdlib.h>

#include <errno.h>

#include <sys/wait.h>

int main(){

pid_t pid;

int ret = 1;

int status;

pid = fork();

if (pid == -1){

// pid == -1 means error occured


printf("can't fork, error occured\n");

exit(EXIT_FAILURE);

else if (pid == 0){

// pid == 0 means child process created

// getpid() returns process id of calling process

// Here It will return process id of child process

printf("child process, pid = %u\n",getpid());

// Here It will return Parent of child Process means Parent process it self

printf("parent of child process, pid = %u\n",getppid());

// the argv list first argument should point to

// filename associated with file being executed

// the array pointer must be terminated by NULL

// pointer

char * argv_list[] = {"ls","-lart","/home",NULL};

// the execv() only return if error occured.

// The return value is -1

execv("ls",argv_list);

exit(0);

else{

// a positive number is returned for the pid of


// parent process

// getppid() returns process id of parent of

// calling process

// Here It will return parent of parent process's ID

printf("Parent Of parent process, pid = %u\n",getppid());

printf("parent process, pid = %u\n",getpid());

// the parent process calls waitpid() on the child

// waitpid() system call suspends execution of

// calling process until a child specified by pid

// argument has changed state

// see wait() man page for all the flags or options

// used here

if (waitpid(pid, &status, 0) > 0) {

if (WIFEXITED(status) && !WEXITSTATUS(status))

printf("program execution successful\n");

else if (WIFEXITED(status) && WEXITSTATUS(status)) {

if (WEXITSTATUS(status) == 127) {

// execv failed

printf("execv failed\n");

else
printf("program terminated normally,"

" but returned a non-zero status\n");

else

printf("program didn't terminate normally\n");

else {

// waitpid() failed

printf("waitpid() failed\n");

exit(0);

return 0;

Q2) #include<stdio.h>
#include<stdlib.h>

int main()
{
    printf("### Kernel Information ###\n\n");
    system("cat /proc/version_signature | awk '{printf \"Kernel Version: %s\\n\", $0}'");
    printf("\n### CPU Information ###\n\n");
    system("cat /proc/cpuinfo | awk '/processor|model/{print}'");
    return 0;
}

Q3) #include<stdio.h>
#include<stdlib.h>
int main()
{
system("clear");
printf("First version \ncpu model \n");
system("cat /proc/cpuinfo |awk 'NR==5{print}' ");
printf("kernel version \n");
system("cat /proc/version");
printf("\nAmount of time lastn booted \n");
system("cat /proc/uptime");
printf("Amount of memory configured in the system.\n");
system("cat /proc/meminfo |awk 'NR==4{print}'");
printf("\nAmount of memory currently available\n");
system("cat /proc/meminfo |awk 'NR==2{print}'");
return(0);
}
Q4) #include<iostream>
#include<sys/stat.h>
using namespace std;
int main(int argc, char*argv[]){
int i;
struct stat buffer;

if(argc<2){
cout<<"USAGE <FILE NAME LIST FOR WHICH STATS IS NEEDED
list>\n"<<argv[1];

for(i=1;i<argc;i++){
cout<<"\nFILE NAME IS = ";
cout<< argv[i]<<endl;

if(stat(argv[i], &buffer)<0){
cout<<"ERROR IN OBTAINING FILE STATS\n";
}

else{
cout<<"\nOWNER USER ID, UID = "<<buffer.st_uid<<"\n\nGROUP ID, GID =
"<<buffer.st_gid<<endl;

cout<<"\nACCESS PERMISSION = "<<buffer.st_mode<<endl;


cout<<"\nACCESS TIME = "<<buffer.st_atime<<"\n";
}
}
return(0);
}
Output:
Q5) // C++ program for implementation of RR scheduling

#include<iostream>

using namespace std;

// Function to find the waiting time for all

// processes

void findWaitingTime(int processes[], int n,

int bt[], int wt[], int quantum)

// Make a copy of burst times bt[] to store remaining

// burst times.

int rem_bt[n];

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

rem_bt[i] = bt[i];

int t = 0; // Current time

// Keep traversing processes in round robin manner

// until all of them are not done.

while (1)
{

bool done = true;

// Traverse all processes one by one repeatedly

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

// If burst time of a process is greater than 0

// then only need to process further

if (rem_bt[i] > 0)

done = false; // There is a pending process

if (rem_bt[i] > quantum)

// Increase the value of t i.e. shows

// how much time a process has been processed

t += quantum;

// Decrease the burst_time of current process

// by quantum

rem_bt[i] -= quantum;

// If burst time is smaller than or equal to

// quantum. Last cycle for this process


else

// Increase the value of t i.e. shows

// how much time a process has been processed

t = t + rem_bt[i];

// Waiting time is current time minus time

// used by this process

wt[i] = t - bt[i];

// As the process gets fully executed

// make its remaining burst time = 0

rem_bt[i] = 0;

// If all processes are done

if (done == true)

break;

// Function to calculate turn around time

void findTurnAroundTime(int processes[], int n,


int bt[], int wt[], int tat[])

// calculating turnaround time by adding

// bt[i] + wt[i]

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

tat[i] = bt[i] + wt[i];

// Function to calculate average time

void findavgTime(int processes[], int n, int bt[],

int quantum)

int wt[n], tat[n], total_wt = 0, total_tat = 0;

// Function to find waiting time of all processes

findWaitingTime(processes, n, bt, wt, quantum);

// Function to find turn around time for all processes

findTurnAroundTime(processes, n, bt, wt, tat);

// Display processes along with all details

cout << "Processes "<< " Burst time "

<< " Waiting time " << " Turn around time\n";

// Calculate total waiting time and total turn


// around time

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

total_wt = total_wt + wt[i];

total_tat = total_tat + tat[i];

cout << " " << i+1 << "\t\t" << bt[i] <<"\t "

<< wt[i] <<"\t\t " << tat[i] <<endl;

cout << "Average waiting time = "

<< (float)total_wt / (float)n;

cout << "\nAverage turn around time = "

<< (float)total_tat / (float)n;

// Driver code

int main()

// process id's

int processes[] = { 1, 2, 3};

int n = sizeof processes / sizeof processes[0];

// Burst time of all processes

int burst_time[] = {10, 5, 8};


// Time quantum

int quantum = 2;

findavgTime(processes, n, burst_time, quantum);

return 0;

Q6) // C++ program for implementation of FCFS

// scheduling

#include<iostream>

using namespace std;

// Function to find the waiting time for all

// processes

void findWaitingTime(int processes[], int n,

                          int bt[], int wt[])


{

    // waiting time for first process is 0

    wt[0] = 0;

    // calculating waiting time

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

        wt[i] =  bt[i-1] + wt[i-1] ;

// Function to calculate turn around time

void findTurnAroundTime( int processes[], int n,

                  int bt[], int wt[], int tat[])

    // calculating turnaround time by adding

    // bt[i] + wt[i]

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

        tat[i] = bt[i] + wt[i];

}
//Function to calculate average time

void findavgTime( int processes[], int n, int bt[])

    int wt[n], tat[n], total_wt = 0, total_tat = 0;

    //Function to find waiting time of all processes

    findWaitingTime(processes, n, bt, wt);

    //Function to find turn around time for all processes

    findTurnAroundTime(processes, n, bt, wt, tat);

    //Display processes along with all details

    cout << "Processes  "<< " Burst time  "

         << " Waiting time  " << " Turn around time\n";

    // Calculate total waiting time and total turn

    // around time


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

    {

        total_wt = total_wt + wt[i];

        total_tat = total_tat + tat[i];

        cout << "   " << i+1 << "\t\t" << bt[i] <<"\t    "

            << wt[i] <<"\t\t  " << tat[i] <<endl;

    }

    cout << "Average waiting time = "

         << (float)total_wt / (float)n;

    cout << "\nAverage turn around time = "

         << (float)total_tat / (float)n;

// Driver code

int main()

    //process id's
    int processes[] = { 1, 2, 3};

    int n = sizeof processes / sizeof processes[0];

    //Burst time of all processes

    int  burst_time[] = {10, 5, 8};

    findavgTime(processes, n,  burst_time);

    return 0;

Q7) #include<iostream>
using namespace std;

int main()

int n,temp,tt=0,min,d,i,j;

float atat=0,awt=0,stat=0,swt=0;

cout<<"enter no of process"<<endl;

cin>>n;

int a[n],b[n],e[n],tat[n],wt[n];

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

cout<<"enter arival time "; //input

cin>>a[i];

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

cout<<"enter brust time "; //input

cin>>b[i];

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

for(j=i+1;j<n;j++)

{
if(b[i]>b[j])

temp=a[i];

a[i]=a[j];

a[j]=temp;

temp=b[i];

b[i]=b[j];

b[j]=temp;

min=a[0];

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

if(min>a[i])

min=a[i];

d=i;

tt=min;

e[d]=tt+b[d];

tt=e[d];
for(i=0;i<n;i++)

if(a[i]!=min)

e[i]=b[i]+tt;

tt=e[i];

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

tat[i]=e[i]-a[i];

stat=stat+tat[i];

wt[i]=tat[i]-b[i];

swt=swt+wt[i];

atat=stat/n;

awt=swt/n;

cout<<"Process Arrival-time(s) Burst-time(s) Waiting-time(s) Turnaround-time(s)\n";

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

cout<<"P"<<i+1<<" "<<a[i]<<" "<<b[i]<<" "<<wt[i]<<" "<<tat[i]<<endl;

}
cout<<"awt="<<awt<<" atat="<<atat; //average waiting time and turn around time

Q8) // C++ program to implement Shortest Job first with Arrival

// Time

#include <iostream>

using namespace std;

int mat[10][6];

void swap(int* a, int* b)

int temp = *a;

*a = *b;

*b = temp;

void arrangeArrival(int num, int mat[][6])

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

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

if (mat[j][1] > mat[j + 1][1]) {

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

swap(mat[j][k], mat[j + 1][k]);

void completionTime(int num, int mat[][6])

int temp, val;

mat[0][3] = mat[0][1] + mat[0][2];

mat[0][5] = mat[0][3] - mat[0][1];

mat[0][4] = mat[0][5] - mat[0][2];

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

temp = mat[i - 1][3];

int low = mat[i][2];

for (int j = i; j < num; j++) {

if (temp >= mat[j][1] && low >= mat[j][2]) {

low = mat[j][2];

val = j;
}

mat[val][3] = temp + mat[val][2];

mat[val][5] = mat[val][3] - mat[val][1];

mat[val][4] = mat[val][5] - mat[val][2];

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

swap(mat[val][k], mat[i][k]);

int main()

int num, temp;

cout << "Enter number of Process: ";

cin >> num;

cout << "...Enter the process ID...\n";

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

cout << "...Process " << i + 1 << "...\n";

cout << "Enter Process Id: ";

cin >> mat[i][0];

cout << "Enter Arrival Time: ";

cin >> mat[i][1];


cout << "Enter Burst Time: ";

cin >> mat[i][2];

cout << "Before Arrange...\n";

cout << "Process ID\tArrival Time\tBurst Time\n";

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

cout << mat[i][0] << "\t\t" << mat[i][1] << "\t\t"

<< mat[i][2] << "\n";

arrangeArrival(num, mat);

completionTime(num, mat);

cout << "Final Result...\n";

cout << "Process ID\tArrival Time\tBurst Time\tWaiting "

"Time\tTurnaround Time\n";

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

cout << mat[i][0] << "\t\t" << mat[i][1] << "\t\t"

<< mat[i][2] << "\t\t" << mat[i][4] << "\t\t"

<< mat[i][5] << "\n";

}
}

Q9) //C++ Implementation of Non-Preemptive Priority Scheduling Algorithm

#include <iostream>

using namespace std;

int main()

int n = 5; //Number of Processes

int CPU = 0; //CPU Current time

int allTime = 0; // Time needed to finish all processes

int arrivaltime[n] = {0, 5, 12, 2, 9};

int bursttime[n] = {11, 28, 2, 10, 16};

int priority[n] = {2, 0, 3, 1, 4};

int ATt[n];
int NoP = n; //number of Processes

int PPt[n];

int waitingTime[n];

int turnaroundTime[n];

int i = 0;

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

PPt[i] = priority[i];

ATt[i] = arrivaltime[i];

int LAT = 0; //LastArrivalTime

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

if (arrivaltime[i] > LAT)

LAT = arrivaltime[i];

int MAX_P = 0; //Max Priority

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

if (PPt[i] > MAX_P)

MAX_P = PPt[i];

int ATi = 0; //Pointing to Arrival Time indix

int P1 = PPt[0]; //Pointing to 1st priority Value

int P2 = PPt[0]; //Pointing to 2nd priority Value


//finding the First Arrival Time and Highest priority Process

int j = -1;

while (NoP > 0 && CPU <= 1000)

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

if ((ATt[i] <= CPU) && (ATt[i] != (LAT + 10)))

if (PPt[i] != (MAX_P + 1))

P2 = PPt[i];

j = 1;

if (P2 < P1)

j = 1;

ATi = i;

P1 = PPt[i];

P2 = PPt[i];

}
if (j == -1)

CPU = CPU + 1;

continue;

else

waitingTime[ATi] = CPU - ATt[ATi];

CPU = CPU + bursttime[ATi];

turnaroundTime[ATi] = CPU - ATt[ATi];

ATt[ATi] = LAT + 10;

j = -1;

PPt[ATi] = MAX_P + 1;

ATi = 0; //Pointing to Arrival Time index

P1 = MAX_P + 1; //Pointing to 1st priority Value

P2 = MAX_P + 1; //Pointing to 2nd priority Value

NoP = NoP - 1;

cout << "\nProcess_Number\tBurst_Time\tPriority\tArrival_Time\tWaiting_Time\


tTurnaround_Time\n\n";

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

cout << "P" << i + 1 << "\t\t" << bursttime[i] << "\t\t" << priority[i] << "\t\t" <<
arrivaltime[i] << "\t\t" << waitingTime[i] << "\t\t" << turnaroundTime[i] << endl;
}

float AvgWT = 0; //Average waiting time

float AVGTaT = 0; // Average Turn around time

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

AvgWT = waitingTime[i] + AvgWT;

AVGTaT = turnaroundTime[i] + AVGTaT;

cout << "Average waiting time = " << AvgWT / n << endl;

cout << "Average turnaround time = " << AVGTaT / n << endl;

Q10) #include <iostream>

#include <algorithm>
#include <iomanip>

#include <string.h>

using namespace std;

struct process {

int pid;

int arrival_time;

int burst_time;

int priority;

int start_time;

int completion_time;

int turnaround_time;

int waiting_time;

int response_time;

};

int main() {

int n;

struct process p[100];

float avg_turnaround_time;

float avg_waiting_time;

float avg_response_time;

float cpu_utilisation;

int total_turnaround_time = 0;
int total_waiting_time = 0;

int total_response_time = 0;

int total_idle_time = 0;

float throughput;

int burst_remaining[100];

int is_completed[100];

memset(is_completed,0,sizeof(is_completed));

cout << setprecision(2) << fixed;

cout<<"Enter the number of processes: ";

cin>>n;

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

cout<<"Enter arrival time of process "<<i+1<<": ";

cin>>p[i].arrival_time;

cout<<"Enter burst time of process "<<i+1<<": ";

cin>>p[i].burst_time;

cout<<"Enter priority of the process "<<i+1<<": ";

cin>>p[i].priority;

p[i].pid = i+1;

burst_remaining[i] = p[i].burst_time;

cout<<endl;

}
int current_time = 0;

int completed = 0;

int prev = 0;

while(completed != n) {

int idx = -1;

int mx = -1;

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

if(p[i].arrival_time <= current_time && is_completed[i] == 0) {

if(p[i].priority > mx) {

mx = p[i].priority;

idx = i;

if(p[i].priority == mx) {

if(p[i].arrival_time < p[idx].arrival_time) {

mx = p[i].priority;

idx = i;

if(idx != -1) {

if(burst_remaining[idx] == p[idx].burst_time) {

p[idx].start_time = current_time;
total_idle_time += p[idx].start_time - prev;

burst_remaining[idx] -= 1;

current_time++;

prev = current_time;

if(burst_remaining[idx] == 0) {

p[idx].completion_time = current_time;

p[idx].turnaround_time = p[idx].completion_time - p[idx].arrival_time;

p[idx].waiting_time = p[idx].turnaround_time - p[idx].burst_time;

p[idx].response_time = p[idx].start_time - p[idx].arrival_time;

total_turnaround_time += p[idx].turnaround_time;

total_waiting_time += p[idx].waiting_time;

total_response_time += p[idx].response_time;

is_completed[idx] = 1;

completed++;

else {

current_time++;

}
int min_arrival_time = 10000000;

int max_completion_time = -1;

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

min_arrival_time = min(min_arrival_time,p[i].arrival_time);

max_completion_time = max(max_completion_time,p[i].completion_time);

avg_turnaround_time = (float) total_turnaround_time / n;

avg_waiting_time = (float) total_waiting_time / n;

avg_response_time = (float) total_response_time / n;

cpu_utilisation = ((max_completion_time - total_idle_time) / (float)


max_completion_time )*100;

throughput = float(n) / (max_completion_time - min_arrival_time);

cout<<endl<<endl;

cout<<"#P\t"<<"AT\t"<<"BT\t"<<"PRI\t"<<"ST\t"<<"CT\t"<<"TAT\t"<<"WT\t"<<"RT\
t"<<"\n"<<endl;

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

cout<<p[i].pid<<"\t"<<p[i].arrival_time<<"\t"<<p[i].burst_time<<"\t"<<p[i].priority<<"\
t"<<p[i].start_time<<"\t"<<p[i].completion_time<<"\t"<<p[i].turnaround_time<<"\
t"<<p[i].waiting_time<<"\t"<<p[i].response_time<<"\t"<<"\n"<<endl;

cout<<"Average Turnaround Time = "<<avg_turnaround_time<<endl;

cout<<"Average Waiting Time = "<<avg_waiting_time<<endl;

cout<<"Average Response Time = "<<avg_response_time<<endl;


cout<<"CPU Utilization = "<<cpu_utilisation<<"%"<<endl;

cout<<"Throughput = "<<throughput<<" process/unit time"<<endl;

/*

AT - Arrival Time of the process

BT - Burst time of the process

ST - Start time of the process

CT - Completion time of the process

TAT - Turnaround time of the process

WT - Waiting time of the process

RT - Response time of the process

Formulas used:

TAT = CT - AT

WT = TAT - BT

RT = ST - AT

*/
Q11) #include <iostream>

#include <algorithm>

#include <iomanip>

#include <string.h>

using namespace std;

struct process {

int pid;

int arrival_time;

int burst_time;

int start_time;

int completion_time;

int turnaround_time;

int waiting_time;
int response_time;

};

int main() {

int n;

struct process p[100];

float avg_turnaround_time;

float avg_waiting_time;

float avg_response_time;

float cpu_utilisation;

int total_turnaround_time = 0;

int total_waiting_time = 0;

int total_response_time = 0;

int total_idle_time = 0;

float throughput;

int burst_remaining[100];

int is_completed[100];

memset(is_completed,0,sizeof(is_completed));

cout << setprecision(2) << fixed;

cout<<"Enter the number of processes: ";

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

cout<<"Enter arrival time of process "<<i+1<<": ";

cin>>p[i].arrival_time;

cout<<"Enter burst time of process "<<i+1<<": ";

cin>>p[i].burst_time;

p[i].pid = i+1;

burst_remaining[i] = p[i].burst_time;

cout<<endl;

int current_time = 0;

int completed = 0;

int prev = 0;

while(completed != n) {

int idx = -1;

int mn = 10000000;

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

if(p[i].arrival_time <= current_time && is_completed[i] == 0) {

if(burst_remaining[i] < mn) {

mn = burst_remaining[i];

idx = i;

if(burst_remaining[i] == mn) {

if(p[i].arrival_time < p[idx].arrival_time) {


mn = burst_remaining[i];

idx = i;

if(idx != -1) {

if(burst_remaining[idx] == p[idx].burst_time) {

p[idx].start_time = current_time;

total_idle_time += p[idx].start_time - prev;

burst_remaining[idx] -= 1;

current_time++;

prev = current_time;

if(burst_remaining[idx] == 0) {

p[idx].completion_time = current_time;

p[idx].turnaround_time = p[idx].completion_time - p[idx].arrival_time;

p[idx].waiting_time = p[idx].turnaround_time - p[idx].burst_time;

p[idx].response_time = p[idx].start_time - p[idx].arrival_time;

total_turnaround_time += p[idx].turnaround_time;

total_waiting_time += p[idx].waiting_time;

total_response_time += p[idx].response_time;
is_completed[idx] = 1;

completed++;

else {

current_time++;

int min_arrival_time = 10000000;

int max_completion_time = -1;

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

min_arrival_time = min(min_arrival_time,p[i].arrival_time);

max_completion_time = max(max_completion_time,p[i].completion_time);

avg_turnaround_time = (float) total_turnaround_time / n;

avg_waiting_time = (float) total_waiting_time / n;

avg_response_time = (float) total_response_time / n;

cpu_utilisation = ((max_completion_time - total_idle_time) / (float)


max_completion_time )*100;

throughput = float(n) / (max_completion_time - min_arrival_time);

cout<<endl<<endl;
cout<<"#P\t"<<"AT\t"<<"BT\t"<<"ST\t"<<"CT\t"<<"TAT\t"<<"WT\t"<<"RT\t"<<"\
n"<<endl;

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

cout<<p[i].pid<<"\t"<<p[i].arrival_time<<"\t"<<p[i].burst_time<<"\t"<<p[i].start_time<<"\
t"<<p[i].completion_time<<"\t"<<p[i].turnaround_time<<"\t"<<p[i].waiting_time<<"\
t"<<p[i].response_time<<"\t"<<"\n"<<endl;

cout<<"Average Turnaround Time = "<<avg_turnaround_time<<endl;

cout<<"Average Waiting Time = "<<avg_waiting_time<<endl;

cout<<"Average Response Time = "<<avg_response_time<<endl;

cout<<"CPU Utilization = "<<cpu_utilisation<<"%"<<endl;

cout<<"Throughput = "<<throughput<<" process/unit time"<<endl;


}

Q12) # include <stdio.h>
# include <pthread.h>

void * thread_sum(void *);
int TotalSum=0;
pthread_mutex_t mVar=PTHREAD_MUTEX_INITIALIZER;

main()
{
    int iNumber,iCount;
    pthread_t tid;
    printf("Enter Number Up to Which You want to Sum :");
    scanf("%d",&iNumber);    
    pthread_create(&tid,NULL,thread_sum,(void *)&iNumber);
    for(iCount=1;iCount<=iNumber;iCount=iCount+2)
    {
        pthread_mutex_lock(&mVar);
        TotalSum=TotalSum + iCount;
        pthread_mutex_unlock(&mVar);
    }
    
    pthread_join(tid,NULL);
    
    printf("Final Sum is : %d \n",TotalSum);
//    return 0;
}

void *thread_sum(void *no)
{
    int *iNumber,iCount;
    iNumber=(int*)no;
    
    for(iCount=2;iCount<=*iNumber;iCount=iCount+2)
    {
        pthread_mutex_lock(&mVar);
        TotalSum=TotalSum + iCount;
        pthread_mutex_unlock(&mVar);
    }
    pthread_exit(NULL);    
}

Q13) First bit

#include<stdio.h>
#include<conio.h>
#define max 25
void main()
{
int frag[max],b[max],f[max],i,j,nb,nf,temp;
static int bf[max],ff[max];
clrscr();
printf("\nEnter the number of blocks:");
scanf("%d",&nb);
printf("Enter the number of files:");
scanf("%d",&nf);
printf("\nEnter the size of the blocks:-\n");
for(i=1;i<=nb;i++)
{
printf("Block %d:",i);
scanf("%d",&b[i]);
}
printf("Enter the size of the files:-\n");
for(i=1;i<=nf;i++)
{
printf("File %d:",i);
scanf("%d",&f[i]);
}
for(i=1;i<=nf;i++)
{
for(j=1;j<=nb;j++)
{
if(bf[j]!=1)
{
temp=b[j]-f[i];
if(temp>=0)
{
ff[i]=j;
break;
}
}
}
frag[i]=temp;
bf[ff[i]]=1;
}
printf("\nFile_no:\tFile_size :\tBlock_no:\tBlock_size:\tFragment");
for(i=1;i<=nf;i++)
printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d",i,f[i],ff[i],b[ff[i]],frag[i]);
getch();
}

Output:
Best fit

#include<stdio.h>
#include<conio.h>
#define max 25
void main()
{
int frag[max],b[max],f[max],i,j,nb,nf,temp,highest=0;
static int bf[max],ff[max];
clrscr();
printf("\nEnter the number of blocks:");
scanf("%d",&nb);
printf("Enter the number of files:");
scanf("%d",&nf);
printf("\nEnter the size of the blocks:-\n");
for(i=1;i<=nb;i++)
{
printf("Block %d:",i);
scanf("%d",&b[i]);
}
printf("Enter the size of the files:-\n");
for(i=1;i<=nf;i++)
{
printf("File %d:",i);
scanf("%d",&f[i]);
}
for(i=1;i<=nf;i++)
{
for(j=1;j<=nb;j++)
{
if(bf[j]!=1) //if bf[j] is not allocated
{
temp=b[j]-f[i];
if(temp>=0)
if(highest<temp)
{
ff[i]=j;
highest=temp;
}
}
}
frag[i]=highest;
bf[ff[i]]=1;
highest=0;
}
printf("\nFile_no  \tFile_size  \tBlock_no  \tBlock_size  \tFragment");
for(i=1;i<=nf;i++)
printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d",i,f[i],ff[i],b[ff[i]],frag[i]);
getch();
}

Output:

Worst bit

#include<stdio.h>
#include<conio.h>
#define max 25
void main()
{
int frag[max],b[max],f[max],i,j,nb,nf,temp,highest=0;
static int bf[max],ff[max];
clrscr();
printf("\nEnter the number of blocks:");
scanf("%d",&nb);
printf("Enter the number of files:");
scanf("%d",&nf);
printf("\nEnter the size of the blocks:-\n");
for(i=1;i<=nb;i++)
{
printf("Block %d:",i);
scanf("%d",&b[i]);
}
printf("Enter the size of the files:-\n");
for(i=1;i<=nf;i++)
{
printf("File %d:",i);
scanf("%d",&f[i]);
}
for(i=1;i<=nf;i++)
{
for(j=1;j<=nb;j++)
{
if(bf[j]!=1) //if bf[j] is not allocated
{
temp=b[j]-f[i];
if(temp>=0)
if(highest<temp)
{
ff[i]=j;
highest=temp;
}
}
}
frag[i]=highest;
bf[ff[i]]=1;
highest=0;
}
printf("\nFile_no  \tFile_size  \tBlock_no  \tBlock_size  \tFragment");
for(i=1;i<=nf;i++)
printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d",i,f[i],ff[i],b[ff[i]],frag[i]);
getch();
}

Output:

You might also like