Sunteți pe pagina 1din 63

UNIX COMMANDS

AIM:
To study and execute the commands in unix.

COMMANDS:
1. DATE command: This command is used to disp;ay the current date and time. Syntax: $date $date+%d Options: a=abbrevated weekday A=Fully weekday b=abbrevated month B=full month C=current day and time C=display the century as a decimal number D=day of the month D=day in mm/dd/yy format H=abbrevated month day H=display the hour L=day of the year M=month of the year M=minute P=display AM or PM S=seconds T=HH:MM:SS format U=week of the year Y=display the year in 2 digit Z=time zone To change the format: Syntax: $date+%H-%M-%s

2.CAL-calander command: This command is used to display the calendar of the year of the particular month of calendar year. Syntax: a.$cal<year>

b.$cal<month><year> here the first syntax gives the entire calendar for given year and the second syntax gives the calendar of reserved month of that year. 3.ECHO command: This command is used to print the arguments on the screen. Syntax: $echo<text> Multiline echo command: 1.To have the output in the same line, the following commands can be used. Syntax: $echo<text\>text 2.to have the output in different line, the following command can be used. Syntax: $echotext >line2 >line3 4.BANNER command: It is used to display the arguments in # symbol. Syntax: $banner<arguments> 5.WHOcommand: It is used to display who are the users connected to our computer. Syntax: $who- options Options: H-display the output with headers. b-display the latest booting date or time when the system was lastly rebooted . 6.WHO AM Icommand: Display the details of the current working directory. Syntax: $who am i

7.ttycommand: It will display the terminal name.

Syntax: $tty 8.Binarycalculator command: It will change the $ mode and in the new mode, arithmetic operations such as +,-,*,/,%,n,sqrt(),length(),=,etc can be performed. This command is used to go to the binary calculus mode. Syntax: $bc Operations ^d $ I base-input base O base-output base are used for base conversions Base: Decimal=1 Binary=2 Octal=8 Hexa=16 9.CLEARcommand: It is used to clear the screen. Syntax: $clear 10.MANcommand: It help us to know about the particular command and its options and working. It is like help command in window. Syntax: $man<command name> 11.Manipulation command: It is used to manipulate the screen. Syntax: %tput<argument> Arguments: 1.clear-to clear the screen. 2.longname-display the complete name of the terminal. 3.SMSO-background become white and foreground become black colour. 4.rmso-background become black and foreground become white colour. 5.cop RC-move to the cursor position to the specified location. 6.cols-display the number of columns in our terminals .

12.List command: It is used to list all the contents in the current working directory. Syntax: $ls-options<arguments> If the command does not contain any argument means it is working in the current directory. Options: a-used to list all files including the hidden files. e-list all the files column wise. d-list all the directories. m-list the files separated by commas. p-list files include/to all directories. r-list the files in reverse alphabetical order. f-list the files based on the list modification date. x-list in column wise sorted order .

DIRECTORY RELATED COMMANDS:


1.PWD command: To print the complete path of the current working directory. Syntax: $pwd 2.MKDIR command: To create or make a new directory in a current directory. Syntax: $mkdir<directory name> 3.CD command: To change or move the directory to the mentioned directory. Syntax: $cd<directory name> 4.RMDIR command: To change or move the directory to the mentioned directory. Syntax: $rmdir<directory name> 5.CD command: To move the previous directory or the present directory. Syntax: $cd

FILE RELATED COMMANDS:


1.create a file: To create a new file in the current directory we use CAT command. Syntax: $cat>file name The >symbol is redirectory we use cat command. 2.Display a file: To display the content of file mentioned we use CAT command without > operator. Syntax: $cat<file name Options: -s to neglect the warning/error message. 3.Copying Contents: To copy the content of one file with another. If file does not exist, a new file is created and if the file exists with some data then it is overwritten. Syntax: $cat<file name source>>><destination file name> $cat<source file name>>><destination file name> It is avoid overwritting. Options: -n content of files with numbers included with blank lines. Syntax: $cat-n<file name>

4.Sorting a file: To sort the contents in alphabetical order in reverse order. Syntax: $sort<file name> Options: $sort-r<file name> 5.Copying Contents from one file to another: To copy the contents from source to destination file. So the both contents are same.

Syntax: $cp<source file name><destination file name> $cp<source file name path><destination file name path> 6.MOVE command: To completely move the contents from source file to destination file and to remove the source file. Syntax: $mv<source file name><destination file name> 7.REMOVE command: To completely move the contents from source file to destination file and to remove the source file. Syntax: $rm<file name> 8.WORDS command: To list the count of no of lines, words, characters. Syntax: $wc<file name> Options: -c to display the no of characters. -l to display only the lines. -w to display the no of words . 9.Line printer: To print the line through the printer, we use lp command. Syntax: $lp<file name> 10.PAGE command: Thes command is used to display the contents of the file page wise and next page can be viewed by pressing the enter key. Syntax: $pg<file name>

RESULT:
Thus the basic commands of unix are studied and verified.

UNIX SHELL COMMANDS

AIM:
To syudy about the UNIX shell programming commands.

INTRODUCTION:
Shell programming is a group of commands together under under signal file name. 1.SHELL KEYWORDS: Echo, read, if, else, case, esac, for, while, do, done, until, set, unset, readonly, shift, export, break, continue, exit, return, trap, wait, eval, exec, unlimit, unmask. 2.SHELL VARIABLES: Shell variables change during the executing of the program. Rules: 1.A variable name is any combination of alphabets, digits and an underscore(_). 2.No commas or blank space are allowed within a variable name. 3.The first character of a variable name must be an alphabet or an underscore(_). 4.Variables names should be of any reasonable length. 5.Variables name are case sensitive. That name, NAME, Name are all different variables. 3.READ STATEMENT: To get the input from the user. Syntax: read x y [no need commas between variables] 4.ECHO STATEMENT: Echo statement is similar to the output statement. Syntax: Echostring (or) Echo$b[for variables]

5.EXP-Expression Command: To perform all arithmetic operations. Syntax: Var=`expjjr $value 1 `+`$value 2` 6.NESTING OF EXPRESSION COMMAND: Syntax: Sum=expr `$value 1+$value 2`+value 3 7.ARITHMETIC OPETRATORS: +,-,/,*,%,^ 8.RELATIONAL OPERATORS: -le : less than (or) equal to. -lt : less than. -gl : greater then (or) equal to. -dt : greater than. -eq : equal to. 9.LOGICAL OPERATORS: And : && Or : || 10.EXECUTION OF SHELL SCRIPT: 1.By using change mode command. 2.$chmod u + x sum sh 3.$sum.sh (or) $ch sum.sh 11.CONDITIONAL STATEMENT: (i)If Statement: This is used to check a condition and if it satisfy the condition if then does the next action, if not it goes to the else part. (ii)IF.ELSE.Statement: Syntax: if cp $source $target then

echo file copied successfully else echo failed to copy the file (iii)NESTED IF Statement: Here sequence of condition are checked and the corresponding performed accordingly. Syntax: if condition then command if condition then command else command fi fi (iv)CASEESAC Statement: This construct helps in execution of shell script based on choice. 12.UNTIL loop: This loop is similar to while loop except that it continues as long as the control connand file. Syntax: until control command do comman done 13.BREAK statement: This command is used to jump out the loop instantly, without waiting to get the control command.

Result:
Thus the unix shell commands are executed and verified.

VI EDITOR COMMANDS

AIM:
To study the various commands operated in vi editor in unix .

COMMANDS:
Starting vi: The vi editor is invoked by giving the following commands in unix prompt. Syntax: $vi<filename> (or) $vi This command would open a display screen with 25 lines and with tilde(~) symbol at the start of each line. The first syntax would save the file in the filename mentioned and for the next the filename must be mentioned at the end. Options: 1.vi+n<filename>-this would point at the nth line(cursor pos). 2.vi-n<filename>-this command is to make the file to read only to change from one made to another press escape key .

INSERTING AND REPLACING COMMANDS:


To move editor from command mode to edit mode, you have to press the <ESC> key. For inserting and replacing the following commands are used. 1.<ESC>a command: This command is used to move the edit mode and start to append after the current character. Syntax: <ESC>a 2.<ESC>A command: This command is also used to append the file, but this command append at the end of current line. Syntax: <ESC>A 3.<ESC>i command: This command is used to insert the text before the current cursor position. Syntax:

<ESC>i 4.<ESC>I command: This command is used to insert the text at the beginning of the current line. Syntax: <ESC>I 5.<ESC>o command: This command is to insert a blank line below the current line and allow insertion of contents. Syntax: <ESC>o 6.<ESC>O command: This command is used to insert a blank line above and allow the insertion of contents. Syntax: <ESC>O 7.<ESC>r command: This command is used to replace the particular character with the given characters. Syntax: <ESC>r x Wher x is the new character. 8.<ESC>R command: This command is used to replace the particular text with a given text. Syntax: <ESC>R text 9.<ESC>s command: This command is used to replace a single character with a group of characters. Syntax: <ESC>s 10.<ESC>S command: This command is used to replace a current line with group of characters. Syntax: <ESC>S

CURSOR MOVEMENT IN VI:


1.<ESC>L: This command is used to move to the previous character typed. It is used to move to left of the text. It can also be used to move character by character (or) a number of characters. Syntax: <ESC>L-to move one character to left <ESC>nL-to move n characters to left 2.<ESC>I: This command is used to move to the right of the cursor (i.e.) to the next character. It can also be used to move the cursor for a number of characters. Syntax: <ESC>I-single character to right <ESC>nI- n character to right 3. <ESC>j: This command is used to move down a single line or a number of lines. Syntax: <ESC>j- single down movement <ESC>nj- n times down movement 4. <ESC>k: This command is used to move up a single line or a number of lines. Syntax: <ESC>k- single line above <ESC>nk- n lines above 5.ENTER (OR) ENTER: This command will move the cursor to the starting of next lines or a group of lines mentioned. Syntax: <ESC>enter <ESC>nenter 6. <ESC>+ command: This command is used to move to the beginning of the next line. Syntax: <ESC>+ <ESC>n+ 7. <ESC>- command:

This command is used to move to the beginning of the previous line. Syntax: <ESC><ESC>n8. <ESC>o command: This command will bring the cursor to the beginning of the same current line. Syntax: <ESC>o 9. <ESC>$ command: This command will bring the cursor to the end of the current line. Syntax: <ESC>$ 10. <ESC>^ command: This command is used to move to first character of first lines. Syntax: <ESC>^ 11. <ESC>b command: This command is used to move back to the previous word (or) a number of words. Syntax: <ESC>b <ESC>nb 12. <ESC>e command: This command is used to move towards and replace the cursor at last character of the word (or) no of words. Syntax: <ESC>e <ESC>ne 13. <ESC>w command: This command is used to move forward by a single word or a group of words. Syntax: <ESC>w <ESC>nw

DELETING THE TEXT FROM VI:


1. <ESC>x command:

To delete a character to right of current cursor positions, this command is used. Syntax: <ESC>x <ESC>nx 2. <ESC>X command: To delete a character to left of current cursor positions, this command is used. Syntax: <ESC>X <ESC>nX 3. <ESC>dw command: This command is to delete a single word or number of words to right of current cursor position. Syntax: <ESC>dw <ESC>ndw 4.<Ecs>db command: This command is to delete a single word to the left of the current cursor position. Syntax: <ESC>db <ESC>ndb 5.<ESC>dd command: This command is used to delete the current line (or) a number of line below the current line. Syntax: <ESC>dd <ESC>ndd 6.<ESC>d$ command: This command is used to delete the text from current cursor position to last character of current line Syntax: <ESC>d$

SAVING AND QUITING FROM VI:


1.<ESC>w command:

To save the given text present in the file Syntax: <ESC>:w 2.<ESC>q! command: To quit the given text without saving. Syntax: <ESC>:q! 3.<ESC>wq command: This command quits the vi editor after saving the text in the mentioned file. Syntax: <ESC>:wq 4.<ESC>x command: This command is same as wq command it saves and quit Syntax: <ESC>x 5.<ESC>q command: This command would quit the window but it would ask for again to save the file. Syntax: <ESC>q

RESULT:
Thus the Vi editor commands are studied and verified.

PROGRAM:
#include<stdio.h> 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 %dand its parent id id%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) { printf("process 3 id is %d and its parent id is%d\n",getpid(),getppid()); } }

OUTPUT:
[user@localhost cse]$ cc fork.c [user@localhost cse]$ ./a.out

Process id is 6901 Parent id is 6900

PROGRAM:
#include<stdio.h>

#include<sys/types.h> #include<unistd.h> 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); } }

OUTPUT:

[user@localhost cse]$ cc exec.c [user@localhost cse]$ ./a.out Parent id is 6407 And root id is 5638 Process1 id is 6408 And its parent id is 6407 Process2 id is 6409 And its parent id is 6408 Process3 id is 6410 And its parent id is 6409

PROGRAM:
#include<stdio.h> #include<sys/types.h>

#include<sys/stat.h> #include<unistd.h> #include<fcntl.h> 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 does not 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); }

OUTPUT:
[user@localhost cse]$ cc exit.c [user@localhost cse]$ ./a.out

Enter file to be copied: sts1.c Source file size: 670 Destination file size before closing: 670 Destination file size after closing: 3

PROGRAM:
#include<stdio.h>

#include<dirent.h> main() { DIR *p; struct dirent *dp; p=opendir("."); 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:
[user@localhost cse]$ cc opendir.c

[user@localhost cse]$ ./a.out 82475730 82418 a.out 82420 fork.c

PROGRAM:
#include<stdio.h>

#include<signal.h> void first(); void second(); int pid; main() { pid=fork(); if(pid!=0) { printf("221"); printf("\nWelcome..."); sleep(3); signal(SIGUSR2,first); kill(pid,SIGUSR1); } else { signal(SIGUSR1,second); } sleep(4); } void first() { printf("\nKilling...\nfirst kill\n"); exit(0); } void second() { printf("\nKilling...\nsecond killed\n"); kill(pid,SIGUSR2); }

OUTPUT:
[user@localhost cse]$ cc system.c

[user@localhost cse]$ ./a.out 221 Killing... second killed Welcome... Killing... first kill

PROGRAM:
#include<stdio.h> main()

{ FILE *ptr,*ptw; char ch; ptw=fopen("hai.c","wf"); printf("Enter the text:"); printf("\nFor save and exit press ctrl+D\n"); while((ch=getchar())!=EOF) { fputc(ch,ptw); if(ch==EOF) exit(); } fclose(ptw); printf("\nYour text:"); ptr=fopen("hai.c","r"); while((ch=fgetc(ptr))!=EOF) printf("%c",ch); fclose(ptr); }

OUTPUT:
[user@localhost cse]$ cc read.c

[user@localhost cse]$ ./a.out Enter the text: For save and exit press ctrl+D hai how r u? Your text: hai how r u?

PROGRAM:
#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); if(n<0) printf("error"); else for(i=0;i<n;i++) printf("%s",namelist[i]->d_name); }

OUTPUT:
[user@localhost cse]$ cc ls.c [user@localhost cse]$ ./a.out ...a.out ls.c

PROGRAM:
#include<stdio.h> #include<sys/types.h> #include<sys/stat.h> #include<unistd.h> 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<word><file>...\n",argv[0]); return -1; } FILE *fp; char *filename; int x=2; for(x;x!=argc;x++) { filename=argv[x]; if((fp=fopen(filename,"r"))==NULL) { printf("failed to open file %s",argv[2]); return -2; } int BUFSIZE=calc_bufsize(filename); char buf[BUFSIZE]; fread(buf,sizeof(char),BUFSIZE,fp); char *ans=strstr(buf,argv[1]); if(ans!=NULL) printf("%s",filename); fclose(fp); } return 0; }

OUTPUT:
[user@localhost cse]$ cc grep.c [user@localhost cse]$ ./a.out usage: <word><file>...

PROGRAM:
#include<stdio.h> main() { int n,b[12],b1[12],w[12],e[12],i,k,t; float aw=0,at=0; printf("Enter the no of jobs:"); scanf("%d",&n); for(i=0;i<n;i++) { printf("Enter the burst time for job%d\n",i+1); scanf("%d",&b[i]); b1[i]=b[i]; } printf("Job\tBurst\tWaiting\tTurnaround\n"); for(i=0;i<n;i++) { if(i==0) { w[0]=0; e[0]=b1[0]; } else { w[i]=e[i-1]; e[i]=e[i-1]+b1[i]; } printf("%d\t%d\t%d\t%d\n",i+1,b1[i],w[i],e[i]); aw+=w[i]; at+=e[i]; } aw/=n; at/=n; printf("Average waiting time is:%f",aw); printf("Average turnaround time is:%f",at); return(0); }

OUTPUT:
[user@localhost cse]$ cc fcfs.c [user@localhost cse]$ ./a.out Enter the no of jobs: 3 Enter the burst time for job1 23 Enter the burst time for job2 12 Enter the burst time for job3 5 Job 1 2 3 Burst 23 12 5 Waiting 0 23 35 Turnaround 23 35 40

Average waiting time is: 19.333334 Average turnaround time is: 32.666668

PROGRAM:
#include<stdio.h> main() { int n,b[12],b1[12],w[12],e[12],i,k,t; float aw=0,at=0; printf("Enter the no of jobs:"); scanf("%d",&n); for(i=0;i<n;i++) { printf("Enter the burst time for job%d\n",i+1); scanf("%d",&b[i]); b1[i]=b[i]; } for(i=0;i<n;i++) { for(k=i+1;k<n;k++) { if(b1[i]>b1[k]) { t=b1[i]; b1[i]=b1[k]; b1[k]=t; } } } printf("Job\tBurst\tWaiting\tTurnaround\n"); for(i=0;i<n;i++) { if(i==0) { w[0]=0; e[0]=b1[0]; } else { w[i]=e[i-1]; e[i]=e[i-1]+b1[i]; } printf("%d\t%d\t%d\t%d\n",i+1,b1[i],w[i],e[i]); aw+=w[i]; at+=e[i]; } aw/=n; at/=n; printf("Average waiting time is:%f",aw); printf("\nAverage turnaround time is:%f",at); return(0); }

OUTPUT:
[user@localhost cse]$ cc sjf.c [user@localhost cse]$ ./a.out Enter the no of jobs:3 Enter the burst time for job1 23 Enter the burst time for job2 12 Enter the burst time for job3 5 Job 1 2 3 Burst 5 12 23 Waiting 0 5 17 Turnaround 5 17 40

Average waiting time is: 7.333333 Average turnaround time is: 20.666666

PROGRAM:
#include<stdio.h> main() { int n,b[12],b1[12],w[12],e[12],pri[12],i,k,t; float aw=0,at=0; printf("\nEnter the no of jobs:"); scanf("%d",&n); for(i=0;i<n;i++) { printf("Enter the burst time and priority for jobs:%d\n",i+1); scanf("%d%d",&b[i],&pri[i]); b1[i]=b[i]; } for(i=0;i<n;i++) { for(k=i+1;k<n;k++) { if(pri[k]<pri[i]) { t=pri[i]; pri[i]=pri[k]; pri[k]=t; t=b1[i]; b1[i]=b1[k]; b1[k]=t; } } } printf("Jobs\tBurst\tPriority\tStart time\tFinish time\n"); for(i=0;i<n;i++) { if(i==0) { w[0]=0; e[0]=b1[0]; } else { w[i]=e[i-1]; e[i]=e[i-1]+b1[i]; } printf("%d\t%d\t%d\t%d\t%d\n",i+1,b1[i],pri[i],w[i],e[i]); aw+=w[i]; at+=e[i]; } aw/=n; at/=n; printf("\nAverage waiting time is:%f",aw);

printf("\nAverage turnaround time is:%f",at); return(0); }

OUTPUT:
[user@localhost cse]$ cc priority.c [user@localhost cse]$ ./a.out Enter the no of jobs: 4 Enter the burst time and priority for jobs: 1 4 5 Enter the burst time and priority for jobs: 2 6 7 Enter the burst time and priority for jobs: 3 7 5 Enter the burst time and priority for jobs: 4 3 7 Jobs 1 2 3 4 Burst 4 7 6 3 Priority 5 5 7 7 Start time 0 4 11 17 Finish time 4 11 17 20

Average waiting time is: 8.000000 Average turnaround time is: 13.000000

PROGRAM:
#include<stdio.h> main() { int i,n,j=0,p[15],ts,tt=0,b[15]; printf("Enter the no of process:"); scanf("%d",&n); for(i=0;i<n;i++) { printf("Enter the process name:"); scanf("%d",&p[i]); printf("Enter the burst time for the process:"); scanf("%d",&b[i]); } printf("Enter slice time:"); scanf("%d",&ts); printf("PROCESS NAME REMANING TIME TOTAL TIME"); while(j<n) { for(i=0;i<n;i++) { if(b[i]>0) { if(b[i]>=ts) { tt+=ts; b[i]=b[i]-ts; printf("\n%d\t\t%d\t\t%d",p[i],b[i],tt); if(b[i]==0) j++; } else { tt+=b[i]; b[i]=0; printf("\n%d\t\t%d\t\t%d",p[i],b[i],tt); } } } } }

OUTPUT:
[user@localhost cse]$ cc robin.c [user@localhost cse]$ ./a.out Enter the no of process: 4 Enter the process name: 1 Enter the burst time for the process: 8 Enter the process name: 2 Enter the burst time for the process: 12 Enter the process name: 3 Enter the burst time for the process: 4 Enter the process name: 4 Enter the burst time for the process: 16 Enter slice time: 4 PROCESS NAME 1 2 3 4 1 2 4 2 4 4 REMANING TIME 4 8 0 12 0 4 8 0 4 0 TOTAL TIME 4 8 12 16 20 24 28 32 36 40

PROGRAM:
#include<stdio.h> #include<sys/shm.h> #include<sys/ipc.h> int main() { int child,shmid,i; char *shmptr; child=fork(); if(!child) { shmid=shmget(2041,32,0666/IPC_CREATE); shmptr=shmat(shmid,0,0); printf("\n PARENT WRITING\n"); for(i=0;i<10;i++) { shmptr[i]='a'+i; putchar(shmptr[i]); } printf("\n\n%s",shmptr); wait(NULL); } else { shmid=shmget(2041,32,0666); shmptr=shmat(shmid,0,0); printf("\n CHILD IS READING:"); for(i=0;i<10;i++) putchar(shmptr[i]); shmdt(NULL); shmat(shmid,IPC_RMID,NULL); } return(0); }

OUTPUT:
[user@localhost cse]$ cc shared.c [user@localhost cse]$ ./a.out PARENT WRITING abcdefghij abcdefghij CHILD IS READING: abcdefghij

PROGRAM:
#include<stdio.h> int main() { int fd[2],child; char a[10]; printf("\nEnter the string to enter into the pipes:"); scanf("%s",a); pipe(fd); child=fork(); if(!child) { close(fd[0]); write(fd[1],a,5); wait(0); } else { close(fd[1]); read(fd[0],a,5); printf("\nThe string reterived from the pipe is:%s",a); } return 0; }

OUTPUT:
[user@localhost cse]$ cc pipe.c [user@localhost cse]$ ./a.out Enter the string to enter into the pipes: computer The string retrieved from the pipe is: computer

PROGRAM:
#include<stdio.h> int currentsize; int n; main() { int ch; printf("\nEnter the size of the buffer:"); scanf("%d",&n); do { printf("\n1.Producer\n2.Consumer\n3.Exit\nEnter your choice:\n"); scanf("%d",&ch); switch(ch) { case 1: producer(); break; case 2: consumer(); break; } }while(ch!=3); } producer() { int item; printf("\nEnter the no of items to be produced:"); scanf("%d",&item); if(0<=(n-(currentsize+item))) { currentsize=currentsize+item; printf("\nThe item has been produced"); printf("\nThe no of items in the buffer is %d",currentsize); } else printf("\nThe buffer is not sufficient"); } consumer() { int pdt; printf("\nEnter the no of items to be consumed:"); scanf("%d",&pdt); if(currentsize>=pdt) { currentsize=currentsize-pdt; printf("\nThe items has been consumed"); printf("\nThe no of items in the buffer is %d",currentsize); }

else printf("\nThe buffer does not have sufficient items"); }

OUTPUT:
[user@localhost cse]$ cc producer.c [user@localhost cse]$ ./a.out Enter the size of the buffer: 20 1. Producer 2. Consumer 3. Exit Enter your choice: 1 Enter the no of items to be produced: 17 The item has been produced The no of items in the buffer is 17 1. Producer 2. Consumer 3. Exit Enter your choice: 2 Enter the no of items to be consumed: 14 The items has been consumed The no of items in the buffer is 3 1. Producer 2. Consumer 3. Exit Enter your choice: 3

PROGRAM:
#include<stdio.h> main() { int a[20],p[20],i,j,n,m,t; printf("\nEnter the no of blocks:"); scanf("%d",&n); for(i=0;i<n;i++) { printf("Enter the %d of block size:",i); scanf("%d",&a[i]); } printf("Enter the no of process\n"); scanf("%d",&m); for(i=0;i<m;i++) { printf("Enter the size of %d of process:",i); scanf("%d",&p[i]); } for(i=0;i<n;i++) { for(j=i+1;j<n;j++) { if(a[i]<a[j]) { t=a[i]; a[i]=a[j]; a[j]=t; } } } for(i=0;i<m;i++) { for(j=i+1;j<m;j++) { t=p[i]; p[i]=p[j]; p[j]=t; } } for(i=0;i<m;i++) { for(j=0;j<m;j++) { if(p[j]==a[i]) { printf("The process %d allocated to %d\n",j,a[i]); p[j]=10000; break;

} } } for(j=0;j<m;j++) { if(p[j]!=10000) { printf("The process %d is not allocated\n",j); } } }

OUTPUT:
[user@localhost cse]$ cc bestfit.c [user@localhost cse]$ ./a.out Enter the no of blocks: 3 Enter the 0 of block size: 300 Enter the 1 of block size: 200 Enter the 2 of block size: 100 Enter the no of process 3 Enter the size of 0 of process: 100 Enter the size of 1 of process: 300 Enter the size of 2 of process: 200 The process 0 allocated to 100 The process 1 allocated to 300 The process 2 allocated to 200

PROGRAM:
#include<stdio.h> main() { int a[20],p[20],i,j,n,m; printf("\nEnter the no of blocks:"); scanf("%d",&n); for(i=0;i<n;i++) { printf("Enter the %d of block size:",i); scanf("%d",&a[i]); } printf("Enter the no of process:\n"); scanf("%d",&m); for(i=0;i<m;i++) { printf("Enter the size of %d of process:",i); scanf("%d",&p[i]); } for(i=0;i<n;i++) { for(j=0;j<m;j++) { if(p[j]<=a[i]) { printf("The process %d allocated to %d\n",j,a[i]); p[j]=1000; break; } } } for(j=0;j<m;j++) { if(p[j]!=1000) { printf("The process %d is not allocated\n",j); } } }

OUTPUT:
[user@localhost cse]$ cc first.c [user@localhost cse]$ ./a.out Enter the no of blocks: 5 Enter the 0 of block size: 500 Enter the 1 of block size: 400 Enter the 2 of block size: 300 Enter the 3 of block size: 200 Enter the 4 of block size: 100 Enter the no of process: 5 Enter the size of 0 of process: 100 Enter the size of 1 of process: 350 Enter the size of 2 of process: 400 Enter the size of 3 of process: 150 Enter the size of 4 of process: 200 The process 0 allocated to 500 The process 1 allocated to 400 The process 3 allocated to 300 The process 4 allocated to 200 The process 2 is not allocated

PROGRAM:
#include<stdio.h> int main() { int i,j,n,a[50],frame[10],no,k,avail,count=0; printf("\nEnter the no of pages:\n"); scanf("%d",&n); printf("\nEnter the page number:\n"); for(i=1;i<=n;i++) scanf("%d",&a[i]); printf("\nEnter the number of frames:"); scanf("%d",&no); for(i=0;i<no;i++) frame[i]=-1; j=0; printf("\tRef string\tPage frame\n"); for(i=1;i<=n;i++) { printf("%d\t\t",a[i]); avail=0; for(k=0;k<no;k++) if(frame[k]==a[i]) avail=1; if(avail==0) { frame[j]=a[i]; j=(j+1)%no; count++; for(k=0;k<no;k++) printf("%d\t",frame[k]); } printf("\n"); } printf("Page fault is:%d",count); return(0); }

OUTPUT:
[user@localhost cse]$ cc fifo.c [user@localhost cse]$ ./a.out Enter the no of pages: 5 Enter the page number: 3 2 1 4 5 Enter the number of frames: 2 3 2 1 4 5 Ref string 3 3 1 1 5 Page frame -1 2 2 4 4

Page fault is: 5

PROGRAM:
#include<stdio.h> main() { int nframes,len,k,u,min,rep,pos[20],i,j=1,t,m,flag,pf=0,frame[20],s[20]; printf("Enter no of frames\n"); scanf("%d",&nframes); for(i=1;i<=nframes;i++) frame[i]=-1; fflush(stdin); printf("Enter length of reference string\n"); scanf("%d",&len); printf("Enter reference string\n"); for(i=1;i<=len;i++) scanf("%d",&s[i]); for(i=1;i<=len;i++) { flag=0; for(k=1;k<=nframes;k++) { if(s[i]==frame[k]) { flag=1; break; } } if(flag!=1) { pf++; if(j>nframes) { for(u=1;u<=nframes;u++) { m=i-1; while(frame[u]!=s[m]) { m--; } pos[frame[u]]=m; } min=pos[frame[1]]; rep=1; for(u=2;u<=nframes;u++) if(pos[frame[u]]<min) { min=pos[frame[u]]; rep=u; } frame[rep]=s[i];

} else frame[j++]=s[i]; } for(k=1;k<=nframes;k++) printf("%d\t",frame[k]); printf("\n"); } printf("No of page faults is %d",pf); }

OUTPUT:
[user@localhost cse]$ cc lru.c [user@localhost cse]$ ./a.out Enter no of frames 3 Enter length of reference string 9 Enter reference string 1 2 3 4 5 6 7 8 9 1 1 1 4 4 4 7 7 7 -1 2 2 2 5 5 5 8 8 -1 -1 3 3 3 6 6 6 9

No. of page faults is 9

PROGRAM:
#include<stdio.h> #define MAX 20 main() { int block_size[MAX],file_size[MAX],no_of_blocks,no_of_files; int temp,highest=0; static int block_flag[MAX],file_flag[MAX]; int i,j; printf("\n enter the no of blocks"); scanf("%d",&no_of_blocks); for(i=1;i<=no_of_blocks;i++) { printf("\nenter the size of memory block%d:",i); scanf("%d",&block_size[i]); } printf("\n enter the number of files:"); scanf("%d",&no_of_files); for(i=1;i<=no_of_files;i++) { printf("\nenter the size of file%d:",i); scanf("%d",&file_size[i]); } for(i=1;i<=no_of_files;i++) { for(j=1;j<=no_of_blocks;j++) { if(block_flag[i]!=1) { temp=block_size[j]-file_size[j]; if(temp>=0) { if(highest<temp) { file_flag[i]=j; highest=temp; }}}} block_flag[file_flag[i]]=1; highest=0; } printf("\nfile number\tfile size\tblock number\tblock size\n"); for(i=1;i<=no_of_files;i++) { printf("%d\t%d\t",i,file_size[i]); printf("%d\t%d\n",file_flag[i],block_size[file_flag[i]]); } }

OUTPUT:
[user@localhost cse]$ cc worst.c [user@localhost cse]$ ./a.out enter the no of blocks3 enter the size of memory block1:15 enter the size of memory block2:16 enter the size of memory block3:17 enter the number of files:2 enter the size of file1:12 enter the size of file2:14 file number file size 1 12 3 17 2 14 3 17 [user@localhost cse]$ block number block size

SRINIVASA INSTITUTE OF ENGINEERING & TECHNOLOGY P A R I VA K K A M CHENNAI-600056

CS2257 OPERATING SYSTEMS LAB RECORD

REGISTER NUMBER:

SRINIVASA INSTITUTE OF ENGINEERING AND TECHNOLOGY P A R I VA K K A M CHENNAI-600056

REGISTER NUMBER: BONAFIDE CERTIFICATE Certified that this is a bonafide record of the work done by --------------------------------------------------Of Second Year (4th Semester) of -------------------------------------------- Department in the Operating Systems Laboratory (CS 2257) during academic year 2009- 2010

Signature of the Staff

Signature of HOD

Submitted for the Practical Examination held on ----------------------------

E te n l E a in r x r a xm e

In r a E a in r te n l x m e

INDEX EX. NO DATE NAME OF THE EXPERIMENT PAGE NO. T. SIGN

S-ar putea să vă placă și