sirisilla
04-09-2008, 06:31 AM
What does a socket call does from kernel perspective.Will it create a structure (allocates memory) and initialises it? and is this structure (allocated memory) is accessible from kernel mode only? and how does the socket call changes the mode of the processor from un-privileged to privilege mode?
what is meant by datagram of the socket?
bwkaz
04-09-2008, 07:01 PM
What does a socket call does from kernel perspective. See net/socket.c in any Linux kernel source tree. The sys_socket() function is what handles the socket() system call.
and is this structure (allocated memory) is accessible from kernel mode only? Nothing in kernel mode is accessible from user mode (except in the case of a bug or an intentional interface to expose the memory). So yes, this structure can only be read from or written to while in kernel mode.
and how does the socket call changes the mode of the processor from un-privileged to privilege mode? The same way every single system call does, and it depends on your CPU. ;) On x86, a system call is made with either the SYSENTER instruction, or the INT instruction. (INT calls a software interrupt, which causes the CPU to change protection levels. SYSENTER does something similar, though I'm not exactly sure how. Supposedly it's faster, though.)
All system calls are made through software interrupt 128 (0x80) on x86 (if it's using INT instead of SYSENTER). The kernel tells which system call was made depending on the value of one of the CPU registers, and it determines the parameters from another set of registers. These values get loaded before the INT or SYSENTER happens.
what is meant by datagram of the socket? Um, the question doesn't make a lot of sense, so I'm not sure how to answer it? :)
A datagram is a block of bytes. Sending a datagram over a socket (if that's what you meant) means transporting that block of bytes, as a unit, to the other end. ("As a unit" means that either the entire datagram makes it, or none of the data makes it. You can't ever receive part of a datagram.) Most of the time, datagram transports are unsequenced and unacknowledged, so the receiver will not receive some datagrams, and will read others out of order.
If you create a SOCK_DGRAM socket, then you're telling the kernel that you will be transporting datagrams over it (or receiving datagrams from it), so the kernel doesn't have to ensure that each datagram is acknowledged, but it does have to keep each one together. (You can also create a SOCK_STREAM socket, which tells the kernel to never lose or reorder any data, but to get rid of all data boundaries. It's a stream, where any number of bytes can be read from it or written to it at a time, not a datagram-by-datagram transport.)