Professional Documents
Culture Documents
Preliminary Questions Report
Preliminary Questions Report
Preliminary Questions Report
RESPONSES
Q1 What is the function htons used for?
This function is used for converting 16-bit quantities from host network to network byte order.
Htons is exactly for uint16_t hostshort.
This function is most often used in conjunction with IPv4 addresses.
https://pubs.opengroup.org/onlinepubs/9699919799/functions/V2_chap02.html#tag_15_12
Q2 What is uint32_t?
Unit32_t is a data type for variables that need to store values from 0 to 2^32-1 without any
negative values. This is important for IP addresses because for sure the size of the number is 32
bits.
https://pubs.opengroup.org/onlinepubs/9699919799/functions/V2_chap02.html#tag_15_12
Q4 What is the value of b in the following code snippet? const ssize_t a = -1; const
size_t b = (size_t) a;
As we can see, in this case ‘a’ is -1 and is ssize_t variable. This means that, as we explain in the
last question, when we want to transfer that data to a size_t, we have to be concient that size_t
data can’t be negative so we need to transform the number.
Number -1 is ‘1111 1111’ in 8 bits, so if we see this number in size_t it will be 2^64-1).
2^64-1 = 18.446.744.073.709.551.616
I tried the code in the linux terminal and this is the number I got, so I deduce that there is a 64 bit
system.
I try it in my terminal & https://pubs.opengroup.org/onlinepubs/9699919799/functions/V2_chap02.html#tag_15_12
Q5 What is the value of b in the code snippet below? const uint32_t a = 256; const
uint8_t b = (uint8_t) a;
When we want to transform one number of 32 bits to a number of 8 bits what we do is truncate
the number. So 256 in 32 bit is 0000 0001 0000 0000, and if we truncate in bit 8 we see that the
number resultant is 0000 0000, so b value in this case is 0.
I already know that & https://pubs.opengroup.org/onlinepubs/9699919799/functions/V2_chap02.html#tag_15_12
Q7 What values for the protocol parameter of the socket function are available for
AF_INET in POSIX? What does it mean that the default value for protocol in socket
is implementation defined?
The different values for the protocol parameter of the socket() function are:
● TCP protocol
● UDP protocol
● SCTP protocol
When the protocol parameter is not defined in the socket() , the behavior is implementation-
defined. This means that the protocol used will be the default protocol selected by the operating
system.
https://pubs.opengroup.org/onlinepubs/9699919799/functions/V2_chap02.html#tag_15_10
https://pubs.opengroup.org/onlinepubs/9699919799/functions/socket.html
ip(7) - Linux manual page (man7.org)
Q12 If we never close any sockets, we may eventually receive an EMFILE error code in
socket. Why?
When we have a lot of sockets open, we have this error because the system can open a limited
number of file descriptors at the same time. This number depends on the operating system. This
can be avoided by closing the sockets we are not using.
https://www.baeldung.com/linux/error-too-many-open-files &
https://pubs.opengroup.org/onlinepubs/9699919799/functions/V2_chap02.html#tag_15_10_07
Q15 Can we modify errno ourselves in our code? And should we?
Modifying errno is possible, but is not recommended. The errno variable is used for attribute a
value to an error. If we modify this it can cause a non-predictable behavior because we can
overwrite a system error that we don’t want to change.
https://pubs.opengroup.org/onlinepubs/9699919799/functions/errno.html#tag_16_110
Q16 Which errors can result from a socket function call? And from sendto? (do not
describe them the error codes, just mention them: EAGAIN)
Socket:
● EACCES
● EAFNOSUPPORT
● EINVAL
● EMFILE
● ENFILE
● ENOBUFS or ENOMEM
● EPROTONOSUPPORT
Sendto:
● EACCES
● EAGAIN or EWOULDBLOCK
● EBADF
● ECONNRESET
● EDESTADDRREQ
● EFAULT
● EINTR
● EINVAL
● EISCONN
● EMSGSIZE
● ENOBUFS
● ENOMEM
● ENOTCONN
● ENOTSOCK
● EOPNOTSUPP
● EPIPE
https://pubs.opengroup.org/onlinepubs/9699919799/functions/socket.html
https://pubs.opengroup.org/onlinepubs/9699919799/functions/sendto.html
Q17 What happens if a program terminates (e.g., exit(EXIT_FAILURE)) while still having
some open sockets (not having called close)?
If a program terminates without closing its open sockets, the operating system will liberate the
resources of that socket, but it depends on the operating system. Leaving open sockets can
behave to a malfunction (explained in Q11). Other systems can wait a period of time before
closing the sockets.
https://pubs.opengroup.org/onlinepubs/9699919799/functions/V2_chap02.html#tag_15_10
Q22 Which of the following statements regarding port 8080 is correct: in_port_t port =
htons(8080), in_port_t port = ntohs(8080), or in_port_t port = 8080? Why?
The correct form is: in_port_t port = htons(8080);
The htons() function is used to convert 16-bit value from host byte order to network byte order.
The ntohs() function is used to convert 16-bit value from network order to host order, but in this case this have no
sense because 8080 is in host order so it would fail.
The in_port_t assign directly the value to the port, and this would fail too.
https://pubs.opengroup.org/onlinepubs/7908799/xns/netinetin.h.html
Q23 Give a proper initialization code for a sockaddr_in structure for port 8080 and
address 127.0.0.0.1.
struct sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_port = htons(8080);
addr.sin_addr.s_addr = inet_addr("127.0.0.1");
Q24 Give a proper initialization code for a sockaddr_in structure for port 8080 and any
address.
struct sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_port = htons(8080);
addr.sin_addr.s_addr = INADDR_ANY;
ip(7) - Linux manual page (man7.org)
Q27 What is valgrind and how can we use it to debug memory problems?
Valgrind is an instrumentation used for building dynamic analysis tools. These tools can automatically detect
memory management and threading bugs, and profile our programs in detail. Valgrind can be used to detect other
errors from the program, but it’s mainly used to detect memory errors.
https://valgrind.org/ & https://pubs.opengroup.org/onlinepubs/9699919799/functions/V2_chap02.html#tag_15_08_03
Q28 Why is it a good practice to free all allocated memory before calling
exit(EXIT_SUCCESS)? (hint: valgrind)
It allows valgrind to check for memory leaks without false positives.
https://pubs.opengroup.org/onlinepubs/9699919799/functions/V2_chap02.html#tag_15_08_03
If address is not a null pointer, the address of the peer for the accepted connection shall be stored in the sockaddr
structure pointed to by address,
https://pubs.opengroup.org/onlinepubs/9699919799/functions/accept.html
b -> New socket. (Create a new socket with the same socket type protocol and address family as the specified
socket, and allocate a new file descriptor for that socket)
https://pubs.opengroup.org/onlinepubs/9699919799/functions/accept.html
Q34 What happens if we listen or connect to an unbound socket? (bind has not been
called).
If we listen without a bind, we will get an error:
[EDESTADDRREQ]
The socket is not bound to a local address, and the protocol does not support listening on an unbound socket.
If the socket has not already been bound to a local address, connect() shall bind it to an address which, unless the
socket's address family is AF_UNIX, is an unused local address.
We can’t connect to an unbound socket because the listen will get an error.
https://pubs.opengroup.org/onlinepubs/9699919799/functions/listen.html
https://pubs.opengroup.org/onlinepubs/9699919799/functions/connect.html
Q35 On a forking server, after accept and fork, why should the parent close the socket
returned by accept? (hint: resource exhaustion)
In a forking server, after accept and fork, the parent should close the socket because it is no longer needed. The
child process will now use this socket to communicate with the client while the parent process continues to listen
for new connections on the listening socket.
https://pubs.opengroup.org/onlinepubs/9699919799/functions/fork.html
Q37 How can we set a socket to perform non-blocking I/O? (give some code)
We need to set O_NONBLOCK flag to the file descriptor of the socket using fcntl(). An example:
int flags;
if (-1 == (flags = fcntl(socket_fd, F_GETFL, 0)))
flags = 0;
fcntl(socket_fd, F_SETFL, flags | O_NONBLOCK);
https://pubs.opengroup.org/onlinepubs/9699919799/functions/fcntl.html
https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/fcntl.h.html
Q38 What happens with a read on a blocking socket if all the requested data is not
available?
The function will block the execution of the program until all the data arrives:
If no messages are available at the socket and O_NONBLOCK is not set on the socket's file descriptor, recv() shall
block until a message arrives.
https://pubs.opengroup.org/onlinepubs/9699919799/functions/socket.html
https://pubs.opengroup.org/onlinepubs/9699919799/functions/recv.html
Q39 What happens with a write on a blocking socket if the socket buffer is full?
The function will block the execution of the program until all the data can be written:
If no messages are available at the socket and O_NONBLOCK is set on the socket's file descriptor, recv() shall fail
and set errno to [EAGAIN] or [EWOULDBLOCK].
https://pubs.opengroup.org/onlinepubs/9699919799/functions/socket.html
https://pubs.opengroup.org/onlinepubs/9699919799/functions/sendto.html
Q40 What happens with a read on a non-blocking socket if all the requested data is not
available? (hint: there are two possibilities)
If no messages are available at the socket and O_NONBLOCK is set on the socket's file descriptor, recv() shall fail
and set errno to [EAGAIN] or [EWOULDBLOCK]. (Returning -1)
https://pubs.opengroup.org/onlinepubs/9699919799/functions/recv.html
Q41 What happens with a write on a non-blocking socket if the data to write does not fit
in the socket buffer? (hint: there are two possibilities)
If space is not available at the sending socket to hold the message to be transmitted and the socket file descriptor
does have O_NONBLOCK set, sendto() shall fail. (Returning -1)
https://pubs.opengroup.org/onlinepubs/9699919799/functions/sendto.html
Q43 Can we mix file descriptors for sockets and disk files in a single poll call?
Yes.
The poll() function shall support regular files, terminal and pseudo-terminal devices, FIFOs, pipes, sockets and [XSR]
[Option Start] STREAMS-based files
https://pubs.opengroup.org/onlinepubs/9699919799/functions/poll.html
Q44 Which events can report a poll call for fds[i].events = POLLIN;?
POLLIN, POLLHUP, POLLERR, POLLNVAL
In addition, poll() shall set the POLLHUP, POLLERR, and POLLNVAL flag in revents if the condition is true, even if the
application did not set the corresponding bit in events.
poll (opengroup.org)
poll(2) - Linux manual page (man7.org)
Q45 Are socket options inherited from a listening socket with accept?
No, accept only creates the same socket type protocol and address family as the specified socket, and allocate a
new file descriptor for that socket.
https://pubs.opengroup.org/onlinepubs/9699919799/functions/accept.html
Q47 After poll signals revents = POLLOUT on a non-blocking socket, will a very large
send block execution?
No, The poll() function shall not be affected by the O_NONBLOCK flag.
https://pubs.opengroup.org/onlinepubs/9699919799/functions/poll.html
Comments: