Friday, October 10, 2008


FRIDAY, OCTOBER 10, 2008

Lab exam question no.3

/*
Three process p1,p2 & p3.
p1 is to create a named pipe & to send a string through that pipe,
p1 should read the msg thrgh keyboard.
p2 is to read msg frm named pipe and to reverse string.
now create a pipe and send the reversed string to the pipe
process p3 read the reversed string from the pipe
ok this is the question??
*/


#include
#include
#include
#include
#include

int main()
{
int pid,fd[2],fdo,i,len;
char msg[20],temp;
mkfifo("fifo",0777); // creating a named pipe
fdo = open("fifo",O_RDWR); // opening the named pipe.
pipe(fd); // creating a pipe.


printf("In Process 1.\n\tEnter a message : ");
scanf("%s",msg);
len = strlen(msg); // length of the message 

/* write the message to the named pipe */ 
printf("\tWriting %s to a named pipe\n",msg);
write(fdo,msg,len);

pid=fork();
if(pid == 0)
{

printf("\nIn Process 2\n\tReading from named pipe.\n"); 
read(fdo,msg,len);
printf( "\tMessage read : %s\n", msg);

for(i=0;i{
temp = msg[i];
msg[i] = msg[len-1-i];
msg[len-1-i] = temp;
}
printf("\tWriting %s to a pipe.\n",msg);
write(fd[1],msg,len);

}


else if(pid>0)
{

pid = fork();

if(pid == 0)
{
printf("\nIn Process 3\n\tReading from the pipe.\n");
read(fd[0],msg,len); 
printf("\tMessage read from pipe : %s\n",msg);
}
printf("\n");
}
}



/*
A sample output. 

dipin@dipin-desktop:~/programs$ ./a.out
In Process 1.
Enter a message : aneesh
Writing aneesh to a named pipe

In Process 2
Reading from named pipe.
Message read : aneesh
Writing hseena to a pipe.

In Process 3
Reading from the pipe.
Message read from pipe : hseena


aneesh@aneesh-desktop:~/programs$ 

*/

No comments: