Showing posts with label C PROGRAM. Show all posts
Showing posts with label C PROGRAM. Show all posts

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);
}



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

Sunday, 27 August 2017

c program for inter-process communication using double pipe (with algorithm)

c program for inter-process communication using double pipe (with algorithm)

aim : to implement inter-process communication using double pipe

algorithm :

step 1 : start
step 2 : declare p1,p2,buff1[25],buff2[25],pid
step 3 : call system calls pipe(p1),pipe(p2)
step 4 : call the fork and initialize pid
step 5 : check if pid==0 then goto step 6 else goto step 11
step 6 : read the message to parent and store on buff1
step 7 : write the message to child using pipe p1
step 8 : call sleep()
step 9 : read the message from parent using pipe p2 and store in buff1
step 10 : print the message from parent
step 11 : read the message from child using pipe p1
step 12 : print the message from child
step 13 : read the message from child
step 14 : write the message to child using pipe2
step 15 : stop

C PROGRAM FOR INTER-PROCESS COMMUNICATION USING SINGLE PIPE (WITH ALGORITHM)

C PROGRAM FOR INTER-PROCESS COMMUNICATION USING SINGLE PIPE (WITH ALGORITHM)

AIM : TO IMPLEMENT INTERPROCESS COMMUNICATION USING SINGLE PIPE

ALGORITHM :

STEP 1 : START

STEP 2 : DECLARE P[2],PID,BUFF

STEP 3 : CALL THE SYSTEM CALL PIPE(P)

STEP 4 : CREATE A PROCESS

STEP 5 : IF THE PROCESS IS CHILD THEN GO TO STEP 6

STEP 6 : READ THE MESSAGE FOR PARENT

STEP 7 : WRITE THE MESSAGE TO PARENT

STEP 8 : CALL SLEEP()

STEP 9 : READ THE MESSAGE FROM PARENT

STEP 10 : PRINT THE MESSAGE FROM PARENT

STEP 11 : IF THE PROCESS IS PARENT, GO TO STEP 12

STEP 12 : READ THE MESSAGE FROM CHILD

STEP 13 : DISPLAY THE MESSAGE FROM CHILD

STEP 14 : READ THE MESSAGE FROM CHILD

STEP 15 : WRITE THE MESSAGE TO CHILD

STEP 16 : STOP


PROGRAM :

#include<stdio.h>
#include<string.h>
void main()
{
     int pid,p[2];
     char buff[20];
     pipe(p);
     pid=fork();
     if(pid==0)
     {
         printf("\n child process \n");
         printf("\n enter the message to parent : \n");
         gets(buff);
         write(p[1],buff,sizeof(buff));
         sleep(8);
         printf("\n message from parent : \n");
         read(p[0],buff,sizeof(buff));
         puts(buff);
         close(p[0]);
    }
    else
    {
         printf("\n parent process ");
         read(p[0],buff,sizeof(buff));
         printf("\n message from child : ");
         puts(buff);
         sleep(4);
         printf("\n enter the message to child : ");
         gets(buff);
         write(p[1],buff,sizeof(buff));
         sleep(2);
         close(p[0]);
    }
}


OUTPUT :

parent process
child process
enter the message to parent :
hai
message from child :
hai
enter the message to child :
hai
message from parent : hai



C PROGRAM TO IMPLEMENT FORK SYSTEM CALL (WITH ALGORITHM)

C PROGRAM TO IMPLEMENT FORK SYSTEM CALL (WITH ALGORITHM)

AIM : TO IMPLEMENT FORK SYSTEM CALL

ALGORITHM :

STEP 1 : START

STEP 2 : CREATE A CHILD PROCESS USING FORK

STEP 3 : IF THE PROCESSID EQUALS 0 GOTO STEP 4 ELSE GOTO STEP5

STEP 4 : IT IS A CHILD PROCESS AND GET ITS ID AND PARENT PROCESSID

STEP 5 : IF PROCESSID>0 IT IS A PARENT PROCESS AND GET ITS ID

STEP 6 : HANDLE THE SYSTEM INTERRUPT(ERROR)

STEP 7 : STOP


PROGRAM :

#include<stdio.h>
void main()
{
   int pid;
   pid=fork();
   if(pid==0)
   {
        printf("\n child process ");
        printf("\n pid of child process : %d \n pid of parent process : %d\n",getpid(),getppid());
   }
   else if(pid>0)
       printf("\n parent process : %d",getpid());
   else
       printf("\n error");
}


OUTPUT :

parent process : 3248
child process
pid of child process : 3249
pid of parent process : 1