BCN 3033: Network Programming - PowerPoint PPT Presentation

1 / 27
About This Presentation
Title:

BCN 3033: Network Programming

Description:

Interprocess communication is a generic term describing the method that ... The flow control of information in the pipe is handled automatically and invisibly ... – PowerPoint PPT presentation

Number of Views:30
Avg rating:3.0/5.0
Slides: 28
Provided by: notesU
Category:

less

Transcript and Presenter's Notes

Title: BCN 3033: Network Programming


1
BCN 3033 Network Programming
  • Chapter 3 Interprocess Communication
  • by
  • Muhammed Ramiza Ramli
  • KUKTEM

2
Overview
  • Pipes
  • FIFOs
  • Shared Memory
  • Message Queue
  • Semaphore

3
IPC
  • Interprocess communication is a generic term
    describing the method that processes use to
    communicate with each other.
  • Another method of exchanging data signal
  • having a common ancestor (parent/child)?
  • problem cannot handle long message between
    process
  • Solution through file
  • IPC have a method which will act as a file for
    read and write into

4
Pipes
  • Signal cannot handle long message between
    process.
  • To achieve this, a 'pipe' is used.
  • A pipe is a 1-way communication channel connects
    one process to another
  • in Unix. a pipe is just like a file where a
    process can 'read' from, or 'write' onto it

5
Using Pipes
  • Usage at the command prompt
  • using '' symbol.
  • syntax
  • ltprocess1gt ltprocess2gt
  • eg.
  • ls -a more
  • which tell the shell to create two processes and
    also create a 'pipe' to link the stdout of 'ls'
    to the stdin of 'more'
  • The flow control of information in the pipe is
    handled automatically and invisibly

6
Pipes C Program
  • A 'pipes' is created using the system call
    'pipe'.
  • If the system call is successful, it will return
    two file descriptor (using array)
  • one for reading
  • one for writing
  • Once created, pipes can be immediately used like
    a normal Unix file

7
Using Pipes
  • pipe system call is prototyped as follows
  • include ltunistd.hgt
  • int pipe (int filedes2)
  • If pipe succeeds, open two file descriptor and
    store thier value in the integer array filedes
  • filedes0 for reading
  • filedes1 for writing
  • pipe also return a value in integer
  • 0 on success or -1 on error

8
Opening pipe
  • Write on one end
  • Read on other end

9
Read / Write
  • Pipes are used to exchange data
  • Normally, a process call pipe and then call fork
    to spawn a child process.
  • Child inherits any open file descriptor from its
    parent, an IPC channel (pipe) has been
    established
  • Processes can communicate via pipe by writing and
    reading

10
Sample program
  • include ltstdio.hgt
  • include ltunistd.hgt
  • include ltsys/types.hgt
  • define MSGSIZE 16
  • char msg1 hello world !
  • int main()?
  • char bufMSGSIZE
  • int my_pipe2
  • pid_t pid
  • pipe(my_pipe)
  • pid fork()
  • if(pid 0)
  • write(my_pipe1,msg1,MSGSIZE)
  • else
  • sleep(1)
  • read(my_pipe0,buf,MSGSIZE)

11
One way communication
  • If a parent is sending data to the child, the
    parent closes filedes0 and writes to
    filedes1, while the child closes filedes1 and
    read from filedes0
  • if a child is feeding data back to the parent,
    the child closes filedes0 and writes to
    filedes1, while the parent closes filedes1
    and read from filedes0

12
Half Duplex
  • Parent write only and child read only

parent filedes1
child filedes0
pipe
13
Read and write
  • Duplex

14
FIFOs
  • FIFOs are also called named pipes because they
    are persistent
  • exist in the file system
  • Allowed unrelated process to exchange data
  • FIFO is a permanent pipe that exist in the system
  • Can be created either directly in the shell or
    within a program
  • Once created, FIFO have a directory entry

15
Fifo or named pipes
  • FIFOs are identical to pipes, except that
  • permanent structure
  • given a filename
  • has an owner
  • has a size
  • an associated access permissions
  • Can be opened, closed and deleted like any file

16
FIFO in shell
  • Named pipes can be created at the shell level
  • Command can be used to create FIFO are
  • mknod
  • mkfifo
  • mknod is used to created a special file in the
    UNIX file system
  • block, character, and pipe
  • syntax mknod NAME TYPE
  • eq mknod my_fifo p

17
mkfifo in shell
  • A simple way to create fifo is by using
    mkfifo command
  • syntax mkfifo option name ...
  • usually used option is -m
  • -m indicates the mode of the newly created
    FIFO
  • example
  • mkfifo -m 600 my_fifo

18
Creating a FIFO
  • The function call to create a FIFO has the same
    name as the shell interface, mkfifo
  • include ltsys/typesgt
  • include ltsys/stat.hgt
  • int mkfifo(const char pathname, mode_t mode)
  • If it succeeds, mkfifo return 0. Otherwise, is
    will return -1

19
mkfifo - program
  • include ltsys/types.hgt
  • include ltsys/stat.hgt
  • include ltstdio.hgt
  • include ltstdlib.hgt
  • int main(int argc, char argv)?
  • mode_t mode 0666
  • if(argc ! 2)
  • printf(USAGE s ltnamegt\n,argv0)
  • exit(EXIT_FAILURE)
  • mkfifo(argv1, mode)
  • return 0

20
Reading and Writing FIFOs
  • Similar to reading and writing regular files
  • Before reading and writing can be done a FIFO
    must be opened first
  • Open the file descriptor
  • int fd open(my_fifo,O_RDONLY)
  • int fd open(my_fifo,O_WRONLY)
  • To read from the fifo
  • int len read(fd, buf, PIPE_BUF)
  • PIPE_BUF is a defined size of a pipe
  • Data read will be stored in character array buf
  • To write into the FIFO
  • write(fd, buf, sizeof(buf))

21
LINUX IPC
  • We have known a simple interprocess
    communication pipe and FIFO
  • A true POSIX IPC consist of
  • Message Queues
  • Shared Memory
  • Semaphores

22
Message queues
  • A technique which allows processes on the same
    machine to exchange formatted message
  • Message queue is a linked list of messages stored
    within the kernel and identified to process by a
    message queue identifier
  • Unlike FIFOs, message in a queue can be retrieved
    in a arbitrary order.
  • Queues are similar to associative arrays

23
Concepts of 'Message'
  • Allow sending and receiving of messages among
    multiple process
  • analogy having a central mailbox in a building
    where many people can deposit and retrieve mails
    from the box
  • Each message have a recipient address (integer)
    assigned by by the sender. A process can
    selectively receive messages based on the type
  • like a mails with recipient address
  • Message stored in a queues are permanent. A
    message is removed only when a process explicitly
    removes it from the queue

24
Sending and retrieving messages
  • When a process sends a message to the queue,
    kernel creates a new message record and puts it
    at the end of the list
  • When a process retrieves a message, kernel copies
    the content to his virtual address, the discard
    from the queue

25
Message APIs
  • Several system call to be used
  • msgget open a message queue for access. If
    needed it will create the queue first
  • msgsend send a message to a message queue
  • msgrcv retrieve a message
  • msgctl manipulate the control data of a
    message queue

26
Shared Memory (shm)?
  • Shared memory is a memory region (a segment) set
    aside by the kernel for the purpose of
    information exchange between multiple process
  • Each process may map the segment into its own
    private address space
  • If one process updates the data in the segment,
    that update is immediately visible to other
    processes

27
shm
  • Two processes attach to two memory location
Write a Comment
User Comments (0)
About PowerShow.com