Friday, October 10, 2008



Lab exam question no.1

write an c program to create 3 processers p1 p2 p3 , process p1 is to read a message through the keyboard and to write that message in a pipe after filtering the vowels process p2 is to read that message from the pipe and to write that message into a named pipe process 3 is to read that message queue and to display that in screen

#include
#include
#include
#include

int main()
{
int pid,fd[2],fdo,i;
char s[10],msg[20],ch[2];
mkfifo("fifo",0777);
fdo = open("fifo",O_RDWR);
pipe(fd);


printf("In Process 1 \n Enter a message : ");
scanf("%s",msg);

for(i=0;msg[i]!='\0';i++)

{

if(msg[i]!='a' && msg[i]!='e' && msg[i]!='i' && msg[i]!='o' && msg[i]!='u' && msg[i]!='A' && msg[i]!='E' && msg[i]!='I' && msg[i]!='O' && msg[i]!='U')
{
write(fd[0],msg[i],1);
printf("%c",msg[i]);
}

}
write(fd[1],"\0",1);
pid=fork();
if(pid == 0)
{

printf("\nIn Process 2 : writing from pipe to a named pipe.\n");
read(fd[0],ch,1);
while(ch[0]!='\0')

write(fdo,ch,1);
read(fd[0],ch,1);
}
}

else if(pid>0)
{

pid = fork();

if(pid == 0)
{
printf("\nIn Process 3 \nThe message from the named pipe is : \n");
read(fdo,ch,1); 
while(ch[0]!='\0')
{
read(fdo,ch,1);
printf("%c",ch[0]);

}
printf("\n");
}


}

No comments: