Professional Documents
Culture Documents
Os LM
Os LM
Os LM
(Implement the following on LINUX or other Unix like platform. Use C for high level language Implementation) 1. Write programs using the following system calls of UNIX operating system: fork, exec, getpid, exit, wait, close, stat, opendir, readdir 2. Write programs using the I/O System calls of UNIX operating system. (open, read, write, etc) 3. Write C programs to simulate UNIX commands like ls, grep, etc. 4. Given the list of processes, their CPU burst times and arrival times. Display/print the Gantt chart for FCFS and SJF. For each of the scheduling policies, compute and print the average waiting time and average turnaround time (2 sessions). 5. Given the list of processes, their CPU burst times and arrival times. Display/print the Gantt chart for Priority and Round robin. For each of the scheduling policies, compute and print the average waiting time and average turnaround time (2 sessions). 6. Develop Application using Inter Inter-Process-Communication (Using shared memory, pipes or message queues). Consumer 7. Implement the Producer-Consumer problem using semaphores(Using UNIX system calls) 8. Implement some Memory management schemes like Paging and Segmentation. 9. Implement some Memory management schemes like FIRST FIT, BEST FIT & WORST FIT. 10. Implement any file allocation techniques(Contiguous, Linked or Indexed)
Page 1
The following is a simple example of fork() #include <stdio.h> #include <sys/types.h> #include <unistd.h> int main(void) { printf("Hello \n"); fork(); printf("bye\n"); return 0; } Hello is printed once by parent process is bye - is printed twice, once by the parent and once by the child Implement in C the cat Unix command using system calls #include<fcntl.h> #include<sys/stat.h> #define BUFSIZE 1 int main(int argc, char **argv) { int fd1; int n; char buf; fd1=open(argv[1],O_RDONLY); printf("SuhritSolutions Printing Files Files\n"); while((n=read(fd1,&buf,1))>0) { printf("%c",buf); /* or write(1,&buf,1); */ } return (0); }
Page 2
Example for getpid() #include <stdio.h> #include <string.h> #include <sys/types.h> void main() { Pid_t pid; fork(); pid=getpid(); if(pid == -1) printf(\n Error in creating process ); n else if(pid == 0) nExecuting printf("\nExecuting in child process, pid=%d and its parent pid = %d ", getpid(),getppid()); else printf("\nExecuting in parent process,pid=%d \n",getppid()); nExecuting }
Example program for execvp(): #include <stdio.h> #include <unistd.h> #include <stdlib.h> int main(int argc, char *argv[]) { execvp(argv[1], &argv[1]); perror("exec failure"); exit(1); }
Page 3
An example of wait(): #include <sys/types.h> #include <sys/wait.h> Void main() { int status; pid_t pid; pid = fork(); if(pid == -1) nERROR printf(\nERROR child not created ); else if (pid == 0) /* child process */ { printf("\n I'm the child!); exit(0); } else /* parent process */ { wait(&status); printf("\n I'm the parent!") n", printf("\n Child returned: %d\n", status) } }
Page 4
Example program for stat() #include <iostream> #include <cstdio> #include <sys/types.h> #include <sys/stat.h> #include <unistd.h> using namespace std; const int N_BITS = 3; int main(int argc, char *argv[ ]) { unsigned int mask = 0700; struct stat buff; x", "-w-", "-wx", "r--", "r-x", "rw-", "rwx"}; static char *perm[] = {"---", "--x", " if (argc > 1) { if ((stat(argv[1], &buff) != -1)) { cout << "Permissions for " << argv[1] << " "; for (int i=3; i; --i) { cout << perm[(buff.st_mode & mask) >> (i (i-1)*N_BITS]; mask >>= N_BITS; } cout << endl; } else { perror(argv[1]); return 1; } } else { cerr << "Usage: " << argv[0] << " file_name [0] file_name\n"; return 2; } return 0;
CHENNAI INSTITUTE OF TECHNOLOGY Page 5
EXAMPLE FOR READ() #include<stdio.h> #include<fcntl.h> #include<stdlib.h> main(int argc,char *argv[]) { int fd,i; char ch[1]; if (argc<2) { printf("Usage: mycat filename\n"); n"); exit(0); } fd=open(argv[1],O_RDONLY); if(fd==-1) printf("%s is not exist",argv[1]); else { printf("Contents of the file %s is : \n",argv[1]); while(read(fd,ch,1)>0) printf("%c",ch[0]); close(fd); }
Example code for ls simulation #include<stdio.h> #include<dirent.h> int main() { struct dirent **namelist; int n,i; char pathname[100]; getcwd(pathname); n=scandir(pathname,&namelist,0,alphasort);
CHENNAI INSTITUTE OF TECHNOLOGY Page 6
Simulation of grep command: #include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <unistd.h> /* calculate filesize */ int calc_bufsize(char *filename) { struct stat st; stat(filename, &st); return ((int)st.st_size); } int main(int argc, char *argv[]) { if(argc < 3) { printf("Usage:\n\t%s <word> <files> ... t%s ...\n", argv[0]); return -1; } FILE *fp; char *filename; int x = 2; /* process each file */ for(x; x != argc; x++) { filename = argv[x]; if( (fp = fopen(filename, "r")) == NULL) { printf("Failed to open file: %s\n", argv[2]); n", return -2; } int BUFSIZE = calc_bufsize(filename); /* read ENTIRE file into buf[] */ char buf[BUFSIZE];
CHENNAI INSTITUTE OF TECHNOLOGY Page 7
fread(&buf, sizeof(char), BUFSIZE, fp); /* search buf for word (case sensitive) */ char *ans = strstr(buf, argv[1]); /* word found, print filename */ if(ans != NULL) printf("%s\n", filename); /* word not found, do nothing */ fclose(fp); } return 0; }
write a c program to implement SJF CPU SCHEDULING ALGORITHM /* SJF CPU SCHEDULING ALGORITHM*/ #include<stdio.h> #include<conio.h> void main() { int i,j,n,bt[10],st[10],et[10],wt[10],temp,tot; /* n=number of jobs bt=burst time st=starting time wt=waiting time*/ /*et=end time*/ float avg; clrscr(); printf("ENTER THE NO.OF JOBS"); scanf("%d",&n); for(i=1;i<=n;i++) { n printf("\n \n ENTER %d PROCESS BURST TIME",i); scanf("%d",&bt[i]); } /* SWAPPING BURST TIMES*/ for(i=1;i<=n;i++) { for(j=i+1;j<=n;j++) { if(bt[i]>bt[j]) { temp=bt[i]; t[i]; bt[i]=bt[j]; bt[j]=temp; } } /* IN CASE OF FIRST JOB*/ if(i==1)
CHENNAI INSTITUTE OF TECHNOLOGY Page 8
{ st[1]=0; et[1]=bt[1]; wt[1]=0; } /* FOR REMAINING CASES*/ else { st[i]=et[i-1]; et[i]=st[i]+bt[i]; wt[i]=st[i]; } } printf("\n\n BURST TIME \t STARTING TIME \t END TIME \t WAIT TIME\n"); t TIME printf("\n ******************************************************** ********************************************************\n"); for(i=1;i<=n;i++) { n printf("\n %5d %15d %15d %15d",bt[i],st[i],et[i],wt[i]); } printf("\n ********************************************************\n"); n ******************************************************** for(i=1,tot=0;i<=n;i++) tot+=wt[i]; avg=(float)tot/n; printf("\n\n\n AVERAGE WAITING TIME=%f",avg); n for(i=1,tot=0;i<=n;i++) tot+=et[i]; avg=(float)tot/n; n printf("\n\n AVERAGE TURNAROUND TIME=%f",avg); for(i=1,tot=0;i<=n;i++) tot+=st[i]; avg=(float)tot/n; printf("\n\n AVERAGE RESPONSE TIME=%f",avg); n getch(); }
Page 9
/*Program to implement Round Robin algorithm*/ #include<stdio.h> #include<conio.h> struct process { char na[20]; int at,bt,ft,tat,rem; float ntat; }Q[5],temp; int rr[20],q,x,k; main() { int f,r,n,i,j,tt=0,qt,t,flag,wt=0; float awt=0,antat=0,atat=0; clrscr(); printf("Enter the no. of jobs:"); scanf("%d",&n); for(r=0;r<n;r++) { printf("Enter process name, arrival time and burst time:"); scanf("%s%d%d",Q[r].na,&Q[r].at,&Q[r].bt); } printf("Enter quantum:"); scanf("%d",&qt); for(i=0;i<n;i++) { for(j=i+1;j<n;j++) {
CHENNAI INSTITUTE OF TECHNOLOGY Page 10
if(Q[i].at>Q[j].at) { temp=Q[i]; Q[i]=Q[j]; Q[j]=temp; } } } for(i=0;i<n;i++) { Q[i].rem=Q[i].bt; Q[i].ft=0; } tt=0; q=0; rr[q]=0; do { for(j=0;j<n;j++) if(tt>=Q[j].at) { x=0; for(k=0;k<=q;k++) if(rr[k]==j) x++; if(x==0) { q++; rr[q]=j; } } if(q==0) i=0; if(Q[i].rem==0) i++; if(i>q) i=(i-1)%q; if(i<=q) { if(Q[i].rem>0) { if(Q[i].rem<qt) { tt+=Q[i].rem; Q[i].rem=0;
CHENNAI INSTITUTE OF TECHNOLOGY Page 11
} else { tt+=qt; Q[i].rem Q[i].rem-=qt; } Q[i].ft=tt; } i++; } flag=0; for(j=0;j<n;j++) if(Q[j].rem>0) flag++; }while(flag!=0); clrscr(); tROUND printf("\n\n\t\tROUND ROBIN ALGORITHM"); printf("\n***************************"); n***************************"); printf("\nprocesses Arrival time burst time finish time tat wt ntat"); nprocesses for(f=0;f<n;f++) { Q[f].at; wt=Q[f].ft-Q[f].bt-Q[f].at; Q[f].tat=Q[f].ft-Q[f].at; Q[f].at; Q[f].ntat=(float)Q[f].tat/Q[f].bt; antat+=Q[f].ntat; atat+=Q[f].tat; awt+=wt; t%d\t%d\t%d\t%d %f", printf("\n\t%s\t%d\t\t%d Q[f].na,Q[f].at,Q[f].bt,Q[f].ft,Q[f].tat,wt,Q[f].ntat); } antat/=n; atat/=n; awt/=n; printf("\nAverage tat is %f",atat); nAverage printf("\nAverage normalised tat is %f",antat); nAverage printf("\n average waiting time is %f",awt); verage getch(); }
Page 12
/* program to implement page replacement using LRU algorithm*/ #include<stdio.h> int i,j=1,k,l,re[30],p[10],ch,no,nr,c,al=0,a,line=6; struct re { int st,l,ps; }opr; main() { clrscr(); printf("enter the length of the reference string:"); scanf("%d",&nr); printf("enter the reference string:"); for(i=1;i<=nr;i++) scanf("%d",&re[i]); printf("\n enter the number of frames:"); n scanf("%d",&no); clrscr(); for(i=1;i<=no;i++) p[i]=-1; opr.st=0; for(i=1;i<=nr;i++) { al=0;
CHENNAI INSTITUTE OF TECHNOLOGY Page 13
opr.st=100; for(c=1;c<=no;c++) if(re[i]==p[c]) al++; if(al==0) { if(j<=no) { p[j]=re[i]; j++; } else { for(k=1;k<=no;k++) { for(ch=i for(ch=i-1;ch>=1;ch--) { a=0; if(p[k]==re[ch]) { a++; break; } } if(a!=0) { if(opr.st>ch) { opr.st=ch; opr.l=re[ch]; opr.ps=k; } } else if(a==0) { opr.ps=k; break; } } p[(opr.ps)]=re[i]; } } display(no,p,i); } printf("\n");
CHENNAI INSTITUTE OF TECHNOLOGY Page 14
getch(); } display(int no,int p[],int i) { int k; if(i==1) { printf("\t\t\t"); for(k=1;k<=no;k++) printf("__"); } printf("\n%d",re[i]); gotoxy(25,line++); for(k=1;k<=no;k++) { printf("|"); printf("_"); if(p[k]!=-1) printf("%d",p[k]); else printf(" "); printf("_"); } printf("|"); }
Page 15
Program to illustrate round robin scheduling algorithm //Program to illustrate round robin scheduling algorithm #include<iostream.h> #include<conio.h> #include<stdlib.h> int main() { clrscr(); char *pro; int i,j=0,k=0,n,*burst,time,count=0,*wait,*rem,temp=0; float sum=0; nEnter cout<<"\n\nEnter no. of processes : "; cin>>n; pro=new char[n]; burst=new int[n]; wait=new int[n]; rem=new int[n]; cout<<"Enter process names : "; //reading process names for(i=0;i<n;i++) { cin>>pro[i]; }
CHENNAI INSTITUTE OF TECHNOLOGY Page 16
cout<<"\nEnter burst times : "; for(i=0;i<n;i++) //reading burst times { cin>>burst[i]; rem[i]=burst[i]; j=j+burst[i]; wait[i]=0; } cout<<"\nEnter the time quantum for round robin scheduling : "; nEnter cin>>time; for(i=1;i<=j;i++) { k++; while(rem[count]==0) { if(count==n-1) count=0; else count++; if(i==j) break; } //cout<<"\nat time : "<<i<<" running process : "<<pro[count]; nat for(temp=0;temp<n;temp++) //calculating waiting time { if((temp!=count)&&rem[temp]!=0) wait[temp]++; } rem[count]--; if(k==time||rem[count]==0) { cout<<" "<<pro[count]<<" "<<i; if(count==n-1) count=0; else count++; k=0; } //cout<<"\nrem time of "<<pro[count]<<" = "<<rem[count]; nrem } for(i=0;i<n;i++) { cout<<"\nWaiting time for process "<<pro[i]<<" = "<<wait[i]; nWaiting sum=sum+wait[i]; }
CHENNAI INSTITUTE OF TECHNOLOGY Page 17
#include<conio.h> Void main() { int n,i,j,sum=0; int arrival[10],service[10],start[10]; int finish[10],wait[10],turn[10]; float avgturn=0.0,avgwait=0.0; start[0]=0; clrscr(); printf("Enter the number of processes:"); scanf("%d",&n); for(i=0;i<n;i++) { printf("Enter the arrival and service time of %d process:",i+1); scanf("%d%d",&arrival[i],&service[i]); } for(i=0;i<n;i++)
CHENNAI INSTITUTE OF TECHNOLOGY Page 18
{ sum=0; for(j=0;j<i;j++) sum=sum+service[j]; start[i]=sum; } for(i=0;i<n;i++) { finish[i]=service[i]+start[i]; wait[i]=start[i]; turn[i]=service[i]+wait[i]; } for(i=0;i<n;i++) { avgwait+=wait[i]; avgturn+=turn[i]; } avgwait/=n; avgturn/=n; printf("\nArrival Service Start Finish Wait Turn nArrival Turn\n"); for(i=0;i<n;i++) t%d\t%d\n",arrival[i],service[i],start[i],finish[i],wait[i],turn[i]); n",arrival[i],service[i],start[i],finish[i],wait[i],turn[i]); printf("%d\t%d\t%d\t%d\t%d printf("\nAverage waiting time=%f",avgwait); nAverage printf("\nAverage turn around time=%f",avgturn); round getch(); } Simulate Paging technique of Memory Management AIM: A program to simulate Paging technique of memory management. PROGRAM: #include<stdio.h> #include<conio.h> main() { int np,ps,i; int *sa; clrscr(); printf("Enter how many pages\n"); n"); scanf("%d",&np); printf("Enter the page size \n"); scanf("%d",&ps); for(i=0;i<np;i++) { sa[i]=(int)malloc(ps);
CHENNAI INSTITUTE OF TECHNOLOGY Page 19
OUTPUT: Input: Enter how many pages: 5 Enter the page size: 4 Output: Page1 Address: 1894 Page2 Address: 1902 Page3 Address: 1910 Page4 Address: 1918 Page5 Address: 1926
Write a c program for message passing using pipes. #include <stdio.h> #include <sys/types.h> #include <unistd.h> int main() { int fd[2]; if(pipe(fd)<0) exit(1); if(fork()) { close(fd[0]); write(fd[1], Message from Suhrit12); } else { char buf[100];
CHENNAI INSTITUTE OF TECHNOLOGY Page 20
Write a C program that implements a producer-consumer system with two processes.(using semaphores) #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <time.h> #include <sys/types.h> #include <sys/ipc.h> #include <sys/sem.h> #define NUM_LOOPS 20 int main(int argc, char* argv[]) { int sem_set_id; union semun sem_val; int child_pid; int i; struct sembuf sem_op;
CHENNAI INSTITUTE OF TECHNOLOGY Page 21
int rc; struct timespec delay; sem_set_id = semget(IPC_PRIVATE, 1, 0600); if (sem_set_id == -1) { perror("main: semget"); exit(1); } printf("semaphore set created, semaphore set id '%d'. '%d'.\n", sem_set_id); sem_val.val = 0; rc = semctl(sem_set_id, 0, SETVAL, sem_val); child_pid = fork(); switch (child_pid) { case -1: perror("fork"); exit(1); case 0: for (i=0; i<NUM_LOOPS; i++) { sem_op.sem_num = 0; sem_op.sem_op = -1; sem_op.sem_flg = 0; semop(sem_set_id, &sem_op, 1); printf("consumer: '%d'\n", i); fflush(stdout); sleep(3); } break; default: for (i=0; i<NUM_LOOPS; i++) { printf("producer: '%d'\n", i); fflush(stdout); sem_op.sem_num = 0; sem_op.sem_op = 1; sem_op.sem_flg = 0; semop(sem_set_id, &sem_op, 1); sleep(2); if (rand() > 3*(RAND_MAX/4)) { delay.tv_sec = 0; delay.tv_nsec = 10; nanosleep(&delay, NULL); }
CHENNAI INSTITUTE OF TECHNOLOGY Page 22
} break; } return 0; }
Write a C program that illustrates inter process communication using shared memory system calls. #include <stdio.h> #include<sys/ipc.h> #include<sys/shm.h> #include<sys/types.h> #define SEGSIZE 100 int main(int argc, char *argv[ ]) { int shmid,cntr; key_t key; char *segptr; char buff[ ]=Hello world; key=ftok(.,s); if((shmid=shmget(key, SEGSIZE, IPC_CREAT | IPC_EXCL | 0666))= = -1) mget(key, { if((shmid=shmget(key,SEGSIZE,0))= = -1)
CHENNAI INSTITUTE OF TECHNOLOGY Page 23
{ perror(shmget); exit(1); } } else { printf(Creating a new shared memory seg \n); printf(SHMID:%d, shmid); } system(ipcs m); if((segptr=shmat(shmid,0,0))= =(char*) =(char*)-1) { perror(shmat); exit(1); } printf(Writing data to shared memory memory\n); strcpy(segptr,buff); printf(DONE\n); printf(Reading data from shared memory memory\n); printf(DATA:-%s\nsegptr); printf(DONE\n); print(Removing shared memory Segment Segment\n); if(shmctl(shmid,IPC_RMID,0)= = -1) printf(Cant Remove Shared memory Segment Segment\n); else printf(Removed Successfully); }
Page 24