AOS - Practical - 3.pdf

4
Practical – 3 Aim : (a) Write a program in C to implement pipe in Ubuntu. (b) Write a program to do inter process communication using Pipe. Hardware / Software Required : Oracle Virtual Machine, Ubuntu.iso file Theory : Pipes Pipes are used to allow one or more processes to have information “flow” between them. The most common example of this is with the shell. $ ls | wc –l As we’ve seen the std-out from the left side (ls) is connected to the std-in on the right side (wc -l).As far the each program is concerned, it is reading or writing as it normally does. Both processes are running concurrently. There are 2 types of pipes: 1)unnamed pipes 2)named pipes The examples we seen at the shell command line are unnamed. They are created, used and destroyed within the life a set of processes. Each end of the pipe has it’s own file descriptor. One end is for reading and one end is for writing. When you are done with a pipe, it is closed like any other file. Named pipes (fifo) have three advantages:- You don't have to start the reading/writing processes at the same time. You can have multiple readers/writers which do not need common ancestry. As a file you can control ownership and permissions.

Transcript of AOS - Practical - 3.pdf

Page 1: AOS - Practical - 3.pdf

Practical – 3

Aim : (a) Write a program in C to implement pipe in Ubuntu.

(b) Write a program to do inter process communication using Pipe.

Hardware / Software Required : Oracle Virtual Machine, Ubuntu.iso file

Theory :

Pipes

Pipes are used to allow one or more processes to have information “flow” between them. The most common

example of this is with the shell.

$ ls | wc –l

As we’ve seen the std-out from the left side (ls) is connected to the std-in on the right side (wc -l).As far the

each program is concerned, it is reading or writing as it normally does. Both processes are running

concurrently.

There are 2 types of pipes:

1)unnamed pipes 2)named pipes

The examples we seen at the shell command line are unnamed. They are created, used and destroyed within

the life a set of processes. Each end of the pipe has it’s own file descriptor. One end is for reading and one end

is for writing. When you are done with a pipe, it is closed like any other file.

Named pipes (fifo) have three advantages:-

• You don't have to start the reading/writing processes at the same time.

• You can have multiple readers/writers which do not need common ancestry.

• As a file you can control ownership and permissions.

Page 2: AOS - Practical - 3.pdf

Program :

(a) Program in C to implement pipe in Ubuntu

#include<stdio.h>

#include <sys/types.h>

#include <unistd.h>

#include <stdlib.h>

#include <string.h>

#define MSGLEN 64

int main()

{ int fd[2]; pid_t pid;

int result;

//Creating a pipe result = pipe (fd); if (result < 0) { //failure in creating a pipe perror("pipe"); exit (1); }

//Creating a child process pid = fork(); if (pid < 0) { //failure in creating a child perror ("fork"); exit(2);

}

if (pid == 0) { //Child process char message[MSGLEN];

while(1) {

//Clearing the message memset (message, 0, sizeof(message)); printf ("Enter a message: ");

scanf ("%s",message);

//Writing message to the pipe

write(fd[1], message, strlen(message));

} exit (0);

}

else { //Parent Process

char message[MSGLEN];

Page 3: AOS - Practical - 3.pdf

while (1) {

//Clearing the message buffer

memset (message, 0, sizeof(message));

//Reading message from the pipe read (fd[0], message, sizeof(message));

printf("Message entered %s\n",message);

}

exit(0);

} }

(b) Program to do inter process communication using Pipe.

#include<stdio.h>

#include<unistd.h>

#include<string.h>

main() { int p1[2],p2[2],p3[2],p4[2]; int i,j=0,k=0,l=0;

char r[10],s[10],t[10],u[10]; printf("\t PROCESS 1.ENTER THE STRING"); scanf("%s",r); pipe(p1); pipe(p2); write(p1[1],r,sizeof(r)); write(p2[1],r,sizeof(r)); int a=fork(); if(a==0) {

printf("\n\t PROCESS 2:it splits the given string\n"); read(p1[0],r,sizeof(r)); int n=strlen(r);

for(i=0;i<n/2;i++)

{ s[i]=r[i];

}

for(i=n/2;i<=n;i++)

{ t[j++]=r[i]; } pipe(p3); pipe(p4); write(p3[1],s,sizeof(s)); write(p4[1],t,sizeof(t)); int b=fork(); if(b==0)

{ printf("p4 %d\t",getpid()); printf("p2 %d\n",getppid()); read(p3[0],s,sizeof(s)); printf("\t PROCESS 4:sub string \t %s \t",s);

printf("no of char=%d \n",strlen(s));

Page 4: AOS - Practical - 3.pdf

} else { int c=fork(); if(c==0) { printf("p5 %d\t",getpid()); printf("p2 %d\n",getppid()); read(p4[0],t,sizeof(t)); printf("\t PROCESS 5:sub string \t %s \t",t);

printf("no of char=%d \n",strlen(t));

} else { wait(); printf("p2 %d\t",getpid()); printf("p1 %d\n",getppid());

} }} else { wait(); int d=fork(); if(d==0)

{

printf("p3 %d\t",getpid()); printf("p1 %d\n",getppid()); read(p2[0],r,sizeof(r));

for(i=strlen(r)-1;i>=0;i--)

{ u[l++]=r[i];

}

for(i=0;i<strlen(r);i++)

{ if(u[i]==r[i]) k++;

else continue;

}

if(k==strlen(r))

printf("\t PROCESS 3: the given string is palindrome\n"); else

printf("\t PROCESS 3: the given string is not palindrome\n");

} else

{

printf("p1 %d\t",getpid());

printf("kernal %d\t\n",getppid());

}

}

}

Conclusion :

By studying this practical we learnt to implement inter process communication using pipes in Ubuntu.