Thursday, 29 September 2011


IMPLEMENTATION OF INTERPROCESS COMMUNICATION USING PIPES

AIM:
            To implement interprocess communication using pipes.

ALGORITHM:

1. Start the process.
2. Create a child process using fork().
3. Now close the read end of the parent using close().
4. Write the data in the pipe using write().
5. Now close the read end of the child using close().
6. Read the data in the pipe using read().
7. Display the string.
8. Stop the process.

PROGRAM:

#include<stdio.h>
int main()
{
int fd[2],child;
char a[10];
printf(“Enter the string to enter into the pipe:\t”);
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(“The string received from the pipe is %s”, a);
}
return 0;
}

OUTPUT:

[root@localhost ~]# ./pipes

Enter the string to enter into the pipe: Welcome

The string retrieved from the pipe is Welcome

RESULT:
Thus the program is executed and the output  is verified.

IMPLEMENTATION OF INTERPROCESS COMMUNICATION USING SHARED MEMORY

AIM:
To implement interprocess communication using shared memory.

ALGORITHM:

1.Start the process.
2.Create the child process using fork().
3.Create the shared memory for parent process using shmget() system call.
4.Now allow the parent process to write in shared memory using shmptr pointer which is return type of shmat().
5.Now across and attach the same shared memory to the child process.
6.The data in the shared memory is read by the child process using the shmptr pointer.
7.Now, detach and reuse the shared memory.
8.Stop the process

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(4041,32,0666| IPC_CREAT);
shmptr=shmat(shmid,0,0);
printf(“\nParent writing:\t”);
for(I=0;I<10;I++)
{
shmptr[I]=’a’+I;
putchar(shmptr[I]);
}
printf(“\n”);
wait(NULL);
}
else
{
shmid=shmget(4041,32,0666);
shmptr=shmat(shmid,0,0);
printf(“\nChild is reading:\t”);
for(I=0;I<10;I++)
putchar(shmptr[I]);
shmdt(NULL);
shmctl(shmid,IPC_RMID,NULL);
}
printf("\n");
return 0;
}

OUTPUT:

[root@localhost ~]# ./ipc

Parent writing:   abcdefghij

Child is reading: abcdefghij

RESULT: 
Thus a interprocess communication using shared memory was implemented in linux platform and the output was verified.

                     IMPLEMENTATION OF PRIORITY SCHEDULING ALGORITHM

AIM:
          To implement the priority scheduling algorithm.

ALGORITHM:

  1. Start the program
  2. Obtain the number of processes from the user
  3. Obtain the CPU burst time and the priorities for each process.
  4. Calculate the waiting time for each process such that the process with lower priority is executed first and the process with highest priority is executed last.
  5. Display all the details for each process along with the average waiting time.
  6. Stop the execution.

PROGRAM:

#include<stdio.h>
int enqueue(int num,int y,int q[]);
int main()
{
int p[5],b[5],w[5],f[5],n,tb,t[5],i,j,tw,tt,temp,wt,pr[10],c[10];
float aw,at;
printf("\n Enter the no. of process:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the burst time & priority:");
scanf("%d",&b[i]);
scanf("%d",&pr[i]);
p[i]=i+1;
}
for(i=0;i<n;i++)
    for(j=i+1;j<n;j++)
         if(pr[i]>pr[j])
         {
            temp=pr[i];
            pr[i]=pr[j];
            pr[j]=temp;
            temp=b[i];
            b[i]=b[j];
            b[j]=temp;
            temp=p[i];
            p[i]=p[j];
            p[j]=temp;
         }
tw=w[0]=tt=t[0]=tb=0;
for(i=0;i<n;i++)
{
w[i]=tb;
tb=tb+b[i];
t[i]=b[i]+w[i];
tw=tw+w[i];
tt=tt+t[i];
}
aw=(float)tw/n;
at=(float)tt/n;
for(i=0;i<n;i++)
c[i]=w[i];
c[n]=t[n-1];
printf("\n Process time Burst time Waiting time Turnaround time\n");
for(i=0;i<n;i++)
printf("\n\t%d\t\t%d\t\t%d\t\t%d\n",p[i],b[i],w[i],t[i]);
printf("\nGantt chart\n");
for(i=0;i<=n;i++)
for(j=i+1;j<=n;j++)
if(c[i]>c[j])
{
temp=c[i];
c[i]=c[j];
c[j]=temp;
}
for(i=0;i<n;i++)
printf("\t p%d",p[i]);
printf("\n");
for(i=0;i<=n;i++)
printf("\t%d",c[i]);
printf("\n The total waiting time=%d",tw);
printf("\n Total turnaround time is=%d",tt);
printf("\n Average waiting time=%f",aw);
printf("\n Average turnaround time=%f",at);
printf("\n");
}

OUTPUT:

Enter the no. of   process:        4

Enter the burst time for P1:    4
Enter the priority for P1:      2

Enter the burst time for P2:    5
Enter the priority for P2:      3

Enter the burst time for P3:    6
Enter the priority for P3:      1

Enter the burst time for P4:    7
Enter the priority for P4:      4

 Process time   Burst time      Waiting time    Turnaround time

        3                     6                         0                       6

        1                    4                          6                       10

        2                    5                         10                      15

        4                     7                         15                      22

 Gantt chart
            P3      P1      P2      P4
        0       6       10      15      22
 The total waiting time=31
 Total turnaround time is=53
 Average waiting time=7.750000
 Average turnaround time=13.250000

RESULT:
Thus the program using priority scheduling was done and the output was verified.

                      IMPLEMENTATION OF ROUND ROBIN ALGORITHM

AIM:
          To implement the round robin algorithm.

 ALGORITHM:

  1. Start the program.
  2. Read the number of process and the time slice.
  3. Read the burst time for each process.
  4. Calculate the total turn around time, average waiting time based on the time slice.
  5. The Gantt chart is prepared based on the times the process gets executed.
  6. Display the result.
  7. Stop the execution.

PROGRAM:

#include<stdio.h>
int enqueue(int num,int y,int q[]);
int main()
{
int queue[20],i,pro[20];
int rear=0,num,tq,bur[20],wt[20],turn[20];
int flag,j=1,temp,burst[20],wait[20];
float avgwt=0,avgturn=0;
wt[0]=0;
printf("\n ROUND ROBIN SCHEDULING\n");
printf("\n\nEnter the number of process:");
scanf("%d",&num);
printf("\nProcess\tBurst time\n");
for(i=0;i<num;i++)
{
printf("\n p%d\t\t",i+1);
scanf("%d",&bur[i]);
burst[i]=bur[i];
}
printf("\n\nEnter the time quantum:");
scanf("%d",&tq);
do
{
flag=0;
for(i=0;i<num;i++)
{
if(bur[i]>=tq)
{
rear=enqueue(i+1,rear,queue);
bur[i]-=tq;
wt[j]=tq+wt[j-1];
j++;
}
else if((bur[i]>0)&&(bur[i]<tq))
{rear=enqueue(i+1,rear,queue);
wt[j]=bur[i]+wt[j-1];
bur[i]=0;
j++;
}
}
for(i=0;i<num;i++)
if(bur[i]==0)
flag++;
}while(flag!=num);
for(i=1;i<=rear;i++)
queue[i-1]=queue[i];
printf("\n\nGANTT CHART\n");

for(i=0;i<rear;i++)
printf(" p%d\t",queue[i]);
printf("\n");
for(i=0;i<=rear;i++)
printf("%d\t",wt[i]);
for(i=0;i<num;i++)
{
wait[i]=0;
temp=0;
for(j=0;j<rear;j++)
{
if(queue[j]==i+1)
{
wait[i]+=(wt[j]-temp);
temp=wt[j+1];
}
}
}
printf("\n\n**********************************************************");
printf("\nProcess\tBurst time \tWait time \tTurnaround time\n");
printf("**********************************************************\n");
for(i=0;i<num;i++)
{
turn[i]=wait[i]+burst[i];
printf("\np%d\t\t%d\t\t%d\t\t%d",i+1,burst[i],wait[i],turn[i]);
avgwt+=wait[i];
avgturn+=turn[i];
}
printf("\n\nAverage Waiting time is %f",avgwt/num);
printf("\n\nAverage Turnaround time is %f",avgturn/num);
}
}
int enqueue(int n,int rear,int queue[])
{
rear++;
queue[rear]=n;
return rear;
}

OUTPUT:

[root@localhost ~]# ./rr

 ROUND ROBIN SCHEDULING


Enter the number of process: 4

Process Burst time

 P1             3

 P2             6

 P3             4

 P4             2


Enter the time quantum: 2


GANTT CHART
        P1     P2      P3      P4      P1      P2      P3      P2
  0        2        4        6        8        9        11       13      15

**********************************************************
      Process                   Burst time      Wait time       Turnaround time
**********************************************************

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

Average waiting time is 7.500000

Average Turnaround time is 11.250000

IMPLEMENTATION OF SJF SCHEDULING
                  
AIM:
To write a C program to perform SJF scheduling in Linux.

ALGORITHM:
1.      Stop the program.
2.      Read the number of request
3.      Read the servicing time for each process
4.      Sort the process by servicing time(i.e. the job having least burst time is executed first)
5.      Calculate the waiting time for all the process.
6.      Calculate the average waiting time.
7.      Draw the gantt chart and print the waiting time, average waiting time, turnaround time and average turnaround time
8.      Stop the process

PROGRAM:

#include<stdio.h>
int  main()
{
int p[5],b[5],w[5],f[5],n,tb,t[5],i,j,tw,tt,temp,wt,c[10];
float aw,at;
printf("\n Enter the no. of process:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the burst time:");
scanf("%d",&b[i]);
p[i]=i+1;
}
for(i=0;i<n;i++)
for(j=i+1;j<n;j++)
if(b[i]>b[j])
{
temp=b[i];
b[i]=b[j];
b[j]=temp;
temp=p[i];
p[i]=p[j];
p[j]=temp;
}
tw=w[0]=tt=t[0]=tb=0;
for(i=0;i<n;i++)
{
w[i]=tb;
tb=tb+b[i];
t[i]=b[i]+w[i];
tw=tw+w[i];
tt=tt+t[i];
}
aw=(float)tw/n;
at=(float)tt/n;
for(i=0;i<n;i++)
c[i]=w[i];
c[n]=t[n-1];
printf("\n Process       Burst time       Waiting time Turnaround time\n");
for(i=0;i<n;i++)
printf("\np%d\t\t%d\t\t%d\t\t%d\n",p[i],b[i],w[i],t[i]);
printf("\nGantt chart\n");
for(i=0;i<=n;i++)
for(j=i+1;j<=n;j++)
if(c[i]>c[j])
{
temp=c[i];
c[i]=c[j];
c[j]=temp;
}
for(i=0;i<n;i++)
printf("\t p%d",p[i]);
printf("\n");
for(i=0;i<=n;i++)
printf("\t%d",c[i]);
printf("\n The total waiting time=%d",tw);
printf("\n Total turnaround time is=%d",tt);
printf("\n Average waiting time=%f",aw);
printf("\n Average turnaround time=%f",at);
printf("\n");
}
}

OUTPUT:

 [root@localhost ~]# ./p5
Enter the no. of process:4
Enter the burst time:3
Enter the burst time:5
Enter the burst time:6
Enter the burst time:1

 Process       Burst time       Waiting time      Turnaround time

P4                     1                        0                               1

P1                     3                        1                               4

P2                    5                         4                               9

P3                    6                         9                              15

Gantt chart
                                        P4      P1        P2      P3
                                        0        1        4       9       15
 The total waiting time=14
 Total turnaround time is=29
 Average waiting time=3.500000
 Average turnaround time=7.250000

IMPLEMENTATION OF FCFS SCHEDULING

AIM:
To write a C Program to perform FCFS Scheduling in Linux.

ALGORITHM:

  1. Start the program.
  2. Read the number of requests
  3. Read the burst(execution) time for each process
  4. The processes are allowed to execute depends of the arrival time.
  5. The waiting time is calculated for all the process.
  6. The gantt chart is drawn representing the process execution.
  7. Calculate the waiting time and the turnaround time for all the process.
  8. Stop the process.

PROGRAM:

#include<stdio.h>
int  main()
{
int a[10],b[10],c[10],n,i,wt=0;
float avgwt;
a[0]=b[0]=0;
printf("\nEnter the no. of processes\t");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("Enter the burst time:");
scanf("%d",&a[i]);
}
printf("\n The entered process are");
for(i=1;i<=n;i++)
printf("\np%d %d",i, a[i]);
printf("\n The gantt chart is:\n");
for(i=1;i<=n;i++)
{
printf("\t p%d",i);
}
printf("\n");
printf("\t%d",b[0]);
for(i=1;i<=n;i++)
{
b[i]=a[i]+b[i-1];
printf("\t%d",b[i]);
}
printf("\n The waiting time is");
for(i=0;i<n;i++)
{
printf("\np%d\t%d",i+1,b[i]);
wt=b[i]+wt;
}
avgwt=wt/i;
printf("\nThe average waiting time is %f",avgwt);
printf("\nThe turnaround time is");
wt=0;
for(i=1;i<=n;i++)
{
printf("\np%d\t%d",i,b[i]);
wt=b[i]+wt;
}
avgwt=wt/i;
printf("\nThe average turnaround time is %f",avgwt);
printf("\n");
}

OUTPUT:
[root@localhost ~]# ./p5
Enter the no. of processes      4
Enter the burst time:4
Enter the burst time:3
Enter the burst time:2
Enter the burst time:5
The entered processes are
P1      4
P2       3
P3       2
P4       5
The gantt chart is:
P1       P2      P3       P4
0         4        7         9       14
The waiting time is
P1      0
P2      4
P3      7
P4      9
The average waiting time is 5.000000
The turnaround time is
P1      4
P2      7
P3      9
P4      14
The average turnaround time is 6.000000

SIMULATION OF GREP COMMAND

  AIM:
 To implement the grep command in C.

ALGORITHM:

  1. Start the process.
  2. Declare the required variables.
  3. Obtain the filename and the required word to be searched from the user.
  4. Open the file and read the contents till the end of the file.
  5. Search the given word in the file using strstr() function, if it matches, then the word is found.
  6. Do this till the end of file is reached.
  7. Stop the process.

PROGRAM:

#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<unistd.h>
Int calc_butsize(char *filename)
{
Struct stat st;
Stat(filename,&st);
Return((int)st.st_size);
}
Int main(int argc,char *argv[])

{
If(argc<3)
Printf(“usage:\n\t %s <word><files>…..\n”,argv[0]);
Return -1;
}

FILE *fp;
Char *filename;
Int *=2;
For(x;x!=argc;x++)
Filename=argv[x];
If((fp=fopen(filename;”r”))==NULL)
{
Printf(“Failed to open file:%s \n “, argv[2]);
Return -2;
}
Int BUFSIZE=calc_bufsize(filename);
Char buf[BUFSIZE];
Fread(&buf,sizeof(char),BUFSIZE,fp);
Char *ans=strstr(buf,argv[1]);
If(ans!=NULL)
Printf(“%s \n”,filename);
Printf(“the word is found “);
Else
Printf(“ the word is not found in the given file \n”);
Fclose(fp);
}

Return 0;
}


OUTPUT:

[cs28@linux ~]$ ./3b “include” 3b.c
3b.c
The given word is found

RESULT:
 Thus the given C Program is executed and output is verified.