Inter process communication using Linux System Calls

28
Inter-Process Communication using Linux System Call API and Standard API Jyoti Prakash 6 th Semester , Information Technology

description

 

Transcript of Inter process communication using Linux System Calls

Page 1: Inter process communication using Linux System Calls

Inter-Process Communication us ing

Linux System Cal l API and Standard API

Jyoti Prakash6th Semester , Information Technology

Page 2: Inter process communication using Linux System Calls

Inter Process Communication

● Ability to communicate between two cooperating process

● Used in many contexts , such as a producer – consumer problem

Process A(Producer)

Process B1(Consumer)Process B4

(Consumer)

Process B2(Consumer)

Process B3(Consumer)

PassesInformation

Passes Information

Passes Information

PRODUCER CONSUMER

Page 3: Inter process communication using Linux System Calls

Methods of Inter-Process Communication

● Methods of IPC

– Pipes– FIFO

– Message Queues

– Shared Memory

● There's another method SOCKETS!!! .... But not that used in this context as the others above

Page 4: Inter process communication using Linux System Calls

Pipes● A pseudofile which redirects data from one process

to other.● Below the “cmd1” and “cmd2” takes input from

stdin and outputs to stdout , but a pipe in-between passes information from “stdout of cmd1” to “stdin of cmd2” which outputs to stdin i.e monitor

● e.g. ls -aRl home | less ; cat file1 file2 file3 | grep sample

● Simplest of all the IPCs

Page 5: Inter process communication using Linux System Calls

Pipes Implementation using Linux SysCall and Standard API

● High Level – User level implementation , end-user not required to

interact with the kernel

– Two functions defined in stdio.h● FILE *popen(const char *command, const char

*open_mode)● int pclose(FILE *stream_to_close)

● Low – Level– Kernel level implementation, end-user required to

interact with the kernel

– System call available in unistd.h● int pipe(int file_descriptor[2])

Page 6: Inter process communication using Linux System Calls

High Level Implementation

● A simple implementation of high level pipes

Page 7: Inter process communication using Linux System Calls

Low Level Implementation

A naive implementation of pipe – passing data in the same process (complete useless , no IPC)

Page 8: Inter process communication using Linux System Calls

Another Low Level Implementation

Use fork() and pipe() -- The logical implementation of pipe

Page 9: Inter process communication using Linux System Calls

Methods of Inter-Process Communication

● Methods of IPC – Pipes

– FIFO– Message Queues

– Shared Memory

● There's another method SOCKETS!!! .... But not that used in this context as the others above

Page 10: Inter process communication using Linux System Calls

FIFO● Stands for First In First Out (obvious in CS)● A named pipe implementation on Linux● Unidirectional in nature (Simplex Communication)● Two system calls available in sys/types.h

– int mkfifo(const char *pathname, mode_t mode)

– int mknod(const char *path, mode_t mode, dev_t dev)

Process A Process B

FIFO Queue File stored in

Somelocation

Page 11: Inter process communication using Linux System Calls

Implementation using mkfifo syscall

FIFO Producer

Contd ...

Page 12: Inter process communication using Linux System Calls

FIFO Client

Page 13: Inter process communication using Linux System Calls

Methods of Inter-Process Communication

● Methods of IPC – Pipes

– FIFO

– Message Queues– Shared Memory

● There's another method SOCKETS!!! .... But not that used in this context as the others above

Page 14: Inter process communication using Linux System Calls

Message Queues● Released in AT & T System V.2 ( a release of Unix)● Part of System V IPC, the other being Shared

Memory and Semaphores● Implementation of Message Passing IPC● Message queue will remain (until deleted) , even if

the process have existed● Can be made unidirectional or bidirectional

Process 1 Process 2Message Queue

Page 15: Inter process communication using Linux System Calls

Implementation in Linux● Uses four functions , defined in msg.h on Unix or a

Linux distro– int msgctl(int msqid, int cmd, struct msqid_ds *buf)

● Control the message queue, by specifying command in cmd

– int msgget(key_t key, int msgflg)● Get a queue with the key specified

– int msgsnd(int msqid, const void *msgp, size_t msgsz, int msgflg)

● Sends the message to the queue– ssize_t msgrcv(int msqid, void *msgp, size_t msgsz, long

msgtyp,int msgflg)● Receives a message from the queue

Contd ...

Page 16: Inter process communication using Linux System Calls

Producer – Consumer Implementation

Contd ...

Producer Program Implementing message queue

Page 17: Inter process communication using Linux System Calls

Implementation (... contd)

Producer Program Implementing message queue

Page 18: Inter process communication using Linux System Calls

Implementation

Consumer for Message Queue

Page 19: Inter process communication using Linux System Calls

Methods of Inter-Process Communication

● Methods of IPC – Pipes

– FIFO

– Message Queues

– Shared Memory● There's another method SOCKETS!!! .... But not

that used in this context as others

Page 20: Inter process communication using Linux System Calls

Shared Memory

● One of the AT & T System V IPC ● Simple concept, keep a memory segment , which can

be shared by two or more processes● Real Implementation : A process creates a memory

segment for IPC from its logical address space and appears in that. Other process can attach that available space for communication.

● One of the most simplest and logical implementation of IPC

Page 21: Inter process communication using Linux System Calls

Visual Representation of Shared Memory

Page 22: Inter process communication using Linux System Calls

Implementation

● There are four functions available in sys/shm.h– void *shmat(int shm_id, const void *shm_addr, int shmflg);

● Attached the memory segment (pointed by shm_id) to the memory space of calling process

– int shmctl(int shm_id, int cmd, struct shmid_ds *buf)● Performs the control operation, specified by cmd)

– int shmdt(const void *shm_addr)● Detaches the memory segment

– int shmget(key_t key, size_t size, int shmflg)● Allocates a shared memory segment

Page 23: Inter process communication using Linux System Calls

Implementation

Shared memory header file shm_com.h

Page 24: Inter process communication using Linux System Calls

Implementation - Producer

Page 25: Inter process communication using Linux System Calls

Implementation - Consumer

Consumer Process Code for Shared Memory

Page 26: Inter process communication using Linux System Calls

References● Modern Operating Systems

– Andrew S. Tanenbaum

● Operating Systems Design and Implementation

– Andrew S. Tanenbaum

● Operating System Concepts

– Bear, Galvin and Silberschatz

● Beginning Linux Programming

– Neil Matthew and Richard Stones

● Beej's Guide to IPC

● MIT OCW on Operating System Engineering

– www.ocw.mit.edu/6-828-fall-2006/6-828-fall-2006/contents/index.htm

Page 27: Inter process communication using Linux System Calls

Any Queries ???

Page 28: Inter process communication using Linux System Calls

Thank you