CPP CPP Interoperability & System Programming 2 — Questions and Answers
Question 1: Which POSIX function creates a new child process?
- spawn()
- createProcess()
- fork() (Correct answer)
- exec()
Correct answer: fork()
fork() creates a new process by duplicating the calling process; the child receives a copy of the parent's address space.
Question 2: What does the mmap() system call do?
- Allocates heap memory like malloc
- Maps a file or anonymous region into the process's virtual address space (Correct answer)
- Creates a memory pool for custom allocators
- Loads a shared library at a fixed address
Correct answer: Maps a file or anonymous region into the process's virtual address space
mmap() maps a file or anonymous memory region into the virtual address space of the process, enabling efficient file I/O and shared memory.
Question 3: What does write() return when it encounters an error?
- 0
- NULL
- -1 (Correct answer)
- The value of errno
Correct answer: -1
Like most POSIX I/O system calls, write() returns -1 on error and sets the global errno to indicate the specific failure.
Question 4: Which signal is delivered to a process when it accesses an invalid memory address?
- SIGTERM
- SIGKILL
- SIGSEGV (Correct answer)
- SIGINT
Correct answer: SIGSEGV
SIGSEGV (segmentation violation) is sent by the kernel when a process attempts to access memory outside its allowed address space.
Question 5: What does the POSIX pipe() function create?
- A connection between two network sockets
- A unidirectional IPC channel with a read end and write end (Correct answer)
- A pipeline of stdio streams
- A communication channel between threads
Correct answer: A unidirectional IPC channel with a read end and write end
pipe() creates a pair of file descriptors forming a unidirectional channel: data written to one end can be read from the other.
Question 6: Which modern POSIX function should be used for hostname-to-address resolution instead of the deprecated gethostbyname()?
- gethostbyname2()
- inet_addr()
- getaddrinfo() (Correct answer)
- nslookup()
Correct answer: getaddrinfo()
getaddrinfo() is the thread-safe, IPv6-capable replacement for the deprecated gethostbyname() for resolving hostnames to addresses.
Which POSIX function creates a new child process?