Os Lab Programs for 4th Sem

May 4, 2017 | Author: smalldon22 | Category: N/A
Share Embed Donate


Short Description

Download Os Lab Programs for 4th Sem...

Description

R.M.K. College of Engg. & Technology

Dept. of CSE & IT

EX:NO: 1 WRITE PROGRAMS USING THE FOLLOWING SYSTEM CALLS OF UNIX OPERATING SYSTEM: Fork, exec, getpid, exit, wait, close, stat, opendir, readdir. FORK & GETPID Aim : To Create a process in the following hierarchy Parent Child1 Child2 Child3 Algorithm 1. Declare the necessary variables. 2. Parent process is the process of the program which is running. 3. Create the child1 process using fork() When parent is active. 4. Create the child2 process using fork() when child1 is active. 5. Create the child3 process using fork() when child2 is active. Program //process1.c #include void main() { int pid1,pid2,pid3; printf("Parent id is %d and root id is %d\n",getpid(),getppid()); pid1=fork(); if(pid1==0) { printf("Process 1 id is %d and its parent id is %d\n",getpid(),getppid()); pid2=fork(); } if(pid2==0) { printf("Process 2 id is %d and its parent id is %d\n",getpid(),getppid()); pid3=fork(); } if(pid1==0&&pid2==0&&pid3==0)

CS2257- OS Lab

Page |1

R.M.K. College of Engg. & Technology

Dept. of CSE & IT

{ printf("Process 3 id is %d and its parent id is %d\n",getpid(),getppid()); } } Sample Output $ cc process1.c $ a.out Parent id is 3553 and root id is 2495 Process 1 id is 3554 and its parent id is 3553 Process 2 id is 3555 and its parent id is 3554 Process 3 id is 3556 and its parent id is 3555 EXECl Aim To Execute a Unix Command (Who) in a ‘C’ program using execl() system call. Problem Description The Unix system call transfers an executable binary files into a process that are the exec family of sytem calls. General Format Execl(filename,arg0,arg1,………argn,0) Char *filename * Arg0* , arg1* ,…….argn* Where filenames are the executable binary files to be transferred into a process. arg0 through argn define the argument to be passed to the process. Program //program1.c #include #include #include void main() { int pid1; pid1=fork(); if(pid1==0) { printf("Process id is %d ",getpid()); printf("and its parent id is %d”,getppid()); execl("/bin/who","who",0); } } Sample Output $ cc program2.c $ a.out

CS2257- OS Lab

Page |2

R.M.K. College of Engg. & Technology

Dept. of CSE & IT

Process id is 3553 and parent id is 2495 Root ttyp2 jun25 03.30 sit ttyp1 jun25 03.30 OPENDIR & READDIR Aim: To write a C program to display the files in the given directory Algorithm: 1. Start the program 2. Declare the variable to the structure dirent (defines the file system-independent directory) and also for DIR 3. Specify the directory path to be displayed using the opendir system call 4. Check for the existence of the directory and read the contents of the directory using readdir system call (returns a pointer to the next active directory entry) 5. Repeat the above step until all the files in the directory are listed 6. Stop the program Program #include #include main() { DIR *p; struct dirent *dp; p=opendir("."); //p=opendir("./shantha"); if(p==NULL) { perror("opendir"); exit(0); } dp=readdir(p); while(p!=NULL) { printf("%d%s\n",dp->d_ino,dp->d_name); dp=readdir(p); } } Output: "di.c" [New] 21L, 239C written [test1@localhost test1]$ cc di.c [test1@localhost test1]$ ./a.out 1049278. 442373.. 1049279.kde 1049364.aa.c.swp 1049285.balan.sh.swp

CS2257- OS Lab

Page |3

R.M.K. College of Engg. & Technology

Dept. of CSE & IT

1049387ar.sh 1049404firstfit.c 1049403wai.c 1049406sta1.c 1049405di.c

RESULT:

CS2257- OS Lab

Page |4

R.M.K. College of Engg. & Technology

Dept. of CSE & IT

EX.NO: 2. UNIX I/O SYSTEM CALLS - STAT, OPEN, CLOSE, EXIT Aim : To implement UNIX I/O system calls open, read , write etc. Algorithm: 1. Create a new file using creat command (Not using FILE pointer). 2. Open the source file and copy its content to new file using read and write command. 3. Find size of the new file before and after closing the file using stat command. Program: #include #include #include #include #include main() { int fd1,fd2,n; char source[30],ch[5]; struct stat s,t,w; fd1=creat("test.txt",0644); printf("Enter the file to be Copied\n"); scanf("%s",source); fd2=open(source,0); if(fd2==-1) { perror("file doesnot exist"); exit(0); } while((n=read(fd2,ch,1))>0) write(fd1,ch,n); close(fd2); stat(source,&s); printf("Source file size=%d\n",s.st_size); fstat(fd1,&t); printf("Destination File size before closing=%d\n",t.st_size); close(fd1); fstat(fd1,&w); printf("Destination File Size after closing=%d\n",w.st_size); } Sample Output: [test1@localhost test1]$ cc sta1.c [test1@localhost test1]$ ./a.out

CS2257- OS Lab

Page |5

R.M.K. College of Engg. & Technology

Dept. of CSE & IT

Enter the file to be Copied sta1.c Source file size=670 Destination File size before closing=670 Destination File Size after closing=3 [test1@localhost test1]$

RESULT:

CS2257- OS Lab

Page |6

R.M.K. College of Engg. & Technology

Dept. of CSE & IT

EX: NO: 3. SIMULATION OF UNIX COMMANDS USING C Aim : To simulate the UNIX command ls. Algorithm : 1. Import dir.h header file 2. Create directory structure variable 3. Using findfirst and findnext methods display the files available in the current directory. Program: #include #include #include char dir[MAXDIR]; void main(void) { struct ffblk ffblk; int done; clrscr(); printf("Directory listing of *.*\n"); done = findfirst("*.*",&ffblk,0); while (!done) { printf(" %s\n", ffblk.ff_name); done = findnext(&ffblk); } getch(); }

CS2257- OS Lab

Page |7

R.M.K. College of Engg. & Technology

Dept. of CSE & IT

GREP Aim : To simulate the UNIX command grep. Algorithm : 1. Create a file with some content. 2. Get any one pattern / string as a input 3. Read file content as strings and compare it with the input pattern 4. If pattern match with file string or part of the string then print that string / line. 5. Close the file. Program: #include #include #include void main() { FILE *fptr; char ch; int i; char p[10], a[50]; clrscr(); fptr=fopen("input.txt","w"); printf("Enter the data to be stored in the file\n"); scanf("%c",&ch); while(ch!='$') { fprintf(fptr,"%c",ch); scanf("%c",&ch); } fclose(fptr); printf("Enter the pattern to be searched"); scanf("%s",p); fptr=fopen("input.txt","r"); i=0; while(!feof(fptr)) { ch=getc(fptr); if( ch!='\n') a[i] = ch; else { a[i]='\0'; if(strnicmp(a,p,strlen(p))==0) printf("%s\n",a);

CS2257- OS Lab

Page |8

R.M.K. College of Engg. & Technology

Dept. of CSE & IT

i=-1; } i++; } fclose(fptr); getch(); } Sample Output: Enter the data to be stored in the file India Country welcome sit well $ Enter the pattern to be searched wel welcome well

RESULT:

CS2257- OS Lab

Page |9

R.M.K. College of Engg. & Technology

Dept. of CSE & IT

EX: NO: 4 SCHEDULING ALGORITHMS – FCFS AND SJF Aim: To compute average waiting time and average turnaround time and to draw Gantt chart using FCFS and SJF FCFS SCHEDULING ALGORITHM Algorithm : 1. Get the no of processes. 2. For each process assign the process id and get the process time. 3. Set the waiting time of the first process as 0 and its turn around time as process time. 4. For each process calculate, Waiting time of process(n) = waiting time of process (n-1) + process time of process (n-1) Turn around time of process(n) = waiting time of process (n) + process time of process (n) 5. Calculate the average waiting time and turn around time. 6. Print the Gantt chart. Program: #include #include #include struct pro { int pid,pt,wt,tat; }; void main() { struct pro p[10]; int n,i,twt,ttat,j,k; float awt,atat; char s[5],e[5]; clrscr(); printf("Enter the no of process\n"); scanf("%d",&n); for(i=1;inext; temp2->next=anode; }

CS2257- OS Lab

P a g e | 25

R.M.K. College of Engg. & Technology

Dept. of CSE & IT

} } printf("Proces id %d \n",allotnode->pid); printf("Start addr %u \n",allotnode->start); printf("End addr %u \n\n",allotnode->end); if(temp1->busy==0) external+=temp1->size; temp1=temp1->next; } printf("Internal Fragmentation = %d\n",internal); printf("External Fragmentation = %d\n",external); } void display() { temp1=freenode; printf("\n\nFree nodes are\n\n\n"); while(temp1!=NULL) { if((temp1->busy)==0) { printf("Start addr %u \n",temp1->start); printf("End addr %u \n",temp1->end); printf("Size %d \n",temp1->size); } temp1=temp1->next; } } Sample Output: Enter the choice: 1.Partition 2.Allocate 3.Display 4.Exit 1 Enter the no of pages 3 Enter the page sizes in Bytes 20 40 60

CS2257- OS Lab

P a g e | 26

R.M.K. College of Engg. & Technology

Dept. of CSE & IT

Enter the choice: 1.Partition 2.Allocate 3.Display 4.Exit 3 Free nodes are Start addr 2310 Endaddr 2330 Size 20 Start addr Endaddr Size

2350 2390 40

Start addr Endaddr Size

2410 2470 60

Enter the choice: 1.Partition 2.Allocate 3.Display 4.Exit 2 Enter the number of programsflag=0; freenode->pid=0; freenode->startaddr=addr; freenode->endaddr=addr+(block_size/2); freenode->next=NULL; while(1) { printf("Enter the choice \n 1.Partition \n 2.To Allocate \n 3.To Print \n 4.To Deallocate \n 5.Exit \n"); scanf("%d",&choice); switch(choice) { case 1 : partition(); break; case 2: allocate(); break; case 3: print(); break; case 4: printf("Enter the process id which you want to Deallocate \n"); scanf("%d",&id); Deallocate(id); break; case 5: exit(0); break; } } } void partition() { int size; node1 = (struct allotnode *)malloc(sizeof(struct allotnode)); printf("Enter the process id\n"); scanf("%d",&node1->pid); printf("Enter the size of the proces"); scanf("%d",&size);

CS2257- OS Lab

P a g e | 30

R.M.K. College of Engg. & Technology

Dept. of CSE & IT

node1->next=freenode; node1->flag=1; if(((int)freenode->endaddr-(int)freenode->startaddr)>=size) { if(headallot==NULL) { headallot=node1; node1->startaddr=addr; t=addr+(size/2); node1->endaddr=t-1; } else { node2->next=node1; t=(int)node2->endaddr; node1->startaddr=t+1; node1->endaddr=t+size; } node2=node1; t=node2->endaddr; freenode->startaddr=t+1; } else printf("Less Memory ! \n\n\n"); } void allocate() { int pid,size,allot=0; node1 = (struct allotnode *)malloc(sizeof(struct allotnode)); printf("Enter the process id\n"); scanf("%d",&pid); printf("Enter the size of the proces"); scanf("%d",&size); temp=headallot; while(temp != NULL) { if(temp->flag==0 && ((int)temp->endaddr - (int)temp->startaddr)>=size && allot==0) { temp->flag=1; temp->pid=pid; allot=1;

CS2257- OS Lab

P a g e | 31

R.M.K. College of Engg. & Technology

Dept. of CSE & IT

node1->flag=0; node1->endaddr=temp->endaddr; temp->endaddr=(int)temp->startaddr+size-1; node1->startaddr=(int)temp->endaddr+1; node1->next=temp->next; temp->next=node1; } temp=temp->next; } } void print() { temp=headallot; printf("\n\nAllocated nodes are\n\n\n"); while(temp != NULL) { if(temp->flag==1) { printf("start addr %d \n",temp->startaddr); printf("pid %d \n",temp->pid); printf("end addr %d \n\n\n",temp->endaddr); } temp=temp->next; } temp=headallot; printf("\n\nFree nodes are\n\n\n"); while(temp != NULL) { if(temp->flag==0) { if(temp->startaddr == temp->endaddr) printf("\n No Free nodes \n"); else { printf("start addr %d \n",temp->startaddr); printf("end addr %d \n\n\n",temp->endaddr); } } temp=temp->next; } } void Deallocate(int id) { temp=headallot;

CS2257- OS Lab

P a g e | 32

R.M.K. College of Engg. & Technology

Dept. of CSE & IT

while(temp!=NULL) { if(temp->pid==id) { temp->pid=0; temp->flag=0; } else printf("\nProcess not available\n"); temp=temp->next; } Merge(); } void Merge() { int f=0; temp=headallot; while(temp!=NULL) { if(temp->flag==0 && temp->next->flag==0) { temp->endaddr=temp->next->endaddr; temp->next=temp->next->next; f=1; } temp=temp->next; } if(f==1) Merge(); } Sample Output:

CS2257- OS Lab

P a g e | 33

R.M.K. College of Engg. & Technology

Dept. of CSE & IT

Enter the Block size 100 Enter the choice 1.Partition 2.To Allocate 3.To Print 4.To Deallocate 5.Exit 1 Enter the process id 1 Enter the size of the process 20 Enter the choice 1.Partition 2.To Allocate 3.To Print 4.To Deallocate 5.Exit 1 Enter the process id 2 Enter the size of the process 40 Enter the choice 1.Partition 2.To Allocate 3.To Print 4.To Deallocate 5.Exit 3 Allocated nodes are start addr 2296 pid 1 end addr 2315 start addr 2216 pid 2 end addr 2355 Free nodes are start addr 2256 end addr 2396

CS2257- OS Lab

P a g e | 34

R.M.K. College of Engg. & Technology

Dept. of CSE & IT

Enter the choice 1.Partition 2.To Allocate 3.To Print 4.To Deallocate 5.Exit 2 Enter the process id 3 Enter the size of the proces 30 Enter the choice 1.Partition 2.To Allocate 3.To Print 4.To Deallocate 5.Exit 3 Allocated nodes are start addr 2296 pid 1 end addr 2315 start addr 2216 pid 2 end addr 2355 start addr 2256 pid 3 end addr 2385 Free nodes are start addr 2286 end addr 2396 Enter the choice 1.Partition 2.To Allocate 3.To Print 4.To Deallocate

CS2257- OS Lab

P a g e | 35

R.M.K. College of Engg. & Technology

Dept. of CSE & IT

5.Exit 4 Enter the process id which you want to Deallocate 2 Enter the choice 1.Partition 2.To Allocate 3.To Print 4.To Deallocate 5.Exit 3 Allocated nodes are start addr 2296 pid 1 end addr 2315 start addr 2256 pid 3 end addr 2385 Free nodes are start addr 2216 end addr 2355 start addr 2286 end addr 2396 Enter the choice 1.Partition 2.To Allocate 3.To Print 4.To Deallocate 5.Exit 2 Enter the process id 2 Enter the size of the process 10 Enter the choice 1.Partition 2.To Allocate

CS2257- OS Lab

P a g e | 36

R.M.K. College of Engg. & Technology

Dept. of CSE & IT

3.To Print 4.To Deallocate 5.Exit 3 Allocated nodes are start addr 2296 pid 1 end addr 2315 start addr 2316 pid 2 end addr 2325 start addr 2256 pid 3 end addr 2385 Free nodes are RESULT: start addr 2226 end addr 2355 start addr 2286 end addr 2396

CS2257- OS Lab

P a g e | 37

View more...

Comments

Copyright ©2017 KUPDF Inc.
SUPPORT KUPDF