week 4 c programming Flashcards

learn it all please

1
Q

what is a socket

A

socket behaves similar to a file (can read/write) once connected
A client connects to a server

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

what is the preprocessor for the socket

A

include <sys/socket.h>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

what is syntax for the socket

A

int sockfd = socket(AF_INET6,
SOCK_STREAM, 0);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

describe sockets in servers

A

Server binds to port, listens on the socket, and
accepts each client connection

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

syntax of sockets in a server

A

bind(sockfd, &srv_struct, len)
listen(sockfd, backlog)
newsockfd = accept(sockfd,
&cl_struct, len);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

describe sockets in client

A

Client connects to IP/port, and then read() and
write() on socket
Now can read() and write() on newsockfd and
close() later

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

syntax of sockets in client

A

connect(sockfd, &addr_struct, len)
write(sockfd, buffer, len)

close(sockfd)
Same as server regarding read/write/close

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

what are the solutions to Applications are demanding more resources

A

Alternative solution:
* Put many processing cores on the microprocessor chip.
* The number of cores doubles with each generation.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

what does the CPU go from to

A

single core to multi core

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

what is coherence

A

Coherence’ is the quality of being logical and consistent.
During program execution, data must remain coherent

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

describe coherence in a multicore system

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

what are the two options to program multicore systems

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

what is a concurrent server

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

describe parallel tasks in concurrent servers

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

how do we describe programs in a serial system

A

sequence of
instructions executed one-by-
one

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

what is a thread

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

syntax to include pthreads

A

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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

create a thread using pthread

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

give me functions that can be passed through pthreads

A

void *foo1();
void foo2(int *);
int *foo3(int *);

20
Q

what functions can not be pointed through pthreads

A

int *foo4(int , int);

21
Q

T *foo (T *, T *); function with multiple data types
how can we create a pthread that accepts this function

A

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);
}

22
Q

describe Shared data objects in a concurrent system

A

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

23
Q

what are the problems with :
Shared data objects in a concurrent system
2 threads
3 scenarios

A

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.

24
Q

what is synchronisation in threads

A

Synchronization in threads programming to make sure that
some events happen in order

25
Q

Synchronization mechanism in Pthreads
3 methods

A
  • Joins
  • Mutual exclusions (“Mutex”)
  • Condition variables
26
Q

what type of function is pthread_join()

A

pthread_join() is a blocking function

27
Q

syntax with pthread Null one using Null and onw without using Null

A

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
);

28
Q

write code for synchronising threads

A

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;
}

29
Q

what is a race condition in threads

A

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

30
Q

how can we solve race condition in C

A

we can enforce mutual
exclusion → threads get exclusive access to the shared
resource in turn

31
Q

what is mutual exclusion

A

threads get exclusive access to the shared
resource in turn

32
Q

what does pthread offer to implement mutual exclusion

A

‘mutex’ to enforce exclusive access by a
thread to a variable or a set of variables

33
Q

Syntax for mutex in pthreads

A

pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;

34
Q

are mutex declared as static or global variables

35
Q

syntax for mutex for serializing access to a shared resource

A


pthread_mutex_lock(&mutex1);
counter++;
pthread_mutex_unlock(&mutex1);

Threads access ‘counter’ serially one after another

36
Q

code that uses 2 threads and pthread mutex

A

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);
}

37
Q

syntax forhow can we allow exclusive access to set of shared objects

A

serialize access to a set of shared objects

38
Q

code for serialize access to a set of shared objects

A

pthread_mutex_lock(&mutex1);
// Access shared object1;
// Access shared object2;
pthread_mutex_unlock(&mutex1);

39
Q

what is the critical section in mutex lock/unlock in C when serializing access to a set of shared objects

A

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

40
Q

is critical section executed serially or concurrently

41
Q

syntax for pthread try lock

A

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

42
Q

what is pthread_mutex_trylock

A

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

43
Q

/ 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

A

Thread1 obtains mutex1
Thread1 waits to obtain mutex2
Thread2 obtains mutex2
Thread2 waits to obtain mutex1
Both threads stalled indefinitely
→ Deadlock

44
Q

what is a conditional variable

A

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

45
Q

synatx for pthread conditional variable

A

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)