National University of Computer and Emerging Sciences: Laboratory Manual
National University of Computer and Emerging Sciences: Laboratory Manual
Laboratory Manual
for
Operating Systems Lab
(CS 205)
Page 1 of 5
Spring 2020 OS-4c
Objectives
Creating a new process using fork
Information sharing between processes using pipes
Important Note:
Comment your code intelligently.
Indent your code properly.
Use meaningful variable names.
Use meaningful prompt lines/labels for input/output.
Use meaningful project and C/C++ file name
1 Pipes
Ordinary pipes allow two processes to communicate in standard producer consumer fashion: the
producer writes to one end of the pipe (the write-end) and the consumer reads from the other end
(the read-end). As a result, ordinary pipes are unidirectional, allowing only one-way
communication. If two-way communication is required, two pipes must be used with each pipe
sending data in a different direction.
Data written to the write end of a pipe can be read from the read end of the pipe.
2 Creating a pipe
On UNIX and Linux systems, ordinary pipes are constructed using the function
Page 2 of 5
Spring 2020 OS-4c
Reference: http://linux.die.net/man/2/pipe
3 Example
Listing 1
#include <iostream>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main()
{
char buf[5];
Page 3 of 5
Spring 2020 OS-4c
return 0;
Listing 2
#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;
Page 4 of 5
Spring 2020 OS-4c
printf("read %s",read_msg);
/* close the write end of the pipe */
close(fd[READ_END]);
}
return 0;
}
4 Failure
When pipe() System Call Fails:
The pipe() system call fails for many reasons, including the following:
1 At least two slots are not empty in the FDT—too many files or pipes are open in the process.
5 Inlab Questions
Design a program using ordinary pipes in which parent process sends a
message from a file named file.txt to a child process, and the child
process remove the occurrences of all the special characters including
&,@,#,%,*,?,&,$,”,and ~. And send the modified version back to the
parent process and the parent process writes the modified data to the
file updated.txt . This will require using two pipes, one for sending the
original message from the first to the second process, and the other for
sending the modified message from the second back to the first process.
Note: Use only read, write and open system calls. Use of Cin, cout, prinf, ofstream, ifstream
etc. will result in zero marks.
Help:
man 2 open
man 2 read
man 2 write
Page 5 of 5