Monday, December 26, 2011

Exercise Program - 7 IMPLEMENTATION OF PRODUCER CONSUMER PROBLEM USING SEMAPHORE

AIM:

To write a C program for implementing Producer Consumer problem using Semaphore.

ALGORITHM:

Step 1: Start the program.

Step 2: Get the choice 1 for Producer, 2 for Consumer.

Step 3: If the choice is 1, the produced buffer size is 1, again if the choice is 1 then buffer size

will be increased[produced buffer = 1 + 1 = 2].

Step 4: If the choice is 2, the consumed buffer size is 1.

Step 5: Stop the program.

PROGRAM:

#include

#include

#define max 5

int n,produced,consumed,buffer[max];

int result,i;

int main()

{

clrscr();

produced=0;

consumed=1;

result=0;

i=0;

while(1)

{

printf("Enter the choice\n");

printf("1.Producer 2.Consumer 3.Exit\n");

scanf("%d",&n);

if(n==3)

exit(0);

else if(n==1)

producer(consumed,buffer[i]);

else if(n==2)

consumer(produced,buffer[i]);

else

exit(0);

}

}

producer(int consumed,int result)

{

if((consumed=1)&&(i

{

result=result+1;

i++;

buffer[i]=result;

produced=1;

printf("Produced bufffer=%d\n",buffer[i]);

consumed=0;

return(produced,result);

}

else

printf("Buffer still to be consumed\n");

}

consumer(int produced,int result)

{

if((produced==1)&&(i>0))

{

consumed=1;

printf("Consumed buffer=%d\n",buffer[i]);

produced=0;

i--;

return(consumed,result);

}

else

printf("Buffer still to be produced");

}

SAMPLE OUTPUT:

[it65@AntiViruS ~]$ cc prodcons.c

[it65@AntiViruS ~]$ ./a.out

Enter the choice

1.Producer 2.Consumer 3.Exit

1

Produced Buffer = 1

Enter the choice

1.Producer 2.Consumer 3.Exit

1

Produced Buffer = 2

Enter the choice

1.Producer 2.Consumer 3.Exit

2

Consumed Buffer = 2

Enter the choice

1.Producer 2.Consumer 3.Exit

2

Consumed Buffer = 1

Enter the choice

1.Producer 2.Consumer 3.Exit

2

Buffer still to be produced

Enter the choice

1.Producer 2.Consumer 3.Exit



RESULT:

Thus C program for implementing Producer Consumer problem using Semaphore has been executed successfully.

Exercise Program - 6 INTERPROCESS COMMUNICATION USING PIPES

AIM:

To write a C program for implementing interprocess communication using pipes.

ALGORITHM:

Step 1: Start the program.

Step 2: Create the child process using fork().

Step 3: Create the pipe structure using pipe().

Step 4: Enter the limit for calculating Fibonacci series at the Parent process.

Step 5: Now close the read end of the parent process using close().

Step 6: Write the data in the pipe using write().

Step 7: Now close the write end of child process using close().

Step 8: Read the data in the pipe using read().

Step 9: Calculate the Fibonacci series using the data read from the pipe.

Step 10: Display the series.

Step 11: Stop the program.

PROGRAM:

#include

#include

#include

#include

#include

main()

{

int pid,pfd[2],n,a,b,c;

if(pipe(pfd)==-1)

{

printf("\nError in pipe connection\n");

exit(1);

}

pid=fork();

if(pid>0)

{

printf("\nParent Process");\

printf("\n\n\tFibonacci Series");

printf("\nEnter the limit for the series:");

scanf("%d",&n);

close(pfd[0]);

write(pfd[1],&n,sizeof(n));

close(pfd[1]);

exit(0);

}

else

{

close(pfd[1]);

read(pfd[0],&n,sizeof(n));

printf("\nChild Process");

a=0;

b=1;

close(pfd[0]);

printf("\nFibonacci Series is:");

printf("\n\n%d\n%d",a,b);

while(n>2)

{

c=a+b;

printf("\n%d",c);

a=b;

b=c;

n--;

}

}

}

SAMPLE OUTPUT:

[it65@AntiViruS ~]$ cc ipc.c

[it65@AntiViruS ~]$ ./a.out

Parent Process

Fibonacci Series

Enter the limit for the series:4

Child Process

Fibonacci Series is:

0

1

1

2


RESULT:

Thus C program for implementing interprocess communication using pipes has been executed successfully.