basic commands of linux os

75
BASIC COMMANDS OF LINUX OS AIM: To study the basic commands of LINUX operating system. COMMANDS: 1. date command: SYNTAX: $date DESCRIPTION: The date command tells us the current date and time. Example: $date Thu Nov 1 09:34:50 DST1984 $- OUTPUT: $date Mon Jul 11 11:14:39 IST2011 $- 2. who command: SYNTAX:$who DESCRIPTION: The output of the who command gives us the detail of the users who have logged into the LINUX system currently. EXAMPLE: $who abc tty0 Sep 26 11:17

Upload: hbhdgyug

Post on 16-Oct-2014

294 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Basic Commands of Linux Os

BASIC COMMANDS OF LINUX OS

AIM:

To study the basic commands of LINUX operating system.

COMMANDS:

1. date command:

SYNTAX: $date

DESCRIPTION: The date command tells us the current date and time.

Example: $dateThu Nov 1 09:34:50 DST1984$-

OUTPUT:$dateMon Jul 11 11:14:39 IST2011$-

2. who command:

SYNTAX:$who

DESCRIPTION: The output of the who command gives us the detail of the users who have logged into the LINUX system currently.

EXAMPLE:$whoabc tty0 Sep 26 11:17xyz tty4 Sep 26 11:30lkg tty9 Sep 26 11:48$-

OUTPUT:$whouser 01 pts/1 2011-07-11 11:11user 07 pts/6 2011-07-11 11:11user 15 pts/4 2011-07-11 11:11

Page 2: Basic Commands of Linux Os

user 10 pts/2 2011-07-11 11:11user 11 pts/5 2011-07-11 11:11user 05 pts/3 2011-07-11 11:11user 06 pts/10 2011-07-11 11:11$-

3. who am i command:

SYNTAX:$who am i

DESCRIPTION: Which gives us the details regarding the login time ad the system’s name for the connection.

EXAMPLE:$who am iuser1 ttya Sep 26 12:20$-

OUTPUT:$ who am iuser10 pts/2 2011/07-11 11:13$-

4.man command:

SYNTAX:$man

DESCRIPTION: If we get struck on something and cannot find an expert to help, we can print any manual page on our terminal with the command ‘man’ command name.

EXAMPLE: $man who$-

OUTPUT:$manWhat manual page do you want?$-

5.head and tail command:

SYNTAX 1: $head[-count][filename]

Page 3: Basic Commands of Linux Os

SYNTAX 2:$tail[+/- number][file]

DESCRIPTION: The head command is used to display the initial part of a text file. This can be through of as a complement command to the tail command, which displays the last part of a text file. By default head command display first 10 lines of a file and tail command at the last 10 lines.

EXAMPLE1: $head -4 wordlistpenbatinkeraser$-

EXAMPLE2:$tail +10 mall$-

OUTPUT1:$head -4 roundr.c#include<stdio.h>#include<conio.h>void main(){int st[10],bt[10],wt[10],tat[10],n,tq;$-

OUTPUT2:$tail +10 roundr.cstat=stat+tat[i];}awt=(float)swt/n;atat=(float)stat/n;printf("Process\tB_time\tW_time\tTAT:\n");for(i=0;i<n;i++)

printf("%d\t%d\t%d\t%d\n",i+1,bt[i],wt[i],tat[i]);printf("\nAvg wait time is %f\nAvg turn around time is %f",awt,atat);getch();}

6. pwd command:

SYNTAX: $pwd

Page 4: Basic Commands of Linux Os

DESCRIPTION: The pwd command has no options that displays full path name for the current directory we are looking for.

EXAMPLE: $pwd/user/temp$-

OUTPUT:$pwd/home/user10/sadhana$-

7. ls command:

SYNTAX: $ls

DESCRIPTION: The ls command displays the list of files in the current working directory.

EXAMPLE: $lsabc-cxyzlkg$-

OUTPUT:$lsroundr.c$-

SUBCOMMANDS OF ls:

i) ls-l=>Lists files in long format. The files are displayed along with their mode, number of links, owners of files, file size, modification and time and file name.

OUTPUT:$ls-ltotal 16-rw-rw-r--1 user10 user10 14 Jul 11 11:43 robin.c-rw-rw-r--1 user10 user10 14 Jul11 11:59 robin.c$-

ii)ls-t=>Lists in order of last modification time (most recent first).

Page 5: Basic Commands of Linux Os

OUTPUT:$ls-troundr.c robin.c$-

iii)ls-a=>Lists all entries , including hidden files.

OUTPUT:$ls-a…..robin.c roundr.c$-

iv)ls-d=>Lists directory file name instead of its contents.

OUTPUT:$ls-d.$-

v)ls-p=>Puts a Slash after each directory.

OUTPUT:$ls-p/robin.c roundr.c$-

vi)ls-u=>Lists in order of last access time.

OUTPUT:$ls-uronin.c roundr.c$-

8. mkdir command:

SYNTAX: $mkdir directory_name

DESCRIPTION: This command is used to create a new directory.

EXAMPLE: $mkdir temp$-

OUTPUT:$mkdir sadhu$-

Page 6: Basic Commands of Linux Os

9. cd command:

SYNTAX: $cd

DESCRIPTION: The cd command is used to change from the working directory to any other directory specified there is no options connected with this.

EXAMPLE: $cd jack$pwd/jack$-

OUTPUT:$cd sadhu$pwd/home/user10/sadhana/sadhu$-

10. rmdir command:

SYNTAX: $rmdir directory_name

DESCRIPTION: This command is used to remove a directory specified in the command line. It requires the specified directory to be empty before removing it.

EXAMPLE: $rmdir temp$-

OUTPUT:$rmdir sadhu$-

11. cat command:

SYNTAX: $cat[option][file]

DESCRIPTION: The cat command help us to list the contents of file we specify. If we do not specify the name of file , it takes input from the standard input.

OUTPUT:

Page 7: Basic Commands of Linux Os

$cat roundr.c#include<stdio.h>#include<conio.h>

void main(){int st[10],bt[10],wt[10],tat[10],n,tq;int i,count=0,swt=0,stat=0,temp,sq=0;float awt=0.0,atat=0.0;clrscr();printf("\nEnter number of processes:");scanf("%d",&n);getch();}

i)cat-s=>Supports warning about non-existent file.

EXAMPLE: $cat ryzTo be or not to be that is the question$-

OUTPUT:$cat-s roundr.c#include<stdio.h>#include<conio.h>void main(){int st[10],bt[10],wt[10],tat[10],n,tq;int i,count=0,swt=0,stat=0,temp,sq=0;float awt=0.0,atat=0.0;clrscr();printf("\nEnter number of processes:");scanf("%d",&n);getch();}$-

12. cp command:

SYNTAX: $cp file target

DESCRIPTION: The cp command is used to create copies of ordinary files.

EXAMPLE: $cp abc lkg

Page 8: Basic Commands of Linux Os

$-

OUTPUT:$cp roundr.c round.c$-

13. ln_link command:

SYNTAX: $ln firstname secondname

DESCRIPTION: The ln command is to establish an additional file name for the some ordinary file.

EXAMPLE: $ln red rose$-

OUTPUT:$roundr.c robin.c$-

14. mv command:

SYNTAX: $mv file target

DESCRIPTION: The mv command is used to rename and move ordinary and directory file. To do this we need both execuite and write permissions.

EXAMPLE: $mv old new$-

OUTPUT:$mv roundr.c jer.c$-

15. rm command:

SYNTAX: $rm [option] file

DESCRIPTION: The rm command is used to remove one or more files from a directory. This can be used to delete all files as directory.

Page 9: Basic Commands of Linux Os

OUTPUT:$rm sadhu.c$-

i)rm-i=>Asjs the user if he wants to delete the file mentioned. When this is combined with-r option, rm asks whether to examine each file in that directory rn-r recursively delete the entire contents of the directory itself.

EXAMPLE:$rm temp/mat$-

OUTPUT:$rm-i robin.crm: remove regular file ‘robin.c’? y$-

RESULT:

Thus the BASIC COMMANDS OF LINUX operating system is studied and the output is verified successfully.

Page 10: Basic Commands of Linux Os

PROGRAM CODING:

#include<stdlib.h>#include<stdio.h>#include<conio.h>void main(){ int burst[10],wait[10],turn[10],n,i; float a_wait,a_turn,t_wait=0,t_turn=0; clrscr(); printf("\t\t\t FIRST COME FIRST SERVE SCHEDULING \n"); printf("\n\n Enter the no of process:"); scanf("%d",&n); for(i=1;i<=n;i++) { printf("\n Enter the burst time for the process %d:",i); scanf("%d",&burst[i]); } wait[0]=0; turn[0]=0; burst[0]=0; for(i=1;i<=n;i++) { wait[i]=wait[i-1]+burst[i-1]; turn[i]=wait[i]+burst[i]; t_wait=t_wait+wait[i]; t_turn=t_turn+turn[i]; } a_wait=t_wait/n; a_turn=t_turn/n; printf("\n\nPROCESS "); printf("BURST TIME\t "); printf("WAITING TIME\t "); printf("TURN AROUND TIME\n") ; for(i=1;i<=n;i++) { printf("\n P%d",i); printf("\t\t%d",burst[i]); printf("\t\t%d",wait[i]); printf("\t\t%d",turn[i]); } printf("\n\n\nThe average waiting time:%f ms",a_wait); printf("\n\nThe average turn around time is: %f ms",a_turn);getch();}

Page 11: Basic Commands of Linux Os

OUTPUT:

FIRST COME FIRST SERVE SCHEDULING

Enter the number of process:4

Enter the burst time for the process 1 : 3Enter the burst time for the process 2 : 6Enter the burst time for the process 3 : 4Enter the burst time for the process 4 : 2

PROCESS BURST TIME WAITING TIME TURN AROUND TIME

P1 3 0 3 P2 6 3 9 P3 4 9 13 P4 2 13 15

The average waiting time: 6.200000 msThe average turn around time: 10.000000 ms

RESULT:

Thus the LINUX program for performing FIRST COME FIRST

SERVE SCHEDULING was executed and output was verified successfully.

Page 12: Basic Commands of Linux Os

PROGRAM CODING:

#include<stdlib.h>#include<stdio.h>#include<conio.h>void main(){ int pro[10],prior[10],burst[10],wait[10],turn[10],n,i,j,temp; float a_wait,a_turn,t_wait=0,t_turn=0; clrscr(); printf("\t\t\t PRIORITY SCHEDULING \n"); printf("\n\n Enter the no of process:"); scanf("%d",&n); for(i=1;i<=n;i++) { pro[i]=i; printf("\n Enter the burst time for the process%d:",i); scanf("%d",&burst[i]); printf("\n Enter the priority for the process%d:",i); scanf("%d",&prior[i]); } for(i=1;i<n;i++) { for(j=1;j<n;j++) { if(prior[j]>prior[j+1]) { temp=prior[j]; prior[j]=prior[j+1]; prior[j+1]=temp; temp=burst[j]; burst[j]=burst[j+1]; burst[j+1]=temp; temp=pro[j]; pro[j]=pro[j+1]; pro[j+1]=temp; } } } wait[0]=0; turn[0]=0; burst[0]=0;for(i=1;i<=n;i++) { wait[i]=wait[i-1]+burst[i-1]; turn[i]=wait[i]+burst[i];

Page 13: Basic Commands of Linux Os

t_wait=t_wait+wait[i]; t_turn=t_turn+turn[i]; } a_wait=t_wait/n; a_turn=t_turn/n; printf("\n\n PROCESS\t "); printf("PRIORITY\t"); printf("BURST TIME\t "); printf("WAITING TIME\t "); printf("TURN AROUND TIME \n") ; for(i=1;i<=n;i++) { printf("\n P%d",pro[i]); printf("\t\t%d",prior[i]); printf("\t\t%d",burst[i]); printf("\t\t%d",wait[i]); printf("\t\t%d",turn[i]); } printf("\n\n The average waiting time:%f ms",a_wait); printf("\n\n The average turn around time :%f ms",a_turn);getch();}

Page 14: Basic Commands of Linux Os

OUTPUT:

PRIORITY SCHEDULING

Enter the number of process:4

Enter the burst time for the process 1 : 3Enter the priority for the process 1 : 2Enter the burst time for the process 2 : 6Enter the priority for the process 2 : 4Enter the burst time for the process 3 : 4Enter the priority for the process 3 : 1Enter the burst time for the process 4 : 2Enter the priority for the process 4 : 3

PROCESS BURST TIME WAITING TIME TURN AROUND TIME

P1 3 4 7 P2 6 9 15 P3 4 0 4 P4 2 7 9

The average waiting time: 5.000000 msThe average turn around time: 8.700000 ms

RESULT:

Thus the LINUX program for performing PRIORITY

SCHEDULING was executed and output was verified successfully.

Page 15: Basic Commands of Linux Os

PROGRAM CODING:

#include<stdlib.h>#include<stdio.h>#include<conio.h>void main(){ int pro[10],p[20],bt[10],bt1[10],wt[10],ta[10],ta1[10],temp[10]; int n,n1,m=0,i,ts,z=1; float a_wt,a_ta,t_wt=0,t_ta=0; clrscr(); printf("\t\t\t ROUND ROBIN SCHEDULING \n"); printf("\n\nEnter the no of process:"); scanf("%d",&n); n1=n; ta1[0]=0; for(i=1;i<=n;i++) { pro[i]=i; printf("\n Enter the burst time for the process%d:",i); scanf("%d",&bt[i]); bt1[i]=bt[i]; temp[i]=0; wt[i]=0; ta[i]=0; } printf("\n\n Enter the time slice:"); scanf("%d",&ts); while(n1!=0) { for(i=1;i<=n;i++) { if(bt1[i]>ts) { m=m+ts; ta1[z]=m; bt1[i]=bt1[i]-ts; temp[i]++; p[z]=i; z++; } else if(bt1[i]!=0) { n1--; wt[i]=m-temp[i]*ts; t_wt=t_wt+wt[i]; m=m+bt1[i];

Page 16: Basic Commands of Linux Os

ta1[z]=m; ta[i]=m; t_ta=t_ta+ta[i]; bt1[i]=0; p[z]=i; z++; } } } a_wt=t_wt/n; a_ta=t_ta/n; printf("\n\nPROCESS "); printf("BURST TIME\t "); printf("WAITING TIME\t "); printf("TURN AROUND TIME\n") ; for(i=1;i<=n;i++) { printf("\n P%d",pro[i]); printf("\t\t%d",bt[i]); printf("\t\t%d",wt[i]); printf("\t\t%d",ta[i]); } printf("\n\n\nThe average waiting time:%f ms",a_wt); printf("\n\nThe average turn around time is:%f ms",a_ta);getch();}

Page 17: Basic Commands of Linux Os

OUTPUT:

ROUND ROBIN SCHEDULING

Enter the number of process:4

Process:1Enter the burst time for the process : 3Process:2Enter the burst time for the process : 6Process:3Enter the burst time for the process : 4Process:4Enter the burst time for the process : 2

Enter the time slice value:2

PROCESS BURST TIME WAITING TIME TURN AROUND TIME P1 3 6 9 P2 6 9 15 P3 4 9 13 P4 2 6 8

The average waiting time: 7.500000 msThe average turn around time: 11.250000 ms

RESULT:

Thus the LINUX program for performing ROUND ROBIN

SCHEDULING was executed and output was verified successfully.

Page 18: Basic Commands of Linux Os

PROGRAM CODING:

#include<stdlib.h>#include<stdio.h>#include<conio.h>void main(){ int pro[10],burst[10],wait[10],turn[10],n,i,j,temp; float a_wait,a_turn,t_wait=0,t_turn=0; clrscr(); printf("\t\t\t SHORTEST JOB FIRST SCHEDULING\n"); printf("\n\nEnter the no. of process:"); scanf("%d",&n); for(i=1;i<=n;i++) { pro[i]=i; printf("\n Enter the burst time for the process%d:",i); scanf("%d",&burst[i]); } for(i=1;i<n;i++) { for(j=1;j<n;j++) { if(burst[j]>burst[j+1]) { temp=burst[j]; burst[j]=burst[j+1]; burst[j+1]=temp; temp=pro[j]; pro[j]=pro[j+1]; pro[j+1]=temp; } } } wait[0]=0; turn[0]=0; burst[0]=0; for(i=1;i<=n;i++) { wait[i]=wait[i-1]+burst[i-1]; turn[i]=wait[i]+burst[i]; t_wait=t_wait+wait[i]; t_turn=t_turn+turn[i]; }

Page 19: Basic Commands of Linux Os

a_wait=t_wait/n; a_turn=t_turn/n; printf("\n\nPROCESS "); printf("BURST TIME\t "); printf("WAITING TIME\t "); printf("TURN AROUND TIME\n") ; for(i=1;i<=n;i++) { printf("\n P%d",pro[i]); printf("\t\t%d",burst[i]); printf("\t\t%d",wait[i]); printf("\t\t%d",turn[i]); } printf("\n\n\n The average waiting time:%f ms",a_wait); printf("\n\n The average turn around time is: %f ms",a_turn);getch();}

Page 20: Basic Commands of Linux Os

OUTPUT:

SHORTEST JOB SCHEDULING

Enter the number of process: 4

Process:1Enter burst time for the process:3Process:2Enter burst time for the process:6Process:3Enter burst time for the process:4Process:4Enter burst time for the process:2

PROCESS BURST TIME WAITING TIME TURN AROUND TIME

P1 3 2 5 P2 6 9 15 P3 4 5 9 P4 2 0 2

The average waiting time: 4.000000 msThe average turn around time: 7.700000 ms

RESULT:

Thus the LINUX program for performing SHORTEST JOB

SCHEDULING was executed and output was verified successfully.

Page 21: Basic Commands of Linux Os

PROGRAM CODING:

#include<stdio.h>#include<stdlib.h>#include<conio.h>void main(){int pid;pid=fork();printf(“\n Hello \n”);if(pid==0){wait[20];printf(“\n \t \t FORK AND WAIT SYSTEM CALL \n \n”);printf(“ \n Child process is executed”);printf(“ \n The Id number of child process is %d \n”,getpid[]);}else if(pid>0){wait[50];printf(“\n Parent process is executed”);printf(“\n The Id number of parent process is %d \n”, getpid[]);}}

Page 22: Basic Commands of Linux Os

OUTPUT:

FORK AND WAIT SYSTEM CALL

HelloChild process is executedThe Id number of child process is 3450

HelloParent process is executedThe Id number of parent process is 3449

RESULT:

Thus the LINUX program for performing FORK AND WAIT SYSTEM

CALL was executed and output was verified successfully.

Page 23: Basic Commands of Linux Os

PROGRAM CODING:

#include<stdio.h>#include<stdlib.h>#include<conio.h>void main(){int pid;pid=fork();printf(“\n Hello \n”);if(pid==0){printf(“\n \t \t CREATE A CHILD PROCESS USING SLEEP \t”);printf(“\n Child process is executed \n”);printf(“\n ID number of the child process is %d \n”,getpid[]);}else if(pid>0){sleep(20);printf(“\n Parent process is executed”);printf(“\n The ID number of parent process is %d \n” getpid[]);}}

Page 24: Basic Commands of Linux Os

OUTPUT:

CREATE A CHILD PROCESS USING SLEEP

HelloChild process is executedThe Id number of child process is 3393

HelloParent process is executedThe Id number of parent process is 3392

RESULT:

Thus the LINUX program for performing CREATE A CHILD PROCESS

USING SLEEP was executed and output was verified successfully.

Page 25: Basic Commands of Linux Os

PROGRAM CODING:

#include<stdio.h>#include<stdlib.h>#include<conio.h>{int i,a;for(i=1;i<=3;i++){printf(“\n \t\t EXIT SYSTEM CALL \t \n”);printf(“Enter the choice(1=exit/2=continue):”);scanf(“%d”,&a);if(a==1){printf(“ You choose to exit”);exit(0);}else{printf(“You choose to continue”);}}}

Page 26: Basic Commands of Linux Os

OUTPUT:

EXIT SYSTEM CALL

Enter the choice(1=exit/2=continue):2

You choose to continue

Enter the choice(1=exit/2=continue):1

You choose to exit

RESULT:

Thus the LINUX program for performing EXIT SYSTEM CALL was

executed and output was verified successfully.

Page 27: Basic Commands of Linux Os

PROGRAM CODING:

#include<stdlib.h>#include<stdio.h>#include<dirent.h>main(int args,char,argv[]){Dir*dirname;struct dirent*preaddr;Dirname=opendir(argv[1]);while(1){preaddr=readdir(dirname);if(preaddr==NULL){closedir(dirname);exit(0);}printf(“\n \t \t DIRECTORY SYSTEM CALL \n \n”);printf(“\n\n Found Entry %s %s,:” argv[1]);preaddr dirname;}}

Page 28: Basic Commands of Linux Os

OUTPUT:

DIRECTORY SYSTEM CALL

Found entry sadhana

Found entry sadhana

RESULT:

Thus the LINUX program for performing DIRECTORY SYSTEM CALL

was executed and output was verified successfully.

Page 29: Basic Commands of Linux Os

PROGRAM CODING:

#include<stdlib.h>#include<stdio.h>#define size 10main (int argc,char *argr[ ]){int i,n,rd,wd,r,fd;char s[size];printf(“\n \t \t FILE SYSTEM CALL USING OPEN AND CLOSE \n “);if(argc<3){printf(“\n Illegal input”);exit(1);}fd=open (argr[1],0);if(fd= =-1){printf(“\n Error occurred”);exit(1);}cr=create(argr[2],9999);if(cr= = -1){printf(“\n File not created”);exit(1);}rd=read(fd,s,size);while(rd>0){wd=write(cr,s,size);rd=read(fd,s,size);}close(fd);close(cr);printf(“\n File completed”);}

Page 30: Basic Commands of Linux Os

OUTPUT:

FILE SYSTEM CALL USING OPEN AND CLOSE

File completed

How are you

RESULT:

Thus the LINUX program for performing FILE SYSTEM CALL USING

OPEN AND CLOSE was executed and output was verified successfully.

Page 31: Basic Commands of Linux Os

PROGRAM CODING:

#include<stdio.h>#include<stdlib.h>main(){char *one[3];one[0]=”1s”;one[1]=”-1”;one[2]=(char *)0;exec(“\bin\1s”,one[0],one[1],one[2]);}

Page 32: Basic Commands of Linux Os

OUTPUT:

SIMULATION OF LS COMMAND

Total 44

-rwxrwxr-x1 iiii to 8 iiii to 8 11570 jan 100:54 a.out

-rw-rw-r-1 iiii to 8 iiii to 8 709 jan 11999 fc.c

RESULT:

Thus the LINUX program for performing SIMULATION OF LS

COMMAND was executed and output was verified successfully.

Page 33: Basic Commands of Linux Os

PROGRAM CODING:

#include<stdlib.h>#include<stdio.h>#include<string.h>void main(){file *fp;char mstr[500],cstro[50],c,*ptr,fname[20];int i=0,count=0;fflush(stdin)printf(“\n \t\t SIMULATION OF GREP COMMAND\n”);printf(“\n Enter the string and file name:”);scanf(“%S%S”,cstr,frame);fp=fopen(fname,”r”);while((c=getc(fp))!=EOF){mstr[i]=c;if(c==’\n’){mstr[i]=’\0’;ptr=strstr(mstr,cstr);if(ptr!=NULL){printf(“\n [%s]found in %s\n”,str,mstr);count++;}i=0;}

++i;}if(count==0){printf(“\n No occurrence found”);else{printf(“\n Occurrence found:%d”,count);}}

Page 34: Basic Commands of Linux Os

OUTPUT:

SIMULATION OF GREP COMMAND

Enter the string and file name:

Hello file1.txt

[hello] found in

Hello world

How are you

RESULT:

Thus the LINUX program for performing SIMULATION OF GREP

COMMAND was executed and output was verified successfully.

Page 35: Basic Commands of Linux Os

PROGRAM CODING:

#include<stdio.h>#include<conio.h>#include<math.h>#include<stdlib.h>void write(){int r=20,I;for(i=1;i<=20;i++){r--;return;}}void main(){int n,s[20],p[20],rd[50];int count=0,I,a;clrscr();printf(“\n\t\t READER-WRITER PROBLEM”);printf(“\n Enter the number of process:”);scanf(“%d”,&n);for(i=1;i<=n;i++){printf(“\n Process-ID:”);scanf(“%D”,&p[i]);printf(“\n Status(w=1/r=0):”);scanf(“%d”,&s[i]);}printf(“\n Process-ID\ttsatus”);for(i=1;i<=n;i++)printf(“\n%d\t\t%d”,p[i],s[i]);for(i=1;i<=n;i++)rd[i]=0;for(i=1;i<=n;i++){if(s[i]==1){printf(“\n Process%d is writing wait”,p[i]);

Page 36: Basic Commands of Linux Os

count++;while(count!=0){write();count--;}printf(“\n\t Writing is completed:”);}else{fd[i]=p[i];}for(i=1;i<=n;i++){if(rd[i]!=0){printf(“\n There is no waiting process:”);printf(“\n Reader List”);for(i=1;i<=n;i++){for(i=0;i<=n;i++)if(rd[i]!=0)printf(“\n Process %d”,rd[i]);}}}if(count!=1)printf(“\n\t There is no reading process:”);getch();}

Page 37: Basic Commands of Linux Os

OUTPUT:

READER-WRITER PROBLEM

Enter the number of process:4Process ID:1Status(w=1/r=0)=1Process ID:2Status(w=1/r=0)=0Process ID:3Status(w=1/r=0)=1Process ID:4Status(w=1/r=0)=0

Process ID status1 12 03 14 0

Process1 is writing waitProcess3 is writing waitWriting is completedThere is no waiting process

Reader ListProcess2Process4There is no reading process

RESULT:

Thus the LINUX program for performing READER –

WRITER PROBLEM was executed and output was verified successfully.

Page 38: Basic Commands of Linux Os

PROGRAM CODING:

#include<stdlib.h>#include<stdio.h>#include<conio.h>int n=5,i,j,f=0;char s,c;void consumer(){ printf("\nThe consumer has consumed %d\n",f); n=n-1; getch();}void producer(){ n=n+1; f=f+1; printf("\nThe producer has produced %d\n",f); getch(); consumer();}void prod(){ n=n+1; f=f+1; printf("\nThe producer has produced %d\n",f); getch();}void cons(){ printf("\nThe consumer has consumed %d\n",f+1); n=n-1; getch(); prod();}void main(){ i=n; clrscr(); printf("\t\t\tPRODUCER CONSUMER PROBLEM\n\n"); printf("\nEnter the state buffer(f/e):"); scanf("%c",&s);

Page 39: Basic Commands of Linux Os

if(s=='e') n=0; else n=1; if(s=='e') { do { if(n==0) producer(); if(n==1) consumer(); }while(f<i); } if(s=='f') { do { if(n==0) prod(); if(n==1) cons(); }while(f<i); } printf("\nExit"); getch();}

Page 40: Basic Commands of Linux Os

OUTPUT:

PRODUCER CONSUMER PROBLEM

Enter the state buffer(f/e):e

The producer has produced 1

The consumer has consumed 1

The producer has produced 2

The consumer has consumed 2

The producer has produced 3

The consumer has consumed 3

The producer has produced 4

The consumer has consumed 4

The producer has produced 5

The consumer has consumed 5

Exit

RESULT:

Thus the LINUX program to performing PRODUCER –

CONSUMER PROBLEM was executed and output was verified successfully.

Page 41: Basic Commands of Linux Os

PROGRAM CODING:

#include<stdlib.h>#include<stdio.h>#include<conio.h>int st[10],temp[10];void eating(int x,int y,int z);void main(){ int i,n=5; clrscr(); printf("\t\t\tDINING PHILOSOPHERS PROBLEM\n\n"); printf("MAIN MENU"); printf("\n*********\n"); printf("1.Hungry\n"); printf("2.Eating\n"); printf("3.Thinking\n\n"); for(i=1;i<=n;i++) { printf("Enter the status of the philosopher %d:",i); scanf("%d",&st[i]); } printf("\n\n"); for(i=1;i<=n;i++) { if(st[i]==3) printf("\nPhilosopher %d is thinking\n",i); if(st[i]==2) printf("\nPhilosopher %d is eating\n",i); if(st[i]==1) { if(i==1) eating(i+1,n,i); else if(i==n) eating(1,i-1,i); else eating(i+1,i-1,i); } } getch();}void eating(int x,int y,int z){ if(temp[x]==1) {

Page 42: Basic Commands of Linux Os

printf("\nPhilosopher %d is hungry and can not eat ",z); printf("since %d has possibility of eating\n",x); return; } if((st[x]!=2)&&(st[y]!=2)) { printf("\nPhilosopher %d is hungry and eating\n",z); st[z]=2; temp[z]=1; } if((st[x]==2)&&(st[y]==2)) { printf("\nPhilosopher %d is hungry but not eating since ",z); printf("philosophers %d and %d are eating\n",x,y); return; } if((st[x]==2)) { printf("\nPhilosopher %d is hungry but not eating since ",z); printf("philosopher %d is eating\n",x); } if((st[y]==2)) { printf("\nPhilosopher %d is hungry but not eating since ",z); printf("philosopher %d is eating\n",y); }}

Page 43: Basic Commands of Linux Os

OUTPUT:

DINING-PHILOSOPHERS PROBLEM

MAIN MENU************1.Hungry2.Eating3.Thinking

Enter the status of the philosopher 1:1Enter the status of the philosopher 2:2Enter the status of the philosopher 3:3Enter the status of the philosopher 4:1Enter the status of the philosopher 5:2

Philosopher 1 is hungry but not eating since philosophers 2 and 5 are eatingPhilosopher 2 is eating Philosopher 3 is thinkingPhilosopher 4 is hungry but not eating since philosopher 5 is eatingPhilosopher 5 is eating

RESULT:

Thus the LINUX program for performing DINING –

PHILOSOPHER PROBLEM was executed and output was verified successfully.

Page 44: Basic Commands of Linux Os

PROGRAM CODING:

#include<stdio.h>#include<iostream.h>#include<conio.h>#include<malloc.h>#include<stdlib.h>struct frame{int frameno;int usagerate;struct frame*next;struct frame*prev;}*head,*tail,*n,*t;int max;int m=0;void fifo();void lru();void allocateframe(int num){int i,count;int ch;for(i=0;i<num && m<max;i++){if(m==max)break;n=(struct frame*)malloc(sizeof (struct frame));m++;n->frameno=m;printf("\n Frame no=%d\n”,n->frameno);printf("Enter the frame usage rate:\n");scanf("%d",&n->usagerate);if(head==NULL){head=n;tail=n;head->prev=NULL;tail->next=NULL;}

Page 45: Basic Commands of Linux Os

else{tail->next=n;n->prev=tail;tail=n;tail->next=NULL;}count=i+1;printf("count=%d \n",count);}printf("Frame details \n");for(t=head;t!=NULL;t=t->next)printf("%d\t\t%d\n",t->frameno,t->usagerate);if(m==max){printf("Page frame cannot be added further\n");printf("Need replacement \n");do{label:printf("Specify one of the following options:\n");printf("1.FIFO replacement \n");printf("2.LRU replacement\n");scanf("%d",&ch);switch(ch){case 1:fifo();break;case 2:lru();break;default:printf("Invalid choice\n');goto label;}count++;}while(count<num);}printf("Frame details \n");for(t=head;t!=NULL;t=t->next)printf("%d\t\t%d\n,"t->frameno,t->usagerate);}

Page 46: Basic Commands of Linux Os

void fifo(){struct frame*temp;if(head!=NULL){temp=head;head=head->next;head->prev=NULL;free(temp);n=(struct frame*)malloc(sizeof(struct frame));m++;n->frameno=m;printf("Frame number %d\n",n->frameno);printf("Enter usage rate of the frame:\n");scanf("%d",&n->usagerate);tail->next=n;n->prev=tail;tail=n;tail->next=NULL;printf("Frame details\n");for(t=head;t!=NULL;t=t->next)printf("%d\t%d\n",t->frameno,t->usagerate);}elseprintf("FIFO replacement not possible\n");return;}void lru(){int pos,min=head->usagerate;struct frame*temp;if(head!=NULL){for(t=head;t!=null;t=t->next){if((min)t->usagerate)min=t->usagerate;}printf(“Minimum =%d \n “,min);for(t=head;t!=NULL;t=t->next){if(min==t->usagerate){if(t==head)

Page 47: Basic Commands of Linux Os

{printf(“Deleted frame =%d \n”,t->frameno);temp=head;head=head->next;head->prev =NULL;free(temp);}else if(t==tail){printf(“Deleted frame =%d \n”,t->frameno);temp=tail;tail=tail->prev;tail->next =NULL;free(temp);}else{printf(“Deleted frame =%d \n”,t->frameno);temp=t;t->prev->next=t->next;t->next->prev=t->prev;free(temp);}}}n=(struct frame*)malloc(sizeof(struct frame));m++;n->frameno=m;printf("Frame number %d\n",n->frameno);printf("Enter usage rate of the frame:\n");scanf("%d",&n->usagerate);tail->next=n;n->next=tail;tail=n;tail->next=NULL;printf("Frame details\n");for(t=head;t!=NULL;t=t->next)printf("%d\t%d\n",t->frameno,t->usagerate);}return;}void main(){int choice,ans,number,max;

Page 48: Basic Commands of Linux Os

clrscr();printf(“\n Enter the maximum number of frames:”);scanf(“%d”,&max);do{label;printf(“\n Specify anyone of the following choice \n”);printf(“\n 1. Allocate Frame”);printf(“\n 2. FIFO Replacement”);printf(“\n 3. LRU Replacement”);printf(“\n Enter your choice:”);scanf(“%d”,&choice);switch(choice){case 1:printf(“\n Enter the number of frames added:”);scanf(“%d”,&number);allocate frame(number);break;case 2:fifo();break;case 3:lru();break;default:printf(“\n Invalid choice”);goto label1;}printf(“\n Do you want to continue (y/n) :”);fflush(stdin);scanf(“%c”,&ans);}while(ans == ‘y’ || ans == ‘Y’);getch();}

Page 49: Basic Commands of Linux Os

OUTPUT:

PAGE REPLACEMENT

Enter the maximum number of frames:3

Specify anyone of the following choice1. Allocate Frame2. FIFO Replacement3. LRU ReplacementEnter your choice:1

Enter the number of frames added:3Frame number :1Enter usage rate of the frame:1Count:1Frame number :2Enter usage rate of the frame:2Count:2Frame number :3Enter usage rate of the frame:3Count:3

Frame details1 62 53 7

Do you want to continue (y/n):ySpecify anyone of the following choice

1. Allocate Frame2. FIFO Replacement3. LRU ReplacementEnter your choice:2

Frame number :4Enter usage rate of the frame:4

Page 50: Basic Commands of Linux Os

Frame details2 53 74 3

Do you want to continue (y/n):ySpecify anyone of the following choice

1. Allocate Frame2. FIFO Replacement3. LRU ReplacementEnter your choice:3

Minimum:3Deleted frame: 4Frame number :5Enter usage rate of the frame:2Frame details2 53 75 2

Do you want to continue (y/n):n

RESULT:

Thus the LINUX program for performing PAGING REPLACEMENT

was executed and output was verified successfully.

Page 51: Basic Commands of Linux Os

PROGRAM CODING:

#include<stdlib.h>#include<stdio.h>#include<conio.h>#define MAX 50struct blockdetails{int bno;int mspace;int ospace;}block[MAX];struct jobdetails{int jno;int space;int status;}job[MAX];int m,n;void allocateablock(){int i;printf(“\n Enter the total number of blocks:”);scanf(“%d”,&n);for(i=0;i<n;i++){block[i].bno=i+1;printf(“\n Block number:%d \n”,block[i].bno);printf(“\n Enter the block space:”);scanf(“%d”,&block[i].ospace);}}void allocatejob(){int i;printf(“\n Enter the total number of jobs:”);scanf(“%d”,&m);for(i=0;i<m;i++){

Page 52: Basic Commands of Linux Os

job[i].jno=i+1;printf(“\n Job number: %d”,job[i].jno);printf(“\n Enter the job space:”);scanf(“%d”,&job[i].space);job[i].status=0;}clrscr();printf(“\n Block number \t Block space”);for(i=0;i<n;i++)printf(“\n %d \t %d”,block[i].bno,block[i].ospace);printf(“\n Job number \t Job space”);for(i=0;i<n;i++)printf(“\n %d \t %d”,job[i].jno,job[i].space);}void firstfit(){int i,j;for(i=0;i<n;i++)block[i].mspace=block[i].ospace;for(j=0;j<m;j++){for(i=0;i<n;i++){if(job[j].space<=block[i].mspace){printf(“\n Job %d is allocated in block %d”,job[j].jno,block[i].bno);job[j].status=1;block[i].mspace=block[i].mspace-job[j].space;break;}}}for(j=0;j<m;j++){if(job[j].status == 0)printf(“\n Job %d is not allocated in any block %d”,job[j].jno);}}void bestfit(){int i,j;struct blockdetails temp;for(i=0;i<n-1;i++){

Page 53: Basic Commands of Linux Os

for(j=i+1;j<n;j++){if(block[i].ospace>block[j].ospace){temp=block[i];block[i]=block[j];block[j]=temp;}}}void worstfit(){int i,j;struct blockdetails temp;for(i=0;i<n-1;i++){for(j=i+1;j<n;j++){if(block[i].ospace<block[j].ospace){temp=block[i];block[i]=block[j];block[j]=temp;}}}firstfit();}void main(){clrscr();allocateblock();allocatejob();printf(“\n First fit”);firstfit();printf(“\n Best fit”);bestfit();printf(“\n Worst fit”);worstfit();getch();}

Page 54: Basic Commands of Linux Os

OUTPUT:

STORAGE PLACEMENT

Enter the total number of blocks:3

Block number:1Enter the block space:6Block number:2Enter the block space:4Block number:3Enter the block space:8

Enter the total number of jobs:3

Job number: 1Enter the job space:4Job number: 2Enter the job space:6Job number: 3Enter the job space:8

Block number Block space

1 62 43 8

Job number Job space

1 42 63 8

Page 55: Basic Commands of Linux Os

First fit

Job 1 is allocated in Block 1Job 2 is allocated in Block 3Job 3 is not allocated in any Block

Best fit

Job 1 is allocated in Block 2Job 2 is allocated in Block 1Job 3 is allocated in Block 3

Worst fit

Job 1 is allocated in Block 3Job 2 is allocated in Block 1Job 3 is not allocated in any Block

RESULT:

Thus the LINUX program for performing STORAGE

REPLACEMENT was executed and output was verified successfully.

Page 56: Basic Commands of Linux Os

DISK SCHEDULING ALGORITHMS

FIRST COME FIRST SERVE SCHEDULING

AIM:

To write a LINUX program to perform FIRST COME FIRST SERVE using scheduling algorithm.

ALGORITHM:

Step1: Start the program.

Step2:Get the number of process.

Step3: Get the processing or execution time(Burst time).

Step4: Calculate average waiting time

Average waiting time=sum of waiting time/no of process

Step5: Calculate average turn around time

Average turn around time=sum of turn around time/no of process

Step6:Display the average waiting time and turn around time.

Step7:Stop the program.

Page 57: Basic Commands of Linux Os

SHORTEST JOB FIRST SCHEDULING

AIM:

To write a LINUX program to perform SHORTEST JOB FIRST SCHEDULING using scheduling algorithm.

ALGORITHM:

Step1: Start the program.

Step2:Get the number of process.

Step3:Get the processing or execution time(Burst time).

Step4:Sort the execution time.

Step5:Calculate average waiting time

Average waiting time=sum of waiting time/no of process

Step6: Calculate average turn around time

Average turn around time=sum of turn around time/no of process

Step7: Display the average waiting time and turn around time.

Step8:Stop the program.

Page 58: Basic Commands of Linux Os

PRIORITY SCHEDULING

AIM:

To write a LINUX program to perform PRIORITY SCHEDULING using scheduling algorithm.

ALGORITHM:

Step1: Start the program.

Step2:Get the number of process.

Step3:Get the processing or execution time(Burst time).

Step4: Sort the execution time on priority basis.

Step5: Calculate average waiting time

Average waiting time=sum of waiting time/no of process

Step6: Calculate average turn around time

Average turn around time=sum of turn around time/no of process

Step7: Display the average waiting time and turn around time.

Step8: Stop the program.

Page 59: Basic Commands of Linux Os

ROUND ROBIN SCHEDULING

AIM:

To write a LINUX program to perform ROUND ROBIN SCHEDULING using scheduling algorithm.

ALGORTIHM:

Step1: Start the program.

Step2: Get the execution time of number of process and the time slice/quantum time.

Step3: Perform step 4-6 until the completion of all the numbers of process.

Step4: If the process needs another cycle for connection to go to step5 else step6.

Step5:a) Calculate the remaining execution time and print the current status.

b) Calculate total time speed.

c) Calculate the individual completion time.

Step6:a) Print the process is completed.

b) Calculate the waiting time waiting time of ith process= total time spent so for its individual completion time in previous cycle.

Step7:Print the individual waiting time of n process and find the total waiting time.

Step8:Print the average waiting time and print the total and average waiting time.

Step9: Stop the program.

Page 60: Basic Commands of Linux Os

SYNCHRONIZATION PROBLEM USING SEMAPHORE

PRODUCER CONSUMER PROBLEM

AIM:

To write a LINUX program to implement the concept of producer consumer problem.

ALGORITHM:

Step1: Start the program.

Step2: The status of the buffer is checked.

Step3: The buffer is incremented for producer and decrement for consumer.

Step4: Both the producer and consumer cannot utilize the buffer. The wait signal take care the process.

Step5: Stop the program.

Page 61: Basic Commands of Linux Os

DINING PHILOSOPHERS PROBLEM

AIM:

To write a LINUX program to implement the concept of producer consumer problem.

ALGORITHM:

Step1:Start the program.

Step2: The status of the plate and chop stick is checked.

Step3: If the plate and the adjacent chop stick is free philosopher sit otherwise the next plate is checked.

Step4: While the next philosopher is seated the previous philosopher seated plate is free and the step3 is continued until all philosopher finishes.

Step5: Stop the program .