Design a system to generate unique id for 1 billion user in distributed system
Mean and Median
You are given say 20 nodes in a distributed system and each node have 1 billion numbers. Find mean and median. You can have some other nodes for co-ordination.
Mean = (sum of all the numbers) / total numbers
Median = Mid element in sorted sequence
Input = [2, 3, 4, 1]
Mean = (2+3+4+1) / 4 =2.5
For median sorted sequence is [1, 2, 3, 4]. Median is 2 or 3.
How ls works?
Distributed systems people talk about NFS
Typical low-level systems folks get into the copy-on-write implementation of fork
Filesystem folks get into finding the blocks that constitute the directory
How file descriptor is generated?
Before I start talking about File Descriptor lets first understand the Library functions and System calls.
System calls are functions that transfer control from the user process to the
operating system kernel.
Functions such as read(), write() etc are system calls.
Library functions typically provide a richer set of features. Library functions are implemented on top of system calls.
The first step is the open() system call which either open an existing file or create a new file.
Multiple parameters can be provided for the requirement. For details of these parameters visit here.
In C program, we use fopen() rather than open() and this is the difference between Library function and System Call. Library function fopen() will internally call open() and configure parameter based on input provided to fopen()
Lets deep dive now into the File descriptor. In above C code we created a FILE* which is file pointer or *file descriptor at Library function level. In reality library function fopen() will call system call open() which will get the file descriptor information from filesystem.
fopen() library call want to embed the file descriptor information with some additional information. For this purpose a struct is created called FILE and in this structure file descriptor information returned by open() system call is stored with other information.