0% found this document useful (0 votes)
72 views15 pages

Lab 4

Here is a C program that implements the described functionality using two ordinary pipes: #include <stdio.h> #include <unistd.h> #include <ctype.h> int main() { int fd1[2], fd2[2]; pipe(fd1); pipe(fd2); if(fork() == 0) { // child process char buf[100]; read(fd1[0], buf, sizeof(buf)); for(int i=0; i<strlen(buf); i++) buf[i] = togglecase(buf[i]); write(fd2[1], buf, sizeof(buf)); return 0
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
72 views15 pages

Lab 4

Here is a C program that implements the described functionality using two ordinary pipes: #include <stdio.h> #include <unistd.h> #include <ctype.h> int main() { int fd1[2], fd2[2]; pipe(fd1); pipe(fd2); if(fork() == 0) { // child process char buf[100]; read(fd1[0], buf, sizeof(buf)); for(int i=0; i<strlen(buf); i++) buf[i] = togglecase(buf[i]); write(fd2[1], buf, sizeof(buf)); return 0
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 15

Operating System I

Lab 4
Chapter 3: Processes

1
Mindmap

2
Interprocess Communication
Introduction

▪ Process can be either Independent or Cooperating process according to whether it’s affecting or
affected by other processes.
▪ Interprocess communication allows cooperating processes to exchange data ( Send data – Receive data).
▪ Interprocess communication can be done through either shared memory or Message passing.
Interprocess Communication
Introduction
The shared-memory model:
● a region of memory that is shared by the
cooperating processes is established.
● Processes can then exchange information by
reading and writing data to the shared region.
● In shared-memory systems, system calls are
required only to establish shared-memory
regions.
● Once shared memory is established, all
accesses are treated as routine memory
accesses, and no assistance from the kernel is
required so it’s Faster than Message Passing.
Interprocess Communication
Introduction
The message-passing model:
● communication takes place by means of
messages exchanged between the
cooperating processes.
● Message passing is useful for exchanging
smaller amounts of data, because no
conflicts need be avoided.
Pipes |
▪ A pipe acts as a conduit (channel ) allowing two processes to
communicate.
▪ Pipes were one of the first IPC mechanisms in early UNIX
systems.
▪ There are two common types of pipes used on both UNIX and
Windows systems: ordinary pipes and named pipes.
Pipes |
In implementing a pipe, four issues must be considered:
1. Does the pipe allow bidirectional communication, or is communication
unidirectional?
2. If two-way communication is allowed, is it half duplex (data can travel only one
way at a time) or full duplex (data can travel in both directions at the same
time)?
3. Must a relationship (such as parent–child) exist between the communicating
processes?
4. Can the pipes communicate over a network, or must the communicating
processes reside on the same machine?
Ordinary Pipes
▪ Ordinary pipes allow two processes to communicate in standard producer–
consumer fashion:
1- The producer writes to one end of the pipe (the write end)
2- 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.
Ordinary Pipes
Ordinary pipes on Windows systems are termed anonymous pipes, and they behave
similarly to their UNIX counterparts:
▪ they are unidirectional and employ parent–child relationships between the
communicating processes.
▪ In addition, reading and writing to the pipe can be accomplished with the ordinary
ReadFile() and WriteFile() functions.
pipe
System Calls > Communication
On UNIX systems, ordinary pipes are constructed using the function

pipe(int fd[ ])

10
System Calls: Communication
pipe()
• This function creates a pipe that is accessed through the int fd[ ] file descriptors:
• fd[0] is the read end of the pipe.
• fd[1] is the write end.
• UNIX treats a pipe as a special type of file. Thus, pipes can be accessed using:
• ordinary read( ) and write() system calls.

Required Include Files

#include <unistd.h>

Function Definition
pipe(int fd[]);

11
System Calls:
Communication
pipe()
Named Pipes
▪ Named pipes provide bidirectional communication, and no parent–child relationship
is required.
▪ Once a named pipe is established, several processes can use it for communication.
▪ Named pipes are referred to as FIFOs in UNIX systems. Once created, they appear as
typical files in the file system.

Note:
• Ordinary pipes are designed for communication between processes that have a
parent–child relationship.
• Named pipes are more general and allow several processes to communicate.
Section 3.7
System Calls: Communication
E-Book Section 3.26
Design a program using ordinary pipes in which one process sends a string message to a second process,
and the second process reverses the case of each character in the message and sends it back to the first
process. For example, if the first process sends the message Hi There, the second process will return hI
tHERE.

Hint:
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 to the first process.

You might also like