Exam 2 CSCE 3600
Which of the following pieces of information is shared among all threads in a single process? (A) Heap (B) Stack (C) Program Counter (PC) (D) None of the above.
(A) Heap
Context switching between processes is carried out by the________. (A) job (long-term) scheduler (B) CPU (short-term) scheduler (C) interrupt handler (D) thread manager
(B) CPU (short-term) scheduler
What does the pipe() system call return if an error occurs during its execution? -1 0 The file descriptor of the pipe A success code
-1
You have an IP address represented as 127.0.0.1 (IPv4) in a human-readable format. Which of the following options correctly represents this IP address in network byte order using the htonl() function in C?
0x7F000001
In UNIX/LINUX the file descriptor associated with your monitor or screen or the standard output is ___________
1
What happens in the following code? unsigned short val = 0x01FF;printf("%u\n",val); The printed value will be ___________ 511 0x01FF 65281 0xFF01
511
What is the octal code for granting read and write permissions to the owner, read-only permission to the group, and no permission to others on a file in a Unix-like file system? 640 740 750 600
640
#include <stdio.h> #include <sys/types.h> #include <unistd.h> int main() { fork(); fork(); fork(); printf("hello\n"); return 0; }
8
Select the true statements about stream sockets Data arrives in the form of a byte-stream Receiver needs to separate messages in stream There is no requirement to segment the data into packets. Data transfer happens using send() and recv() functions
Data arrives in the form of a byte-stream Receiver needs to separate messages in stream There is no requirement to segment the data into packets. Data transfer happens using send() and recv() functions
In a C program, what does a socket address typically look like when combining an IP address and a port number?
A structure, e.g., struct sockaddr_in
A socket system call returns _______for the created socket A) A file descriptor B) A process ID C) A thread ID D) A memory address
A) A file descriptor
Which system call associates an address with a socket? A) bind() B) connect() C) listen() D) accept()
A) bind()
Port numbers Select all that apply A) can be set by bind() in the POSIX socket APIs B) are assigned only to the end of a connection which calls accept() C) are typically used by routers to decide where to send messages
A) can be set by bind() in the POSIX socket APIs B) are assigned only to the end of a connection which calls accept()
Thread-safe
An atomic increment and decrement operation is
In a multitasking operating system, what is the main cost associated with context switching between processes? A) CPU time spent performing the context switch B) Memory overhead associated with saving and restoring process state C) Delay incurred in suspending and resuming processes D) Disk I/O operations required to swap process data to and from memory
B) Memory overhead associated with saving and restoring process state
In a client-server networking application, which process typically creates a new child process to handle each incoming client connection? A) The client process B) The server process C) Both the client and server processes D) Neither the client nor server process
B) The server process
In a multithreaded application, why is thread synchronization needed? A) To improve thread performance B) To prevent threads from accessing shared resources simultaneously C) To reduce the number of threads in theapplication D) To prevent threads from crashing the application
B) To prevent threads from accessing shared resources simultaneously
What is the primary purpose of the exec family of system calls (execl, execv, execve) in Unix-based operating systems? A) To create a new process B) To terminate a process C) To replace the current process with a new process D) To synchronize access to shared resources between multiple processes
B) To terminate a process
A pthread_create function returns_______ for the newly created thread. A) A file descriptor B) A process ID C) A thread ID D) A memory address
C) A thread ID
Which of the following statements is true about long-term scheduler? A) It is responsible for context switching between processes. B) It is responsible for selecting processes from memory for execution. C) It determines which programs are selected for execution from a queue of new processes. D) It is responsible for allocating memory to processes.
C) It determines which programs are selected for execution from a queue of new processes.
What is the primary responsibility of a thread manager in a multithreading environment? Allocating memory for thread execution Managing the execution order of threads Handling I/O operations for threads Coordinating the creation, scheduling, and termination of threads
Coordinating the creation, scheduling, and termination of threads
Semaphore
Counting Mechanism for Resource Allocation
A fork() function returns _______ for the newly created process. A) A file descriptor B) A process ID C) A thread ID D) 0
D) 0
Which of the following statements is true regarding processes and their requirements? A) A process is the same as an executing program. B) A process doesn't require any resources to accomplishits task. C) A process requires only CPU time to accomplish its task. D) A process needs certain resources, including CPU time,memory, files, and I/O devices, to accomplish its task.
D) A process needs certain resources, including CPU time,memory, files, and I/O devices, to accomplish its task.
A process control block should contain ________ . (A) the process state (B) a list of open files (C) the contents of CPU registers (D) all of the above
D) all of the above
#include <stdio.h> #include <fcntl.h> #include <unistd.h> int main() { int fd1, fd2, fd3;fd1 = open("file1.txt", O_RDWR | O_TRUNC); fd2 = open("file2.txt", O_RDWR | O_TRUNC); fd3 = dup2(fd1, 5); close(fd1);write(fd2, "Hello", 5); lseek(fd3, 0, SEEK_SET); char buffer[10];read(fd3, buffer, 5); buffer[5] = '\0'; printf("%s\n", buffer); close(fd2);} The program closes file descriptor 2 successfully after reading from it.
False
A thread can be bound to multiple processes. True False
False
Because the thread scheduling algorithm can swap between threads at any time, you must program the order in which the threads will attempt to access the shared data
False
If one thread of a process is holding the lock on a mutex, and another thread of the process attempts to lock the mutex, the whole process is blocked.
False
fork() returns a 0 on failure
False
When is atomicity guaranteed for the write() operation? For requests of exactly 4096 bytes For requests with a size typically around 4096 bytes or less For all write() requests, regardless of size Atomicity is not guaranteed for write() operations
For requests with a size typically around 4096 bytes or less
When a process places an I/O request, it is placed in the _________________
I/O Queue
What is the primary purpose of the SIGHUP signal in Unix-like operating systems? Terminate a process immediately Restart a process Notify a process of changes in terminal status or disconnect Suspend a process temporarily
Notify a process of changes in terminal status or disconnect
Choose the modes of opening a file
O_RDONLY O_RDWR O_WRONLY
Which function translates a host name to an IP address? gethostbyname resolveIP hostname_to_IP getIPaddress
gethostbyname
Signals can be generated from these sources.
hardware, keyboard and C program
After a successful call to execv (Select all that apply) execv returns the new process ID file descriptors other than 0, 1, 2 (stdin, stdout, and stderr) are closed instructions are executed at the beginning of main() Answer an executable specified as an argument to execv is loaded
instructions are executed at the beginning of main() Answer an executable specified as an argument to execv is loaded
What is the general syntax of the pipe() system call? int create_pipe(fd); void open_pipe(int fd[2]); int pipe(fd[2]); pipe();
int pipe(fd[2]);
Which are true of the (counting) semaphore operation called "wait()"?
it decrements the value of the semaphore if the value becomes negative, the calling process is blocked A lower-level mutual exclusion mechanism is needed, to ensure that the fetching and storing of the semaphore value are done atomically.
Which of the following are true of the pthread_mutex_lock() operation?
it takes a reference to a mutex as a parameter it is intended to provide mutual exclusion
Choose the CPU-bound processes among these choices mathematical computations image and video processing file I/O operations scientific applications interactive applications that wait for user input data analysis
mathematical computations image and video processing scientific applications data analysis
The following happens in sem_post(sem_t *sem); sem_post() increments (unlocks) the semaphore pointed to by sem it returns a zero on success if the semaphore's value becomes greater than zero as a result of sem_post() then another thread blocked in a sem_wait() call will be woken up to lock the semaphore None of the above
sem_post() increments (unlocks) the semaphore pointed to by sem it returns a zero on success if the semaphore's value becomes greater than zero as a result of sem_post() then another thread blocked in a sem_wait() call will be woken up to lock the semaphore
accept()
server side
bind()
server side
listen()
server side
The ___________ function is used to create a socket (communication endpoint) and returns a file descriptor that can be used for further socket operations. endpoint() createsocket() fork() socket()
socket()
<sys/types.h> header defines the various data types used in socket related functions. Choose the common types you encounter here. ssize_t socklen_t size_t pid_t
ssize_t socklen_t size_t pid_t
When a system call is made, the CPU will run in _____________ mode
supervisory
Match the different sections of a process's user-level context with the corresponding data stored text: data: heap: stack:
text: source code data: static and global variable initialized at runtime heap: dynamic memory allocated at runtime stack: return addresses, function parameters, variables, etc
This is a collection of memory locations that a process can access.
virtual address space
Consider the following code segment: int main() { if (fork() > 0) sleep(100); return 0; } Execution of this code could result in the creation of a(n) orphan process zombie process process that executes forever None of the above
zombie process
On a typical implementation of POSIX, using read() to read one byte from a file descriptor with a call like:read(fd, buffer, 1);where the file descriptor is opened to a file on a hard disk will: A) cause the OS to request exactly one byte from the disk, then store thatbyte in the application's buffer B) cause read to return an error because the hard disk can only read data inlarger blocks C) cause the OS to request a block of data (larger than one byte) from disk and store it in the OS's memory, then copy one byte of that into the application's buffer D) cause the OS to request a block of data (larger than one byte) from disk,then copy the entire block into the application's buffer
C) cause the OS to request a block of data (larger than one byte) from disk and store it in the OS's memory, then copy one byte of that into the application's buffer
In a system that supports multithreaded processes, which of the following are likely to be associated with an individual thread (i.e., different for different threads within the process)?
Execution state (running, ready, etc.) Saved context (when not running) Execution stack Set of accessible open files
Which of the following statements about mutex locks is true? Mutex locks are only used for thread synchronization within a single process Mutex locks provide a way to ensure exclusive access to a shared resource among multiple processes Mutex locks are typically implemented using semaphores Mutex locks are designed exclusively for managing memory allocation in C programs
Mutex locks are only used for thread synchronization within a single process
Spurious wakeup
Non-deterministic events
Why is the gets(theBuffer) function considered a high-security risk? It requires a password for input. It can overwrite memory and lead to buffer overflow vulnerabilities. It is inefficient for large data transfers. It uses stderr for error messages
It can overwrite memory and lead to buffer overflow vulnerabilities.
Suppose P, Q and R are co-operating processes satisfying Mutual Exclusion condition. If the process Q is executing in its critical section then, .................
P' does not executes in critical section 'R' does not executes in critical section
A _________ is where the current state of a thread or process is stored so the execution of that thread can resume at a later time. This enables a single CPU to manage multiple threads or processes.
PCB
Process Control Block (PCB) contains the following information related to a process
Process ID Program Counter Process Priority List of open files
A ________ situation occurs when multiple threads are waiting on one another to release CPU resources so they can run. For example, this can happen when a single thread has exclusive priority but needs resources from a waiting thread, or all the threads are depending on one another to release needed resources.
Race Condition
This signal cannot be ignored or blocked SIGINT SIGSEGV SIGHUP SIGKILL
SIGKILL
____________ and ____________ are custom signals that can be used for user-defined purposes, such as triggering specific actions within a process. SIGHUP and SIGKILL SIGUSR1 and SIGUSR2 SIGILL and SIGSEGV SIGINT and SIGKILL
SIGUSR1 and SIGUSR2
What types of sockets are typically used for communication within the same machine?
Stream sockets
Which protocol is commonly used for communication over the internet using sockets?
TCP/IP
What is the default action when a process receives the SIGINT signal?
Terminate the process
In the write() function, what does the count parameter represent?
The maximum number of bytes to append to the end of the pipe
What is stored in a register during a context switch for a register-level context in processes?
The program counter (PC) and some processor status flags
#include <stdio.h> #include <fcntl.h> #include <unistd.h> int main() { int fd1, fd2, fd3;fd1 = open("file1.txt", O_RDWR | O_TRUNC); fd2 = open("file2.txt", O_RDWR | O_TRUNC); fd3 = dup2(fd1, 5); close(fd1);write(fd2, "Hello", 5); lseek(fd3, 0, SEEK_SET); char buffer[10];read(fd3, buffer, 5); buffer[5] = '\0'; printf("%s\n", buffer); close(fd2);} The program successfully opens two files, "file1.txt" and "file2.txt, and writes the string "Hello" to "file2.txt."
True
In Unix-like file systems, what does the execute permission (x) on a file indicate for a user? The user can read the file The user can modify (write to) the file The user can execute the file as a program The user can list the contents of the directory containing the file
The user can execute the file as a program
What does it mean when it's mentioned that dup2() performs the operation "atomically"?
There is no time lapse between closing newfd and duplicating oldfd into its spot
After a successful dup2() call, what is the relationship between oldfd and newfd? They refer to different files They become disconnected from the file system They refer to the same file They no longer exist
They refer to the same file
What is the primary advantage of using threads in a multi-threaded application?
Threads allow for concurrent execution and better resource utilization
Deadlock
Threads make no progress because of this circular chain of dependency mechanism
In a C program, when should you use fflush(stdout);? To clear the screen and start displaying fresh output. To flush input from the keyboard buffer. To close the standard output stream (stdout). To ensure that any buffered output to the standard output (stdout) is immediately written to the screen.
To ensure that any buffered output to the standard output (stdout) is immediately written to the screen.
What is the primary purpose of standard I/O streams in C programming?
To provide an abstraction for file descriptors and buffers
When might a developer choose to use SIGUSR1 and SIGUSR2 signals for interprocess communication?
To request a process to perform a specific action or task
#include <stdio.h> #include <fcntl.h> #include <unistd.h> int main() { int fd1, fd2, fd3;fd1 = open("file1.txt", O_RDWR | O_TRUNC); fd2 = open("file2.txt", O_RDWR | O_TRUNC); fd3 = dup2(fd1, 5); close(fd1);write(fd2, "Hello", 5); lseek(fd3, 0, SEEK_SET); char buffer[10];read(fd3, buffer, 5); buffer[5] = '\0'; printf("%s\n", buffer); close(fd2);} After close(fd1), file descriptor 1 is closed, and its functionality is taken over by file descriptor 5, which is why writes to file descriptor 5 are performed on "file1.txt."
True
#include <stdio.h> #include <fcntl.h> #include <unistd.h> int main() { int fd1, fd2, fd3;fd1 = open("file1.txt", O_RDWR | O_TRUNC); fd2 = open("file2.txt", O_RDWR | O_TRUNC); fd3 = dup2(fd1, 5); close(fd1);write(fd2, "Hello", 5); lseek(fd3, 0, SEEK_SET); char buffer[10];read(fd3, buffer, 5); buffer[5] = '\0'; printf("%s\n", buffer); close(fd2);} The program successfully reads 5 characters from "file1.txt" using file descriptor 5 and prints the string "Hello."
True
A "strong" semaphore is one that always achieves mutual exclusion.
True
A condition variable is used to wait until a particular condition is true. Condition variables must be used in conjunction with a mutex lock.
True
A deadlock might occur when one thread tries to send a message from user A to user B while another tries to send a message from user B to user A True False
True
In blocking I/O, a program waits for the operation to complete and can be delayed, potentially causing slower performance. True False
True
Say the critical section is protected by the exitSemaphore. When a thread finishes its critical section, it signals this by calling sem_post(&exitSemaphore) True False
True
The function of a block special file is to provide access to the hard drive device in fixed-size blocks or chunks, each typically consisting of multiple bytes. True False
True
True or False A race condition occurs when multiple concurrent threads compete to run first. If the thread that wins the race isn't the one that was supposed to run first, the code may exhibit unexpected behavior. You can resolve this problem with synchronization.
True
True or False If there are no connection requests and the socket is not non-blocking, accept () function blocks till a connection requests comes in.
True
fputc() is used for buffered character output (it writes a character to a specified file stream and allows buffering for improved performance) but putc() is a more general function and can be used for unbuffered character output or buffered character output depending on the file stream True False
True
wait() or sleep() functions can introduce synchronization between concurrently executing processes
True
Condition variables
Used to Coordinate Threads' Execution
Choose the correct statements about select() Waits on multiple file descriptors/sockets and timeout Application does not consume CPU cycles while waiting Returns when file descriptors/sockets are ready to be read or written or they have an error or timeout exceeded Has a disadvantage that it does not scale to large number of file descriptors/sockets
Waits on multiple file descriptors/sockets and timeout Application does not consume CPU cycles while waiting Returns when file descriptors/sockets are ready to be read or written or they have an error or timeout exceeded Has a disadvantage that it does not scale to large number of file descriptors/sockets
When should IO multiplexing be used? When you want to perform multiple CPU-bound tasks in parallel. When you need to handle concurrent I/O operations, such as reading from or writing to multiple sockets or files. When you want to optimize the performance of a single-threaded program. When you need to synchronize access to critical sections of your code.
When you need to handle concurrent I/O operations, such as reading from or writing to multiple sockets or files.
ssh and http can be implemented using this socket type. a connection-oriented socket like TCP a connectionless socket like UDP both TCP and UDP only RAW sockets
a connection-oriented socket like TCP
Which parameter specifies the maximum number of established connections waiting to be accepted (using accept()) in conjunction with the listen() function? max_clients connection_limit backlog accept_queue
backlog
recvfrom()
both server side and client side
socket()
both server side and client side
connect()
client side