week 4 c programming Flashcards
learn it all please
what is a socket
socket behaves similar to a file (can read/write) once connected
A client connects to a server
what is the preprocessor for the socket
include <sys/socket.h>
what is syntax for the socket
int sockfd = socket(AF_INET6,
SOCK_STREAM, 0);
describe sockets in servers
Server binds to port, listens on the socket, and
accepts each client connection
syntax of sockets in a server
bind(sockfd, &srv_struct, len)
listen(sockfd, backlog)
newsockfd = accept(sockfd,
&cl_struct, len);
describe sockets in client
Client connects to IP/port, and then read() and
write() on socket
Now can read() and write() on newsockfd and
close() later
syntax of sockets in client
connect(sockfd, &addr_struct, len)
write(sockfd, buffer, len)
…
close(sockfd)
Same as server regarding read/write/close
what are the solutions to Applications are demanding more resources
Alternative solution:
* Put many processing cores on the microprocessor chip.
* The number of cores doubles with each generation.
what does the CPU go from to
single core to multi core
what is coherence
Coherence’ is the quality of being logical and consistent.
During program execution, data must remain coherent
describe coherence in a multicore system
On a multicore system, a data variable may reside in multiple
caches and might get updated ‘locally’ by its local core
→ this causes coherence problem, and protocols are needed
what are the two options to program multicore systems
Option 1: Program directly targeting processor cores
* Programmer takes care of synchronization
* Painful and error-prone
* Option 2: Use a concurrency platform
* It abstracts processor cores, handles synchronization and
communication protocols, and performs load balancing.
* Hence offers much easier multicore programming environment
* Examples:
* Pthreads and WinAPI threads
* OpenMP
what is a concurrent server
all servers running at the same time
All clients get served by the server.
Even if one client causes blocking, the other clients do not have to wait
describe parallel tasks in concurrent servers
Task2 is parallel to Task1 and Task3
* Parallel tasks are always concurrent.
* Concurrent tasks may not be parallel (Task1 and Task3)
* So, ‘concurrency’ is a more general term
how do we describe programs in a serial system
sequence of
instructions executed one-by-
one
what is a thread
A thread of execution is the smallest sequence of programmed
instructions that can be managed independently by a scheduler,
which is typically a part of the operating system
syntax to include pthreads
include <pthread.h></pthread.h>
- There are many functions related to pthreads.
- On a UNIX-based system, a list of these functions can
typically be obtained with man -k pthread - To know about a specific function see the man page.
- When linking, you must link to the pthread library using
create a thread using pthread
int pthread_create(
pthread_t *thread_id, // ID number for thread
const pthread_attr_t *attr, // controls thread attributes
void (function)(void *), // function to be executed
void *arg // argument of function
)
int pthread_create(
pthread_t *thread_id, // ID number for thread
NULL, // we set it to default thread attributes
void (function)(void *), // function to be executed
void *arg // argument of function
);
returns -> 0 if thread is successful
returns non zero value to indicate error
give me functions that can be passed through pthreads
void *foo1();
void foo2(int *);
int *foo3(int *);
what functions can not be pointed through pthreads
int *foo4(int , int);
T *foo (T *, T *); function with multiple data types
how can we create a pthread that accepts this function
Solution: Pack arguments into a struct and create a wrapper,
which takes the compound argument and unpacks before
passing the unpacked arguments to foo()
typedef struct Compound {
T *a, *b;
} Compound_t;
T * foo_wrapper(Compound_t *c) {
// This can be passed to pthread_create
T *d;
d = foo(c->a, c->b);
}
describe Shared data objects in a concurrent system
Cooperation between concurrent threads leads to the sharing of
Global data objects, Heap objects Files, etc.
Lack of synchronization leads to chaos and wrong calculations
what are the problems with :
Shared data objects in a concurrent system
2 threads
3 scenarios
Scenario 1:
Main thread computes r1+r2 before thread1 and thread2 produces the
results. Both r1 and r2 will be wrong
Scenario 2:
Main thread computes r1+r2 after thread1 but before thread2 produces.
So, r1 will have correct but r2 will have wrong values.
Thus r will be wrong.
Scenario 3:
Luckily, main thread computes r1+r2 after thread1 and thread2 produce.
Luckily, r will be correct.
what is synchronisation in threads
Synchronization in threads programming to make sure that
some events happen in order
Synchronization mechanism in Pthreads
3 methods
- Joins
- Mutual exclusions (“Mutex”)
- Condition variables
what type of function is pthread_join()
pthread_join() is a blocking function
syntax with pthread Null one using Null and onw without using Null
Without NULL
int pthread_join(
pthread_t thread_id, // ID of thread to “join”
void **value_pntr // address of function’s return value
);
with NULL
int pthread_join(
pthread_t thread_id, // ID of thread to “join”
NULL
);
write code for synchronising threads
int r1 = 0, r2 = 0;
int main(void){
pthread_t thread1, thread2;
pthread_create(&thread1,
NULL,
(void *) do_one_thing,
(void *) &r1);
pthread_create(&thread2,
NULL,
(void *) do_another_thing,
(void *) &r2);
pthread_join(thread1, NULL);
pthread_join(thread2, NULL);
do_wrap_up(r1, r2);
return 0;
}
what is a race condition in threads
A race condition occurs when two or more threads perform
operations on the same data, but the results depend on the
order in which these operations are performed
how can we solve race condition in C
we can enforce mutual
exclusion → threads get exclusive access to the shared
resource in turn
what is mutual exclusion
threads get exclusive access to the shared
resource in turn
what does pthread offer to implement mutual exclusion
‘mutex’ to enforce exclusive access by a
thread to a variable or a set of variables
Syntax for mutex in pthreads
pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;
are mutex declared as static or global variables
global
syntax for mutex for serializing access to a shared resource
…
pthread_mutex_lock(&mutex1);
counter++;
pthread_mutex_unlock(&mutex1);
…
Threads access ‘counter’ serially one after another
code that uses 2 threads and pthread mutex
pthread_mutex_t mutex1 =
PTHREAD_MUTEX_INITIALIZER;
int counter = 0;
int main() {
int rc1, rc2;
pthread_t thread1, thread2;
// Two threads execute functionC() concurrently
pthread_create(&thread1, NULL, &functionC, NULL);
pthread_create(&thread2, NULL, &functionC, NULL);
pthread_join(thread1, NULL);
pthread_join(thread2, NULL);
return 0;
}
void *functionC() {
pthread_mutex_lock(&mutex1);
counter++;
printf(“Counter value: %d\n”, counter);
pthread_mutex_unlock(&mutex1);
}
syntax forhow can we allow exclusive access to set of shared objects
serialize access to a set of shared objects
code for serialize access to a set of shared objects
pthread_mutex_lock(&mutex1);
// Access shared object1;
// Access shared object2;
pthread_mutex_unlock(&mutex1);
what is the critical section in mutex lock/unlock in C when serializing access to a set of shared objects
pthread_mutex_lock(&mutex1);
//crticial region
// Access shared object1;
// Access shared object2;
//end of critical region
pthread_mutex_unlock(&mutex1);
shared objects in between the unlock lock are the critical region
The code segment that resides between mutex_lock() and
mutex_unlock() is called ‘critical section’ or ‘critical region’.
Critical section is executed serially by the threads
is critical section executed serially or concurrently
serially
syntax for pthread try lock
int pthread_mutex_trylock(pthread_mutex_t *mutex);
* Tries to lock a mutex object.
* If the mutex object is available, then it is locked and 0 is
returned.
* Otherwise, the function call returns nonzero. It will not
what is pthread_mutex_trylock
Tries to lock a mutex object.
* If the mutex object is available, then it is locked and 0 is
returned.
* Otherwise, the function call returns nonzero. It will not
wait for the object to be free
/ Thread1
void *do_one_thing() {
…
pthread_mutex_lock(&mutex1);
pthread_mutex_lock(&mutex2);
…
pthread_mutex_lock(&mutex2);
pthread_mutex_lock(&mutex1);
…
}
/ Thread2
void *do_another_thing() {
…
pthread_mutex_lock(&mutex2);
pthread_mutex_lock(&mutex1);
…
pthread_mutex_lock(&mutex1);
pthread_mutex_lock(&mutex2);
…
}
what leads to a pitfall betweens these two threads
Thread1 obtains mutex1
Thread1 waits to obtain mutex2
Thread2 obtains mutex2
Thread2 waits to obtain mutex1
Both threads stalled indefinitely
→ Deadlock
what is a conditional variable
A condition variable is used to synchronize threads based on
the value of data (i.e., a condition).
* One thread waits until data reaches a particular value or until a
certain event occurs.
* Then, another thread sends a signal when the event occurs.
* Receiving the signal, the waiting thread wakes up
synatx for pthread conditional variable
A condition variable is a variable of type pthread_cond_t
* A thread goes to waiting state based on ‘condition to happen’ by:
Function takes two arguments: the condition variable and a
mutex variable associated with the condition variable.
* Waking thread based on condition
pthread_cond_t condition_cond = PTHREAD_COND_INITIALIZER;
pthread_cond_wait(&condition_cond, &condition_mutex);
pthread_cond_signal(&condition_cond)