You are on page 1of 46

ANNAI TERESA COLLEGE OF ENGINEERING

THIRUNAVALUR-607 204

RECORD NOTE BOOK DEPARTMENT OF COMPUTER SCIENCE &ENGINEERING

NAME

:.

REG.NO :. YEAR :SEM:..

SUBJECT:

BONAFIDE CERTIFICATE
NAME REG.NO YEAR :. :. :SEM:

SUBJECT :.

Certified that this is the bonafide record of work done by the above student in the ..laboratory during the year 2011-2012.

SIGNATURE OF THE STAFF I/C

SIGNATURE OF THE HOD

Submitted for the university practical examination held on

INTERNAL EXAMINER

EXTERNAL EXAMINER

INDEX

EX.NO

DATE

NAME OF THE EXPERIMENT

PAGE NO

MARKS

INITIALS

EX.NO

DATE

NAME OF THE EXPERIMENT

PAGE NO

MARKS

INITIALS

EX.NO:
DATE:

SYSTEM CALLS -I

Aim:
To write a c program for system calls using Linux operating system.

Algorithm:
Step1: Start the program Step2: Create a child process using fork () system call Step3: Execute a separate command in child process using execlp() system call Step4: Then enter the any UNIX command for display the result Step5: Then display the parent process id Step6: Print the child complete inside parent Step7: The parent process is complete print and exit Step8: Stop the program.

Program:
#include<unistd.h> #include<stdlib.h> #include<stdio.h> main() { int pid; char buff[100]; pid=fork() if(pid==0) { Printf(enter any UNIX command \n); Scanf(%s,buff); Execlp(buff,buff,NULL); } Else { Wait(NULL); Printf(parent process id is %d,getpid()); Printf(\n child complete.inside parent\n); Exit(0); } }

OUTPUT:
$cc ex1.c $./a.out Enter any unix command Date Wed dec 28 09:13:54IST2011 Parent process id is2375 Child complete inside parent

RESULT:
Thus the above program was verified and executed successfully.

EX.NO:
DATE:

SYSTEM CALLS -II

Aim:
To write a c program perform the system call Linux command of operating system.

Algorithm:
Step1: Start the program Step2: get the name of the directory as input Step3: To open a directory and read the file name Step4: Read the file names one by one until end of directory is reached And displayed Step5: close the directory Step6: Stop the program.

Program:
#include<stdio.h> #include<sys/types.h> #include<dirent.h> #include<stdlib.h> struct dirent*dptr; main() { char buff[256]; DIR*dirp; printf(\n enter directory name:); scanf(%s,buff); if((dirp=opendir(buff))==NULL) { printf(error); exit(1); } while(dptr=readdir(dirp)) { Printf(%s\n,dptr->d_name); } closedir(drip) }

OUTPUT:
$cc ex2.c $./a.out Enter directory name: e1.c e2 .c e3.c

RESULT: Thus the above program was verified and executed successfully.

10

EX.NO:
DATE: I/O SYSTEM CALLS

Aim:
To write a c program to perform the I/O system call UNIX operating system.

Algorithm:
Step1: Start the program. Step2: Read the input Step3: Initialize a global variable character to same value Step4: Create a child process and change the value of the character Step5: Write the content into file and print the content Step6: Close the file Step7: Parent process begins and reads the context of file Step8: Print the content of the file and close the file Step9: Stop the program.

11

Program:
#include<stdio.h> #include<fcntl.h> main () { int fp,pid; char ch=G; pid=fork(); if(pid==0) { fp=open(data.txt,O_WRONGLY); ch=M; write(fp,&ch,1); printf(in child the character is %c\n,ch); close(fp); } else { Wait(0); fp=open(data.txt,O_RDONLY); read(fp,&ch,1); printf(character after parent read %c\n,ch); close(fp); } }

12

OUTPUT:
$ cat data.txt M $ cc file.c $ ./a.out In child the character is G In child the character after change is M Character after parent read M

Result:
Thus the above program was verified and executed successfully. 13

EX.NO:
DATE: SIMULATION OF LS COMMAND

Aim:
To write a c program to perform the stimulation of ls commands using Linux operating system.

Algorithm:
Step1: Start the program Step2: Read the input Step3: Initialize a global variable character to some value Step4: Create a child process and change the value of the character Step5: Write the content into file and print the content Step6: close the file Step7: Parent process begin and reads the content of file Step8: Print the content of file and close the file Step9: Stop the program.

14

Program:
#include<stdio.h> #include<dirent.h> main() { struct dirent**namelist; int n,i; char pathname[100]; getcwd(pathname); n=scandir(pathname,&namrelist,0,alphasort); if(n<0) printf(error\n); else { For(i=0;i<n;i++) { Printf(%s\n ,namelist[i]->d_name); } } }

15

OUTPUT:
$ cc ls.c $./a.out . .. a.out e ex1.c ex2.c fifo.c grep.c ipc.c

RESULT: Thus the above program was verified and executed successfully

16

EX.NO:
DATE:

FIRST COME FIRST SERVE SCHEDULING


Aim:
To write a c program to implement FCFS scheduling algorithm.

Algorithm:
Step1: Start the program Step2: Initialize the necessary variable Step3: Accept the number process in ready queue Step4: For each process in the ready queue read the process name and the CPU Burst time Step5: Set the waiting time the first process Step6: Calculate the waiting time of first each process Step7: Then calculate the average wait time and average turnaround time of the Process Step8: Stop the program.

17

Program:
#include<stdio.h> main() { int i,n,a[10],b[10],c[10],sum=0,sum1=0,wait=0; float avgwait,avgturn; printf(\nOUTPUT\n); printf(--------------); printf(enter the number of process); scanf(%d,&n); for(i=0;i<n;i++) { printf(\n enter the brust time for the process %d:,i+1); scanf(%d,&a[i]);} for(i=0;i<n;i++) { c[i]=sum; sum=sum+a[i]; b[i]=sum;} printf(\nProcess\tBrust Time\tWaiting Time\tTurnaround Time\n); for(i=0;i<n;i++) { printf(\nP%d\t\t%d\t\t%d\t\t%d,i+1,a[i],c[i],b[i]); } for(i=0;i<n;i++) { sum1=sum1+b[i];} for(i=0;i<(n-1);i++) { wait=wait+b[i];} avgwait=wait/n; printf(\n\n The Average Waiting Time Is:%f ms,avgwait); avgturn=sum1/n; printf(\n\n The Average TurnAround Time Is%f ms,avgturn); printf(\n GANTT CHART\n); printf(---------------------------------\n); for(i=0;i<n;i++) { Printf(P%d|,i+1); } printf(\n--------------------------\n); printf(0); for(i=0;i<n;i++) { printf(\t%d,b[i]);}} 18

Output:
Enter the number of process: 3 Enter the burst time of process1: 24 Enter the burst time of process2: 3 Enter the burst time of process3: 3 Process P1 P2 P3 Burst Time 24 3 3 Waiting Time 0 24 27 Turnaround Time 24 27 30

Gantt chart .. | p1 | p2 | p3 | 0 24 27 30 .. The Average waiting time Is: 17.0000ms The Average turnaround time Is: 27.0000ms

RESULT: Thus the above program was verified and executed successfully.

19

EX.NO:
DATE: AIM To write a program for verify and execute the shortest first job algorithm ALGORITHM 1. 2. 3. 4. 5. 6. 7. 8. Start the program. Declare the variables. Get the no. of processes and the burst time for each process. To call main function and structure. Using for loop set the process[i] value. Using for loop print the result. Print the values. Stop the program.

SHORTEST JOB FIRST SCHEDULING

20

PROGRAM #include<stdio.h> struct processes { int pid,bt,wt,tat; }process[10]; main() { struct processes temp; int n,i,j,twt,ttat,tbt; float awt,atat,tput; ttat=tbt=twt=0; printf("\nEnter the number of process:\n"); scanf("%d",&n); for(i=0;i<n;i++) { printf("Enter the burst time of process[%d]",i); scanf("%d",&process[i].bt); process[i].pid=i; tbt=tbt+process[i].bt; } for(i=0;i<n;i++) { for(j=i+1;j<n;j++) { if(process[i].bt>process[j].bt) { temp=process[i]; process[i]=process[j]; process[j]=temp; } } } ttat=process[0].tat=process[0].bt; ttat=process[0].wt=0; for(i=0;i<n;i++) { process[i].tat=process[i-1].tat+process[i].bt; process[i].wt=process[i].tat-process[i].bt; ttat=ttat+process[i].tat; twt=process[i].wt+twt; } atat=(float)ttat/n; awt=(float)twt/n; tput=(float)tbt/n; printf("\n Process id\t Burst time\t Turnaround time\t Waiting time\n"); for(i=0;i<n;i++) {

21

printf("\t%d\t\t%d\t\t%d\t\t%d\n",process[i].pid,process[i].bt,process[i].tat, process[i].wt); } printf("\n\nAverage turnaround time= %f",atat); printf("\nAverage waiting time = %f",awt); printf("\nThrough put = %f",tput); printf("\n\n\t\tGANTT CHART\n"); printf("\n\t\t----------------------\n\t\t"); for(i=0;i<n-1;i++) printf("%d-->",process[i].tat); printf("%d",process[i].tat); printf("\n\t\t----------------------\n\t\t"); }

22

OUTPUT [cse27@localhost siva7]$ cc ex19.c [cse27@localhost siva7]$ ./a.out Enter the number of process: 5 Enter the burst time of process[0]4 Enter the burst time of process[1]1 Enter the burst time of process[2]3 Enter the burst time of process[3]2 Enter the burst time of process[4]5 Process id Burst time Turnaround time 1 1 1 0 3 2 3 1 2 3 6 3 0 4 10 6 4 5 15 10 Average turnaround time= 7.000000 Average waiting time = 4.000000 Through put = 3.000000 GANTT CHART -------------------------1-->3-->6-->10-->15 --------------------------

Waiting time

RESULT Thus the program for the shortest first job algorithm is executed and verified successfully. 23

EX.NO:
DATE: AIM To write a program for verify and execute the priority scheduling algorithm ALGORITHM 1. 2. 3. 4. 5. 6. 7. Start the program. Inside declare variables. Using for loop get the values of process[i].bt. And also print the value equilise the values. Then using for loop print the variables value. Print the result. Stop the program.

PRIORITY SCHEDULING

24

PROGRAM #include<stdio.h> struct processes { int pid,bt,wt,tat,pty; }process[10]; main() { struct processes temp; int i,j,n,twt,tbt; float awt,atat,tput=0.0; int ttat=tbt=twt=0; printf("\n Enter the number of process:"); scanf("%d",&n); for(i=0;i<n;i++) { printf("\n Enter the burst time of proces[%d]",i+1); scanf("%d",&process[i].bt); process[i].pid=i+1; tbt=tbt+process[i].bt; } for(i=0;i<n;i++) { printf("\n Enter the priority of the process[%d]",i+1); scanf("%d",&process[i].pty); } for(i=0;i<n;i++) { for(j=i+1;j<n;j++) { if(process[i].pty<process[j].pty) { temp=process[i]; process[i]=process[j]; process[j]=temp; } } } ttat=process[0].tat=process[0].bt; twt=process[0].wt=0; for(i=0;i<n;i++) { process[i].tat=process[i-1].tat+process[i].bt; process[i].wt=process[i].tat-process[i].bt; 25

ttat=ttat+process[i].tat; twt=process[i].wt+twt; } atat=(float)ttat/n; awt=(float)twt/n; tput=(float)tbt/n; printf("\n Process id\t Burst time\t Turnaround time\t Waiting time\n"); for(i=0;i<n;i++) { printf("\t %d\t\t %d\t\t %d\t\t %d\n", process[i].pid, process[i].bt, process[i].tat, process[i].wt); } printf("\n\nAverage turnaround time= %f",atat); printf("\nAverage waiting time = %f",awt); printf("\nThrough put = %f",tput); printf("\n\n\t\tGANTT CHART\n"); printf("\n\t\t----------------------\n\t\t"); for(i=0;i<n-1;i++) printf("%d-->",process[i].tat); printf("%d",process[i].tat); printf("\n\t\t----------------------\n\t\t"); }

26

OUTPUT [cse27@localhost siva7]$ cc ex21.c [cse27@localhost siva7]$ ./a.out Enter the number of process:5 Enter the burst time of process[1]3 Enter the burst time of process[2]2 Enter the burst time of process[3]7 Enter the burst time of process[4]9 Enter the burst time of process[5]10 Enter the priority of the process[1]3 Enter the priority of the process[2]5 Enter the priority of the process[3]4 Enter the priority of the process[4]2 Enter the priority of the process[5]1 Process id Burst time Turnaround time 2 2 2 3 7 9 1 3 12 4 9 21 5 10 31 Average turnaround time= 15.400000 Average waiting time = 8.800000 Through put = 6.200000 GANTT CHART --------------------------2-->9-->12-->21-->31 --------------------------Waiting time 0 2 9 12 21

RESULT Thus the program for the priority scheduling algorithm is executed and verified successfully. 27

EX.NO:
DATE:

ROUND ROBIN SCHEDULING


AIM To write a program for verify and execute the Round Robin scheduling algorithm ALGORITHM 1. 2. 3. 4. 5. 6. 7. Start the program. Declare the variable. Using for loop check the conditions. Using while loop check whether it is zero or not. Assign using for loop check the values. Print the results. Stop the program.

28

PROGRAM #include<stdio.h> main() { int p[10],bt[10],q[10],wt[10],s[20],t[10],b[10]; int i,j=0,time,a=0,var,n,count; float aw=0.0,at=0.0; printf("\n Enter the time slice : "); scanf("%d",&time); printf("\n Enter the number of process :"); scanf("%d",&n); for(i=0;i<n;i++) { printf("\n Enter the burst time of process p[%d]",i+1); scanf("%d",&bt[i]); b[i]=bt[i]; p[i]=i+1; q[i]=0; } count=n; while(count!=0) { for(i=0;i<n;i++) { if(b[i]!=0) { b[i]=b[i]-time; if(b[i]<=0) { var=time+b[i]; s[a]=p[i]; wt[i]=j; j=j+var; count=count-1; b[i]=0; } else { s[a]=p[i]; q[i]=q[i]+1; wt[i]=j; j=j+time; } a++; } 29

} } for(i=0;i<n;i++) { wt[i]=wt[i]-(q[i]*time); t[i]=wt[i]+bt[i]; } printf("\n Process id\t Burst time\t Waiting time\t Turnaround time\n"); for(i=0;i<n;i++) { printf("\t %d\t\t %d\t\t %d\t\t %d\n",p[i],bt[i],wt[i],t[i]); } printf("\n\n\t\tGANTT CHART\n"); printf("\n\t\t----------------------\n\t\t"); for(i=0;i<n-1;i++) { printf("%d-->",t[i]); } printf("%d",t[i]); printf("\n\t\t----------------------\n\t\t"); }

30

OUTPUT [cse27@localhost siva7]$ cc ex20.c [cse27@localhost siva7]$ ./a.out Enter the time slice : 2 Enter the number of process :5 Enter the burst time of process p[1]4 Enter the burst time of process p[2]2 Enter the burst time of process p[3]3 Enter the burst time of process p[4]3 Enter the burst time of process p[5]2 Process id Burst time Waiting time 1 4 8 2 2 2 3 3 10 4 3 11 5 2 8 GANTT CHART ----------------------------12-->4-->13-->14-->10 ----------------------------Turnaround time 12 4 13 14 10

RESULT Thus the program for the Round Robin scheduling algorithm is executed and verified successfully. 31

EX.NO:
DATE:

IPC USING SHARED MEMORY

Aim:
To write a c program of performing the IPC using shared memory using Linux operating system.

Algorithm:
Step1: Start the program Step2: Create the child process using fork() Step3: Create shared memory for parent process using get() system call Step4: Now across and attach the some shared memory to the child process Step5: The data in the shared memory is read by the child process using the pointer Step6: Now detach and release the shared memory Step7: Stop the program.

32

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-CREAT); shmptr=shmat(shmid,0.0); printf(\nparent writing\n): for(i=0;i<10;i++) { shmptr[i]=a+i; putchar(shmptr[i]) } } else { shmid=shmget(2041,32,0666); schmptr=shmat(shmid,0.0); printf(\n child is reading:); for(i=0;i<10;i++) putchar(shmptr[i]); shmd t(NULL); shmc t1(shmid,IPC_RMID,NULL); } return 0; }

33

OUTPUT:
Parent is reading abcdefghij Child is reading abcdefghij

Result:
Thus the above program was verified and executed successfully. 34

EX.NO:
DATE:

IPC USING PIPE

Aim:
To write a c program of performing the IPC using pipe shared memory using Linux operating system.

Algorithm:
Step1: Start the program Step2: Create the child process using fork () Step3: Create the pipe structure using pipe() Step4: Now allow the read and of the parent process using close() Step5: Read the data in the pipe using read() Step6: Display the string Step7: Stop the program.

35

Program:
#include<stdio.h> int main() { int fd[2],child; char a[10]; printf(\n enter the string to enter into the pipe:); 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(\n the string is retrived from the pipe\n,a); } return 0; }

36

Output:
Enter the string to enter into the pipe ROSE The string is retrieved from the pipe ROSE

Result:
Thus the above program was verified and executed successfully.

37

EX.NO:
DATE:

PRODUCER CONSUMER USING SEMAPHORE


AIM To write a program for verify and execute the consumer problem using semaphores. ALGORITHM 1. 2. 3. 4. 5. 6. 7. Start the program. To call main function. Get the choice and print it. Declare i value and check whether item->buffer. To call producer function and print now many no of item available. Print the result. Stop the program.

38

PROGRAM #include<stdio.h> void producer(); void consumer(); int i,item=0,buff=4,a[4],ch; main() { printf(" Producer Consumer Problem."); do { printf("\n Size of the buffer is %d.\n",buff); printf(" Number of items in buffer %d.\n",item); printf(" 1.Producer\n 2.Consumer\n 3.Exit\n Enter your choice: "); scanf("%d",&ch); if(ch==1) producer(); else if(ch==2) consumer(); }while(ch!=3); } void producer() { if(i==0) { printf("\n Enter how much item to be produced: "); scanf("%d",&item); if(item>buff) { printf("\n The buffer will overflow!"); printf("\n Re-enter below buffer size.\n"); producer(); } else { for(i=1;i<=item;i++) { printf("\n Enter the %d element: ",i); scanf("%d",&a[i]); } i=i-1; } } 39

else if(i!=0) printf("\n Producer is waiting..."); } void consumer() { if(i>0) { printf("\n Consumer consumes %d items.\n",a[i]); i--; item--; } else printf("\n No item are available for producer...\n"); }

40

OUTPUT [cse27@localhost siva7]$ cc ex22.c [cse27@localhost siva7]$ ./a.out Producer Consumer Problem. Size of the buffer is 4. Number of items in buffer 0. 1.Producer 2.Consumer 3.Exit Enter your choice: 1 Enter how much item to be produced: 3 Enter the 1 element: 2 Enter the 2 element: 4 Enter the 3 element: 7 Size of the buffer is 4. Number of items in buffer 3. 1.Producer 2.Consumer 3.Exit Enter your choice: 2 Consumer consumes 7 items. Size of the buffer is 4. Number of items in buffer 2. 1.Producer 2.Consumer 3.Exit Enter your choice: 2 Consumer consumes 4 items. Size of the buffer is 4. Number of items in buffer 1. 1.Producer 2.Consumer 3.Exit Enter your choice: 2 Consumer consumes 2 items. 41

Size of the buffer is 4. Number of items in buffer 0. 1.Producer 2.Consumer 3.Exit Enter your choice: 2 No item are available for producer... Size of the buffer is 4. Number of items in buffer 0. 1.Producer 2.Consumer 3.Exit Enter your choice: 3

RESULT Thus the program for the consumer problem using semaphores algorithm is executed and verified successfully. 42

EX.NO: DATE: Aim:


To write a c program to allocate memory using first fit, best fit, worst fit algorithm. FIRST FIT, BEST FIT, WORST FIT MEMORY MANAGEMENT ALGORITHMS

Algorithm:
Step1: Start the program Step2: Get the number of processor and size of each processors Step3: Get the number of the segment and segment size Step4: The segment is allocated to various process based on the best fit algorithm otherwise the process cant be stored in any segment Step5: The segment is allocated to various process based on the worst fit algorithm Step6: The segment is allocated to various process based on the best fit algorithm Step7:Compare all the unused memory space to find which algorithm makes the most efficient use of memory Step8: Stop the program.

43

PROGRAM:
#include<stdio.h> void firstfit(int ff[],int ps); void bestfit(int bf[],int ps); void worstfit(int wf[],int ps); int n; void main() { int ff[10],bf[10],wf[10],ps[10]; int i,j,m; system("clear"); printf("\n Enter no of Free Blocks:"); scanf("%d",&n); printf("\nEnter Each block size:"); for(i=0;i<n;i++) { scanf("%d",&ff[i]); wf[i]=bf[i]=ff[i]; } printf("\nEnter the total no of process:"); scanf("%d",&m); printf("\nEnter size of each process:"); for(i=0;i<m;i++) scanf("%d",&ps[i]); printf("\n\t Process Bestfit Firstfit Worstfit\n"); for(i=0;i<m;i++) { bestfit(bf, ps[i]); firstfit(ff, ps[i]); worstfit(wf, ps[i]); } printf("\n\n"); } void firstfit(int ff[] ,int ps) { int i; for(i=0;i<n;i++) if(ff[i]>=ps) { ff[i]-=ps; printf("\t Block % d \t",i+1); return; } printf("\t Not available"); return; 44

} void bestfit(int bf[], int ps) { int min=999,i,bno,flag=0; for(i=0;i<n;i++) if(bf[i] <min && bf[i]>ps) { min=bf[i]; bno=i; flag=1; } if (flag==1) { bf[bno]-=ps; printf("\t%d\t block %d",ps,bno+1); } else printf("\t%d Not available",ps); return; } void worstfit(int wf[ ],int ps) { int max=0,i,bno ; for(i=0;i<n;i++) if(wf[i]>max) { max=wf[i]; bno=i; } if(max>=ps) { wf[bno]-=ps; printf("\t Block %d\n",bno+1); } else { printf("\t Not available"); return; } }

45

OUTPUT:
$cc fit.c $./a.out Enter no of free blocks: 2 Enter each block size: 120 100 Enter total number of process: 2 Enter size of each process: 50 20 Process 50 20 best fit block2 block2 first fit block1 block1 worst fit block1 block2

Result :
Thus the above program was verified and executed successfully.

46

You might also like