Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
284 views

Basic Commands of Linux Os

Who command gives us the detail of the users who have logged into the LINUX system currently. 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 umanP command name.

Uploaded by

hbhdgyug
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
284 views

Basic Commands of Linux Os

Who command gives us the detail of the users who have logged into the LINUX system currently. 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 umanP command name.

Uploaded by

hbhdgyug
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 61

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 xyz tty4 Sep 26 11:30 lkg tty9 Sep 26 11:48 $OUTPUT: $who user 01 pts/1 2011-07-11 11:11 user 07 pts/6 2011-07-11 11:11 user 15 pts/4 2011-07-11 11:11

user 10 pts/2 2011-07-11 11:11 user 11 pts/5 2011-07-11 11:11 user 05 pts/3 2011-07-11 11:11 user 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 systems name for the connection. EXAMPLE: $who am i user1 ttya Sep 26 12:20 $-

OUTPUT: $ who am i user10 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: $man What manual page do you want? $5.head and tail command: SYNTAX 1: $head[-count][filename]

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 wordlist pen bat ink eraser $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.c stat=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

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: $ls abc-c xyz lkg $OUTPUT: $ls roundr.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-l total 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).

OUTPUT: $ls-t roundr.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-u ronin.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 $-

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:

$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 ryz To 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

$-

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 $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.

robin.c

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.c rm: remove regular file robin.c? y $-

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

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(); }

OUTPUT:

FIRST COME FIRST SERVE SCHEDULING Enter the number of process:4 Enter the burst time for the process 1 : 3 Enter the burst time for the process 2 : 6 Enter the burst time for the process 3 : 4 Enter the burst time for the process 4 : 2

PROCESS P1 P2 P3 P4

BURST TIME 3 6 4 2

WAITING TIME 0 3 9 13

TURN AROUND TIME 3 9 13 15

The average waiting time: 6.200000 ms The 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.

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];

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(); }

OUTPUT: PRIORITY SCHEDULING

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

PROCESS P1 P2 P3 P4

BURST TIME 3 6 4 2

WAITING TIME 4 9 0 7

TURN AROUND TIME 7 15 4 9

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

RESULT: Thus the LINUX program for performing PRIORITY SCHEDULING was executed and output was verified successfully.

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];

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(); }

OUTPUT:

ROUND ROBIN SCHEDULING

Enter the number of process:4 Process:1 Enter the burst time for the process : 3 Process:2 Enter the burst time for the process : 6 Process:3 Enter the burst time for the process : 4 Process:4 Enter the burst time for the process : 2 Enter the time slice value:2 PROCESS P1 P2 P3 P4 BURST TIME 3 6 4 2 WAITING TIME 6 9 9 6 TURN AROUND TIME 9 15 13 8

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

RESULT: Thus the LINUX program for performing ROUND ROBIN SCHEDULING was executed and output was verified successfully.

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]; }

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(); }

OUTPUT:

SHORTEST JOB SCHEDULING

Enter the number of process: 4 Process:1 Enter burst time for the process:3 Process:2 Enter burst time for the process:6 Process:3 Enter burst time for the process:4 Process:4 Enter burst time for the process:2 PROCESS P1 P2 P3 P4 BURST TIME 3 6 4 2 WAITING TIME 2 9 5 0 TURN AROUND TIME 5 15 9 2

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

RESULT: Thus the LINUX program for performing SHORTEST JOB SCHEDULING was executed and output was verified successfully.

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[]); } }

OUTPUT: FORK AND WAIT SYSTEM CALL Hello Child process is executed The Id number of child process is 3450

Hello Parent process is executed The 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.

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[]); } }

OUTPUT: CREATE A CHILD PROCESS USING SLEEP Hello Child process is executed The Id number of child process is 3393

Hello Parent process is executed The 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.

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); } } }

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.

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; } }

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.

PROGRAM CODING:

#include<stdlib.h> #include<stdio.h> #define size 10 main (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); }

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.

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]); }

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.

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); } }

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.

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]);

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(); }

OUTPUT: READER-WRITER PROBLEM Enter the number of process:4 Process ID:1 Status(w=1/r=0)=1 Process ID:2 Status(w=1/r=0)=0 Process ID:3 Status(w=1/r=0)=1 Process ID:4 Status(w=1/r=0)=0 Process ID 1 2 3 4 status 1 0 1 0

Process 1 is writing wait Process 3 is writing wait Writing is completed There is no waiting process Reader List Process 2 Process 4 There is no reading process

RESULT: Thus the LINUX program for performing READER WRITER PROBLEM was executed and output was verified successfully.

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);

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(); }

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.

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) {

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); } }

OUTPUT:

DINING-PHILOSOPHERS PROBLEM

MAIN MENU ************ 1.Hungry 2.Eating 3.Thinking Enter the status of the philosopher 1:1 Enter the status of the philosopher 2:2 Enter the status of the philosopher 3:3 Enter the status of the philosopher 4:1 Enter the status of the philosopher 5:2 Philosopher 1 is hungry but not eating since philosophers 2 and 5 are eating Philosopher 2 is eating Philosopher 3 is thinking Philosopher 4 is hungry but not eating since philosopher 5 is eating Philosopher 5 is eating

RESULT:

Thus the LINUX program for performing DINING PHILOSOPHER PROBLEM was executed and output was verified successfully.

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; }

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); }

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); } else printf("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)

{ 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;

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(); }

OUTPUT: PAGE REPLACEMENT

Enter the maximum number of frames:3 Specify anyone of the following choice 1. Allocate Frame 2. FIFO Replacement 3. LRU Replacement Enter your choice:1 Enter the number of frames added:3 Frame number :1 Enter usage rate of the frame:1 Count:1 Frame number :2 Enter usage rate of the frame:2 Count:2 Frame number :3 Enter usage rate of the frame:3 Count:3 Frame details 1 6 2 5 3 7 Do you want to continue (y/n):y Specify anyone of the following choice 1. Allocate Frame 2. FIFO Replacement 3. LRU Replacement Enter your choice:2 Frame number :4 Enter usage rate of the frame:4

Frame details 2 5 3 7 4 3 Do you want to continue (y/n):y Specify anyone of the following choice 1. Allocate Frame 2. FIFO Replacement 3. LRU Replacement Enter your choice:3 Minimum:3 Deleted frame: 4 Frame number :5 Enter usage rate of the frame:2 Frame details 2 5 3 7 5 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.

PROGRAM CODING:

#include<stdlib.h> #include<stdio.h> #include<conio.h> #define MAX 50 struct 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++) {

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++) {

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(); }

OUTPUT: STORAGE PLACEMENT

Enter the total number of blocks:3 Block number:1 Enter the block space:6 Block number:2 Enter the block space:4 Block number:3 Enter the block space:8 Enter the total number of jobs:3 Job number: 1 Enter the job space:4 Job number: 2 Enter the job space:6 Job number: 3 Enter the job space:8

Block number 1 2 3

Block space 6 4 8

Job number 1 2 3

Job space 4 6 8

First fit Job 1 is allocated in Block 1 Job 2 is allocated in Block 3 Job 3 is not allocated in any Block Best fit Job 1 is allocated in Block 2 Job 2 is allocated in Block 1 Job 3 is allocated in Block 3 Worst fit Job 1 is allocated in Block 3 Job 2 is allocated in Block 1 Job 3 is not allocated in any Block

RESULT:

Thus the LINUX program for performing STORAGE REPLACEMENT was executed and output was verified successfully.

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.

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.

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.

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.

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.

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 .

You might also like