Assignment no.2
Operation System
Submitted to:
Mr. Usman Zia
Submitted By
Mubaraka Nazir
17581556-037
Rabia Zafar
17581556-045
Aqsa Rehman
17581556-023
Saba Noor
17581556-040
BS(Information Technology)
4th
–“A”
Submitted Date:
March 26,2019
Pipe
Pipe act as conduit that allows the two processes to communicate with each other, Although the
processes have some limitations. Data can be send through the pipes is limited.
In the implementation pipes have some issues
 Whether the pipes allow communication between two processes is the unidirectional or
bidirectional.
 If two way communication is allowed, is it half duplex or full duplex.
 If the communication is half duplex it means that data can travel in only one way at a time.
 If the communicationisfull duplex it means that data can travel in both directions at the same
types.
There must be a parent child relationship between communicating process.
Type of the pipes
 Ordinary Pipes
 Named Pipes
Both types of the pipes are used in both window and UNIX systems.
Ordinary Pipes
 Ordinary pipes allow communications in standard procedure-consumer style.
 Procedure write from one end of the pipe
 Procedure read from the other end of the pipe
 Ordinary pipes are unidirectional.
 Require parent child relationship between the communicating processes.
Onlyone endof the pipe canbe reador write the data. If one endis readingthe data than other
end will not be able to write data and if one end is writing the data than other end will not be
able to read the data
PARENT SIDE process
#include <sys/types.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#define BUFFER SIZE 25
#define READ END 0
#define WRITE END 1
int main(void)
{
char write msg[BUFFER SIZE] = "Greetings";
char read msg[BUFFER SIZE];
int fd[2];
pid t pid;
/* create the pipe */
if (pipe(fd) == -1) {
fprintf(stderr,"Pipe failed");
return 1;
}
/* fork a child process */
pid = fork();
if (pid < 0) { /* error occurred */
fprintf(stderr, "Fork Failed");
return 1;
}
if (pid > 0) { /* parent process */
/* close the unused end of the pipe */
close(fd[READ END]);
/* write to the pipe */
write(fd[WRITE END], write msg, strlen(write msg)+1);
/* close the write end of the pipe */
close(fd[WRITE END]);
}
else { /* child process */
/* close the unused end of the pipe */
close(fd[WRITE END]);
/* read from the pipe */
read(fd[READ END], read msg, BUFFER SIZE);
printf("read %s",read msg);
/* close the write end of the pipe */
close(fd[READ END]);
}
return 0;
}
CHILD SIDE
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#define BUFFER SIZE 25
int main(VOID)
{
HANDLE ReadHandle, WriteHandle;
STARTUPINFO si;
PROCESS INFORMATION pi;
char message[BUFFER SIZE] = "Greetings";
DWORD written;
Named Pipes:
 Named Pipes are more powerful than ordinary pipes.
 Communication in the named pipes is bidirectional.
 No parent relationship is compulsory in the Named Pipes for communicating process.
 Several processes can be use named pipes for communications.
 They are provided on the both UNIX and Window systems
 Usually referred to as FIFOs in the UNIX systems.
 Named Pipes are created with CreateNamePipe() function.
Implementation
/* set up security attributes allowing pipes to be inherited */
SECURITY ATTRIBUTES sa = {sizeof(SECURITY ATTRIBUTES),NULL,TRUE};
/* allocate memory */
ZeroMemory(&pi, sizeof(pi));
/* create the pipe */
if (!CreatePipe(&ReadHandle, &WriteHandle, &sa, 0)) {
fprintf(stderr, "Create Pipe Failed");
return 1;
}
/* establish the START INFO structure for the child process */
GetStartupInfo(&si);
si.hStdOutput = GetStdHandle(STD OUTPUT HANDLE);
/* redirect standard input to the read end of the pipe */
si.hStdInput = ReadHandle;
si.dwFlags = STARTF USESTDHANDLES;
/* don’t allow the child to inherit the write end of pipe */
SetHandleInformation(WriteHandle, HANDLE FLAG INHERIT, 0);
/* create the child process */
CreateProcess(NULL, "child.exe", NULL, NULL,
TRUE, /* inherit handles */
0, NULL, NULL, &si, &pi);
/* close the unused end of the pipe */
CloseHandle(ReadHandle);
/* the parent writes to the pipe */
if (!WriteFile(WriteHandle, message,BUFFER SIZE,&written,NULL))
fprintf(stderr, "Error writing to pipe.");
/* close the write end of the pipe */
CloseHandle(WriteHandle);
/* wait for the child to exit */
WaitForSingleObject(pi.hProcess, INFINITE);
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
return 0;
}
Child process
#include <stdio.h>
#include <windows.h>
#define BUFFER SIZE 25
int main(VOID)
{
HANDLE Readhandle;
CHAR buffer[BUFFER SIZE];
DWORD read;
/* get the read handle of the pipe */
ReadHandle = GetStdHandle(STD INPUT HANDLE);
/* the child reads from the pipe */
if (ReadFile(ReadHandle, buffer, BUFFER SIZE, &read, NULL))
printf("child read %s",buffer);
else
fprintf(stderr, "Error reading from pipe");
return 0;
}

17581556 045 (1)

  • 1.
    Assignment no.2 Operation System Submittedto: Mr. Usman Zia Submitted By Mubaraka Nazir 17581556-037 Rabia Zafar 17581556-045 Aqsa Rehman 17581556-023 Saba Noor 17581556-040 BS(Information Technology) 4th –“A” Submitted Date: March 26,2019
  • 2.
    Pipe Pipe act asconduit that allows the two processes to communicate with each other, Although the processes have some limitations. Data can be send through the pipes is limited. In the implementation pipes have some issues  Whether the pipes allow communication between two processes is the unidirectional or bidirectional.  If two way communication is allowed, is it half duplex or full duplex.  If the communication is half duplex it means that data can travel in only one way at a time.  If the communicationisfull duplex it means that data can travel in both directions at the same types.
  • 3.
    There must bea parent child relationship between communicating process. Type of the pipes  Ordinary Pipes  Named Pipes Both types of the pipes are used in both window and UNIX systems. Ordinary Pipes  Ordinary pipes allow communications in standard procedure-consumer style.  Procedure write from one end of the pipe  Procedure read from the other end of the pipe  Ordinary pipes are unidirectional.  Require parent child relationship between the communicating processes. Onlyone endof the pipe canbe reador write the data. If one endis readingthe data than other end will not be able to write data and if one end is writing the data than other end will not be able to read the data PARENT SIDE process #include <sys/types.h> #include <stdio.h> #include <string.h> #include <unistd.h> #define BUFFER SIZE 25 #define READ END 0 #define WRITE END 1
  • 4.
    int main(void) { char writemsg[BUFFER SIZE] = "Greetings"; char read msg[BUFFER SIZE]; int fd[2]; pid t pid; /* create the pipe */ if (pipe(fd) == -1) { fprintf(stderr,"Pipe failed"); return 1; } /* fork a child process */ pid = fork(); if (pid < 0) { /* error occurred */ fprintf(stderr, "Fork Failed"); return 1; } if (pid > 0) { /* parent process */ /* close the unused end of the pipe */ close(fd[READ END]); /* write to the pipe */ write(fd[WRITE END], write msg, strlen(write msg)+1); /* close the write end of the pipe */ close(fd[WRITE END]); } else { /* child process */ /* close the unused end of the pipe */ close(fd[WRITE END]); /* read from the pipe */ read(fd[READ END], read msg, BUFFER SIZE); printf("read %s",read msg); /* close the write end of the pipe */ close(fd[READ END]); } return 0; } CHILD SIDE #include <stdio.h> #include <stdlib.h> #include <windows.h> #define BUFFER SIZE 25 int main(VOID) {
  • 5.
    HANDLE ReadHandle, WriteHandle; STARTUPINFOsi; PROCESS INFORMATION pi; char message[BUFFER SIZE] = "Greetings"; DWORD written; Named Pipes:  Named Pipes are more powerful than ordinary pipes.  Communication in the named pipes is bidirectional.  No parent relationship is compulsory in the Named Pipes for communicating process.  Several processes can be use named pipes for communications.  They are provided on the both UNIX and Window systems  Usually referred to as FIFOs in the UNIX systems.  Named Pipes are created with CreateNamePipe() function. Implementation /* set up security attributes allowing pipes to be inherited */ SECURITY ATTRIBUTES sa = {sizeof(SECURITY ATTRIBUTES),NULL,TRUE}; /* allocate memory */ ZeroMemory(&pi, sizeof(pi)); /* create the pipe */ if (!CreatePipe(&ReadHandle, &WriteHandle, &sa, 0)) { fprintf(stderr, "Create Pipe Failed"); return 1; } /* establish the START INFO structure for the child process */ GetStartupInfo(&si); si.hStdOutput = GetStdHandle(STD OUTPUT HANDLE); /* redirect standard input to the read end of the pipe */ si.hStdInput = ReadHandle; si.dwFlags = STARTF USESTDHANDLES; /* don’t allow the child to inherit the write end of pipe */ SetHandleInformation(WriteHandle, HANDLE FLAG INHERIT, 0); /* create the child process */ CreateProcess(NULL, "child.exe", NULL, NULL, TRUE, /* inherit handles */ 0, NULL, NULL, &si, &pi); /* close the unused end of the pipe */ CloseHandle(ReadHandle); /* the parent writes to the pipe */ if (!WriteFile(WriteHandle, message,BUFFER SIZE,&written,NULL)) fprintf(stderr, "Error writing to pipe."); /* close the write end of the pipe */ CloseHandle(WriteHandle);
  • 6.
    /* wait forthe child to exit */ WaitForSingleObject(pi.hProcess, INFINITE); CloseHandle(pi.hProcess); CloseHandle(pi.hThread); return 0; } Child process #include <stdio.h> #include <windows.h> #define BUFFER SIZE 25 int main(VOID) { HANDLE Readhandle; CHAR buffer[BUFFER SIZE]; DWORD read; /* get the read handle of the pipe */ ReadHandle = GetStdHandle(STD INPUT HANDLE); /* the child reads from the pipe */ if (ReadFile(ReadHandle, buffer, BUFFER SIZE, &read, NULL)) printf("child read %s",buffer); else fprintf(stderr, "Error reading from pipe"); return 0; }