System Programming
Linux Book : Link
Semaphore and Mutex
- Different between mutex and semaphore
- Discuss a real time example for semaphore
- How to synchronize two process for accessing shared memeory
- Discuss the semaphore pthread API for semaphore
- What are pthread api used for thread creation and semaphore
- Difference between binary semaphore and mutex
- What is difference between semaphore and conditional variable ?
- Complete semaphore concept, check video
Shared Memory
- What is shared Memory ? Refer video link
- What are different api for shared memory ?
- Pthread library API Basic knowledge
- What are different param of shmget()
- shmat() API
- ftok() api for shared memeory used
- shmctl
- shmdt()
Notes:
- The initial value of the semaphore that allows only one of the many processes to enter their critical sections, is 1.
Socket Programming
Pipes:
Code example:-
#include <unistd.h>
int main()
{
int pfd[2], fv;
pipe(pfd);
fv = fork();
if (fv)
{
close(pfd[0]);
dup2(pfd[1],STDOUT_FILENO);
execlp("cat","cat",NULL);
}
else
{
close(pfd[1]);
dup2(pfd[0],STDIN_FILENO);
execlp("tr","tr"," ","x",NULL);
}
return 0;
}
Important point: comment out the lines that close the unneeded ends of the pipe, recompile, and run. Looks like it runs just fine, right? Wrong! If you do a ps you'll see that the tr process is still running. Why? Because it's reading from a pipe whose write end is still open in some process ... namely the tr process itself. So close those unneeded pipe ends!
Second point: We really should've put in a check for failure in the calls to pipe and fork.
Third point: Writing to a pipe whose read end is closed (i.e. no process has an open descriptor to its read end) causes a SIGPIPE signal to be sent.
Fourth point: Pipes provide synchronous I/O for IPC unlike signals. Processes communicating via pipes must be running on the same host, i.e. processes on different computers cannot communicate via pipes. We'll later learn about sockets that allow for pipe-like communication between processes on different hosts.
Random interview Questions
- Which IPC is faster for process communication?
- Fastest way to exchange data between C++ and Python?
- What is the theoretical maximum number of open TCP connections that a modern Linux box can have
what is the disadvantage of Named pipe ?
they do not scale on multiple computers like sockets since they rely on filesystem (assuming shared filesystem is not an option)
- If you do not need speed, sockets are the easiest way to go!
- If what you are looking at is speed, the fastest solution is shared Memory, not named pipes.
What is the theoretical maximum number of open TCP connections that a modern Linux box can have ?
Ports are 16-bit numbers, therefore the maximum number of connections any given client can have to any given host port is 64K.However, multiple clients can each have up to 64K connections to some server's port, and if the server has multiple ports or either is multi-homed then you can multiply that further.
Which IPC is faster ?
What is difference between Named pipe and pipes ?
What is demerits of using pipe ipc ?
How many tuples are there in a connection?
A TCP connection is identified by a 5-tuple. That means one tuple, with 5 elements. The five elements are:
1. Protocol. This is often omitted as it is understood that we are talking about TCP, which leaves 4.
2. Source IP address.
3. Source port.
4. Target IP address.
5. Target port.
Import link for Socket info: Link
Comments
Post a Comment