two fifty semn
Make Utility:
"make" is a build automation tool that manages dependencies in a project. It reads a file called Makefile to determine how to compile and link a program.
C preprocessor directives start with ____ symbol. ( Similar fill in the blank questions)
#
Which symbol is the beginning character of all preprocessor directives? (include, define ...)
#
Define macro named "PLUS" that can replace the "+" operator in a C program.
#define PLUS +
In a C program. Which variant of the include directive is used to include system header files? (Hint: remember how stdio.h is included)
#include <filename>
Which vim command(s) save changes in the current file and quit(s) the "vim text editor" ?
:wq
What is the value of var after the following C program snippet? (Assume string.h is included in the program) var = strcmp ("COME ON", "CMSC257");
A positive value
Convert the following Hexadecimal(Base 16) numbers to Decimal(Base 10) Numbers. (Results are two digit decimal numbers) A) 0x11 = B) 0x22 = C) 0x1A =
A) 17 B) 34 C) 26
Convert following binary (Base 2) numbers to two digits hexadecimal (Base 16) numbers A) 0b01011110 = 0x B) 0b11110100 = 0x C) 0b00011100 = 0x
A) 5E B) F4 C) 1C
Which statement(s) are correct for make files? A) Enables the end user to build and install your package without knowing the details of how that is done B) Figures out automatically which files are out of date C) Issues command to create the intermediate and final project files D) Limited to C, C++ and Java Programming Languages
A, B, C
What is the result of the following operations between two 8-digit binary numbers? Enter the result in two digits in hexadecimal format. (Do not enter in binary form. First, find the result in binary format, then convert the result to hexadecimal) A: 10011010 & 01011100 B: 11010101 | 00011010 C: 11101001 ^ 01101101 D: ~ 11011001
A: 18 B: DF C: 84 D: 26
Concurrency and Parallel Processing: Difference:
Concurrency is the execution of multiple tasks simultaneously, but not necessarily in parallel. Parallel processing involves the simultaneous execution of multiple tasks in parallel.
examples of system programs
Debuggers (Such as gdb) Compilers (such as gcc) Operating Systems (Such as Windows, Linux, Android)
Global variables
Def: Variable declared outside of a function ▪ Virtual memory contains exactly one instance of any global variable
GDB
GDB is used for debugging C programs. It allows setting breakpoints, inspecting variables, and stepping through code.
What will be the output of the following C program? (For space reasons, we are not checking error return codes. You can assume that all functions return normally.) A program using signal, fork, wait, waitpid, exit Information: When a child process stops or terminates, SIGCHLD is sent to the parent process. The default response to the signal is to ignore it. The signal can be caught.
In the test question: A process calls fork then the child process does something (manipulates a variable). When the child process terminates, the parent process will receive SIGCHLD signal. Parent process has a signal handler installed for SIGCHLD.
Macros in C:
Macros in C are preprocessor directives that are used to define constant values or to perform text manipulation before the compilation process. Example: '#define PI 3.14'
Enforcing Mutual Exclusion
Need to guarantee mutually exclusive access for each criticalsection. Classic solution: ▪ Semaphores Other approaches (out of our scope) ▪ Mutex and condition variables (Pthreads) ▪ Monitors (Java)
Parallel Processing Efficiency
Parallel processing improves efficiency by dividing a task into subtasks that can be executed concurrently.
The program in the following figure has a bug. The thread is supposed to sleep for 1 second and then print a string. However, when we run it on our system, nothing prints. Why? How you can fix that problem?
Parent process is not waiting for the thread. Whole process terminates before the child prints anything. We need to use pthread_join before line 9, after line 8 Pthread_join (&tid, NULL)
Speedup Definition:
Speedup is the ratio of the time taken for a task without parallel processing to the time taken with parallel processing.
calculate the speedup and parallel efficiency for the execution times in the following table
Speedup: Sp = T1 / Tp Efficiency: Ep = Sp /p = T1 /(pTp) multiply with 100 to find percentage
Static Variables:
Static variables in a function retain their values between function calls and have file scope. Example: void counter() { static int count = 0; count++; }
exit System Call:
The exit system call is used to terminate a process. It returns the exit status to the parent process.
Signal Function:
The signal function is used to handle signals in Unix. Example: #include <signal.h> void handle_signal(int signum) { // Signal handling code } signal(SIGINT, handle_signal);
Shared and Not Shared Among Threads:
Threads of the same process share the same memory space (shared), but each thread has its own stack and registers (not shared).
What will be the output of the following code snippet? #include <stdio.h> void main(){ char c[] = "VCURAMS"; char *p =c; printf("%s", p + 2) ;}
URAMS
Match the file symbols in a Linux operating system with their meaning ~ .. / .
User home folder parent folder root folder current folder
Valgrind:
Valgrind helps in detecting memory leaks and errors by simulating a virtual machine and running programs through it.
Which statement allocates memory in the heap for an array named arr with 25 'double' type elements? (Assume a 'double' is 8 bytes) (Hint: malloc() function has a single input parameter while the calloc() function has two input parameters)
arr = (double*) malloc (25 * sizeof(double));
Which command gives read and execute permission only (no write permission) to owner user, group and others for the file named file1.
chmod 555 file1
Which command adds read permission to others for a file named as "file1" in a Linux System?
chmod o+r file1
In a C program, Memory that is allocated using "malloc" or "calloc" functions (dynamically allocated memory) persists until (choose all that apply)
exec() family functions are called (such as execv()) Memory is deallocated using free exit() function is called Program Terminates
fork vs exec:
fork creates a new process by duplicating the calling process, while exec replaces the current process image with a new one.
Which system call returns twice? (Once to the parent and once to the child process)
fork()
Let's say we allocated memory for pointer ptr as float *num = (float *) malloc (sizeof(float)); How can we deallocate memory that was allocated by using "malloc" if we no more need the memory pointed by "ptr"?
free(num);
Which gdb command will list breakpoints
info breakpoints
kill and raise System Calls:
kill is used to send a signal to a process, and raise is a function that sends a signal to the calling process.
semaphore
on-negative global integer synchronization variable.Manipulated by P and V operations.
open vs fopen:
open is a system call used for low-level file handling, providing more control. fopen is a standard library function that provides a higher-level interface for file handling.
Choose the correct answer to fill in the blank space, so that the program prints the area of the rectangle initialized in the main function.
p3-> width * p3->height;
What is returned by the wait() system call after a child process terminates (assume no error)?
process id of the terminating child process
Basic Pthread Functions
pthread_join: Waits for a thread to terminate. pthread_cancel: Cancels a thread. pthread_exit: Exits the calling thread. pthread_create: Creates a new thread.
Semaphore Fill in the Blanks:
sem_init(&mutex, 0, 1); sem_init(&empty, 0, BUFFER_SIZE); sem_init(&full, 0, 0); // Semaphore operations sem_wait(&empty); sem_post(&full);
A pizza oven can contain five pizzas, but the oven narrow opening allows only one cook at a time to either put a pizza in the oven or to take one out. Given that there will be more than one cook preparing pizzas at any given time, complete the missing lines in the following C procedure pseudocode. (Hint: Initialize the semaphores to integer values, then use P and V operations(P(&oven, P(&access), V(&oven), V(&access))) on oven and access to synchronize processes. Prevent deadlock condition, (50pts)
semaphore oven = __5___; semaphore access = _1___; // the mutex make_pizza(int size, int toppings) { prepare_pizza(size, toppings); __P(&oven);__________________ __P(&access);_________________ put_into_oven(); ___V(&access);_________________ wait_until_done(); ___P(&access);________________ take_from_oven(); ___V(&access);_________________ ___V(&oven);_________________ } // make_pizza
Which specifier restricts the visibility of a symbol within a single file where it is declared, assuming the program is a multi-file C program. (Assuming variable or function is declared or defined within the global context. Local variables are already visible only within the function)
static
Let's say we have the following code within the support.c file. Which of the following are strong symbols in that file for C Linker. (Assume variables are global. Choose all that apply) ? #include <stdio.h> int a; int b = 6; void p1() { return; } int p2(int c) {return 2*c;}
void p1() { return; } int p2(int c) {return 2*c;} int b = 6;
What is the return type of "calloc" function?
void*
wait and waitpid:
wait and waitpid are system calls used to make a parent process wait until its child process terminates. They return the child process's exit status.
Which system call(s) can be used by a parent process to perform reaping (releasing some resources used for the process) on a terminated child?(So that the terminated child will not remain as a Zombie process and remain in the Operating System tables) (Choose all that apply)
waitpid wait
Which ones are feasible outputs for the following program? (Hint: Drawing a process diagram will help) #include "csapp.h" int main(){ int x = 1; if (Fork() != 0) printf ("x=%d\n", ++x); printf ("x=%d\n", - - x); exit(0); }
x=2 x=1 x=0 OR x=0 x=2 x=1 OR x=2 x=0 x=1
local static variables
▪ Def: Variable declared inside function with the static attribute ▪ Virtual memory contains exactly one instance of any local static variable.
local variables
▪ Def: Variable declared inside function without static attribute ▪ Each thread stack contains one instance of each local variable
P(s) locking
▪ If s is nonzero, then decrement s by 1 and return immediately. ▪ Test and decrement operations occur atomically (indivisibly) ▪ If s is zero, then suspend thread until s becomes nonzero and the thread is restarted by a V operation. ▪ After restarting, the P operation decrements s and returns control to the caller.
V(s) unloxking/releasing
▪ Increment s by 1. ▪ Increment operation occurs atomically ▪ If there are any threads blocked in a P operation waiting for s to become non- zero, then restart exactly one of those threads, which then completes its P operation by decrementing s.
What is returned by the fork() system call to the child process? (assume it was successful)
0
What is the decimal equivalent of 8 bit hexadecimal number using 2's complement (signed) representation :
0xF4
gcc options 1) -Wall 2) -g 3) -o
1) enables all compiler's warning messages 2) generates debug information to be used by GDB debugger 3) writes the build output to an output file (otherwise output will be a.out)
Match the phases of compilation process with their short descriptions 1): Compiler 2): Preprocessor
1): Generates assembly program. Checks if program is correct grammatically and prints error message if not 2): Modifies the original program in C according to the directives that begin with #character
Match gdb (GNU debugger) commands with their actions: 1) break (b): 2) delete (d): 3) list: 4) where: 5) next (n): 6) step (s): 7) save breakpoint b.txt: 8) continue (c):
1): creates a breakpoint 2): deletes a breakpoint 3): prints 10 lines of code 4): shows stack frames within the stack 5): steps the program forward one statement, regardless of the kind of statement it is on 6): moves the program forward one statement, but "steps into" a program-defined function 7): Correct match: saves breakpoints to a file named as b.txt 8): continues running the program from that point till it terminates or hits another breakpoint
What is the output of the following C program? #include <stdio.h> int main() { typedef int x; x a = 5, b = 5, c; c = (a * 2) / 2 + b; printf("%d",c); return 0; }
10
System-level I/O:
In a scenario where parent and child are reading from a copied file descriptor, the parent might use read and the child might use read or fread based on whether low-level or standard library functions are used.