CS149 Midterm 1 Polling Questions

¡Supera tus tareas y exámenes ahora con Quizwiz!

What output will be at line A? #include <sys/types.h> #include <stdio.h> #include <unistd.h> int value 5; int main() { pid_t pid; pid = fork(); if (pid==0) // child process value += 15; return 0; } else if (pid > 0) { // parent process wait (NULL); printf ("PARENT: value = %d", value); // LINE A return 0; } } A) 5 B) 15 C) 20 D) 35

A) 5 (fork() will return 0 in the child process even though the child's PID isn't actually 0. Meanwhile in the parent process it will return a value >0. Additionally, both processes will have different memory addresses for "value" even if it were a static variable)

What will this code output? #include<stdio.h> int main() { static int var = 5; printf("%d ",var--); if(var) main(); } A) 5 4 3 2 1 B) 4 3 2 1 C) 5 5 5 5 5 D) 4 3 2 1 0

A) 5 4 3 2 1

Including the initial parent process, how many processes are created by the program? #include <stdio.h> #include <unistd.h> int main() { /* fork a child process */ fork(); /* fork another child process */ fork(); /* and fork another */ fork(); return 0; } A) 8 B) 16 C) 4 D) 3

A) 8

What will this print out? int main() { int e; char ch; printf("\n Enter a character : "); scanf("%c",&ch); e=ch; printf("\n The ASCII value of the character is : %d",e); return 0; } A) Integers B) Characters C) Ints and Chars D) Nothing

A) Integers

In an OS is a "program" the same as a "process"? A) True (Yes) B) False (No)

B) False (No)

Most C programs (UNIX) that worked with old C standards are also compatible with new C standards. This is A) Backward compatibility B) Forward compatibility

B) Forward compatibility

Which of the following statements concerning open source operating systems is true? A) Oracle Solaris is open source. B) Source code is freely available to read. C) They are always more secure than commercial, closed systems. D) All open source operating systems share the same set of goals.

B) Source code is freely available to read.

Including the initial parent process, how many processes are created by the program? #include <stdio.h> #include <unistd.h> int main() { int i; for (i = 0; i < 4; i++) fork(); return 0; } A.) 8 B.) 16 C.) 4 D.) 3

B.) 16

In what order are the operators evaluated? w + 3 != y - 1 && x A.) +, !=, -, && B) +, -, &&, != C) +, -, !=, &&

C) +, -, !=, &&

This print statement will print to: FILE *myFile; //note it is a pointer to a FILE myFile = fopen("fileout.txt", 'w'); fprintf( myFile, "my %s has %d chars\n", "string format", 30); A) stdout B) stderr C) A file on the filesystem (hard disk) D) All of these

C) A file on the filesystem (hard disk)

What will this print out? int main() { int i; i=0; do { printf("%d %c \n",i,i); i++; } while(i<=126); return 0; } A) Integers B) Characters C) Ints and Chars D) Nothing

C) Ints and Chars

For IPC communication the OS employs an internally defined buffer data-structure, which is defined: A. In user space B. As a separate area which is neither in user space nor in kernel space C. In kernel space

C. In kernel space

Which of the following is true in the context of Inter-process communication: A. It is like a user defined procedure call. B. It is like user defined a function call. C. It is a system call (provided by the kernel) D. None of the above

C. It is a system call (provided by the kernel)

What will this code output? #include<stdio.h> int main() { static int i=5; //Because static, ALL //Calls of main ues the same i if(--i){ main(); printf("%d ",i); } } A) 5 4 3 2 1 B) 4 3 2 1 C) 1 D) 0 0 0 0

D) 0 0 0 0

Assume the two following programs run in parrallel. Which is an impossible output? (I put a space around the 0 digit that PROGRAM 2 prints so it's easier for you to read) PROGRAM 1: int value = 0; int main(int argc, char *argv[]) { while (1) { printf("%d", value); value++; } return 0; } PROGRAM 2: int value; int main(int argc, char *argv[]) { value = 0; printf("%d", value); return 0; } A) 01234 0 5678.... B) 0123456789 0 1011... C) 012 0 3456.... D) 0123 0 1234....

D) 0123 0 1234.... (This one is impossible because PROGRAM 1 and 2 do not share the same value variable so PROGRAM 1 isn't affected by PROGRAM 2 and continues to sequentially print the digits)

When a child process is created inside fork(), which of the following is TRUE in terms of the execution or address space of the child process? A) The child process runs concurrently with the parent. B) The child process has a new program loaded into it. C) The child at start will be a clone (duplicate) of the parent. D) All of the above

D) All of the above

When fork() creates a new child process: A) The child gets copies of the parent's file descriptors B) The child gets its own separate address space C) The child gets a copy (clone or duplicate) of its parent process's memory segments D) All of the above

D) All of the above

#define in a C program may be A) Constant. B) Preprocessor directive. C) Replaced by preprocessor in code before compilation. D) All of the above.

D) All of the above.

#include "mylib.h" in a C program means A) Include header files in code before compilation. B) mylib is a user-defined library of functions under this directory. C) Contains function definitions that may be called in this file. D) All of the above.

D) All of the above.

#include<stdio.h> in a C program means A) Include header files in code before compilation. B) Stdio is a predefined standard library of functions under a system path. C) Contains function definitions that may be called in this .c code file. D) All of the above.

D) All of the above.

What will this code output? #include<stdio.h> char *getString() { char str[] = "Will I be printed?"; return str; } int main() { printf("%s", getString()); getchar(); } A) "Will I be printed?" B) 0 C) 1 D) Doesn't work

D) Doesn't work (str is local to getString)

In operating system, each process has its own __________ A) address space and global variables B) open files C) pending alarms, signals and signal handlers D) all of the above E) mmap memory space

D) all of the above

The ____ of a process contains temporary data such as function parameters, return addresses, and local variables. A) text section B) data section C) program counter D) stack

D) stack

Which of the following are true in the case of a pipe as a mechanism of IPC? A. A pipe is for uni-directional communication. B. A pipe uses a buffer and the size of the buffer can be specified by the user. C. Pipes can NOT support broadcast. D. All of the above

D. All of the above (It's NOT a broadcast since it's between 2 processes only)

Choose the incorrect statements. A. It is possible for C programs to directly make system calls B. Library functions can use system call C. System calls execute in kernel mode D. Library functions do not use system calls for privileged functions

D. Library functions do not use system calls for privileged functions

Which operator is evaluated first in C? w + 3 > x - y * z A.) + B.) - C.) > D.) *

D.) *

What would a computer without an operating system be like? A.) The software applications you wrote couldn't send requests to the hardware through standard APIs B.) There would only be one program running at a time and exiting C.) There would be no graphical user interface D.) All of these

D.) All of these

A process can be terminated due to __________ A.) normal exit B.) fatal error C.) killed by another process D.) all of the above E.) all of its children terminating will terminate parent too

D.) all of the above

Operating system a: is a collection of programs b: provides user-interface c: is a resource manager d: all of the above e: none of the above

D: all the above

Run the code under Canvas code/ipc/fork_pipe.c. What does it do? (The code was way too long to paste here) A. It makes 2 pipes between parent and child processes. B. It executes commands from the user in forked processes. C. It sends a string from parent to child through a pipe. D. Parent process takes a string from stdin and sends it to the child process, which appends it to another string and sends it back to the parent to be printed. E. A, C and D

E. A, C and D

Run the code under Canvas code/ipc/connect2.c. What does it do?(The code was way too long to paste here) A. It makes a pipe between 2 child processes. B. It executes commands from the user in forked processes. C. It sends a string from parent to child through a pipe. D. One process takes a string from stdin and sends it to the other process, which appends it to another string and prints it. E. Both A and B

E. Both A and B

A stack is a sequentially ordered data structure that uses the first in, first out (FIFO) principle for adding and removing items. Yes No

No (LIFO not FIFO)

An OS should be able to handle interrupts or signals that are triggered by either hardware or software True or False?

True

In C, 1D array of int can be defined as follows and both are correct? int array1D[4] = {1,2,3,4}; int array1D[] = {1,2,3,4}; True False

True

In a C program a mandatory main() function is the start of execution? True or False?

True

Memory-mapped files may continue to exist in the system after the creating process has terminated. True False

True

Program counter or EIP specifies the current (next) instruction to execute. A) True B) False

True

The kernel layer manages all the hardware dependent functions True or False

True

True or False. This program reads commands from stdin and executes them. #include "apue.h" #include <sys/wait.h> Int main(void) { char buf[MAXLINE); / from apue.h / pid_t pid; int status; printf("%% "); /* print prompt (printf requires to print ) */ while (fgets(buf, MAXLINE, stdin) != NULL) { if (buf[strlen(buf) 1] == '\n') buf[strlen(buf) 1]=0; / replace newline with null/ if ((pid fork()) < 0) { //child { err_sys("fork error"); } else if (pid == 0) { execlp(buf, buf, (char *)0); err_ret("couldn't execute: %s", buf); exit(127); } //parent if ((pid waitpid(pid, &status, 0)) < 0) err_sys("waitpid error"); printf("%% "); } exit(0); }

True

True of False. Will the Newlines '\n' left over from scanf will cause issues in fgets? #include <stdio.h> int main(void) { int age; char name [256); printf("Input your age:"); scanf("%d", &age); /* Input 10 */ printf("Input your full name [firstname lastname)"); fgets(name, sizeof name, stdin); /* Doesn't read? */ return 0; }

True (the user will press enter after their answer, but in most cases scanf is built to stop once it receives a " " or "\n" meaning newline will be left over for for the fgets() to read which will only read the single "\n" character and stop before the next line of input).

True or False? The Newline '\n' left over from line 1 is a delimiter and will thus be ok, but the next '\n' left from line 2 will be read in line 3. This means that lines 3 and 4 will be off track. Put a space to ignore spaces int main(int argc, char *argv[]) { int a, b; char c1, c2; printf("Enter something: "); scanf("%d", &a); //line 1 printf("Enter other something: "); scanf("%d", &b); //line 2 printf("Enter a char: "); scanf("%c", &c1); //line 3 printf("Enter another char: "); scanf("%c", &c2); // line 4 printf("Done"); // line 5 system("PAUSE"); return 0; } A) True (Yes) B) False (scanf does not discard newlines, but you can do scanf("%d\n"), and does not discard char %s where int %d are expected)

True (the user will press enter after their answer, but in most cases scanf is built to stop once it receives a " " or "\n" meaning newline will be left over for for the scanf("%c") lines to read which will only read the single "\n" character, thus 3 and 4 will be offtrack).

How many data structures does the kernel maintain in memory per process that contain information about an open file (based on the previous figure)? a) 3 b) 2 c) 5 d) 1

a) 3

There are ___ distinct permission modes of opening a file. a) 9 (R, W, X and for users: owner, group, others) b) 4 (R, W, X) c) 2 (shared, private) d) 1

a) 9 (R, W, X and for users: owner, group, others)

A system call is a routine built into the kernel and performs a basic but privileged function for the user. a) True b) False

a) True

What is the output of this program (syscalls/lseek1-2.c)? #include<stdio.h> #include<fcntl.h> int main() { int fd, count; char ch; fd = open("out.txt",O_RDWR|O_CREAT); write(fd,"s",1); lseek(fd,0,SEEK_SET); write(fd,"d",1); lseek(fd,0,0); //flag 0 is SEEK_SET read(fd,&ch,1); printf("%c\n",ch); return 0; } a) d b) s c) sd d) none of the mentioned

a) d

In the output of this program, the string "/* Linux */" will be added at the ____ of the source file #include<stdio.h> #include<stdlib.h> #include<fcntl.h> int main() { int fd; fd = open("src.c",O_RDWR|O_APPEND); write(fd,"/* Linux */",11); return 0; } a) end b) beginning c) second line d) third line

a) end

The vnode table is also called _____ a) inode table b) file table c) virtual table d) virtual ntable

a) inode table

All UNIX and LINUX systems have one thing in common which is ____ a) set of system calls (POSIX) b) set of commands c) set of instructions d) set of text editors

a) set of system calls (POSIX)

Which system call returns the process identifier of a terminated child? a) wait b) exit c) fork d) get

a) wait

#include "apue.h" int main(void) printf("hello world from process ID %ld\n", (long)getpid()); exit(0); } a. The process ID of the current process b. The parent process ID c. The child process ID d. All of the above

a. The process ID of the current process

System calls and library functions are the same. a) True b) False

b) False

The address of the next instruction to be executed by the current process is provided by the __________ a) CPU registers b) Program counter in the EIP register (in Intel x86 arch) c) Process stack d) Pipe

b) Program counter in the EIP register (in Intel x86 arch)

Which of the following offset is used with lseek system call to set the offset pointer relative to the end of the file? a) SEEK_SET b) SEEK_END c) SEEK_CUR d) SEEK_CR

b) SEEK_END

What is interprocess communication (IPC)? a) communication within the process b) communication between two processes c) communication between two threads of same process d) communication between the process and the user

b) communication between two processes

What is the output of this program? #include<stdio.h> #include<fcntl.h> int main() { pid_t fd; char ch; int count; fd = open("src.c",O_RDONLY); do{ count = read(fd,&ch,1); printf("%c",ch); }while(count); return 0; } a) it will print nothing b) it will print the source code of the source file "src.c" c) segmentation fault d) none of the mentioned

b) it will print the source code of the source file "src.c"

____ system call is used for writing to a file. a) read b) write c) close d) seek

b) write

When a pipe is defined - its either end can be utilized freely for reading / writing a. True b. False

b. False (B - One end is for reading and the other is for writing)

Which of the following system call is used for closing a file? a) open b) lseek c) close d) write

c) close

Which of the following system call is used for opening or creating a file? a) read b) write c) open/fopen d) close

c) open/fopen

For reading input, which of the following system call is used? a) write b) rd c) read d) change

c) read

When a process creates a new process using the fork() operation, which of the following states is shared between the parent process and the child process? a. Stack b. Heap c. Shared memory segments d. None of the above

c. Shared memory segments Only the shared memory segments are shared between the parent process and the newly forked child process. Copies of the stack and the heap are made for the newly created process

close system call returns ____ a) 0 b) -1 c) 1 d) 0 or -1

d) 0 or -1 0 is success, -1 is failure.

write system call may return -1 when ___________ a) if disk fills up while write is in progress b) when file doesn't exist c) if the file size exceeds the system's limit d) All of these

d) All of these

A process's stack frame does not contain __________ a) Function parameters b) Local variables c) Return addresses d) Process ID nor PID of its parent process

d) Process ID nor PID of its parent process (The process ID is stored by the OS at higher level data structures. The stack stores function-call specific information.)

____ system call is used for positioning the offset pointer. a) read b) write c) open d) lseek

d) lseek

What does the following code from Canvas code/ipc/parent_child_pipe.c do? int main(void) { int pfds[2]; char buf[30]; pipe(pfds); if (!fork()) { printf(" CHILD: writing to pipe\n"); write(pfds[1], "test", 5); printf(" CHILD: exiting\n"); } else { printf("PARENT: reading from pipe\n"); read(pfds[0], buf, 5); printf("PARENT: read \"%s\"\n", buf); wait(NULL); } return 0; } a. It makes a pipe between 2 processes. b. It executes commands from the user in forked processes. c. Child sends a string from child to parent through a pipe. d. Parent sends a string from parent to child through a pipe. e. Both a and c

e. Both a and c

Run the code under Canvas code/ipc/pipe2.c (from APUE ipc1). What does it do? (The code was way too long to paste here) a. It makes a pipe between 2 processes (parent and a child). b. It executes given commands from the user in forked processes. c. It sends a string "hello world" from parent to child through a pipe. d. It pipes a given file through /bin/more, so the user can scroll through it. e. Both a and d

e. Both a and d

The following is a valid function definition: void PrintItem() { body consists of C statements; } A) True B) False

true

Run the code under Canvas code/ipc/mcopy2.c (it is from APUE src advio directory). What does it do? (The code was way too long to paste here) A.) Copies a file from src to dest using mmap B.) Copies a file from src to dest using message passing C.) Prints "Hello World" D.) Concatenates 2 strings

A.) Copies a file from src to dest using mmap

Which illustrates the actual order of evaluation via parentheses? (num1 == 9) || (num2 == 0) && (num3 == 0) A) (num1 == 9) || ((num2 == 0) && (num3 == 0)) B) ((num1 == 9) || (num2 == 0)) && (num3 == 0) C) (num1 == 9) || (num2 == (0 && num3) == 0) D) (((num1 == (9 || num2) == 0) && num3) == 0)

A) (num1 == 9) || ((num2 == 0) && (num3 == 0)) Note: && has higher precedence than ||

Which of the following statements is true? A) Memory mapping (mmap) allows bi-directional communication between processes. B) Only the parent and child processes can use memory mapping (mmap) for communication. C) Reading & writing to ordinary pipes on UNIX systems is performed exactly as file I/O with the Hark Disk Drive. D) Pipes/sockets can only be used by communicating processes on the same machine.

A) Memory mapping (mmap) allows bi-directional communication between processes. (note: In the sense that you use file descriptors for the input and output of a pipe, choice "C" also has a valid point. But pipes don't write to the filesystem like mmap is doing. Pipe data is stored in memory buffers and not written to the filesystem.)

If age == 28, then what does this evaluate to? (16 < age < 25) A) True (1) B) False (0)

A) True (1) Note: 16 < 28 is evaluated. The resulting "true" becomes 1 which means 1 < 25 which is also true.

Will the following two statements both read a single integer from standard input? fscanf(stdin, "%d", &x); scanf("%d", &x); A) True (Yes) B) False (No)

A) True (Yes)

Will this fgets read the first char too? #include <stdio.h> int main(void) { char line[256]; char ch; if (fgets(line, sizeof line, stdin) == NULL) { printf("Input error.\n"); exit(1); } ch = line[0]; printf("Character read: %c\n", ch); return 0; } A) True (Yes) B) False (No) Not if the first character is a space

A) True (Yes)

Which of the following operating systems is not open source? A) Windows B) BSD UNIX C) Linux D) PCLinuxOS

A) Windows

In Unix, which system call creates the new process? a) fork b) create c) new d) wait

A) fork

A(n) ________ is the unit of work in a system. A) process B) operating system C) timer D) mode bit

A) process

These 3 print statements will print in sequence to: printf("%s", "Hello world\n"); //printf defaults to stdout(1) fprintf(stdout, "%s", "Hello world\n"); //stdout is a FILE * fprintf(stderr, "%s", "Hello world!\n"); //stderr is a FILE * A) stdout, stdout, stderr B) stderr, stdout, stderr C) stderr, stderr, stderr D) stdout, stdout, stdout

A) stdout, stdout, stderr

What are the PID values at lines A, B, C, D? Assume parent pid is 500 and child pid is 503. #include <sys/types.h> #include <stdio.h> #include <unistd.h> int main() { pid_t pid, pid1; /* fork a child process */ pid = fork(); if (pid < 0) { /* error occurred */ fprintf(stderr, "Fork Failed"); return 1; } else if (pid == 0) { /* child process */ pid1= getpid(); printf("child: pid = %d", pid); /* A */ printf("child: pid1 = %d", pid1); /* B */ else { /* parent process */ pid1 getpid(); printf("parent: pid = %d", pid); /* C */ printf("parent: pid1 = %d", pid1); /* D */ wait (NULL); } return 0; } A.) A = 0, B = 503, C = 503, D = 500 B.) A = 500, B = 503, C = 503, D = 500 C.) A = 500, B = 503, C = 500, D = 503 D.) A = 0, B = 500, C = 500, D = 503

A.) A = 0, B = 503, C = 503, D = 500

What output will be at line X and line Y? #include <sys/types.h> #include <stdio.h> #include <unistd.h> #define SIZE 5 int nums [SIZE] = \{0, 1, 2, 3, 4\} int main() { int i; pid_t pid; pid fork(); if (pid == 0) { for (i = 0; i < SIZE; i++) { nums [i] *= - i; printf("CHILD: %d ", nums[i]); /* LINE X */ } else if (pid > 0) { wait (NULL); for (i = 0 i <SIZE; i++) { printf("PARENT: %d ", nums [i]); /* LINE Y */} } return 0; } A.) X: 0, -1, -4, -9, -16. Y: 0, 1, 2, 3, 4. B.) X: 0, 1, 2, 3, 4. Y: 0, 1, 2, 3, 4. C.) X: 0, -1, -4, -9, -16. Y: 0, -1, -4, -9, -16. D.) X: 0, -1, -2, -3, -4. Y: 0, -1, -2, -3, -4.

A.) X: 0, -1, -4, -9, -16. Y: 0, 1, 2, 3, 4.

The operating system model consists of a: kernel layer, service layer, and user interface layer b: hardware layer, and software layer c: multitasking, time sharing, and multiuser d: system layer, utility layer, and application layer e: none of the above

A: kernel layer, service layer, and user interface layer

In C, 2D array of int can be defined as follows correctly? int array2D[2][4] = {1,2,3,4,5,6,7,8}; /* (i) */ int array2D[][4] = {1,2,3,4,5,6,7,8}; /* (ii) */ int array2D[2][] = {1,2,3,4,5,6,7,8}; /* (iii) */ int array2D[][] = {1,2,3,4,5,6,7,8}; /* (iv) */ A) (i) is correct B) (i) and (ii) only are correct C) (i), (ii), and (iii) only are correct D) (i), (ii), (iii), and (iv) (all are correct)

B) (i) and (ii) only are correct

What does the following code from Canvas code/ipc/pipe1.c do? #include "apue.h" int main(void) { int ; int fd[2]; pid_t pid; char line [MAXLINE]; if (pipe(fd) < 0) err_sys("pipe eггог"); if ((pid = fork()) < 0) { err_sys("fork error"); } else if (pid > 0) { /* parent */ close(fd[0]); write(fd[1], "hello world\n", 12); } else { /* child */ close(fd[1]); n = read(fd[0], Line, MAXLINE); write(STDOUT_FILENO, line, n); } exit(0); } A. It makes a pipe between 2 processes. B. It executes commands from the user in forked processes. C. It sends a string "hello world" from parent to child through a pipe. D. It pipes a given file through /bin/more, so the user can scroll through it. E. Both a and c

E. Both a and c

When will line J be reached? #include <sys/types.h> #include <stdio.h> #include <unistd.h> int main() { /* fork a child process */ pid_t pid; pid = fork(); if (pid < 0) { /* error occurred */ fprintf(stderr, "Fork Failed"); return 1; } else if (pid == 0) { /* child process */ execlp("/bin/ls", "ls", NULL); printf("LINE J"); } else { /* parent process */ /* parent will wait for the child to complete */ wait (NULL); printf("Child Complete"); } return 0; } A.) If an error occurs in the call to exec() (e.g. max number of processes exceeded) B.) If the call to exec succeeds C.) Line J always runs D.) Line J never runs under normal conditions (e.g. abnormal condition is if the cmd does not exist) E.) A and D

E.) A and D (A If an error occurs in exec, like command is invalid, it will go to next line. Exec usually goes to the exec's code and skips where it left off. D Exec will replace the memory if it succeeds)

Why are pipes better than using temporary files (on the hard disk) for IPC? A.) pipes are implemented in memory (RAM), which is faster than disk. B.) with files would have to wait for a file to be completely written (fflush) before next step C.) programs can produce lots of I/O, so better to run them concurrently rather than wait for one to finish before starting the next one D.) you might fill up the disk with large intermediate files or fill your inode quota with too many files E.) all of these are reasons

E.) all of these are reasons

A new browser process is created by the Chrome browser for every new website that is visited (in the SAME tab). True False

False

An operating system is far easier to port-to move to some other hardware-if it is written in a lower-level language (like Assembly). True or False?

False

The following is a valid function definition for a function that returns two items: int, int GetItems() { ... } True False

False

The operating system kernel consists of all system and application programs in a computer. True or False?

False

True or False. Scanf will discard a char input by the user if the specifier %d (int) is expected. #include <stdio.h> int main(void) { int i; while(1) { if (scanf("%d", &i) != 1) { /* Input "abc" */ printf("Invalid input. Try again\n"); } else { break; } } printf("Int read: %d\n", 1); return 0; }

False. (If Scanf recieves the wrong type like char instead of int, it will not remove the input from the stdin)

A virtual machine (e.g. in UTM or VirtualBox or VMWare) can be switched like any process in the operating system. Yes No

Yes


Conjuntos de estudio relacionados

Accounting 2: Chapter 5 (exam 2)

View Set

First Aid p. 232, Serum tumor markers and psammoma bodies

View Set

Ambush Marketing and Sponsorship

View Set