Friday 12 January 2018

C PROGRAM TO IMPLEMENT PRODUCER CONSUMER PROBLEM (WITH ALGORITHM)

AIM : To implement producer consumer problem

ALGORITHM :

algorithm for wait(s)

step 1 : decrement the value of s
step 2 : return s

algorithm for signal(s)

step 1 : increment the value of s
step 2 : return s

algorithm for producer

step 1 : set mutex = wait(mutex)
step 2 : set full = signal(full)
step 3 : set empty = wait(empty)
step 4 : increment x
step 5 : display producer produces the the item x

algorithm for consumer

step 1 : set mutex = wait(mutex)
step 2 : set full = wait(full)
step 3 : set empty = signal(empty)
step 4 : display consumer consumes item x;
step 5 : decrement x
step 6 : set mutex = signal(mutex)

algorithm for main

step 1 : create a menu driven option
 1.1 : producer
 1.2 : consumer
 1.3 : exit
step 2 : read the choice n
step 3 : if ch==1.1 then
 3.1 : if mutex==1 and empty!=0
  3.1.1 : call producer
 3.2 : else display buffer full
step 4 : if ch==1.2 then
 4.1 : if mutex==1 and full!=0 then
  4.1.1 : call consumer
 4.2 : else display buffer is empty
step 5 : if ch==1.3 then
 5.1 : exit()


PROGRAM : 

#include<stdio.h>
#include<stdlib.h>
int mutex=1,full=0,empty=3,x=0;
int wait(int s)
{
return --s;
}
int signal(int s)
{
return ++s;
}
void producer()
{
mutex=wait(mutex);
full=signal(full);
empty=wait(empty);
x++;
printf("\nproducer produces the item %d",x);
mutex=signal(mutex);
}
void consumer()
{
mutex=wait(mutex);
full=wait(full);
empty=signal(empty);
printf("\nthe consumer consumes the item %d",x);
x--;
mutex=signal(mutex);
}

void main()
{
int ch;
do
{
printf("\n1.producer 2.consumer 3.exit\n");
printf("\nenter the choice:");
scanf("%d",&ch);
switch(ch)
{
case 1 : if(mutex==1 && empty!=0)
 producer();
case 2 : if(mutex==1 && full!=0)
  consumer();
 else
   printf("\nbuffer is empty");
case 3 : exit(0);
}
}while(ch<=3);
}



Friday 5 January 2018

8051 PROGRAM TO FIND THE LARGEST ELEMENT IN AN ARRAY(WITH ALGORITHM)

AIM : To find the largest element in an array

ALGORITHM :

step 1   : start
step 2   : load DPTR with address 4200
step 3   : load 50h with 00
step 4   : move the content of location pointed by DPTR to A
step 5   : move A to the reg R0
step 6   : increment DPTR
step 7   : move the content of location pointed by DPTR to A
step 8   : compare 50h to A and jump to step 14 if not equal
step 9   : increment DPTR
step 10 : decrement RO reg. and if zero flag not set then go to step 7
step 11 : move 50h to A
step 12 : move the content of A to location pointed by DPTR
step 13 : stort jump to step 13
step 14 : jump to step 9 if any carry
step 15 : move A to 50h
step 16 : stop

PROGRAM

ADDRESS   LABEL   MNEMONICS           

4100                          MOV DPTR,#4200   
4103                          MOV 50H,#00
4106                          MOVX A,@DPTR
4107                          MOV R0,A
4108                          INC DPTR
4109            L1          MOVX A,@DPTR
410A                          CJNE A,50H,4115
410D           L2           INC DPTR
410E                          DJNE R0,L1
4110                           MOV A,50H
4112                           MOVX @DPTR,A
4113            L3           SJMP L3
4115                           JC L2
4117                           MOV 50H,A
4119                           SJMP L2


OUTPUT :

4200 : 05
4201 : 09
4202 : 06
4203 : 





Tuesday 2 January 2018

C PROGRAM FOR INTER PROCESS COMMUNICATION USING SHARED MEMORY (WITH ALGORITHM)

AIM : To implement inter-process communication using shared memory

ALGORITHM :

step 1 : start
step 2 : declare shid, pid as integer and *msg as char
step 3 : get the shid of existing shared memory using shmget()
step 4 : attach the existing shared memory to address using shmat()
step 5 : create a process using fork()
step 6 : if it is a parent process then do
             a) read the message
             b) write it to shared memory
step 7 : if it is a child process then read the message from shared memory and print it.
step 8 : stop

PROGRAM :

#include<stdio.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/shm.h>
#include<sys/unistd.h>

int main()
{

  int pid, shid;
  char *msg;
  shid = shmget(IPC_PRIVATE,1,IPC_CREAT|0666);
  msg = shmat(shid,0,0);
  pid = fork();
  if(pid>0)
  {
      printf("PARENT PROCESS");
      printf("\n enter the message to child : ");
      scanf("%s",msg);
      printf("\n message is written into the shared memory by parent");
   }
   if(pid == 0)
   {
       sleep(6);
       printf("\n CHILD PROCESS");
       printf("\n child reads the message %s : ",msg);
    }
  exit(0);
}


OUTPUT 

compile : gcc filename.c
run         : ./a.out

enter the message to child : mother
control in parent process
message is written into the shared memory by parent
control in child process
child reads the message
          mother