3. SHELL PROGRAMMING 3.1.

SQUARE OF A GIVEN NUMBER

PROGRAM

echo "Enter a number:" read n sq=` expr \$n \* \$n` echo "Answer is: \$sq"

OUTPUT

Enter a number: 4 Answer is: 16 Enter a number: 5 Answer is: 25

3.2. CHECKING EQUALITY OF TWO NUMBERS

PROGRAM

echo "Enter a:" read a echo "enter b:" read b if [ \$a -eq \$b ] then echo "The given numbers are equal" else echo "The given numbers are not equal" fi

OUTPUT

Enter a: 20 Enter b: 30 The given numbers are not equal Enter a: 15 Enter b: 15 The given numbers are equal

3.3. GREATEST OF TWO GIVEN NUMBERS

PROGRAM

echo "Enter a:" read a echo "enter b:" read b if [ \$a -gt \$b ] then echo "a is greater" else echo "b is greater" fi

OUTPUT

Enter a: 24 Enter b: 12 a is greater Enter a: 15 Enter b: 20 b is greater

3.4. GREATEST OF THREE NUMBERS

PROGRAM

echo "Enter a:" read a echo "enter b:" read b echo "Enter c:" read c if [ \$a -gt \$b -a \$a -gt \$c ] then echo "a is greater" else if [ \$b -gt \$c -a \$b -gt \$a ] echo "b is greater" else echo "c is greater" fi fi

OUTPUT

Enter a:24 Enter b:12 Enter c:35 c is greater

3.5. BASIC CALCULATOR
PROGRAM

echo "Enter two numbers" read a b echo "Menu:1.add 2.sub 3.mul 4.div" echo "Enter your option:" read op ans=0 case '\$op' in 1) ans=` expr \$a \+ \$b` expr "\$a+\$b=\$ans";; 2) ans=` expr \$a \- \$b` expr "\$a-\$b=\$ans";; 3) ans=` expr \$a \* \$b` expr "\$a*\$b=\$ans";; 4)ans=` expr \$a \/ \$b` expr "\$a/\$b=\$ans";; 5)exit;; esac

OUTPUT

3-2=1

4. PROCESS MANAGEMENT
4.1. PROCESS CREATION PROGRAM

main ( ) { fork ( ); fork ( ); printf ("hello world\n"); }
OUTPUT

hello world hello world hello world hello world

4.2. PROCESS ID DETERMINATION
PROGRAM main ( ) { int pid; pid=fork( ); printf ("\n Process identification is:%d",getpid( )); }

OUTPUT

Process identification is: 19119 Process identification is: 19118

4.3. PROCESS SYNCHRONIZATION
PROGRAM main ( ) { int pid; pid=fork( ); if (pid==0) { printf ('\n child process id is:%d",getpid( )); printf ("\n child parent process id is:%d",getpid( )) } else { printf ('\n\n parent process id is:%d",getpid( )); printf ("\n parent process id is:%d\n,getpid( )); } }

OUTPUT Child process id is: 19134 Child parent process id is: 19134 Parent process id is: 19133 Parent process id is: 19133

5. FILE MANAGEMENT 5.1. COPYING A FILE PROGRAM
#include<fcntl.h> #nclude<fcntl.h> #include<sys\types.h> #define BUFSIZE 1024 int main(void) { int fd1,fd2,n; char buf[BUFSIZE]; fd1=open ("file 1",O_RDONLY); fd2=open ("file 2",O_WRONLY | O_CREAT || O_TRUNC); while ((n==read (fd1,buf,BUFSIZE))>0 write (fd2,buf,n); close9fd1); close (fd2); exit (0);

OUTPUT
\$cc copy.c \$./a.out \$vi file1 hai,hello!how r u? \$vi file2(empty file) \$cmp file1 file2 \$vi file2 hai,hello!how r u?

5.2. REVERSING A FIILE PROGRAM
#include<fcntl.h> #include<unistd.h> int main(int arg c,char **argv) { char buf; int size,fd; fd=open(arg v[1],O_RDONLY); Size=lseek (fd,-1,SEEK_END); while (size_>==0) { read (fd,&buf,1); write (STDOUTFILENO,&BUF,1); lseek(fd,-2,SEEK_CUR); } }

OUTPUT
\$cc reverse.c \$. /a.out \$. /a.out file1 EGELLOC GNIREENIGNE AHAM

5.3. MODIFYING A FILE PROGRAM
#include<stdio.h> #include<fcntl.h> int main(void) { mode_t old_mode,new_mode; old_mode=umask(0); printf("previous umask value:%0\n",old_mode); open ("file 1",O_RDONLY | O_CREAT,0777); umask(old_mode); open ("file2",O_RDWR | O_CREAT,0764); exit (0); }

OUTPUT

6. FIRST COME FIRST SERVE SCHEDULING ALGORITHM PROGRAM
#include<stdio.h> main() { int i,n,bt[10],wt[10],tat[10]; float a_wt,t_wt=0,t_tat=0,a_tat; printf("enter the number of process:"); scanf("%d",&n); for(i=0;i<n;i++) { printf("\n enter the burst time of process %d:",i); scanf("%d",&bt[i]); } wt[0]=0; for(i=1;i<n;i++) { wt[i]=bt[i-1]+wt[i-1]; t_wt=t_wt+wt[i]; } a_wt=t_wt/n; for(i=0;i<=n;i++) { tat[i]=bt[i]+wt[i]; t_wt=t_tat+tat[i]; } printf("\n process \t burst time\n"); for(i=0;i<n;i++) { printf("\np%d\t\t%d",i,bt[i]); } printf("\n\n process\twaiting time\n"); for(i=0;i<n;i++) { printf("\np%d\t\t%d",i,wt[i]); } printf("\n\n average waiting time:%.2f",a_wt); printf("\n\n process\tturnaround time\n"); for(i=0;i<n;i++) { printf("\np%d\t\t%d",i,tat[i]); } a_tat=t_tat/n; printf("\n\n average turnaround time :%.2f\n",a_tat);}

OUTPUT
Enter the number of process: 3 Enter the burst time of process 0:3 Enter the burst time of process 1:4 Enter the burst time of process 2:5 Process p0 p1 p2 Process p0 p1 p2 burst time 3 4 5 waiting time 0 3 7

Average waiting time: 3.33 Process p0 p1 p2 turnaround time 3 7 12

Average turnaround time: 4.00

7. ROUND ROBIN SCHEDULING ALGORITHM PROGRAM
#include<stdio.h> #include<conio.h> void main() { int process_time[10],pro_time[10],process_name[10]; int i,j,temp=0,index[10],max; int a_time=0,f_time=0,tw_time=0; float ta_time=0.0; char pro_name[5][10],pro_temp[5]; for(i=0;i<3;i++) { printf("process name:"); scanf("%s",&process_name[i]); printf("process time:"); scanf("%d",&process_time[i]); } printf("-------------\n"); for(i=0;i<3;i++) { printf("%s\t",pro_name[i]); } printf("\n"); printf("--------------\n"); max=process_time[0]; for(i=1;i<3;i++) { if (process_time[i]>max) { max=process_time[i]; } } for(i=0;i<=max/2;i++) { printf("%d\t%d\t%d\n",process_time[0],process_time[1],process_time[2]); process_time[0]-=2; process_time[1]-=2; process_time[2]-=2; if(process_time[0]<2) { process_time[0]=0; } if(process_time[1]<2)

{ process_time[1]=0; } if(process_time[2]<2) { process_time[2]=0; } } }

OUTPUT
Process name: p0 Process time: 2 Process name: p1 Process time: 5 Process name: p2 Process time: 7

Process Name Waiting Time

2 0

5 3 0

7 5 3

Turnaround Time 0

8. SHORTEST JOB FIRST ALGORITHM

PROGRAM
#include<stdio.h> #include<conio.h> void main() { float tot,turn=0.0,turnard=0.0; char proc[10],t1; int bt[10],wt[10],i,r=0; int temp,j=0,num; printf("enter the number of process:\n"); scanf("%d",&num); for(i=0;i<num;i++) { printf("enter the process %d:name\n",i+1); scanf("%s",&proc[i]); printf("enter the burst time of process of process %d:\n"); scanf("%d",&bt[i]); } for(i=0;i<num;i++) { for(j=i;j<num;j++) if(bt[i]>bt[j]) { temp=bt[i]; bt[i]=bt[j]; bt[j]=temp; t1=proc[i]; proc[i]=proc[j]; proc[j]=t1; } } for(i=0;i<num;i++) { wt[i]=r; r=r+bt[i]; } for(i=0;i<num;i++) tot=tot+wt[i]; turnard=tot/num; printf("\t process \\t start time \t burst time \t finish time \t wait time \n"); for(i=0;i<num;i++) { printf("\t%c\t\t%d\t\t%d\t\t%d\t\t%d \n",proc[i],wt[i],bt[i],(wt[i]

+bt[i],wt[i])); } printf("total waititng time =%0.2 fns\n",tot); printf("turnaround time=%0.2 fns \n",tot); }

OUTPUT
Enter the number of process: 3 Enter the process 1 name p0 Enter the burst time of process of process 2050: 4 Enter the process 2 name p1 Enter the burst time of process of process 2050: 5 Enter the process 3 name p3 Enter the burst time of process of process 2050: 7 Process start time burst time p p p 0 4 9 4 5 7 finish time 0 4 9 wait time 2050 2050 2050

Total waiting time =2.4 Turnaround time=3.6

9. MEMORY MANAGEMENT-FIRST FIT, BEST FIT &WORST FIT-CONTIGUOUS MEMORY ALLOCATION PROGRAM
#include<stdio.h> void first_fit(); void best_fit(); int i,no_holes,holes2[10],holes1[10],holes[10]; int no_process,process[10],process1[10]; main() { printf("enter the number of holes"); scanf("%d",&no_holes); for(i=1;i<=no_holes;i++) { printf("enter the size of %d hole",i); scanf("%d",&holes[i]); holes1[i]=holes[i]; holes2[i]=holes[i]; } printf("enter the number of process"); scanf("%d",&no_process); for(i=1;i<=no_process;i++) { printf("enter the size of %d process",i); scanf("%d",&process[i]); process1[i]=process[i]; } first_fit(); best_fit(); } void first_fit() { int j=1,k=1,flag=0; while(flag!=no_holes) { if(holes[k]>0) { if(holes[k]>process[i]) { printf("\n process %d is allocated holes %d \n\n",j,k); holes[k]=0; process[j]=0;flag++;

j=1;k=1; } else { if(k<=no_holes) { k++; flag++; } } { if(j<no_process) j++; } } else k++; } for(i=1;i<=no_holes;i++) { if(holes[i]>0) printf("The hole %d is not yet allocated having size as %d \n\n",i,process[i]); } } void best_fit() { int j=1,k,i,temp; for(i=1;i<=no_holes;i++) { for(j=1;j<=no_holes;j++) { if(process1[i]<=holes1[j]) { printf("\n\n process %d",i); for(k=1;k<=no_holes;k++) { if(holes2[k]!=0) { if(holes1[j]=holes2[k]) { printf("is allocated to hole %d\n\n",k); process1[i]=0; holes1[j]=0; holes2[k]=0; break;

} } } break; } } } for(i=1;i<no_holes;i++) { if(holes2[i]>0) printf("the hole %d is not yet allocated",i); } for(i=1;i<=no_process;i++) { if(process1[i]>0) printf("the process %d is not yet allocated having size %d \n",i,process1[i]); } }

OUTPUT
Enter the number of holes 3 Enter the size of 1 hole 6 Enter the size of 2 hole 7 Enter the size of 3 hole 8 Enter the number of process 3 Enter the size of 1 process 3 Enter the size of 2 processes 2 Enter the size of 3 processes 4 Process 1 is allocated holes 1 Process 2 is allocated holes 2 Process 2 is allocated holes 3

Process 1is allocated to hole 1

Process 2is allocated to hole 2

Process 3is allocated to hole 3

10. MEMORY ALLOCATION WITH PAGES PROGRAM
#include<stdio.h> #include<conio.h> main () { int k,m,n,x,i,j,pn,ps,ch,choice; int page[15][15],paget[10],frame[50]; printf("\n implementation of memory allocation using paging \n"); printf("\n\n enter the total number of pages"); scanf("%d",&pn); printf("\n\n enter the page size:"); scanf("%d",&ps); for(i=0;i<ps;i++) { printf("\n\n enter the frame number to store elements of page %d",n); scanf("%d",paget[i]); for(j=0;j<ps;j++) { printf("\n\n offset %d in page %d",j,i); scanf("%d",&page[i][j]); k=paget[i]*ps+j; frame[k]=page[i][j]; } } printf("\n\n page number offset elements"); for(i=0;i<pn;i++) { for(j=0;j<ps;j++) { printf("\n\n %d\t %d\t %d",i,j,page[i][j]); } printf("\n"); } do { printf("\n\n\t menu"); printf("\n 1.retrieve from memory \n 2.exit"); printf("\n\n enter your choice:"); scanf("%d",&ch); switch(ch) { case 1:

printf("\n\n enter page number:"); scanf("%d",&m); printf("\n\n enter offset:"); scanf("%d",&n); if((m<=pn)&&(n<=ps)) { i=paget[m]; k=i*ps+n; printf("\n element is %d",frame[k]); } else { printf("invalid input"); break; } break; } printf("\n\n do you want to continue? 1.yes,2.no"); scanf("%d",&choice); } while(choice==1); }

OUTPUT
Enter the page size: 2 Enter the frame number to store elements of page 3399 2 Offset 0 in page 0 100 Offset 1 in page 0 245 Enter the frame number to store elements of page 3399 2 Offset 0 in page 1 300 Offset 1 in page 1 400 Page number offset elements 0 0 1 1 Menu 1. Retrieve from memory 2. Exit Enter your choice: 1 Enter page number: 2 Enter offset: 0 Element is 300 Do you want to continue? 1. yes, 2.no 0 1 0 1 100 245 300 400

11. INTER PROCESS COMMUNICATION USING SEMAPHORE PROGRAM
#include<stdio.h> #include<sys/ipc.h> #include<sys/types.h> #include<unistd.h> #include<sys/sem.h> #include<sys/shm.h> #define MUTEX 0 #define DE 1 void reader(void); void writer(void); int semid; int shmid; int SHM_R; int SHM_W; void*shmptr; int rc=0; struct sembuf d={0,-1,SEM_UNDO}; struct sembuf u={0,1,ipc_NOWAIT}; struct sembuf u1={1,1,ipc_NOWAIT}; struct sembuf d1={1,-1,SEM_UNDO}; void reader(void) { int flag,read; flag=1; while(flag) { semop(MUTEX,&d,1); shmptr=shmat(shmid,0,SHM_R); rc=rc+1; printf("\n numbers of readers %d \n",rc); if(rc==1) semop(DE,&d1,1); semop(MUTEX,&u,1); read=*(int*)shmptr; printf("\n read process \n value is %d \n",read); semop(MUTEX,&d,1); rc=rc-1; if(rc==0) semop(DE,&u1,1); semop(MUTEX,&u,1); printf("\n reading complete.....\n"); flag=0;

} } void writer(void) { int flag,write; flag=1; shmptr=shmat(shmid,0,SHM_W); while(flag) { semop(DE,&d,1); printf("value written:"); scanf("%d",&write); printf("\n writer process......\n"); *(int*)shmptr=write; printf("\n the value written is %d:",write); semop(DE,&u1,1); flag=0; } } main() { semid=shmget(ipc_PRIVATE,2,066611,ipc_CREAT); semct1(semid,MUTEX,SETVAL,1); semct1(semid,DE,SETVAL,1); shmid=shmget(ipc_PRIVATE,1,066611,ipc_CREAT); if(fork()==0) writer(); sleep(5); if(fork()>=0) { sleep(5); reader(); } reader(); }