├── v1.pdf ├── README.md ├── Programs ├── 22.cc ├── 05.cc ├── 02.cc ├── 08.cc ├── 04.cc ├── 17.cc ├── 03.cc ├── 13.cc ├── 20.cc ├── 07.cc ├── 15.cc ├── 12.cc ├── 06.cc ├── 11.cc ├── 18.cc ├── 14.cc ├── 16.cc ├── 21.cc ├── 19.cc ├── 09.cc ├── 01.cc ├── 23,24,25.cc └── 10.cc └── LICENSE /v1.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jishanshaikh4/CSE-317-Operating-Systems-Laboratory/HEAD/v1.pdf -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # CSE-317-Operating-Systems-Laboratory 2 | 3 | Operating Systems Laboratory (C/C++) programs for Spring 2018 (CSE-317) 4 | -------------------------------------------------------------------------------- /Programs/22.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | sem_t mutex; 7 | void* thread(void* arg){ 8 | //wait 9 | sem_wait(&mutex); 10 | printf("\nEntered..\n"); 11 | 12 | //critical section 13 | sleep(4); 14 | 15 | //signal 16 | printf("\nJust Exiting...\n"); 17 | sem_post(&mutex); 18 | } 19 | 20 | int main(){ 21 | sem_init(&mutex, 0, 1); 22 | pthread_t t1,t2; 23 | pthread_create(&t1,NULL,thread,NULL); 24 | sleep(2); 25 | pthread_create(&t2,NULL,thread,NULL); 26 | pthread_join(t1,NULL); 27 | pthread_join(t2,NULL); 28 | sem_destroy(&mutex); 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /Programs/05.cc: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main(){ 5 | int ms,i,ps[20],n,size,p[20],s,intr=0; 6 | printf("Enter size of memory:"); 7 | scanf("%d",&ms); 8 | printf("Enter memory for OS:"); 9 | scanf("%d",&s); 10 | ms-=s; 11 | printf("Enter no.of partitions to be divided:"); 12 | scanf("%d",&n); 13 | size=ms/n; 14 | for(i=0;i 2 | using namespace std; 3 | 4 | int a,b,u,v,n,i,j,ne=1; 5 | 6 | int visited[10]={0},min,mincost=0,cost[10][10]; 7 | 8 | int main(){ 9 | printf("\nEnter the number of nodes:"); 10 | scanf("%d",&n); 11 | printf("\nEnter the adjacency matrix:\n"); 12 | for(i=1;i<=n;i++) 13 | for(j=1;j<=n;j++){ 14 | scanf("%d",&cost[i][j]); 15 | if(cost[i][j]==0) 16 | cost[i][j]=999; 17 | } 18 | visited[1]=1; 19 | printf("\n"); 20 | while(ne < n){ 21 | for(i=1,min=999;i<=n;i++) 22 | for(j=1;j<=n;j++) 23 | if(cost[i][j]< min) 24 | if(visited[i]!=0){ 25 | min=cost[i][j]; 26 | a=u=i; 27 | b=v=j; 28 | } 29 | if(visited[u]==0 || visited[v]==0){ 30 | printf("\n Edge %d:(%d %d) cost:%d",ne++,a,b,min); 31 | mincost+=min; 32 | visited[b]=1; 33 | } 34 | cost[a][b]=cost[b][a]=999; 35 | } 36 | printf("\n Minimun cost=%d",mincost); 37 | return 0; 38 | } 39 | -------------------------------------------------------------------------------- /Programs/08.cc: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main(){ 5 | int a[10],b[10],x[10],i,j,smallest,count=0,time,n; 6 | double avg=0,tt=0,end; 7 | printf("enter the number of Processes:\n"); 8 | scanf("%d",&n); 9 | printf("enter arrival time\n"); 10 | for(i=0;i0) 24 | smallest=i; 25 | } 26 | b[smallest]--; 27 | if(b[smallest]==0){ 28 | count++; 29 | end=time+1; 30 | avg=avg+end-a[smallest]-x[smallest]; 31 | tt= tt+end-a[smallest]; 32 | } 33 | } 34 | printf("\n\nAverage waiting time = %lf\n",avg/n); 35 | printf("Average Turnaround time = %lf",tt/n); 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /Programs/04.cc: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main(){ 5 | int ms,i,ps[20],n,size,p[20],s,intr=0; 6 | printf("Enter size of memory:"); 7 | scanf("%d",&ms); 8 | printf("Enter memory for OS:"); 9 | scanf("%d",&s); 10 | ms-=s; 11 | printf("Enter no.of partitions to be divided:"); 12 | scanf("%d",&n); 13 | size=ms/n; 14 | for(i=0;i 2 | using namespace std; 3 | 4 | int pageFaults(vector pages, int n, int f){ 5 | unordered_set s; 6 | 7 | queue indexes; 8 | 9 | int page_faults = 0; 10 | for(int i=0; i> n >> f; 35 | vector pages(n); 36 | for(int i=0; i> pages[i]; 38 | int page_faults = pageFaults(pages, n, f); 39 | cout << "Page faults: " << page_faults; 40 | cout << "\nPage fault percentage: " << float(page_faults/n*100); 41 | return 0; 42 | } 43 | -------------------------------------------------------------------------------- /Programs/03.cc: -------------------------------------------------------------------------------- 1 | // Uniprogramming Implementation in C++ 2 | // By: Jishan Shaikh 3 | 4 | #include 5 | using namespace std; 6 | 7 | int main(){ 8 | float ttat=0, tat=0, cpuu=0, iou, tp=0, total=0, average=0, ipuu=0; 9 | int n; 10 | cin >> n; 11 | int p[n][5]; 12 | for(int i=0; i> p[i][j]; 16 | ttat = ttat + p[i][j]; 17 | tat = tat + p[i][j]; 18 | } 19 | cout << "Turn around time of process " << i << " is " << ttat << endl; 20 | total += ttat; 21 | } 22 | average = total/n; 23 | cout << "Average turn around time is " << average << endl; 24 | for(int i=0; i 2 | using namespace std; 3 | 4 | int main(){ 5 | int bsize[10], psize[10], bno, pno, flags[10], allocation[10], i, j; 6 | for(i = 0; i < 10; i++){ 7 | flags[i] = 0; 8 | allocation[i] = -1; 9 | } 10 | printf("Enter no. of blocks: "); 11 | scanf("%d", &bno); 12 | printf("\nEnter size of each block: "); 13 | for(i = 0; i < bno; i++) 14 | scanf("%d", &bsize[i]); 15 | printf("\nEnter no. of processes: "); 16 | scanf("%d", &pno); 17 | printf("\nEnter size of each process: "); 18 | for(i = 0; i < pno; i++) 19 | scanf("%d", &psize[i]); 20 | for(i = 0; i < pno; i++) 21 | for(j = 0; j < bno; j++) 22 | if(flags[j] == 0 && bsize[j] >= psize[i]){ 23 | allocation[j] = i; 24 | flags[j] = 1; 25 | break; 26 | } 27 | printf("\nBlock no.\tsize\t\tprocess no.\t\tsize"); 28 | for(i = 0; i < bno; i++){ 29 | printf("\n%d\t\t%d\t\t", i+1, bsize[i]); 30 | if(flags[i] == 1) 31 | printf("%d\t\t\t%d",allocation[i]+1,psize[allocation[i]]); 32 | else 33 | printf("Not allocated"); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /Programs/20.cc: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int mutex=1,full=0,empty=3,x=0; 5 | int wait(int s){ 6 | return (--s); 7 | } 8 | int signal(int s){ 9 | return(++s); 10 | } 11 | void producer(){ 12 | mutex=wait(mutex); 13 | full=signal(full); 14 | empty=wait(empty); 15 | x++; 16 | printf("\nProducer produces the item %d",x); 17 | mutex=signal(mutex); 18 | } 19 | 20 | void consumer(){ 21 | mutex=wait(mutex); 22 | full=wait(full); 23 | empty=signal(empty); 24 | printf("\nConsumer consumes item %d",x); 25 | x--; 26 | mutex=signal(mutex); 27 | } 28 | int main(){ 29 | printf("\n1.Producer\n2.Consumer\n3.Exit"); 30 | while(1){ 31 | printf("\nEnter your choice:"); 32 | scanf("%d",&n); 33 | switch(n){ 34 | case 1: if((mutex==1)&&(empty!=0)) 35 | producer(); 36 | else 37 | printf("Buffer is full!!"); 38 | break; 39 | case 2: if((mutex==1)&&(full!=0)) 40 | consumer(); 41 | else 42 | printf("Buffer is empty!!"); 43 | break; 44 | case 3: 45 | exit(0); 46 | break; 47 | } 48 | } 49 | return 0; 50 | } 51 | -------------------------------------------------------------------------------- /Programs/07.cc: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main(){ 5 | int bt[20],p[20],wt[20],tat[20],i,j,n,total=0,pos,temp; 6 | float avg_wt,avg_tat; 7 | printf("Enter number of process:"); 8 | scanf("%d",&n); 9 | printf("\nEnter Burst Time:\n"); 10 | for(i=0;i 2 | using namespace std; 3 | 4 | int main(){ 5 | int fragment[20],b[20],p[20],i,j,nb,np,temp,lowest=9999; 6 | static int barray[20],parray[20]; 7 | printf("\n\t\t\tMemory Management Scheme - Best Fit"); 8 | printf("\nEnter the number of blocks:"); 9 | scanf("%d",&nb); 10 | printf("Enter the number of processes:"); 11 | scanf("%d",&np); 12 | printf("\nEnter the size of the blocks:-\n"); 13 | for(i=1;i<=nb;i++){ 14 | printf("Block no.%d:",i); 15 | scanf("%d",&b[i]); 16 | } 17 | 18 | printf("\nEnter the size of the processes :-\n"); 19 | for(i=1;i<=np;i++){ 20 | printf("Process no.%d:",i); 21 | scanf("%d",&p[i]); 22 | } 23 | for(i=1;i<=np;i++){ 24 | for(j=1;j<=nb;j++){ 25 | if(barray[j]!=1){ 26 | temp=b[j]-p[i]; 27 | if(temp>=0) 28 | if(lowest>temp){ 29 | parray[i]=j; 30 | lowest=temp; 31 | } 32 | } 33 | } 34 | fragment[i]=lowest; 35 | barray[parray[i]]=1; 36 | lowest=10000; 37 | } 38 | printf("\nProcess_no\tProcess_size\tBlock_no\tBlock_size\tFragment"); 39 | for(i=1;i<=np && parray[i]!=0;i++) 40 | printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d",i,p[i],parray[i],b[parray[i]],fragment[i]; 41 | } 42 | -------------------------------------------------------------------------------- /Programs/12.cc: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | const int n = 3, r = 1; 5 | 6 | bool Safe(int processes[], int avail[], int maxm[][r], int allot[][r]){ 7 | int need[n][r]; 8 | for(int i=0; i work[j]) 27 | break; 28 | 29 | if(j == r){ 30 | for(int k=0 ; k 2 | using namespace std; 3 | 4 | int main(){ 5 | float process[500],aTime[500],bTime[500],abTime[500],wTime[500],tat_time[500]; 6 | int n = 0,i = 0 ; 7 | float aw_time = 0, atat_time = 0; 8 | printf("\nEnter the number of process : "); 9 | scanf("%d",&n); 10 | 11 | printf("Enter the Arrival time and Burst time.\n\n"); 12 | printf("\tA_Time B_Time\n"); 13 | for(i = 0 ; i < n ; i++){ 14 | process[i]=i+1; 15 | printf("P%d :\t", i+1); 16 | scanf("%f\t%f",&aTime[i],&bTime[i]); 17 | } 18 | printf("\n\nProcess\tA_Time\tB_Time\n"); 19 | for(i = 0 ; i < n ; i++){ 20 | printf("P[%d]\t%.2f\t%.2f\n",i,aTime[i],bTime[i]); 21 | } 22 | wTime[0] = 0; 23 | tat_time[0] = bTime[0]; 24 | abTime[0] = bTime[0]+aTime[0]; 25 | for( i = 1 ; i < n ; i++){ 26 | abTime[i] = abTime[i-1] + bTime[i]; 27 | tat_time[i] = abTime[i] - aTime[i]; 28 | wTime[i] = tat_time[i] - bTime[i]; 29 | } 30 | for(i = 0 ; i < n ; i++){ 31 | aw_time = aw_time + wTime[i]; 32 | atat_time = atat_time + tat_time[i]; 33 | } 34 | printf("\tA_time\tB_time\tC_time\tTat_time W_time\n"); 35 | for(i = 0 ; i < n ; i++){ 36 | printf("P[%d]\t%.2f\t%0.2f\t%0.2f\t%0.2f\t%0.2f\n",i,aTime[i],bTime[i],abTime[i],tat_time[i],wTime[i]); 37 | } 38 | printf("\nAverage waiting time : %0.2f", aw_time/n); 39 | return 0; 40 | } 41 | -------------------------------------------------------------------------------- /Programs/11.cc: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | int count,j,n,time,remain,flag=0,time_quantum; 6 | int wait_time=0,turnaround_time=0,at[10],bt[10],rt[10]; 7 | printf("Enter Total Process:\t "); 8 | scanf("%d",&n); 9 | remain=n; 10 | for(count=0;count0) { 21 | time+=rt[count]; 22 | rt[count]=0; 23 | flag=1; 24 | } 25 | else if(rt[count]>0){ 26 | rt[count]-=time_quantum; 27 | time+=time_quantum; 28 | } 29 | if(rt[count]==0 && flag==1) { 30 | remain--; 31 | printf("P[%d]\t|\t%d\t|\t%d\n",count+1,time-at[count],time-at[count]-bt[count]); 32 | wait_time+=time-at[count]-bt[count]; 33 | turnaround_time+=time-at[count]; 34 | flag=0; 35 | } 36 | if(count==n-1) 37 | count=0; 38 | else if(at[count+1]<=time) 39 | count++; 40 | else 41 | count=0; 42 | } 43 | printf("\nAverage Waiting Time= %f\n",wait_time*1.0/n); 44 | printf("Avg Turnaround Time = %f",turnaround_time*1.0/n); 45 | return 0; 46 | } 47 | -------------------------------------------------------------------------------- /Programs/18.cc: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | bool search(int key, vector frame){ // Page exists in frame? 5 | for(int i=0; i page, vector frame, int n, int index){ 13 | int res = -1, farthest = index; 14 | for(int i=0; ifarthest){ 19 | farthest=j; 20 | res=i; 21 | } 22 | break; 23 | } 24 | } 25 | 26 | if(j==n) 27 | return i; 28 | } 29 | 30 | if(res==-1) 31 | return 0; 32 | return res; 33 | } 34 | 35 | void optimal(vector page, int n, int m){ 36 | vector frame; 37 | 38 | int hits = 0; 39 | for(int i=0; i> n >> m; 64 | vector page(n); 65 | for(int i=0; i> page[i]; 67 | } 68 | optimal(page, n, m); 69 | return 0; 70 | } 71 | -------------------------------------------------------------------------------- /Programs/14.cc: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main(){ 5 | int memory_size[10][2], process_size[10][3]; 6 | int i, j, total_processes = 0, total_memory = 0; 7 | printf("\nEnter the Total Number of Processes:\t"); 8 | scanf("%d", &total_processes); 9 | printf("\nEnter the Size of Each Process\n"); 10 | for(int i = 0; i < total_processes; i++){ 11 | printf("Enter Size of Process %d:\t", i + 1); 12 | scanf("%d", &process_size[i][0]); 13 | process_size[i][1] = 0; 14 | process_size[i][2] = i; 15 | } 16 | printf("\nEnter Total Memory Blocks:\t"); 17 | scanf("%d", &total_memory); 18 | printf("\nEnter the Size of Each Block:\n"); 19 | for(i = 0; i < total_processes; i++){ 20 | printf("Enter Size of Block %d:\t", i + 1); 21 | scanf("%d", &memory_size[i][0]); 22 | memory_size[i][1] = 0; 23 | } 24 | for(i = 0; i < total_processes; i++){ 25 | while(j < total_memory){ 26 | if(memory_size[j][1] == 0 && process_size[i][0] <=memory_size[j][0]){ 27 | process_size[i][1] = 1; 28 | memory_size[j][1] = 1; 29 | printf("\nProcess [%d] Allocated to Memory Block:\t%d", i + 1, j + 1); 30 | break; 31 | } 32 | j++; 33 | } 34 | } 35 | for(i = 0; i < total_memory; i++){ 36 | if(process_size[i][1] == 0){ 37 | printf("\nProcess [%d] Unallocated\n", i + 1); 38 | } 39 | } 40 | printf("\n"); 41 | return 0; 42 | } 43 | -------------------------------------------------------------------------------- /Programs/16.cc: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main(){ 5 | int fragments[10], blocks[10], files[10]; 6 | int m, n, number_of_blocks, number_of_files, temp, top = 0; 7 | static int block_arr[10], file_arr[10]; 8 | printf("\nEnter the Total Number of Blocks:\t"); 9 | scanf("%d",&number_of_blocks); 10 | printf("Enter the Total Number of Files:\t"); 11 | scanf("%d",&number_of_files); 12 | printf("\nEnter the Size of the Blocks:\n"); 13 | for(m = 0; m < number_of_blocks; m++) { 14 | printf("Block No.[%d]:\t", m + 1); 15 | scanf("%d", &blocks[m]); 16 | } 17 | printf("Enter the Size of the Files:\n"); 18 | for(m = 0; m < number_of_files; m++){ 19 | printf("File No.[%d]:\t", m + 1); 20 | scanf("%d", &files[m]); 21 | } 22 | for(m = 0; m < number_of_files; m++){ 23 | for(n = 0; n < number_of_blocks; n++){ 24 | if(block_arr[n] != 1){ 25 | temp = blocks[n] - files[m]; 26 | if(temp >= 0){ 27 | if(top < temp){ 28 | file_arr[m] = n; 29 | top = temp; 30 | } 31 | } 32 | } 33 | fragments[m] = top; 34 | block_arr[file_arr[m]] = 1; 35 | top = 0; 36 | } 37 | } 38 | printf("\nFile Number\tFile Size\tBlock Number\tBlock Size\tFragment"); 39 | for(m = 0; m < number_of_files; m++){ 40 | printf("\n%d\t\t%d\t\t%d\t\t%d\t\t%d", m, files[m], file_arr[m], blocks[file_arr[m]], fragments[m]); 41 | } 42 | printf("\n"); 43 | return 0; 44 | } 45 | -------------------------------------------------------------------------------- /Programs/21.cc: -------------------------------------------------------------------------------- 1 | #include "pthread.h" 2 | #include "stdio.h" 3 | 4 | // Importing POSIX Operating System API library 5 | #include "unistd.h" 6 | 7 | #include "string.h" 8 | 9 | #define MEMBAR __sync_synchronize() 10 | #define THREAD_COUNT 8 11 | 12 | volatile int tickets[THREAD_COUNT]; 13 | volatile int choosing[THREAD_COUNT]; 14 | 15 | volatile int resource; 16 | 17 | void lock(int thread){ 18 | choosing[thread] = 1; 19 | 20 | MEMBAR; 21 | 22 | int max_ticket = 0; 23 | 24 | for (int i = 0; i < THREAD_COUNT; ++i) { 25 | int ticket = tickets[i]; 26 | max_ticket = ticket > max_ticket ? ticket : max_ticket; 27 | } 28 | tickets[thread] = max_ticket + 1; 29 | 30 | MEMBAR; 31 | choosing[thread] = 0; 32 | MEMBAR; 33 | 34 | for (int other = 0; other < THREAD_COUNT; ++other) { 35 | while (choosing[other]) { 36 | } 37 | MEMBAR; 38 | while (tickets[other] != 0 && (tickets[other] < tickets[thread] || (tickets[other] == tickets[thread] && other < thread))){ 39 | } 40 | } 41 | } 42 | 43 | void unlock(int thread) { 44 | MEMBAR; 45 | tickets[thread] = 0; 46 | } 47 | 48 | void use_resource(int thread) { 49 | if (resource != 0) { 50 | printf("Resource was acquired by %d, but is still in-use by %d!\n", 51 | thread, resource); 52 | } 53 | resource = thread; 54 | printf("%d using resource...\n", thread); 55 | 56 | MEMBAR; 57 | sleep(2); 58 | resource = 0; 59 | } 60 | 61 | void* thread_body(void* arg) { 62 | long thread = (long)arg; 63 | lock(thread); 64 | use_resource(thread); 65 | unlock(thread); 66 | return NULL; 67 | } 68 | 69 | int main(){ 70 | memset((void*)tickets, 0, sizeof(tickets)); 71 | memset((void*)choosing, 0, sizeof(choosing)); 72 | resource = 0; 73 | pthread_t threads[THREAD_COUNT]; 74 | 75 | for (int i = 0; i < THREAD_COUNT; ++i){ 76 | pthread_create(&threads[i], NULL, &thread_body, (void*)((long)i)); 77 | } 78 | for (int i = 0; i < THREAD_COUNT; ++i){ 79 | pthread_join(threads[i], NULL); 80 | } 81 | return 0; 82 | } 83 | -------------------------------------------------------------------------------- /Programs/19.cc: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int getLeastRecentlyUsed(vector temp, unordered_set s){ 5 | int n = temp.size(); 6 | for(int i=0; i removee(int val, vector temp){ 15 | int n = temp.size(); 16 | vector t; 17 | for(int i=0; i pages, int n, int f){ 32 | // set s is main memory, and temp is order of processes used 33 | unordered_set s; 34 | 35 | vector temp; 36 | 37 | int page_faults = 0; 38 | for(int i=0; i> n >> f; 63 | vector pages(n); 64 | for(int i=0; i> pages[i]; 66 | int page_faults = pageFaults(pages, n, f); 67 | cout << "Page faults: " << page_faults; 68 | cout << "\nPage fault percentage: " << float((float)page_faults/( 69 | return 0; 70 | } 71 | -------------------------------------------------------------------------------- /Programs/09.cc: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main(){ 5 | int burst_time[20], process[20], waiting_time[20], turnaround_time[20], priority[20]; 6 | int i, j, limit, sum = 0, position, temp; 7 | float average_wait_time, average_turnaround_time; 8 | printf("Enter Total Number of Processes:\t"); 9 | scanf("%d", &limit); 10 | printf("\nEnter Burst Time and Priority For %d Processes\n", limit); 11 | for(i = 0; i < limit; i++){ 12 | printf("\nProcess[%d]\n", i + 1); 13 | printf("Process Burst Time:\t"); 14 | scanf("%d", &burst_time[i]); 15 | printf("Process Priority:\t"); 16 | scanf("%d", &priority[i]); 17 | process[i] = i + 1; 18 | } 19 | for(i = 0; i < limit; i++){ 20 | position = i; 21 | for(j = i + 1; j < limit; j++){ 22 | if(priority[j] < priority[position]) 23 | position = j; 24 | } 25 | temp = priority[i]; 26 | priority[i] = priority[position]; 27 | priority[position] = temp; 28 | temp = burst_time[i]; 29 | burst_time[i] = burst_time[position]; 30 | burst_time[position] = temp; 31 | temp = process[i]; 32 | process[i] = process[position]; 33 | process[position] = temp; 34 | } 35 | waiting_time[0] = 0; 36 | for(i = 1; i < limit; i++){ 37 | waiting_time[i] = 0; 38 | for(j = 0; j < i; j++) 39 | waiting_time[i] = waiting_time[i] + burst_time[j]; 40 | 41 | sum = sum + waiting_time[i]; 42 | } 43 | average_wait_time = sum / limit; 44 | sum = 0; 45 | printf("\nProcess ID\t\tBurst Time\t Waiting Time\t Turnaround Time\n"); 46 | for(i = 0; i < limit; i++){ 47 | turnaround_time[i] = burst_time[i] + waiting_time[i]; 48 | sum = sum + turnaround_time[i]; 49 | printf("\nProcess[%d]\t\t%d\t\t %d\t\t %d\n", process[i], burst_time[i], waiting_time[i], turnaround_time[i]); 50 | } 51 | average_turnaround_time = sum / limit; 52 | printf("\nAverage Waiting Time:\t%f", average_wait_time); 53 | printf("\nAverage Turnaround Time:\t%f\n", average_turnaround_time); 54 | return 0; 55 | } 56 | -------------------------------------------------------------------------------- /Programs/01.cc: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | typedef pair iPair; 5 | 6 | struct Graph{ 7 | int V, E; 8 | vector< pair > edges; 9 | Graph(int V, int E){ 10 | this->V = V; 11 | this->E = E; 12 | } 13 | void addEdge(int u, int v, int w){ 14 | edges.push_back({w, {u, v}}); 15 | } 16 | int kruskalMST(); 17 | }; 18 | 19 | struct DisjointSets{ 20 | int *parent, *rnk; 21 | int n; 22 | DisjointSets(int n){ 23 | this->n = n; 24 | parent = new int[n+1]; 25 | rnk = new int[n+1]; 26 | 27 | for (int i = 0; i <= n; i++){ 28 | rnk[i] = 0; 29 | parent[i] = i; 30 | } 31 | } 32 | int find(int u){ 33 | if (u != parent[u]) 34 | parent[u] = find(parent[u]); 35 | return parent[u]; 36 | } 37 | void merge(int x, int y){ 38 | x = find(x), y = find(y); 39 | if (rnk[x] > rnk[y]) 40 | parent[y] = x; 41 | else // If rnk[x] <= rnk[y] 42 | parent[x] = y; 43 | if (rnk[x] == rnk[y]) 44 | rnk[y]++; 45 | } 46 | }; 47 | 48 | int Graph::kruskalMST(){ 49 | int mst_wt = 0; 50 | 51 | sort(edges.begin(), edges.end()); 52 | 53 | DisjointSets ds(V); 54 | 55 | vector< pair >::iterator it; 56 | for (it=edges.begin(); it!=edges.end(); it++){ 57 | int u = it->second.first; 58 | int v = it->second.second; 59 | int set_u = ds.find(u); 60 | int set_v = ds.find(v); 61 | if (set_u != set_v){ 62 | cout << u << " - " << v << endl; 63 | mst_wt += it->first; 64 | ds.merge(set_u, set_v); 65 | } 66 | } 67 | return mst_wt; 68 | } 69 | 70 | int main(){ 71 | int V = 9, E = 14; 72 | Graph g(V, E); 73 | g.addEdge(0, 4, 8); 74 | g.addEdge(0, 7, 8); 75 | g.addEdge(1, 2, 8); 76 | g.addEdge(1, 7, 11); 77 | g.addEdge(2, 3, 7); 78 | g.addEdge(2, 8, 2); 79 | g.addEdge(2, 5, 4); 80 | g.addEdge(3, 4, 9); 81 | g.addEdge(3, 5, 14); 82 | g.addEdge(4, 5, 10); 83 | g.addEdge(5, 6, 2); 84 | g.addEdge(6, 7, 1); 85 | g.addEdge(6, 8, 6); 86 | g.addEdge(7, 8, 7); 87 | cout << "Edges of MST are \n"; 88 | int mst_wt = g.kruskalMST(); 89 | cout << "\nWeight of MST is " << mst_wt; 90 | return 0; 91 | } 92 | -------------------------------------------------------------------------------- /Programs/23,24,25.cc: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void fcfs(int noq, int qu[10], int st){ 5 | int i,s=0; 6 | for(i=0;i abs(st - qu[i])){ 20 | min = abs(st-qu[i]); 21 | p = i; 22 | } 23 | } 24 | if(min == 999) 25 | break; 26 | visit[p]=1; 27 | s=s + min; 28 | st = qu[p]; 29 | } 30 | printf("\n Total seek time is: %d",s); 31 | } 32 | 33 | void scan(int noq, int qu[10], int st, int ch){ 34 | int i,j,s=0; 35 | for(i=0;i= 0;j--){ 38 | s=s+abs(st - qu[j]); 39 | st = qu[j]; 40 | } 41 | if(ch == 3){ 42 | s = s + abs(st - 0); 43 | st = 0; 44 | } 45 | for(j = 1;j < noq;j++){ 46 | s= s + abs(st - qu[j]); 47 | st = qu[j]; 48 | } 49 | break; 50 | } 51 | } 52 | printf("\n Total seek time : %d",s); 53 | } 54 | 55 | int main(){ 56 | int n,qu[20],st,i,j,t,noq,ch,visit[20]; 57 | printf("\n Enter the maximum number of cylinders : "); 58 | scanf("%d",&n); 59 | printf("enter number of queue elements"); 60 | scanf("%d",&noq); 61 | printf("\n Enter the work queue"); 62 | for(i=0;i 2){ 77 | for(i=0;iqu[j]){ 80 | t=qu[i]; 81 | qu[i] = qu[j]; 82 | qu[j] = t; 83 | } 84 | } 85 | switch(ch){ 86 | case 1: printf("\n FCFS \n"); 87 | printf("\n*****\n"); 88 | fcfs(noq,qu,st); 89 | break; 90 | 91 | case 2: printf("\n SSTF \n"); 92 | printf("\n*****\n"); 93 | sstf(noq,qu,st,visit); 94 | break; 95 | case 3: printf("\n SCAN \n"); 96 | printf("\n*****\n"); 97 | scan(noq,qu,st,ch); 98 | break; 99 | case 4: exit(0); 100 | } 101 | } 102 | } 103 | -------------------------------------------------------------------------------- /Programs/10.cc: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | struct process{ 5 | char process_name; 6 | int arrival_time, burst_time, ct, waiting_time, turnaround_time, priority; 7 | int status; 8 | }process_queue[10]; 9 | 10 | int limit; 11 | 12 | void Arrival_Time_Sorting(){ 13 | struct process temp; 14 | int i, j; 15 | for(i = 0; i < limit - 1; i++) 16 | for(j = i + 1; j < limit; j++) 17 | if(process_queue[i].arrival_time > process_queue[j].arrival_time){ 18 | temp = process_queue[i]; 19 | process_queue[i] = process_queue[j]; 20 | process_queue[j] = temp; 21 | } 22 | } 23 | 24 | int main(){ 25 | int i, time = 0, burst_time = 0, largest; 26 | char c; 27 | float wait_time = 0, turnaround_time = 0, average_waiting_time, average_turnaround_time; 28 | printf("\nEnter Total Number of Processes:\t"); 29 | scanf("%d", &limit); 30 | for(i = 0, c = 'A'; i < limit; i++, c++){ 31 | process_queue[i].process_name = c; 32 | printf("\nEnter Details For Process[%C]:\n", process_queue[i].process_name); 33 | printf("Enter Arrival Time:\t"); 34 | scanf("%d", &process_queue[i].arrival_time ); 35 | printf("Enter Burst Time:\t"); 36 | scanf("%d", &process_queue[i].burst_time); 37 | printf("Enter Priority:\t"); 38 | scanf("%d", &process_queue[i].priority); 39 | process_queue[i].status = 0; 40 | burst_time = burst_time + process_queue[i].burst_time; 41 | } 42 | Arrival_Time_Sorting(); 43 | process_queue[9].priority = -9999; 44 | printf("\nProcess Name\tArrival Time\tBurst Time\tPriority\tWaiting Time"); 45 | for(time = process_queue[0].arrival_time; time < burst_time;){ 46 | largest = 9; 47 | for(i = 0; i < limit; i++) 48 | if(process_queue[i].arrival_time <= time && process_queue[i].status != 1 && process_queue[i].priority > process_queue[largest].priority) 49 | { 50 | largest = i; 51 | } 52 | 53 | time = time + process_queue[largest].burst_time; 54 | process_queue[largest].ct = time; 55 | process_queue[largest].waiting_time = process_queue[largest].ct - process_queue[largest].arrival_time - process_queue[largest].burst_time; 56 | process_queue[largest].turnaround_time = process_queue[largest].ct - process_queue[largest].arrival_time; 57 | process_queue[largest].status = 1; 58 | wait_time = wait_time + process_queue[largest].waiting_time; 59 | turnaround_time = turnaround_time + process_queue[largest].turnaround_time; 60 | printf("\n%c\t\t%d\t\t%d\t\t%d\t\t%d", process_queue[largest].process_name, process_queue[largest].arrival_time, process_queue[largest].burst_time, process_queue[largest].priority, process_queue[largest].waiting_time); 61 | } 62 | average_waiting_time = wait_time / limit; 63 | average_turnaround_time = turnaround_time / limit; 64 | printf("\n\nAverage waiting time:\t%f\n", average_waiting_time); 65 | printf("Average Turnaround Time:\t%f\n", average_turnaround_time); 66 | } 67 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Creative Commons Legal Code 2 | 3 | CC0 1.0 Universal 4 | 5 | CREATIVE COMMONS CORPORATION IS NOT A LAW FIRM AND DOES NOT PROVIDE 6 | LEGAL SERVICES. DISTRIBUTION OF THIS DOCUMENT DOES NOT CREATE AN 7 | ATTORNEY-CLIENT RELATIONSHIP. CREATIVE COMMONS PROVIDES THIS 8 | INFORMATION ON AN "AS-IS" BASIS. CREATIVE COMMONS MAKES NO WARRANTIES 9 | REGARDING THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS 10 | PROVIDED HEREUNDER, AND DISCLAIMS LIABILITY FOR DAMAGES RESULTING FROM 11 | THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS PROVIDED 12 | HEREUNDER. 13 | 14 | Statement of Purpose 15 | 16 | The laws of most jurisdictions throughout the world automatically confer 17 | exclusive Copyright and Related Rights (defined below) upon the creator 18 | and subsequent owner(s) (each and all, an "owner") of an original work of 19 | authorship and/or a database (each, a "Work"). 20 | 21 | Certain owners wish to permanently relinquish those rights to a Work for 22 | the purpose of contributing to a commons of creative, cultural and 23 | scientific works ("Commons") that the public can reliably and without fear 24 | of later claims of infringement build upon, modify, incorporate in other 25 | works, reuse and redistribute as freely as possible in any form whatsoever 26 | and for any purposes, including without limitation commercial purposes. 27 | These owners may contribute to the Commons to promote the ideal of a free 28 | culture and the further production of creative, cultural and scientific 29 | works, or to gain reputation or greater distribution for their Work in 30 | part through the use and efforts of others. 31 | 32 | For these and/or other purposes and motivations, and without any 33 | expectation of additional consideration or compensation, the person 34 | associating CC0 with a Work (the "Affirmer"), to the extent that he or she 35 | is an owner of Copyright and Related Rights in the Work, voluntarily 36 | elects to apply CC0 to the Work and publicly distribute the Work under its 37 | terms, with knowledge of his or her Copyright and Related Rights in the 38 | Work and the meaning and intended legal effect of CC0 on those rights. 39 | 40 | 1. Copyright and Related Rights. A Work made available under CC0 may be 41 | protected by copyright and related or neighboring rights ("Copyright and 42 | Related Rights"). Copyright and Related Rights include, but are not 43 | limited to, the following: 44 | 45 | i. the right to reproduce, adapt, distribute, perform, display, 46 | communicate, and translate a Work; 47 | ii. moral rights retained by the original author(s) and/or performer(s); 48 | iii. publicity and privacy rights pertaining to a person's image or 49 | likeness depicted in a Work; 50 | iv. rights protecting against unfair competition in regards to a Work, 51 | subject to the limitations in paragraph 4(a), below; 52 | v. rights protecting the extraction, dissemination, use and reuse of data 53 | in a Work; 54 | vi. database rights (such as those arising under Directive 96/9/EC of the 55 | European Parliament and of the Council of 11 March 1996 on the legal 56 | protection of databases, and under any national implementation 57 | thereof, including any amended or successor version of such 58 | directive); and 59 | vii. other similar, equivalent or corresponding rights throughout the 60 | world based on applicable law or treaty, and any national 61 | implementations thereof. 62 | 63 | 2. Waiver. To the greatest extent permitted by, but not in contravention 64 | of, applicable law, Affirmer hereby overtly, fully, permanently, 65 | irrevocably and unconditionally waives, abandons, and surrenders all of 66 | Affirmer's Copyright and Related Rights and associated claims and causes 67 | of action, whether now known or unknown (including existing as well as 68 | future claims and causes of action), in the Work (i) in all territories 69 | worldwide, (ii) for the maximum duration provided by applicable law or 70 | treaty (including future time extensions), (iii) in any current or future 71 | medium and for any number of copies, and (iv) for any purpose whatsoever, 72 | including without limitation commercial, advertising or promotional 73 | purposes (the "Waiver"). Affirmer makes the Waiver for the benefit of each 74 | member of the public at large and to the detriment of Affirmer's heirs and 75 | successors, fully intending that such Waiver shall not be subject to 76 | revocation, rescission, cancellation, termination, or any other legal or 77 | equitable action to disrupt the quiet enjoyment of the Work by the public 78 | as contemplated by Affirmer's express Statement of Purpose. 79 | 80 | 3. Public License Fallback. Should any part of the Waiver for any reason 81 | be judged legally invalid or ineffective under applicable law, then the 82 | Waiver shall be preserved to the maximum extent permitted taking into 83 | account Affirmer's express Statement of Purpose. In addition, to the 84 | extent the Waiver is so judged Affirmer hereby grants to each affected 85 | person a royalty-free, non transferable, non sublicensable, non exclusive, 86 | irrevocable and unconditional license to exercise Affirmer's Copyright and 87 | Related Rights in the Work (i) in all territories worldwide, (ii) for the 88 | maximum duration provided by applicable law or treaty (including future 89 | time extensions), (iii) in any current or future medium and for any number 90 | of copies, and (iv) for any purpose whatsoever, including without 91 | limitation commercial, advertising or promotional purposes (the 92 | "License"). The License shall be deemed effective as of the date CC0 was 93 | applied by Affirmer to the Work. Should any part of the License for any 94 | reason be judged legally invalid or ineffective under applicable law, such 95 | partial invalidity or ineffectiveness shall not invalidate the remainder 96 | of the License, and in such case Affirmer hereby affirms that he or she 97 | will not (i) exercise any of his or her remaining Copyright and Related 98 | Rights in the Work or (ii) assert any associated claims and causes of 99 | action with respect to the Work, in either case contrary to Affirmer's 100 | express Statement of Purpose. 101 | 102 | 4. Limitations and Disclaimers. 103 | 104 | a. No trademark or patent rights held by Affirmer are waived, abandoned, 105 | surrendered, licensed or otherwise affected by this document. 106 | b. Affirmer offers the Work as-is and makes no representations or 107 | warranties of any kind concerning the Work, express, implied, 108 | statutory or otherwise, including without limitation warranties of 109 | title, merchantability, fitness for a particular purpose, non 110 | infringement, or the absence of latent or other defects, accuracy, or 111 | the present or absence of errors, whether or not discoverable, all to 112 | the greatest extent permissible under applicable law. 113 | c. Affirmer disclaims responsibility for clearing rights of other persons 114 | that may apply to the Work or any use thereof, including without 115 | limitation any person's Copyright and Related Rights in the Work. 116 | Further, Affirmer disclaims responsibility for obtaining any necessary 117 | consents, permissions or other rights required for any use of the 118 | Work. 119 | d. Affirmer understands and acknowledges that Creative Commons is not a 120 | party to this document and has no duty or obligation with respect to 121 | this CC0 or use of the Work. 122 | --------------------------------------------------------------------------------