Programs

28 and 29: Write a program to create a child process. The parent process reads the name of the user from the user and sends it to the child process. The child process then checks for the user name in a file it maintains and if the user name is found in the file it send the current system time to the parent process. The parent process then displays it on the screen. Implement the above using message queues as inter-process communication.
Mesg.h
#include
#include
#include
#include

#define MKEY1 4164L
#define MKEY2 4266L

#define PERMS 0666

typedef struct
{

long mtype;
char mdata[50];
}
mesg;

server.c

#include “mesg.h”
#include
#include
mesg msg;
main()
{
time_t timer;
char buf[10],*date,b[10];
int k,count=0;
FILE *f;
int mq_id1,mq_id2;
int n;
if( ( mq_id1=msgget(MKEY1,IPC_CREAT|0666) ) < 0)
{
printf("Server:error openenig queue");
exit(1);
}
msgrcv(mq_id1,&msg,50,10L,0);
if((mq_id2=msgget(MKEY2,IPC_CREAT|0666))<0)
{
printf("sender:error creating queue");
exit(1);
}

f=fopen("s.txt","r");
while (!feof(f))
{

k=strlen(msg.mdata);
msg.mdata[k]='';
fscanf(f,"%s",buf);

printf("%d %d \n",strlen(msg.mdata),strlen(buf));
if( (strcmp(msg.mdata,buf))==0)
{
printf("entered \n");
count++;
break;
}

}
fclose(f);
printf("coutt %d",count);
if(count==0)
{

strcpy(msg.mdata,"no");
}
else
{

timer=time(NULL);
date=asctime(localtime(&timer));
strcpy(msg.mdata,date);
}
msgsnd(mq_id2,&msg,50,0);
}

Client.c
#include "mesg.h"
mesg msg1,msg2;
main()
{
int mq_id1,mq_id2;
int n;
if((mq_id1=msgget(MKEY1,PERMS|IPC_CREAT))<0)
{
printf("Client:error creating queue");
exit(1);
}
if((mq_id2=msgget(MKEY2,PERMS|IPC_CREAT))<0)
{
printf("Client:error creating queue");
exit(1);
}
msg1.mtype=10L;
n=read(0,msg1.mdata,50);

msg1.mdata[n-1]=''; // to find the exact lenght of the word
msgsnd(mq_id1,&msg1,50,0);
msgrcv(mq_id2,&msg2,50,10L,0);
write(1,msg2.mdata,50);
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 31 other followers