CS 270 Final Exam UKY

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

What does the write system call return? What do positive, negative, and zero mean? Why might the return value be less than expected, even if it is positive?

-write returns the number of bytes written. -write returns -1 when an error occurs. -write returns 0 when count is zero and there are no errors or no error detections has occurred. -The return value may be less than expected due to insufficient disk space, write being blocked to a socket, pipe.

Which section of an executable or object file would the global def int table[4]; be in?

.bss

Which section of an executable or object file would the global def int table[4] = {1, 2, 4, 8}; be in?

.data

Which section of an executable or object file would the global def int main() {...} be in?

.text

Write a piece of code that uses the low-level Unix I/O system calls (not stdio or iostreams) that does the following: 1. Open an existing file named "data.txt" for writing, truncating the file to zero bytes if it already exists and creating it if it does not exist. 2. Write the line "Magic\n" to the file. 3. Close the file. 4. If there was an error at any step, print an error message and exit the program. 5. Include the definitions of any variables used by your code.

//Opening File int fd; fd = open("data.txt", O_WRONLY | O_CREAT | O_TRUNC, 0666); // ← SATAN MADE IT IN :( //Error opening file if(fd < 0){ perror("open data.txt failed"); exit(1); }

Suppose I run two copies of a program at the same time. In both copies, the global variable table is at address 0x4105f0. Why don't these two copies of the table overwrite one another?

Because with virtual memory they get mapped to different addresses within RAM or to the disk

In a network client, which system call specifies the address and port to connect to?

Connect(using struct sockaddr_in)

When a process tries to access a page that is marked as invalid in the page table, is that always an error? If not, what must be done to make the page valid?

No there is not always an error. Could be that the address to the physical address is not mapped in the page table. The OS reads in the page and places it into RAM. OS then maps the page table to the new page

What does the p in pthreads stand for?

POSIX

List at least two reasons a correctly-functioning program without errors might still cause a CPU exception.

Page Fault Traps - specifically syscalls, it is the expected result

If we determine that a system call failed, what library function should be used to obtain a readable error message? (There are two possible answers.)

Perror() or strerror()

What is special about the IP address 127.0.0.1

This is the localhost address. Every computer on a network refers to itself at this address

Suppose we establish a network connection, then have both the client and server try to read from the connection as their first actions. Nothing will happen? What is the name for situations like this where two or more processes or threads are unable to make progress because each is waiting for the other?

This situation is referred to as a Deadlock.

List at least two advantages and two disadvantages of thread-based network servers compared to process-based network servers.

Thread-based can easily share resources among threads and are less expensive than process-based. However, they can produce rare bugs that are hard to debug and are more complex than process-based

List at least two things that are shared among all the threads in a single process. List at least two things that are not shared (i.e., each thread has its own copy).

Threads share: Address space, heap, static data, code segments, file descriptors, global variables, child processes, pending alarms, signals and signal handlers, accounting information. Threads DON'T share: Program counter, registers, stack, state

What goes into the .bss section of an executable or object file?

Uninitialized global variables "Block Started by Symbol" "Better Save Space" has section header but occupies no space

The signature of the pthread_create function is: int pthread_create(pthread_t *star*thread, const pthread_attr_t *star*attr, void *star*(*star*start_routine) (void *star*), void *star*arg); What argument will be passed to the start_routine when it is called?

a function to run in the thread

What is the difference between a synchronous exception and an asynchronous exception?

an asynchronous exception is caused by something outside the processor, while a synchronous exception is caused by executing an instruction (either deliberately, a "trap"; or not, a "fault" or "abort")

In a network server, which system call specifies the address and port to listen on?

bind //listen sets up for accept; accept waits for connection, returns file descriptor number

Write an execvp call to execute the command "ls -l /". Define any necessary variables: you will need at least an array of C string pointers.

char *star*cmd = "ls"; char *star*argv[3]; argv[0] = "ls"; argv[1] = "-l" argv[2] = NULL; execvp(cmd,argv);

What goes into the .text section of an executable or object file?

code

Suppose file descriptor 5 refers to an open file that we want to supply as standard input to another program. What system call and parameters would we use to set up that redirection? Should we call the system call before calling fork, after fork in the parent process, or after fork in the child process?

dup2(5, 0) after and in the child process (dup doesn't let you control where it goes)

In the following program, the child process exits with a random exit status. pid_t pid = fork(); if (/*star* A: is this the child process? *star*/) { /*star* child process *star*/ exit(rand() % 10); } else { /*star* parent process *star*/ /*star* B: wait for child to terminate *star*/ /*star* C: print the exit status *star*/ } Write the code to determine and print the exit status of the child process, using the macros WIFEXITED and WEXITSTATUS.

if(WIFEXITED(status)){ int exit_status = WEXITSTATUS(status); printf("Exit status of child is %d\n",exit_status); }

What are two differences between execve and execvp?

execve has an array of pointers to environment variables that are explicitly passed to the new process image while execvp uses the PATH environment variable to find the file named in the path argument to be executed.

What library function can be used to translate a host name such as "www.google.com" and a port name or number such as "http" or "80" into a struct sockaddr?

getaddrinfo

Given the declaration: int (*star*handler)(double); what is the name of the variable being declared?

handler

List at least two differences between UDP and TCP.

-TCP is reliable and UDP is unreliable -Packets arrive on order with TCP whereas they need to be managed by an application layer for UDP. -TCP sockets communicate as sequences of bytes, while UDP sockets communicate as packets called "datagrams".

If we do not intend to use the function from the previous question, (pthread_join()) what library call should we use to indicate this?

#include <pthread.h> pthread_detach(pthread_self())

In the following program, the child process exits with a random exit status. pid_t pid = fork(); if (/*star* A: is this the child process? *star*/) { /*star* child process *star*/ exit(rand() % 10); } else { /*star* parent process *star*/ /*star* B: wait for child to terminate *star*/ /*star* C: print the exit status *star*/ } What expression goes in slot A to check whether this is the child process?

(pid == 0)

What value is returned by open() if there was an error? What value is returned if there was not an error?

- open() returns -1 if there was an error. - returns a file descriptor if it is successful

List three different reasons that modern operating systems use virtual memory, and explain how virtual memory addresses those reasons.

-*VM uses main memory efficiently:* -Uses RAM as a cache for parts of a virtual address space -Supports paging-Storing pages on disk -*VM simplifies memory management:* -Each process gets the same uniform linear address space. Has a standard layout -*VM isolates address spaces:* -One process can't interfere with another's memory -User program cannot access privileged kernel information and code

What is the difference between a program and a process?

-A program is a group of instructions to carry out a task while a process is a program in execution.

What signal is sent to a process when one of its children terminates? What is the default behavior when a process receives that signal?

-SIGCHILD is sent to a process when a child terminates. The default behavior is to affect sleep() in some capacity. *star*SIGCHLD is ignored by default

What does it mean when we say TCP is reliable?

-TCP is considered reliable because it guarantees the delivery of data to the destination router. UDP cannot guarantee this, which is why UDP is considered unreliable

Write a piece of code that uses the low-level Unix I/O system calls (not stdio or iostreams) that does the following: 1. Open a file named "data.txt" for reading. 2. Read up to 512 bytes from the file into an array named buf. 3. Close the file. 4. If there was an error at any step, print an error message and exit the program. 5. Include the definitions of any variables used by your code.

//Opening the file int fd; fd = open("data.txt", O_RDWR | O_CREAT, 0666); //Close the program if there is an issue if(fd < 0){ perror("File Descriptor issues"); exit(-1); } //Continue else{ //Reading file up to 512 bytes char buf[512]; ssize_t Thing = read(fd, buf, 512); //If an error occurred if(Thing < 0){ perror("Error reading file"); exit(-2); } //Closing the file else{ int Ending = close(fd); //Error closing the file if(Ending < 0){ perror("Error closing file"); exit(-3); } } }

What are the three standard file descriptors that are (usually) opened before a program begins executing? List the file descriptor numbers and the names for each of those descriptors.

0: stdin 1: stdout 2: stderr

Approximately how many possible IPv4 addresses are there?

2^32 = 4,294,967,296

If foo.c contains global definition int secret = 4; and bar.c contains global def int secret = 8; what happens when foo.c and bar.c are linked?

A linking error should occur. Can't define same variable twice globally

What is the difference between a thread and a process? How are they similar?

A process is an executing instance of an application and a thread is a path of execution within a process. Threads within the same process share the same address space, whereas different processes do not.

What is a race condition? Give an example, either in computing or in the real world.

A race condition occurs when a system attempts to perform two or more operations at the same time, but due to the nature of the system, the operations must be done in the proper sequence to be done correctly. For example, take a look at the following code: if(x == 5){ //If x were to change before the next line, its value could be different than the expected answer of 10. y = x *star* 2; }

What signal is sent when the user presses ctrl-c? What is the default behavior when a process receives that signal?

A signal interrupt "SIGINT" is sent with ctrl-c. The default behavior is to terminate the process.

What is the difference between a trap exception and a fault exception?

A trap is an instruction that deliberately and explicitly causes an exception, for example the "syscall" instruction or a breakpoint. A fault exception is an exception raised by the computer hardware when a program accesses a memory page that is not current mapped by the memory management unit into the virtual address space of a process.

What is a zombie process? What causes processes to become zombies? How do you free the resources associated with a zombie process?

A zombie process is a process that has completed execution but still has an entry in the process table. It is caused by using fork() without terminating a process completely. It can be prevented by using some form of wait() command, which will suspend the execution of the parent until the child is terminated.

If a system call fails, what global variable holds a code indicating the reason for the failure?

Errno (int)

Is this a valid IPv4 address? 204.12.9.40.42

INVALID -- Cannot exceed 4 distinct numbers

Is this a valid IPv4 address? 4.72.8

INVALID -- Must have 4 distinct numbers

Is this a valid IPv4 address? 10.300.5.15

INVALID -- No value above 255 is allowed

Why might two processes share the same page of physical memory?

If a process is created through fork() and clone() is used to create a thread(). The threads created will share the same physical and virtual memory, with fork() creating a child process which uses a separate virtual address space for calling the parent.

Why is it unsafe to call malloc inside a signal handler?

In order to call a signal inside a signal handler, it must be an async-signal-safe function. malloc is not an async-signal-safe function

What goes into the .data section of an executable or object file?

Initialized global variables

If foo.c contains global def int secret = 4; and bar.c contains global char secret[16]; what happened when foo.c and bar.c are linked? How much memory is reserved for secret?

Int declaration of secret will override since it is a strong variable. 8 bytes of memory?

In a network server, in what order should we call the following system calls? bind, accept, socket, listen?

Socket, bind(define address to listen on), listen, accept

Given the declaration: int (*star*handler)(double); what is the meaning of the double?

It is the parameter of the function

Given the declaration: int (*star*handler)(double); what is the meaning of the int?

It is the return type of the function

Given the declaration: int (*star*handler)(double); what kind of thing does the variable point to?

It points to a function that takes a double as an arg and returns an int

If the OS needs to allocate a new page of memory, but there are no free pages in physical memory, how does the OS page fault handler resolve that situation?

It relocates an address within physical memory to the disk then replaces the spot left open with the new memory.

What is the output of the following code? Assume that both calls to fork() are successful fork(); fork(); fprintf(stdout, "!"); exit(0);

Program will output: "!!!!" (Without quotations marks). Child: ! Parent: ! Child: ! Parent: !

What goes into the .rodata section of an executable or object file?

Read only data: jump tables, ..

The accept system call accepts a socket file descriptor as one parameter, and returns a different socket file descriptor. What is the difference between the two sockets?

The Parameter - listening file descriptor (it has no connection) The Return - a connected file descriptor It returns a different file descriptor so that the listening file descriptor can keep listening.

Assuming there was not an error, fork() returns two different values. What value is returned to the child process, and what value is returned to the parent process?

The child process will return a 0 value while the parent will return a positive value. If the fork() returns a negative number, there was an error.

The signature of the pthread_create function is: int pthread_create(pthread_t *star*thread, const pthread_attr_t *star*attr, void *star*(*star*start_routine) (void *star*), void *star*arg); Why is the first argument a pointer?

The first argument is the location where the ID of the new thread should be stored.

One of these two programs produces zombie processes. Which is it, and why does the other not produce zombies? while (fork()) ; /*star* do nothing *star*/ exit(0); while (!fork()) ; /*star* do nothing *star*/ exit(0);

The first program will produce zombie processes as fork only returns 0 for children, meaning that the parent is hung in the while loop no matter if it succeeds or fails, making all the children become zombie processes. In number 2, the parent dies, so the orphaned children are reparented to PID 1 (init(just a loop calling wait()))

In a client-server protocol, which endpoint of the connection usually uses a well-known port? Which endpoint usually uses an ephemeral port?

The server would use a well-known port while the client would use an ephemeral port.

How would you make a program not terminate when the user presses ctrl-c?

Using the signal() function to ignore the SIGINT (keyboard interrupt) signal.

Is this a valid IPv4 address? 128.163.146.21

VALID

Is this a valid IPv4 address? 127.0.0.1

VALID -- Local host reserved address

Suppose we need to scan through a file to find the first occurrence of the character 'A'. We could do this with a loop that reads one character at a time. Why would such a loop be much faster if it used a stdio call such as fgetc, rather than the low-level read() system call?

When you do a system call, you need a context switch. Every call to read() requires one. Context switches can be thousands of clock cycles worth of time. fgetc() does buffering since it is stdio. So it only needs one system call for every few thousand bytes

Why use dynamic linking over statically linked? (Statically can run on any linux machine regardless of installed libraries)

You can change individual files of dynamically linked executable w/o recompiling. Also, saves space.

In the following program, the child process exits with a random exit status. pid_t pid = fork(); if (/*star* A: is this the child process? *star*/) { /*star* child process *star*/ exit(rand() % 10); } else { /*star* parent process *star*/ /*star* B: wait for child to terminate *star*/ /*star* C: print the exit status *star*/ } Write the waitpid call to wait for the child process to terminate and save its exit status. Declare any necessary variables. You may ignore error handling.

int status; pid_t w_pid = waitpid(pid, &status, WUNTRACED);

What command-line program can be used to send a signal to a process?

kill

What command-line program can be used to translate a host name into an IP address? There are two possible answers.

nslookup hostname

What function is the equivalent of waitpid() for threads?

pthread_join()

What system call is used to disconnect a network connection?

shutdown() or close()


संबंधित स्टडी सेट्स

Exploring the World of Business - Midterm Review

View Set

Lecture: CNS (Brain and Spinal Cord)

View Set

Its a BIG ONE: Acinetobacter baumanii, Spingomonas paucimobilis, Stenotrophomonas maltophilia Vibrio and Aeromonas Pseudomonas, Burkholderia, Alcaligenes, Achromobacter Campylobacter and Helicobacter.

View Set

Perceptions Quiz AP Classroom ?'s -- Ms. Fitz

View Set

Interchange Book 2 Unit 4 Language summary

View Set

Music for the Listener - Quiz Week 9 (Ch 23-25)

View Set

STARTING A BUSINESS / FRANCHISES / PARTNERSHIPS

View Set