Communication and Synchronization of concurrent tasks
Preview:
Citation preview
- Slide 1
- Communication and Synchronization of concurrent tasks
- Slide 2
- Slide 3
- Communication: the passing of information from one task to
another Synchronisation the satisfaction of constraints on the
interleaving of the actions of tasks
- Slide 4
- Communication requires synchronisation; Synchronisation can be
considered as content less communication. Communication is also
considered as Cooperation
- Slide 5
- Dependency Relationships A B A B A B A NULL B
- Slide 6
- posix_queue object is used to communicate between processes. An
interface to the POSIX message queue, a linked list of strings.
Contains the names of the files that the worker processes are to
search to find the code.
- Slide 7
- Example of unidirectional dependency Threads can also
communicate with other threads within the address space of their
process by using global variables and data structures. If two
threads wanted to pass data between them, thread A would write the
name of the file to a global variable, and thread B would simply
read that variable.
- Slide 8
- Slide 9
- Example of bidirectional dependency Two First - In, First - Out
(FIFO) pipes. A pipe is a data structure that forms a communication
channel between two processes.
- Slide 10
- Slide 11
- Cooperation dependency Task A requires a resource that Task B
owns and Task B must release the resource before Task A can use it
Example: write access to the posix_queue
- Slide 12
- Counting Tasks Dependencies Consider three threads A, B, and C
Let n is the number of threads and k is the number of threads
involved in the dependency. Possible threads involved in
dependency= C( n, k ) =
- Slide 13
- Each combination can be considered as a simple graph. An
adjacency matrix is used to represent dependency relationships for
two thread combinations. An adjacency matrix is a graph G = ( V, E
) in which V is the set of vertices or nodes of the graph and E is
the set of edges such that: A ( i, j ) = 1 if ( i, j ) is an
element of E = 0 otherwise A ( i, j ) A( j, i )
- Slide 14
- Consider tasks A,B, and C
- Slide 15
- C- Communication Co- Cooperation
- Slide 16
- Unified Modeling Language (UML) dependency
- Slide 17
- Interprocess Communication A process sends data to another
process or makes another process aware of an event by means of
operating system APIs,
- Slide 18
- Persistence of IPC The persistence of an object refers to the
existence of an object during or beyond the execution of the
program, process, or thread that created it.
- Slide 19
- Storage class specifies how long an object exists during the
execution of a program. Automatic static dynamic.
- Slide 20
- IPC entities reside in the filesystem, in kernel space, or in
user space Persistence is concerned with the existence of the
object
- Slide 21
- An IPC object with filesystem persistence exists until the
object is deleted explicitly. If the kernel is rebooted, the object
will keep its value. Kernel persistence defines IPC objects that
remain in existence until the kernel is rebooted or the object is
deleted explicitly. An IPC object with process persistence exists
until the process that created the object closes it.
- Slide 22
- Slide 23
- Environment Variables & Command - Line Arguments
Environment variables store system - dependent information such as
paths to directories that contain commands, libraries, functions,
and procedures used by a process.
- Slide 24
- int posix_spawn(pid_t *restrict pid, const char *restrict path,
const posix_spawn_file_action *file_actions, const
posix_spawnattr_t *restrict attrp, char *const argv [restrict ],
char *const envp [restrict ]);
- Slide 25
- Files simplest and most flexible means of transferring or
sharing data.
- Slide 26
- Steps in the file - transferring process: 1. The name of the
file has to be communicated. 2. You must verify the existence of
the file. 3. Be sure that the correct permissions are granted to
access to the file. 4. Open the file. 5. Synchronize access to the
file. 6. While reading/writing to the file, check to see if the
stream is good and that it s not at the end of the file. 7. Close
the file.
- Slide 27
- Shared Memory Using POSIX Shared Memory The shared memory maps:
a file internal memory to the shared memory region
- Slide 28
- #include void *mmap(void *addr, size_t len, int prot, int
flags, int fd,off_t offset); int mumap(void *addr, size_t
len);
- Slide 29
- Slide 30
- fd =open(file_name,O_RDWR); ptr =casting
(mmap(NULL,sizeof(type), PROT_READ, MAP_SHARED, fd, 0));
- Slide 31
- #include int shm_open(const char *name, int oflag, mode_t
mode); int shm_unlink(const char *name); oflag is a bit mask
created by ORing together one of these flags: O_RDONLY or
O_RDWR
- Slide 32
- fd =sh_open(memory _name,O_RDWR,MODE); ptr =casting
(mmap(NULL,sizeof(type ), PROT_READ, MAP_SHARED, fd, 0)); use
semaphores between processes: sem_wait(sem);...*ptr;
sem_post(sem);
- Slide 33
- Slide 34
- Pipes communication channels used to transfer data between
processes. Anonymous Named (also called FIFO)
- Slide 35
- Slide 36
- Slide 37
- Slide 38
- Named Pipes (FIFO) created with mkfifo(): #include int
mkfifo(const char *pathname, mode_t mode); int unlink(const char
*pathname);
- Slide 39
- Program that creates a named pipe using namespace std;
#include
- Slide 40
- int main(int argc,char *argv [],char *envp []) { fstream pipe;
if(mkfifo(Channel-one ,S_IRUSR |S_IWUSR|S_IRGRP|S_IWGRP)==-1) {
cerr