










































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!











































rd
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); }
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}'");
Q4) #include
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
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
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
//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};
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)