Multithreading, Multiprocessing, and IPC

0.0(0)
studied byStudied by 0 people
0.0(0)
full-widthCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/22

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

23 Terms

1
New cards

Pass-by-value vs Pass-by-reference

  • Pass-by-value: the function gets a copy → changes don't affect the original

    • int addFIve(int x);

  • Pass-by-reference: the function gets the original → changes affect the original

    • int scanf(const char *format,…)

    • void add_five(int *x);

2
New cards

Process in RAM

knowt flashcard image
3
New cards

Process States

  • NEW - the process is in the stage of being created

  • READY - the process has all the resources available that it needs to run (but CPU is not currently working on this process’s instructions)

  • RUNNING - CPU is working on this process’s instructions

  • WAITING - the process cannot run at the moment, it is waiting for some resource to become available or for some event to occur

    • ex. process may be waiting for keyboard input, disk access request, inter-process messages, a timer to go off, or a child process to finish

  • TERMINATED - process has completed

4
New cards

POSIX Threads Library <pthread.h>

  • allows spawning of new concurrent process flow

  • most effective on multi-processor/multi-core systems where the process flow can be scheduled to run on another processor

    • thus gaining speed through parallel/distributed processing

  • must include -lpthread to your gcc arguments

5
New cards

each thread has a unique…

  1. Thread ID

  2. set of registers, stack pointer

  3. stack for local variables, return addresses

  4. signal mask

  5. priority

  6. return value: errno

<ol><li><p><strong>Thread ID</strong></p></li><li><p>set of registers, stack pointer</p></li><li><p><strong>stack for local variables, return addresses</strong></p></li><li><p>signal mask</p></li><li><p>priority</p></li><li><p><strong>return value: errno</strong></p></li></ol><p></p>
6
New cards

important functions

int pthread_create(pthread_t * thread, const pthread_attr_t * attr, void * (start_routine)(void *), void *arg);

  • thread - returns the thread id

  • attr - set to NULL if default thread attributes are used (else define yourself)

  • function - pointer to the function to be threaded

  • arg - pointer to the argument for the function, to pass multiple arguments, send a pointer to a structure

7
New cards

In-class exercise: Passing a struct into a thread

$ cp /user/cmlucero/threads/struct_thread.c ~/struct_thread.c

  • In the print_student function, cast the void pointer to a Student struct and print the contents

  • In the main function, use pthread_create for both Student structs

  • Remember to pthread_join

  • NOTE: There are no return values being used for this example

8
New cards

Mutexes in Threads

knowt flashcard image
9
New cards

Parrallel Threading

$ cp /user/cmlucero/threads/parallel.c ~/parallel.c
- First find the average in a single-threaded approach by filling in the
calculate_average_single_thread() function.
- For the multithreaded approach, the thread function you should use is
calculate_average()
- Pass in the Range struct to that function to determine which part of the array
you’ll be solving.

10
New cards

multithreading vs multiprocess

knowt flashcard image
11
New cards

fork()

the creator (parent) and created (child) processes, by default, do not share memory

  • whatever data the parent contains is copied to the child process but alterations aren’t seen by either

<p>the creator (<strong>parent</strong>) and created (<strong>child</strong>) processes, by default, <strong>do not share memory</strong></p><ul><li><p>whatever data the parent contains is copied to the child process but alterations aren’t seen by either</p></li></ul><p></p>
12
New cards

Shared Memory

  • unlike shared files, doing IPC over shared memory gives it the benefit of faster memory access

  • whenever shared memory comes into the picture with a writer (whether multi-process or multi-threading) so does the risk of a memory-based race condition; hence the usage of semaphores

13
New cards

Semaphores

  • general semaphore (aka counting semaphore) has a value that can be incremented/decremented

    • typically initialized to zero

    • consider a shop that rents bicycles (100 in stock): Every time a bike is rented, the semaphore is decremented by 1, when a bike is returned it’s incremented by 1. Rentals can continue until the value hits 0; must halt until at least one bike is returned.

  • binary semaphore is a special case requiring only two values: 0 and 1

    • in this situation, the semaphore acts as a mutex (mutual exclusion construct)

14
New cards

Sockets

  • channel-based communication mechanism between processes

  • shared files, shared memory, and pipes all deal with IPC within the same computer

  • IPC Sockets (aka UNIX domain sockets) enable channel-based communication for processes on the same physical device (host)

    • rely upon the local system kernel to support communication

      • ex. a local file as a socket address

  • Network Sockets enable this kind of IPC for processes that can run on different hosts, thereby bringing networking into play

15
New cards

Network Sockets

  • need support from an underlying protocol such as TCP/IP (Transmission Control Protocol) or the lower-level UDP (User Datagram Protocol)

  • you can still use network sockets to have two processes communicate with each other on the same machine by leveraging the localhost (127.0.0.1) network address

  • network sockets are externally identified to other hosts by a socket address (defined by the transport protocol (TCP vs UDP), IP address, and port

<ul><li><p>need support from an underlying protocol such as <strong>TCP/IP</strong> (Transmission Control Protocol) or the lower-level <strong>UDP</strong> (User Datagram Protocol)</p></li><li><p>you can still use network sockets to have two processes communicate with each other on the same machine by leveraging the <em>localhost</em> (127.0.0.1) network address</p></li><li><p>network sockets are&nbsp;externally identified to other hosts by a s<strong>ocket address</strong>&nbsp;(defined by the transport protocol (TCP vs UDP), <strong>IP address</strong>, and <strong>port</strong></p></li></ul><p></p>
16
New cards

Using Sockets (Server)

The application programming interface (API) for the network protocol stack creates a handle for each socket created by an application (socket/file descriptor).

  1. socket() - get a file descriptor for the socket connection

  2. bind() - bind the socket to an address on the server’s host

  3. listen() - listen for client requests

  4. accept() - accept a client’s request

17
New cards

socket()

  • #include <sys/socket.h>

  • creates the socket by taking in 3 arguments and returning an integer for the file descriptor

  • int socket(int domain, int type, int protocol);

  • domain: AD_INET vs AF_LOCAL

  • type: SOCK_STREAM, SOCK_DGRAM, SOCK_SEQPACKET

  • protocol: 0 for system default

<ul><li><p><code>#include &lt;sys/socket.h&gt;</code></p></li><li><p>creates the socket by taking in 3 arguments and returning an integer for the file descriptor</p></li><li><p><code>int socket(int domain, int type, int protocol);</code></p></li><li><p><strong>domain</strong>: AD_INET vs AF_LOCAL</p></li><li><p><strong>type</strong>: SOCK_STREAM, SOCK_DGRAM, SOCK_SEQPACKET</p></li><li><p><strong>protocol</strong>: 0 for system default</p></li></ul><p></p>
18
New cards

bind()

  • #include <sys/socket.h>

  • binds the socket to a memory address

  • int bind(int socket, const struct sockaddr *address, socklen_t address_len);

  • socket - file descriptor

  • address - the sockaddr we defined when creating the socket

  • address_len - the length of the sockaddr

<ul><li><p><code>#include &lt;sys/socket.h&gt;</code></p></li><li><p>binds the socket to a memory address</p></li><li><p><code>int bind(int socket, const struct sockaddr *address, socklen_t address_len);</code></p></li><li><p><strong>socket</strong> - file descriptor</p></li><li><p><strong>address</strong>&nbsp;- the<strong> sockaddr</strong> we defined when creating the socket</p></li><li><p><strong>address_len</strong>&nbsp;- the length of the sockaddr</p></li></ul><p></p>
19
New cards

listen()

  • #include <sys/socket.h>

  • listen for socket connections and limit the queue of incoming connections

  • int listen(int socket, int backlog);

  • socket - file descriptor

  • backlog - limits the number of outstanding connections to a socket’s listening queue

<ul><li><p><code>#include &lt;sys/socket.h&gt;</code></p></li><li><p>listen for socket connections and limit the queue of incoming connections</p></li><li><p><code>int listen(int socket, int backlog);</code></p></li><li><p><strong>socket</strong>&nbsp;- file descriptor</p></li><li><p><strong>backlog</strong>&nbsp;- limits the number of outstanding connections to a socket’s listening queue</p></li></ul><p></p>
20
New cards

accept()

  • #include <sys/socket.h>

  • accept a new connection to the socket

  • int accept(int socket, struct sockaddr *address, socketlen_t *address_len);

  • defaults to a blocking wait: the server will do nothing until a client attempts to connect and then it proceeds

    • returns -1 if there is an error, otherwise it returns the file descriptor

  • to cleanup a connection use:

    • close(client_fd);

  • when accept() succeeds, the server can then read a client request and respond to them with write:

    • ssize_t read(int fildes, void buf, size_t nbyte, off_t offset);

    • ssize_t write(int fildes, const void buf, size_t nbyte);

<ul><li><p><code>#include &lt;sys/socket.h&gt;</code></p></li><li><p>accept a new connection to the socket</p></li><li><p><code>int accept(int socket, struct sockaddr *address, socketlen_t *address_len);</code></p></li><li><p>defaults to a <strong><em>blocking wait: </em></strong>the server will do nothing until a client attempts to connect and then it proceeds</p><ul><li><p>returns -1 if there is an error, otherwise it returns the file descriptor</p></li></ul></li><li><p>to cleanup a connection use:</p><ul><li><p><code>close(client_fd);</code></p></li></ul></li><li><p>when accept() succeeds, the server can then read a client request and respond to them with write:</p><ul><li><p><code>ssize_t read(int fildes, void buf, size_t nbyte, off_t offset);</code></p></li><li><p><code>ssize_t write(int fildes, const void buf, size_t nbyte);</code></p></li></ul></li></ul><p></p>
21
New cards

Using Sockets (Client)

knowt flashcard image
22
New cards

connect()

  • #include <sys/socket.h>

  • attempts to make a connection on a socket

  • int connect(int socket, const struct sockaddr *address, socketlen_t address_len);

  • socket - file descriptor

  • address - points to a sockaddr we defined when creating the socket

  • address_len - length of the sockaddr

  • can fail for a number of reasons:

    • wrong server address

    • too many clients already connected to the server

  • if connect() succeeds, the client can write requests to the server and subsequently use a loop to read responses from the server

  • when finished REMEMBER: close(socketfd);

<ul><li><p><code>#include &lt;sys/socket.h&gt;</code></p></li><li><p>attempts to make a connection on a socket</p></li><li><p><code>int connect(int socket, const struct sockaddr *address, socketlen_t address_len);</code></p></li><li><p><strong>socket </strong>- file descriptor</p></li><li><p><strong>address </strong>- points to a <strong>sockaddr</strong> we defined when creating the socket</p></li><li><p><strong>address_len </strong>- length of the <strong>sockaddr</strong></p></li><li><p>can fail for a number of reasons:</p><ul><li><p>wrong server address</p></li><li><p>too many clients already connected to the server</p></li></ul></li><li><p>if <strong>connect()</strong> succeeds, the client can write requests to the server and subsequently use a loop to read responses from the server</p></li><li><p>when finished REMEMBER: <code>close(socketfd);</code></p></li></ul><p></p>
23
New cards

Useful Libraries

knowt flashcard image