True CSE 156 - The Programs of our Networks

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

Named semaphores are persistent, what do you need to call after a system restart?

sem_unlink()

What are the two core semaphore functions?

sem_wait() and sem_post().

Named and unnamed semaphores. Which of the two provide multiple accesses in a single process or between related processes?

Unnamed semaphores.

What is the fix to "Failing to protect shared variables?"

Use a lock or semaphore operations. Synchronization operations will slow down code however.

True or False: Fork() returns two completely independent copies of the original process.

True!

True or False: All functions in the standard C Library are thread-safe.

False. Strtok, system, strerror, and more are thread-unsafe. Most are thread-safe, however.

If a thread needs more than one mutex, how do you avoid a deadlock?

Follow a strict order, or use a mutex back-off loop.

Forks vs Threads Which of the two would be better suited for a program that calls exec's family of functions?

Forks, as exec replaces the current process.

Free card. int pthread_mutex_lock(pthread_mutex_t *m) and int pthread_mutex_unlock(pthread_mutex_t *m) lock and unlock the mutex respectively.

Free card.

What does it mean for "a=data" to be non-atomic in the context of threads?

If data changes while it's being loaded into a, a could receive half of the old data and half of the new data.

Programs that have Blocking I/O can do 3 things. What is it called when they just create a new thread/process for each IO call?

Synchronous IO

int sem_wait(sem_t *sem); { ??? } What is this doing?

waiting until value of s > 0 decrement the value of s by 1

Functions called from a thread must be thread safe. Name the four groups that thread-unsafe functions fall into. #4 - Calling ??? functions

Calling thread-unsafe functions, like strtok.

Mutexes avoid data races, and allow one to protect an operation, but they don't permit one to wait until another thread completes an arbitrary activity. What is the name of the thing that does achieve this?

Condition Variables. They're declared with pthread_cond_t *cond;

Producer Check if any spots to put things in, if yes, decrease number of empty spots Lock mutex Put something in Unlock mutex Increase number of filled spots

Consumer Check if any spots to take things from, if yes, decrease number of filled spots Lock mutex Take something out Unlock mutex Increase number of empty spots

What does fork() produce a copy of?

The calling process.

Thread 1: Thread 2: a = data; b = data; a++; b--; data = a; data = b; Threads are executed in an arbitrary order. What does data equal?

+1, 0, or -1. It's impossible to tell. Data = data + 1 and - 1.

What does fork() return in the new process (child)?

0

What's a safe way to avoid deadlocks?

Use timed, non-blocking calls.

What causes a priority inversion?

You need three threads with different priorities, so lower priority threads prevent high priority threads from running.

All UNIX systems are multi-tasking; what system call do they use to run processes simultaneously?

fork()

Which functions are specific to unnamed semaphores?

sem_init() and sem_destroy

Threads share a common address space. What is the most dangerous situation that can arise as a result of this?

A data race.

Data races are a major concern when using threads, but they're not the only danger. What is a deadlock?

A deadlock occurs when you permanently lock a program by locking multiple mutexes in the wrong order, such as locking a mutex behind another mutex. If the second mutex is not available, the thread will wait. If another has the same pair of mutexes in reverse order however, it will be unable to unlock the second mutex, as it would be stuck behind the first one. This creates a deadlock.

There are two typical uses for fork(). Use 1: The new process does other tasks, such as network concurrent servers. What's the other typical use?

A process wants to execute another program. You need to use exec() as well.

pthread_create description time. int pthread_create(pthread_t *thread_id, const pthread_attr_t *attributes, void* (*thread_function) (void*), void *arguments); What is void *arguments?

Arguments is a void* pointer that will be passed into the thread_function. You can put anything here, really, just make sure to cast it as a (void*). It's difficult to use with arrays though, so consider passing a struct instead. Example, with structName myStruct: (void*) &myStruct. Inside the thread, you'll need to cast this back to a myStruct, like this: structName myStruct = *(structName*)ptr; (assuming ptr is the void argument name for the thread)

Programs that have Blocking I/O can do 3 things. What is it called when they have to deal with complex polls or selects?

Asynchronous IO

Producer for int i = 0; i < something; i++ make_thing(in) sem_wait(&empty) pthread_mutex_lock(&mutex) buffer[X] = in pthread_mutex_unlock(&mutex) sem_post(&full)

Consumer for int i = 0; i < something; i++ sem_wait(&full) pthread_mutex_lock(&mutex) out = buffer[X] pthread_mutex_unlock(&mutex) sem_post(&empty) consume_thing(out)

Producer if (&empty > 0) Lock buffer[X] = in Unlock (filled++)

Consumer if (&filled > 0) Lock out = buffer[X] Unlock (empty++)

Load, update, and store operations can become interleaved/tangled when being used in what?

Different threads

int pthread_mutex_trylock(pthread_mutex_t *m); tries to acquire the lock if it's available, and returns 0 on success. What does it return on failure?

EBUSY

Each process created by fork has its own address space. What does this mean?

Each copy has their own copy of the same variable, independent from the other.

Threads are cheaper than processes, and can easily share data, but run the risk of race conditions and deadlocks. What comes at the cost of easily shareable data?

Error-prone synchronization code within a process, like access to any global variable.

What's the difference between the execl* functions and the execv* functions?

Execl* uses a list of parameters, while execv* uses an array.

Functions called from a thread must be thread safe. Name the four groups that thread-unsafe functions fall into. #1 - Failing to protect ???

Failing to protect shared variables.

pid_t fork(void) What does fork return?

In the parent, the child's PID In the child, 0

What does pthread_join() do?

It causes the program to wait for a thread to terminate before continuing.

Condition variable functions. int pthread_cond_destroy(pthread_cond_t *cond); What does this do?

It deallocates all resources associated with a condition variable, destroying it.

What does the e in execle and execve mean?

It denotes usage of an environmental variable or parameter.

What does pthread_exit() do?

It exits the current thread. It's essentially the same as exit(), but only for that one thread.

Condition variable functions. int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mut); What does this function do?

It forces a thread to WAIT until both the mutex is UNLOCKED and a SIGNAL/BROADCAST has been sent to that cond, telling it to proceed. IT ALSO RE-LOCKS THE MUTEX AFTER.

int pthread_mutex_destroy(pthread_mutex_t *m); deallocated a mutex and destroys it. To use this function, the mutex must be what?

It must be unlocked, or it returns EBUSY.

Condition variable functions. int pthread_cond_signal(pthread_cond_t *cond); What does this function do when used instead of signal?

It unlocks ALL threads blocked by the condition variable. They still need to exit the block one at a time though.

Condition variable functions. int pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mut, const struct timespec *abstime); What is const struct timespec *abstime?

It's a struct, representing the absolute time of the day. struct timespec { time_t tv_sec; long tv_nsec; }

pthread_create description time. int pthread_create(pthread_t *thread_id, const pthread_attr_t *attributes, void* (*thread_function) (void*), void *arguments); What is const pthread_attr_t *attributes?

It's an opaque data type that lets you tune some parameters. To use the default, pass NULL.

What is a pthread_t?

It's an opaque type that acts as a handler for a thread. In simpler terms, it's a unique name for each thread.

What is a Semaphore?

It's another synchronization primitive, used for synchronization.

Condition variable functions. int pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mut, const struct timespec *abstime); What does this function do?

It's like wait, but it will automatically unlock at a certain, absolute time of day. If the time has passed, it returns ETIMEDOUT.

pthread_create description time. int pthread_create(pthread_t *thread_id, const pthread_attr_t *attributes, void* (*thread_function) (void*), void *arguments); What is pthread_t *thread_id?

It's the ID for a the thread. Make a pthread_t thread, then pass it as &thread. (ie: pthread_create(&thread...);)

Condition variable functions. int pthread_cond_init(pthread_cond_t *m, const pthread_condattr_t *attr); pthread_cond_t *m is the condition variable, what is pthread_condattr_t *attr?

It's the attribute for the variable. Just pass NULL into it to use the defaults.

pthread_join description time. int pthread_join(pthread_t thread, void **status_ptr); What is void **status_ptr?

It's the exit status of the terminated thread. (pthread_exit((void*)someValueRepresentingStatus))

pthread_create description time. int pthread_create(pthread_t *thread_id, const pthread_attr_t *attributes, void* (*thread_function) (void*), void *arguments); What is void *(*thread_function) (void*)?

It's the new function you'll be running the pthread on. Must be initialized as such void *myFunction(void* ptr).

pthread_exit description time. int pthread_exit(void *status) What is void* status?

It's the return value from the thread_function itself. It must be typecast as a void* to be returned, but it can be cast back after.

pthread_join description time. int pthread_join(pthread_t thread, void **status_ptr); What is pthread_t thread?

It's the thread that the program is waiting on.

There's no guarantee that the producer or the consumer will go before the other, but if there are no slots available to put things in, then this also means that there ARE slots that can be taken from, so the consumer WILL go.

Likewise, if there's no slots to take things from, this also means that there ARE slots that can be filled, so the producer WILL go.

What is the fix for "Calling thread-unsafe functions (libraries)?"

Modify the function so it only calls thread-safe functions.

A program that runs processes on separate cores is called a ??? Process program?

Multiple Process program.

Pipes or other inter-process communication (IPCs) can be used as an alternative to fork(), but they're inefficient. Why are they inefficient?

Switching between processes is expensive. Variables must be synchronized, and this is slow. There's a limit on the number of processes that can efficiently function.

Named and unnamed semaphores. Which of the two are shared across multiple processes?

Named semaphores.

A function is re-entrant if it accesses no shared variables when called from multiple threads. Are they a proper subset of thread-safe functions?

No, there's still a small number that are thread-unsafe.

What is the fix for "Returning a pointer to a static variable?"

Option 1: Rewrite code so caller passes pointer to struct. This comes with the issue of requiring changes in the caller and callee. Option 2: Lock the variable and copy it. Only requires simple changes in the caller, and none in the callee. The caller must free the memory though.

What kinds of functions rely on a persistent state across multiple function invocations?

Random number generator - Relies on static state. strtok() - It breaks a string into smaller tokens that rely on NULL leading to the next token.

Functions called from a thread must be thread safe. Name the four groups that thread-unsafe functions fall into. #2 - Relying on ??? across invocations

Relying on persistent state across invocations.

Functions called from a thread must be thread safe. Name the four groups that thread-unsafe functions fall into. #3 - Returning a pointer to a ???

Returning a pointer to a static variable.

What is the fix for "Relying on persistent state across multiple function invocations?"

Rewrite the function so that the caller passes in all necessary states. (ie, strtok_r() has an extra argument where it stores the pointer to the start of the next token, which is then passed back into itself.)

Programs that have Blocking I/O can do 3 things. What is it called when they wait for each other before commencing the next?

Serial IO

Consider a concurrent server using multiple processes. What is the simplest way to achieve this?

Spawn a new process for each new client.

Forks vs Threads Which of the two supports concurrency?

Threads

For Producer-Consumer, there's 2 semaphores. The producer has a semaphore corresponding to the number of available slots to put things in.

The consumer has a semaphore corresponding to the number of slots that have things in them.

There's a diagram for the exec functions. I'll try to draw it here. execlp execl execle | | | V V V execvp - > execv - > execve What do the left calls specify?

The left calls specify filename. Use PATH variable unless the filename has a /.

What does fork() return in the calling process?

The new process's (child's) pid.

There's a diagram for the exec functions. I'll try to draw it here. execlp execl execle | | | V V V execvp - > execv - > execve What do the top calls specify?

The top row calls specify each argument separately.

What are fork processes identified by?

Their process ID (pid)

In Producer-Consumer, the main thread spawns producer and consumer threads, then waits for them to complete. How do the producer and consumer threads communicate?

They communicate through their shared FIFO buffer.

Condition variable functions. int pthread_cond_signal(pthread_cond_t *cond); This function wakes up AT LEAST one thread blocked by the condition variable. What does each thread need to do before they return?

They need to re-acquire (lock) the mutex before the will return, so this will force the threads to exit the block one at a time.

How are semaphores initialized?

They're initialized with an integer. (ie semaphore n = 1, or as a counter, semaphore n > 1)

Forks vs Threads Which of the two is better suited for usage in application servers?

Threads, as they feature concurrency. Forks can also be used, but if there's a large cache, fork is unsuitable.

Forks vs Threads Which of the two would be better suited for programs that use multiple processors?

Threads, if your thread library supports it.

Forks vs Threads Which of the two is better suited for user interfaces?

Threads, the UI can respond to the user while operations are in progress. (ie, a music player has a UI, audio processor, and file/network IO)

Forks vs Threads Which of the two would be better suited for Blocking I/O programs?

Threads, they improve performance even under complex situations where Fork would slow.

True or False: Open descriptors in parent before fork are shared with child.

True!

Mutexes are use for locking and unlocking threads. What does this mean?

When a mutex is locked, any threads that reach that line of code will be prevented from continuing/blocked UNTIL the original thread unlocks it.

When the producer goes, it checks if there are slots that can be filled, and if there are, it locks the mutex, fills a slot, unlocks the mutex, then sends a signal saying "there's something in a slot now" by sending a post to the consumer's semaphore.

When the consumer goes, it checks if there's a slot that is filled, and if so, it locks the mutex, takes the slot, unlocks the mutex, then sends a signal saying "there's an empty slot now" by sending a post to the producer's semaphore.

Separate processes provide memory protection and stability, but suffer challenges when...?

When they work on parts of the same task/problem.

How do we prevent data races to threads that need to use the same variable? What is the name of this in pthreads?

While data is being used by another thread, block it with a mutex, causing the other thread to wait for the original thread to finish using it first.

What function do you need to use to run a file on Unix?

exec()

Which of the following two statements are true? exec() generates a new process with its own ID when it's called exec() replaces the currently running program with the one it's calling, but keeps the same process ID

exec() replaces the currently running program with the one it's calling, but keeps the same process ID. The other one is actually the functionality of fork()

Name the 6 types of exec functions. You don't need to state what they do.

execl() execle() execlp() execv() execve() ececvp()

Which exec functions cannot use path names, and must use file names?

execlp() and execvp().

Producer Consumer Problem with Semaphores in C

https://www.youtube.com/watch?v=caFjPdWsJDU

Producer Consumer Problem with Condition Variables in C++

https://www.youtube.com/watch?v=rKGq9AlfPR4

int sem_post(sem_t *s) { ??? } What is this doing?

incrementing the value of s by 1 if there are 1 or more threads waiting, wake 1

Out of the following thread-unsafe functions, which do not have re-entrant version? asctime gethostbyaddr inet_ntoa localtime gethostbyname rand ctime

inet_ntoa. The rest all have a function_r version.

What function compares it two thread ids are equal to each other, and what are its returns?

int pthread_equal(pthread_t thread1, pthread_t thread2); Returns 0 iff the two threads are NOT the same thread, returns nonzero otherwise.

There's videos that explain this better, so I'll use these cards to explain semaphores a little more.

post increases the value of a semaphore by 1, while wait decreases it iff it's positive

What function does a thread call to get its own thread ID?

pthead_t pthread_self();

How do you make a new mutex?

pthread_mutex_t myMutex = PTHREAD_MUTEX_INITIALIZER OR int pthread_mutex_init(pthread_mutex_t *m, const pthread_mutexattr_t *attr); Pass NULL as the second parameter. Returns 0 if successful. Just use the first option.

Unlike fork(), threads share a common address space, and are scheduled internally in a process. What's the most popular thread we've used in this class?

pthreads (POSIX threads)

After calling sem_unlink(), what do you need to call to establish new semaphores?

sem_open(). sem_t sem_open(const char *name, int oflag, mode_t mode, unsigned int value);

Free card. Here's the remaining semaphore functions.

sem_trywait(sem_t *s); sem_timedwait(sem_t *s, const struct timespec *abstime);


Set pelajaran terkait

Wentzel Earth and Space Chapter 8 Quiz

View Set

Psychobiology and psychopharmacology

View Set

ch 3 demand, supply, and market equilibrium

View Set

Responsible Government - Political Accountability 3.0

View Set

Pediatric Hematologic Disorders (Practice Questions)

View Set

Experimental designs and internal/external validity, March 29

View Set

MENTAL HEALTH; CHAPTER 24: COGNITIVE DISORDERS:

View Set