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