0% found this document useful (0 votes)
103 views

National University of Computer and Emerging Sciences: Laboratory Manual

The document is a laboratory manual for an Operating Systems lab course. It provides objectives and instructions for exercises involving processes and inter-process communication using pipes. The exercises will have students create pipes between processes to share data between a parent and child process, with the child process modifying the data.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
103 views

National University of Computer and Emerging Sciences: Laboratory Manual

The document is a laboratory manual for an Operating Systems lab course. It provides objectives and instructions for exercises involving processes and inter-process communication using pipes. The exercises will have students create pipes between processes to share data between a parent and child process, with the child process modifying the data.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

National University of Computer and Emerging Sciences

Laboratory Manual

for
Operating Systems Lab

(CS 205)

Course Instructor Mr. Saad Farooq


Lab Instructor(s) Zain Ghazanfar
Zumirrah Khalid
Section CS-4C
Semester Spring 2020

Department of Computer Science


FAST-NU, Lahore, Pakistan

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.

References: Operating System concepts Page no 142 section 3.6.3.1

A pipe has a read end and a write end.

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

• int pipe(int fd[2]) -- creates a pipe

• returns two file descriptors, fd[0], fd[1].

• fd[0] is the read-end of the pipe

• fd[1] is the write-end.

Page 2 of 5
Spring 2020 OS-4c

• fd[0] is opened for reading,


• fd[1] for writing. pipe() returns 0 on success, -1 on failure and sets errno accordingly.
• The standard programming model is that after the pipe has been set up, two (or more)
cooperative processes will be created by a fork and data will be passed using read() and write().
• Pipes opened with pipe() should be closed with
close(int fd).

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>

using namespace std;

int main()

{
char buf[5];

int fd = open ("file.txt" , O_RDONLY, S_IRUSR);


ssize_t size = read(fd, buf, sizeof (buf));

write(2, buf, size);

while (size > 0)


{

size = read(fd, buf, sizeof (buf));


write(2, buf, size);

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;

/* 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);

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

You might also like