Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Operating system practical file du, Assignments of Operating Systems

All 13 practicals of operating system sem 3 Delhi university

Typology: Assignments

2019/2020

Uploaded on 11/29/2021

kanchan-sagar
kanchan-sagar 🇮🇳

5

(2)

10 documents

1 / 50

Toggle sidebar

Related documents


Partial preview of the text

Download Operating system practical file du and more Assignments Operating Systems in PDF only on Docsity!

OPERATING SYSTEM

KANCHAN SAGAR

20/CS/

SEM = 3

rd

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 - 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 #include<sys/stat.h> using namespace std; int main(int argc, char*argv[]){ int i; struct stat buffer; if(argc<2){ cout<<"USAGE
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 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 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 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 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"; }