What is a process?
- A process is in main memory, ready for execution(waiting for CPU).
- A process is not a program, but each program may have multiple processes, running concurrently or one after another.
- Same program can be run as multiple processes concurrently(at the same time).
- A body of code to execute
- A reserved piece of memory
- It's own set of file descriptors
- A unique process id
How do processes communicate with each other?
- Shared Memory (common areas in ram - main memory) - Shared memory allows processes to share parts of their virtual address space. Shared memory regions allow processes to communicate by reading and writing to and from same region of memory.
- Signals (interupts like SIGINT, SIGKILL,..)
- Message Queues (data + server process) - Messages allow processes to send formatted data streams to arbitrary processes. Thus messages allow processes to cooperatively function by exchanging messages.
- Semaphores (mainly for OS resource control)- Allow processes to synchronize execution using flags - This is used for things like OS telling if one has read access, write access. Semaphores allow two or more processes to coordinate access to shared resources and other behaviours.
- Pipes(The '|' character from the command line)
- FIFO (A named Pipe, made with mknod)
- Sockets (point-to-point, 2 way communication)
Shared Memory
Shared memory is about two processes sharing a common segment of memory that they can both read to and write from to communicate with one another. Just a chunk of memory.
Because it’s just memory, shared memory is the fastest IPC mechanism of them all.
/dev/shm is an implementation of shared memory. It is an efficient means of passing data between programs. One program will create a memory portion, which other processes (if permitted) can access. This will result into speeding up things on Linux. Recent Linux distributions based on the 2.6 kernel have started to offer /dev/shm as shared memory in the form of a RAM disk, more specifically as a world-writable directory (a directory in which every user of the system can create files) that is stored in memory. Both the RedHat and Debian based distributions include it by default. Support for this type of RAM disk is completely optional within the kernel configuration file.
The main problem with shared memory is the race-condition. One very simple way of solving is semaphores.
Semaphores
They allow different processes to synchronize their access to certain resources.
In Computer Science, the most common and simplest kind of semaphore is called a binary semaphore because they have two states locked or unlocked. These act much like traffic lights.
When a process wants exclusive access to a resource, shared memory being an example, they attempt to lock the semaphore associated with that resource. If the semaphore they are attempting to lock is already locked, the caller is suspended, otherwise they are granted to lock. When you’ve finished doing whatever you wanted to do, you unlock the resource and any processes that have attempted to lock that semaphore in the meantime are woken up again to attempt the lock again. This way only one process can have access to the resource at once.
Pipes
- Pipes are one way stream to send data between two programs. Mainly, pipes can be defined as one way byte streams between related processes(parent/child interaction).
- The system assures that the order of output is same as the order of input(FIFO) and that no data is lost in communications.
- Programmed with the C function pipe() from <sys/unistd.h> or at the command line with "|"
- Modern implementations use sockets as the basis for pipes.
FIFO
Socket
Called BSD sockets, since the idea of sockets was developed by BSD.
- Allow point-to-point, two way communication between processes.
- They serve as endpoints in communication. The end points have IP addresess or filenames.
- All sockets have a type(selected when created) and options(chosen on fly - say like how long a socket should be open till they get timed out)
- They exist in "domains" which dictate how they are addressed and their communication protocols.
- There are 23 different socket domains. The most commonly used are the UNIX and INET domains.
- Created without names and must be bound to one.
- Communication between sockets is not symmetrical, but follows a client/server model.
There are 4 major types of BSD sockets, which dictates how data is sent and received and which protocols can be used to govern this exchange of information.
BSD socket types
full-duplex means data can be send back and forth at same time.
- Stream - full duplex, sequential stream of data.
- Datagram - full-duplex, non-sequential data transmission. Used for intermittent transmission.
- Sequential Packet - full duplex, reliable, sequential connection for datagrams of fixed size.
- Raw - An unregualted socket for constructing new socket types and protocols.
Sockets will only connect with others of the same type and protocol.
Let us see about two important socket domains - Unix socket and Inet socket
Unix sockets
- used for local communication between processes
- They are bound to pathnames, which do not already exist in the filesystem (/tmp directory)
How to find them?
netstat -an --unix
Proto RefCnt Flags Type State I-Node Path
unix 2 [ ACC ] STREAM LISTENING 14270 /tmp/orbit-gdm/linc-8db-0-41bf49d5a8a9b
unix 2 [ ACC ] STREAM LISTENING 14041 @/tmp/gdm-greeter-NhVFwyLS
unix 2 [ ACC ] STREAM LISTENING 14342 /tmp/orbit-gdm/linc-8d4-0-28534edab128c
unix 2 [ ACC ] STREAM LISTENING 12082 /var/run/acpid.socket
unix 2 [ ACC ] STREAM LISTENING 7945 @/com/ubuntu/upstart
unix 2 [ ACC ] STREAM LISTENING 14477 /tmp/orbit-gdm/linc-8ee-0-1aab978fd6906
unix 2 [ ACC ] STREAM LISTENING 14481 /tmp/orbit-gdm/linc-8f0-0-52db2ebee2075
unix 2 [ ACC ] STREAM LISTENING 14485 /tmp/orbit-gdm/linc-8ea-0-6c2623f0e22da
unix 2 [ ACC ] STREAM LISTENING 10925 /var/run/rpcbind.sock
unix 2 [ ACC ] STREAM LISTENING 14602 /tmp/orbit-gdm/linc-8fa-0-4933106a2345e
unix 2 [ ACC ] STREAM LISTENING 13129 public/cleanup
unix 2 [ ACC ] STREAM LISTENING 14184 @/tmp/gdm-session-sYKCyvao
unix 2 [ ACC ] STREAM LISTENING 13969 @/tmp/.X11-unix/X0
unix 2 [ ACC ] STREAM LISTENING 14162 @/tmp/dbus-2sHX0FJO2L
unix 2 [ ACC ] STREAM LISTENING 11167 /var/run/dbus/system_bus_socket
unix 2 [ ACC ] STREAM LISTENING 11387 /var/run/avahi-daemon/socket
unix 2 [ ACC ] STREAM LISTENING 14803 /tmp/orbit-gdm/linc-8fb-0-6c6f4de337f7f
unix 2 [ ACC ] STREAM LISTENING 14831 /tmp/orbit-gdm/linc-8fc-0-4e60774e3fdc5
unix 2 [ ] DGRAM 8114 @/org/kernel/udev/udevd
unix 2 [ ACC ] STREAM LISTENING 15045 /var/lib/gdm/.pulse/2d622416adabbaa7f7ffa3c10000000f-runtime/native
unix 23 [ ] DGRAM 10630 /dev/log
unix 2 [ ] DGRAM 11130 /var/run/fcm/fcm_clif
unix 2 [ ] DGRAM 12269 @/org/freedesktop/hal/udev_event
unix 2 [ ACC ] STREAM LISTENING 11886 /var/run/cups/cups.sock
unix 2 [ ACC ] STREAM LISTENING 12644 /var/run/pcscd.comm
unix 2 [ ACC ] STREAM LISTENING 14205 @/tmp/.ICE-unix/2260
unix 2 [ ACC ] STREAM LISTENING 13136 private/tlsmgr
unix 2 [ ] DGRAM 11128 @0001b
unix 2 [ ACC ] STREAM LISTENING 13140 private/rewrite
unix 2 [ ACC ] STREAM LISTENING 13144 private/bounce
unix 2 [ ACC ] STREAM LISTENING 13148 private/defer
unix 2 [ ACC ] STREAM LISTENING 13152 private/trace
unix 2 [ ACC ] STREAM LISTENING 13156 private/verify
unix 2 [ ACC ] STREAM LISTENING 13160 public/flush
unix 2 [ ACC ] STREAM LISTENING 13164 private/proxymap
unix 2 [ ACC ] STREAM LISTENING 13168 private/proxywrite
unix 2 [ ACC ] STREAM LISTENING 13172
INET domain sockets
- used for remote connection between processes
- They are bound to an IP address(dotted quads) and a port number(65,356).
netstat -an --inet
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 127.0.0.1:199 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:52053 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN
tcp 0 52 192.168.1.7:22 192.168.1.2:52471 ESTABLISHED
udp 0 0 0.0.0.0:5353 0.0.0.0:*
udp 0 0 0.0.0.0:111 0.0.0.0:*
udp 0 0 0.0.0.0:631 0.0.0.0:*
udp 0 0 0.0.0.0:768 0.0.0.0:*
udp 0 0 0.0.0.0:901 0.0.0.0:*
udp 0 0 0.0.0.0:51085 0.0.0.0:*
udp 0 0 0.0.0.0:43032 0.0.0.0:*
udp 0 0 0.0.0.0:161 0.0.0.0:*
udp 0 0 0.0.0.0:68 0.0.0.0:*
Socket Protocols
There are two major protocols, one for stream sockets(TCP) and the other for datagram sockets(UDP)
No comments:
Post a Comment