Untitled

 avatar
unknown
plain_text
a year ago
17 kB
15
Indexable
#include<stdio.h>
#include<stdlib.h>
int main()
{
 printf("### Kernel Information ###\n\n");
 system("cat /proc/version | awk '{printf \"Kernel Version: %s\\n\", $0}'");
 printf("\n### CPU Information ###\n\n");
 system("cat /proc/cpuinfo | awk '/processor|model/{print}'");
 return 0;
}

#include<stdio.h>
#include<stdlib.h>
int main()
{
printf("\n### Memory status ###\n");
system("cat /proc/meminfo | awk '/Mem/ {print}'");
return 0;
}
-----------------------------------------------------------------------------------------------
fcfs schd
#include <stdio.h>
int main() {
int num_processes, i;
float avg_waiting_time = 0, avg_turnaround_time = 0;
printf("Enter the number of processes: ");
scanf("%d", &num_processes);
int process_id[num_processes], arrival_time[num_processes], burst_time[num_processes],
waiting_time[num_processes], turnaround_time[num_processes];
// Input arrival times and burst times
printf("Enter arrival times and burst times:\n");
for (i = 0; i < num_processes; i++) {
printf("Process %d: ", i+1);
scanf("%d %d", &arrival_time[i], &burst_time[i]);
process_id[i] = i+1;
}
// Calculate waiting and turnaround times
int completion_time = 0;
for (i = 0; i < num_processes; i++) {
if (completion_time < arrival_time[i]) {
completion_time = arrival_time[i];
}
waiting_time[i] = completion_time - arrival_time[i];
turnaround_time[i] = waiting_time[i] + burst_time[i];
completion_time += burst_time[i];
avg_waiting_time += waiting_time[i];
avg_turnaround_time += turnaround_time[i];
}
// Calculate average waiting and turnaround times
avg_waiting_time /= num_processes;
avg_turnaround_time /= num_processes;
// Output results
printf("Process ID\tArrival Time\tBurst Time\tWaiting Time\tTurnaround Time\n");
for (i = 0; i < num_processes; i++) {
printf("%d\t\t%d\t\t%d\t\t%d\t\t%d\n", process_id[i], arrival_time[i], burst_time[i],
waiting_time[i], turnaround_time[i]);
}
printf("Average Waiting Time: %.2f\n", avg_waiting_time);
printf("Average Turnaround Time: %.2f\n", avg_turnaround_time);
return 0;
}

-----------------------------------------------------------------------------------

round robin
#include<stdio.h>
#include<conio.h>

void main()
{
 // initlialize the variable name
 int i, NOP, sum=0,count=0, y, quant, wt=0, tat=0, at[10], bt[10], temp[10];
 float avg_wt, avg_tat;
 printf(" Total number of process in the system: ");
 scanf("%d", &NOP);
 y = NOP; // Assign the number of process to variable y

// Use for loop to enter the details of the process like Arrival time and the Burst Time
for(i=0; i<NOP; i++)
{
printf("\n Enter the Arrival and Burst time of the Process[%d]\n", i+1);
printf(" Arrival time is: \t"); // Accept arrival time
scanf("%d", &at[i]);
printf(" \nBurst time is: \t"); // Accept the Burst time
scanf("%d", &bt[i]);
temp[i] = bt[i]; // store the burst time in temp array
}
// Accept the Time qunat
printf("Enter the Time Quantum for the process: \t");
scanf("%d", &quant);
// Display the process No, burst time, Turn Around Time and the waiting time
printf("\n Process No \t\t Burst Time \t\t TAT \t\t Waiting Time ");
for(sum=0, i = 0; y!=0; )
{
if(temp[i] <= quant && temp[i] > 0) // define the conditions
{
 sum = sum + temp[i];
 temp[i] = 0;
 count=1;
 }
 else if(temp[i] > 0)
 {
 temp[i] = temp[i] - quant;
 sum = sum + quant;
 }
 if(temp[i]==0 && count==1)
 {
 y--; //decrement the process no.
 printf("\nProcess No[%d] \t\t %d\t\t\t\t %d\t\t\t %d", i+1, bt[i], sum-at[i], sum-at[i]-bt[i]);
 wt = wt+sum-at[i]-bt[i];
 tat = tat+sum-at[i];
 count =0;
 }
 if(i==NOP-1)
 {
 i=0;
 }
 else if(at[i+1]<=sum)
 {
 i++;
 }
 else
 {
 i=0;
 }
}
// represents the average waiting time and Turn Around time
avg_wt = wt * 1.0/NOP;
avg_tat = tat * 1.0/NOP;
printf("\n Average Turn Around Time: \t%f", avg_wt);
printf("\n Average Waiting Time: \t%f", avg_tat);
getch();
}
-----------------------------------------------------------------------------------

priority 
#include <stdio.h>
#define MAX_PROCESSES 10
int main() {
 int burst_time[MAX_PROCESSES], waiting_time[MAX_PROCESSES],
turnaround_time[MAX_PROCESSES], priority[MAX_PROCESSES];
 int num_processes, i, j, min_index, temp;
 float avg_waiting_time = 0, avg_turnaround_time = 0;
 printf("Enter the number of processes: ");
 scanf("%d", &num_processes);
 // Input burst times and priorities
 printf("Enter burst times and priorities:\n");
 for (i = 0; i < num_processes; i++) {
 printf("Process %d: ", i+1);
 scanf("%d%d", &burst_time[i], &priority[i]);
 }
 // Sort processes by priority (lowest first) using selection sort
 for (i = 0; i < num_processes - 1; i++) {
 min_index = i;
 for (j = i + 1; j < num_processes; j++) {
 if (priority[j] < priority[min_index]) {
 min_index = j;
 }
 }
 // Swap burst time, priority, and process order
 temp = burst_time[i];
 burst_time[i] = burst_time[min_index];
 burst_time[min_index] = temp;
 temp = priority[i];
 priority[i] = priority[min_index];
 priority[min_index] = temp;
 }
 // Calculate waiting and turnaround times
 waiting_time[0] = 0; // first process has zero waiting time
 turnaround_time[0] = burst_time[0]; // first process has turnaround time equal to burst time
 for (i = 1; i < num_processes; i++) {
 waiting_time[i] = waiting_time[i-1] + burst_time[i-1];
 turnaround_time[i] = waiting_time[i] + burst_time[i];
 }
 // Calculate average waiting and turnaround times
 for (i = 0; i < num_processes; i++) {
 avg_waiting_time += waiting_time[i];
 avg_turnaround_time += turnaround_time[i];
 }
 avg_waiting_time /= num_processes;
 avg_turnaround_time /= num_processes;
 // Output results
 printf("Burst Time\tPriority\tWaiting Time\tTurnaround Time\n");
 for (i = 0; i < num_processes; i++) {
 printf("%d\t\t%d\t\t%d\t\t%d\n", burst_time[i], priority[i], waiting_time[i],
turnaround_time[i]);
 }
 printf("Average Waiting Time: %.2f\n", avg_waiting_time);
 printf("Average Turnaround Time: %.2f\n", avg_turnaround_time);
 return 0;
}
-----------------------------------------------------------------------------------

first fit
#include<stdio.h>
#include<stdlib.h>
void main()
{
int bsize[10], psize[10], bno, pno, flags[10], allocation[10], i, j;
for(i = 0; i < 10; i++)
{
flags[i] = 0;
allocation[i] = -1;
}
printf("Enter no. of blocks: "); scanf("%d", &bno);
printf("\nEnter size of each block: "); for(i = 0; i < bno; i++)
scanf("%d", &bsize[i]);
printf("\nEnter no. of processes: "); scanf("%d", &pno);
printf("\nEnter size of each process: "); for(i = 0; i < pno; i++)
scanf("%d", &psize[i]);
for(i = 0; i < pno; i++)//allocation as per first fit for(j = 0; j < bno; j++)
if(flags[j] == 0 && bsize[j] >= psize[i])
{
allocation[j] = i; flags[j] = 1; break;
}
//display allocation details
printf("\nBlock no.\tsize\t\tprocess no.\t\tsize"); for(i = 0; i < bno; i++)
{
printf("\n%d\t\t%d\t\t", i+1, bsize[i]); if(flags[i] == 1)
printf("%d\t\t\t%d",allocation[i]+1,psize[allocation[i]]); else
printf("Not allocated");
}
}
-----------------------------------------------------------------------------------

best fit
#include<stdio.h>
#include<stdlib.h>
void main()
{
int fragment[20],b[20],p[20],i,j,nb,np,temp,lowest=9999; static int barray[20],parray[20];
printf("\n\t\t\tMemory Management Scheme - Best Fit");
printf("\nEnter the number of blocks:"); scanf("%d",&nb);
printf("Enter the number of processes:"); scanf("%d",&np);
printf("\nEnter the size of the blocks:-\n"); for(i=1;i<=nb;i++)
{
printf("Block no.%d:",i);
scanf("%d",&b[i]);
}
printf("\nEnter the size of the processes :-\n"); for(i=1;i<=np;i++)
{
printf("Process no.%d:",i);
scanf("%d",&p[i]);
}
for(i=1;i<=np;i++)
{
for(j=1;j<=nb;j++)
{
if(barray[j]!=1)
{
temp=b[j]-p[i];
if(temp>=0)
if(lowest>temp)
{
parray[i]=j;
lowest=temp;
}
}
}
fragment[i]=lowest; barray[parray[i]]=1; lowest=10000;
}
printf("\nProcess_no\tProcess_size\tBlock_no\tBlock_size\tFragment"); for(i=1;i<=np &&
parray[i]!=0;i++) 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]);
}

-----------------------------------------------------------------------------------

worst fit
#include<stdio.h>
int main()
{
int n,n1,i;
printf("enter the number of processes:");
scanf("%d",&n);
int process[n];
printf("\n enter the size of processes:\n");
for(i=0;i<n;i++)
{
scanf("%d",&process[i]);
}
printf("enter the no of memoryblocks:");
scanf("%d",&n1);
int blocks[n1];
printf("\n enter the size of blocks:\n");
int total=0;
for(i=0;i<n1;i++)
{
scanf("%d",&blocks[i]);
total=total+blocks[i];
}
int process1[n1];
int job[n1];
int frag[n1];
int check[n1];
for(i=0;i<n1;i++)
{
check[i]=0;
}
int j,used=0; i=0;
while(i<n)
{
int max=-1,j1=-1,k=-1,max1; for(j=0;j<n1;j++)
{
max1=blocks[j]; if(max1>=max&&check[j]==0&&max1>=process[i])
{
max=max1; j1=j;
}
else
{
if(check[j]==0)
{
process1[j]=0;
job[j]=0;
frag[j]=blocks[j];
}
}
}
if(k!=j1)
{
process1[j1]=process[i];
job[j1]=i+1;
frag[j1]=blocks[j1]-process[i];
used=used+process[i];
check[j1]=1;
int l;
}
i++;
}
printf("blocksize\tprocess size\tprocessno\tfragmentation\n");
for(i=0;i<n1;i++)
{
printf("%d\t\t%d\t\t%d\t\t%d\n",blocks[i],process1[i],job[i],frag[i]);
}
printf("totalmemoryallocation:%d\n",total);
printf("memoryused:%d\n",used);
}

-----------------------------------------------------------------------------------

fcfs page replacement
#include<stdio.h>
#include<stdlib.h>
int fsize;
int frm[15];
void display();
void main()
{
int pg[100],nPage,i,j,pf=0,top=-1,temp,flag=0;
printf("\n Enter frame size:");
scanf("%d",&fsize);
printf("\n Enter number of pages:");
scanf("%d",&nPage);
for(i=0;i< nPage;i++)
{
printf("\n Enter page[%d]:",i+1);
scanf("%d",&pg[i]);
}
for(i=0;i< fsize;i++) frm[i]=-1;
printf("\n page | \t Frame content ");
printf("\n "); for(j=0;j< nPage;j++)
{
flag=0;
for(i=0;i< fsize;i++)
{
if(frm[i]==pg[j])
{
flag=1; break;
}
}
if(flag==0)
{
if(top==fsize-1)
{
top=-1;
}
pf++; top++;
frm[top]=pg[j];
}
printf("\n %d |",pg[j]);
display();
}
printf("\n ");
printf("\n total page fault:%d",pf);
}
void display()
{
int i;
for(i=0;i< fsize;i++) printf("\t %d",frm[i]);
}
-----------------------------------------------------------------------------------

optimal 
#include<stdio.h>
int main()
{
 int no_of_frames, no_of_pages, frames[10], pages[30], temp[10], flag1, flag2, flag3, i, j, k,
pos, max, faults = 0;
 printf("Enter number of frames: ");
 scanf("%d", &no_of_frames);
 printf("Enter number of pages: ");
 scanf("%d", &no_of_pages);
 printf("Enter page reference string: ");
 for(i = 0; i < no_of_pages; ++i){
 scanf("%d", &pages[i]);
 }
 for(i = 0; i < no_of_frames; ++i){
 frames[i] = -1;
 }
 for(i = 0; i < no_of_pages; ++i){
 flag1 = flag2 = 0;

 for(j = 0; j < no_of_frames; ++j){
 if(frames[j] == pages[i]){
 flag1 = flag2 = 1;
 break;
 }
 }
 if(flag1 == 0){
 for(j = 0; j < no_of_frames; ++j){
 if(frames[j] == -1){
 faults++;
 frames[j] = pages[i];
 flag2 = 1;
 break;
 }
 }
 }

 if(flag2 == 0){
 flag3 =0;
 for(j = 0; j < no_of_frames; ++j){
 temp[j] = -1;

 for(k = i + 1; k < no_of_pages; ++k){
 if(frames[j] == pages[k]){
 temp[j] = k;
 break;
 }
 }
 }
 for(j = 0; j < no_of_frames; ++j){
 if(temp[j] == -1){
 pos = j;
 flag3 = 1;
 break;
 }
 }
 if(flag3 ==0){
 max = temp[0];
 pos = 0;
 for(j = 1; j< no_of_frames; ++j){
 if(temp[j] > max){
 max = temp[j];
 pos = j;
 }
 }
 }
frames[pos] = pages[i];
faults++;
 }
 printf("\n");
 for(j = 0; j < no_of_frames; ++j){
 printf("%d\t", frames[j]);
 }
 }
 printf("\n\nTotal Page Faults = %d", faults)
 return 0;
}

-----------------------------------------------------------------------------------

LRU replacement 
#include <stdio.h>
int findLRU(int time[], int n)
{
 int i, minimum = time[0], pos = 0;
 for (i = 1; i < n; ++i)
 {
 if (time[i] < minimum)
 {
 minimum = time[i];
 pos = i;
 }
 }
 return pos;
}
int main()
{
 int no_of_frames, no_of_pages, frames[10], pages[30], counter = 0, time[10], flag1, flag2, i, j,
pos, faults = 0;
 printf("Enter number of frames: ");
 scanf("%d", &no_of_frames);
 printf("Enter number of pages: ");
 scanf("%d", &no_of_pages);
 printf("Enter reference string: ");
 for (i = 0; i < no_of_pages; ++i)
 {
 scanf("%d", &pages[i]);
 }
 for (i = 0; i < no_of_frames; ++i)
 {
 frames[i] = -1;
 }
 for (i = 0; i < no_of_pages; ++i)
 {
 flag1 = flag2 = 0;
 for (j = 0; j < no_of_frames; ++j)
 {
 if (frames[j] == pages[i])
 {
 counter++;
 time[j] = counter;
 flag1 = flag2 = 1;
 break;
 }
 }
 if (flag1 == 0)
 {
 for (j = 0; j < no_of_frames; ++j)
 {
 if (frames[j] == -1)
 {
 counter++;
 faults++;
 frames[j] = pages[i];
 time[j] = counter;
 flag2 = 1;
 break;
 }
 }
 }
 if (flag2 == 0)
 {
 pos = findLRU(time, no_of_frames);
 counter++;
 faults++;
 frames[pos] = pages[i];
 time[pos] = counter;
 }
 printf("\n");
 for (j = 0; j < no_of_frames; ++j)
 {
 printf("%d\t", frames[j]);
 }
 }
 printf("\nTotal Page Faults = %d", faults);
 return 0;
}
-----------------------------------------------------------------------------------

fcfs disk 

#include<math.h>
#include<stdio.h>
#include<stdlib.h>
int main()
{
int i,n,req[50],mov=0,cp; printf("enter the current position\n");
scanf("%d",&cp);
printf("enter the number of requests\n");
scanf("%d",&n);
printf("enter the request order\n");
for(i=0;i<n;i++)
{
scanf("%d",&req[i]);
}
mov=mov+abs(cp-req[0]); // abs is used to calculate the absolute value printf("%d ->
%d",cp,req[0]);
for(i=1;i<n;i++)
{
mov=mov+abs(req[i]-req[i-1]);
printf(" -> %d",req[i]);
}
printf("\n");
printf("total head movement = %d\n",mov);
}
-----------------------------------------------------------------------------------

sstf disk 

#include<math.h>
#include<stdio.h>
#include<stdlib.h>
int main()
{
int i,n,k,req[50],mov=0,cp,index[50],min,a[50],j=0,mini,cp1; printf("enter the current
position\n");
scanf("%d",&cp);
printf("enter the number of requests\n"); scanf("%d",&n);
cp1=cp;
printf("enter the request order\n"); for(i=0;i<n;i++)
{
scanf("%d",&req[i]);
}
for(k=0;k<n;k++)
{
for(i=0;i<n;i++)
{
index[i]=abs(cp-req[i]); // calculate distance of each request from current position
}
// to find the nearest request
min=index[0];
mini=0; for(i=1;i<n;i++)
{
if(min>index[i])
{
min=index[i]; mini=i;
}
}
a[j]=req[mini]; j++;
cp=req[mini]; // change the current position value to next request req[mini]=999;
} // the request that is processed its value is changed so that it is not processed again
printf("Sequence is : ");
printf("%d",cp1);
mov=mov+abs(cp1-a[0]); // head movement printf(" -> %d",a[0]);
for(i=1;i<n;i++)
{
mov=mov+abs(a[i]-a[i-1]); ///head movement printf(" -> %d",a[i]);
}
printf("\n");
printf("total head movement = %d\n",mov);
}

-----------------------------------------------------------------------------------


fork
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include<stdlib.h>
#include <sys/wait.h>
int main(int argc, char *argv[])
{
int a,b,c;
EX NO:
IMPLEMENTATION OF FORK()
DATE:
printf("Enter the two numbers\n"); scanf("%d%d", &a,&b);
pid_t p1 = fork();
pid_t p2= fork();
if (p1>0)
{
c=a+b;
printf("ADDITION=%d\n",c);
}
if(p1==0)
{
c=a-b;
printf("\nSUBTRACTION =%d\n",c);
}
if (p2>0)
{
c=a*b;
printf("\nMULTIPLICATION =%d\n",c);
}
if(p2==0)
{
c=a/b; printf("\nDIVISION=%d\n",c);
}
}
-----------------------------------------------------------------------------------

threads
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
int a=10,b=5;
void *add(void *vargp)
{
printf("ADDITION=%d\n",a+b);
return NULL;
}
void *sub(void *vargp)
{
printf("SUBTRACTION=%d\n",a-b);
return NULL;
}
void *mul(void *vargp)
{
printf("MULTIPLICATION=%d\n",a*b);
return NULL;
}
void *division(void *vargp)
{
printf("DIVISION=%d\n",a/b);
return NULL;
}
int main()
{
pthread_t thread_id ;
pthread_create(&thread_id , NULL, add, NULL); pthread_create(&thread_id, NULL, sub,
NULL); pthread_create(&thread_id, NULL, mul, NULL); pthread_create(&thread_id, NULL,
division, NULL);
pthread_join(thread_id, NULL); exit(0);
}
-----------------------------------------------------------------------------------
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include<string.h>
// program for interprocess communication using pipes
int main(void)
{
int fd[2], nbytes; pid_t childpid;
char string[50] ;
char readbuffer[80];
pipe(fd);
printf("enter the string\n");
scanf("%s", string);
childpid = fork();
if(childpid==-1)
{
perror("fork"); return(1);
}
if(childpid == 0)
{
/* Child process closes up input side of pipe */ close(fd[0]);
/* Send "string" through the output side of pipe */
write(fd[1], string, (strlen(string)+1));
return(0);
}
else
{
/* Parent process closes up output side of pipe */close(fd[1]);
/* Read in a string from the pipe */
nbytes = read(fd[0], readbuffer,sizeof(readbuffer));
printf("Received string: %s", readbuffer);
 }

 return 0;
 }


Editor is loading...
Leave a Comment