CS 351 Week 15 Course Review Objectives: 1. To review the contents from different weeks. 2. To have a complete understanding of important concepts from different weeks. Concepts: 1. Important Concepts from Different weeks Outline: 1. Final Exam Instructions 2. Discussion of important topics References: 1. Weekly Notes for the entire course CS 351 Week 15 Page 1
CS 351: Week 15 Lecture Notes Datatypes: i) The different data types are: a) Integer b) Character c) Boolean ii) An identifier is used for any variable, function, data definition, etc. An identifier is a combination of alphanumeric characters, the first being a letter of the alphabet or an underline.the remaining being any letter of the alphabet, any numeric digit, or the underline. iii) void main() { int a,b,c; printf( /n enter any two integer numbers ); scanf( \n %d,%d,&a,&b); c=a+b; printf(\n The addition of two numbers is %d,c); Virtual Memory: i) Virtual memory is hardware technique where the system appears to have more memory that it actually does. This is done by time-sharing, the physical memory and storage parts of the memory one disk when they are not actively being used. ii) Benefits of virtual paging: Protection - a process's page numbers correspond only to physical memory addressable by the process Allocation - physical pages allocated/de-allocated as needed by processes (memory on demand) Signal Handling: An application program can specify a function called a signal handler to be invoked when a specific signal is received. When a signal handler is invoked on receipt of a signal, it is said to catch the signal. A process can deal with a signal in one of the following ways: The process can let the default action happen The process can block the signal (some signals cannot be ignored) The process can catch the signal with a handler. Signal handlers usually execute on the current stack of the process. This lets the signal handler return to the point that execution was interrupted in the process. CS 351 Week 15 Page 2
This can be changed on a per-signal basis so that a signal handler executes on a special stack. If a process must resume in a different context than the interrupted one, it must restore the previous context itself ii) Binary Search int binsearch(int x, int v[], int n) { int low, high, mid; low = 0; high = n - 1; while (low <= high) { mid = (low + high) / 2; if (x < v[mid]) { high = mid - 1; else if ( x > v[mid]) { low = mid + 1; else /* found match */ return mid; return -1; /* no match */ Message Passing - Underlying state changes are hidden because message passing abstraction is used to implement message sending. - Message passing model based programming languages define message passing as sending of a data item to communication end point. - Used as a means of inter-process communication - Different message passing styles: o Distributed Computing o SOAP o Actor Model Implementations o Amorphous Computing o Anti-objects - Message Passing Models are one of several computational models for conceptualizing program operations. - Message Passing Model defined as: o Set of processes having only local memory o Processes communicate by sending and receiving messages o Transfer of data between processes requires co-operative operations to be performed by each process. - Other Models include: CS 351 Week 15 Page 3
o Data parallelism o Shared memory o Remote memory operation o Threads o Etc., - Advantages Hardware match Functionality Performance MPI stands for Message Passing Interface. MPI is a library specification for message-passing, proposed as a standard by a broadly based committee of vendors, implementors, and users. The MPI standard is available. Timers: MPI was designed for high performance on both massively parallel machines and on workstation clusters. MPI is widely available, with both free available and vendor-supplied implementations. MPI was developed by a broadly based committee of vendors, implementors, and users. Information for implementors of MPI is available. Test Suites for MPI implementations are available. i) Timers are registered events that fire at programmed interval. ii) Two methods o WM_TIMER message (simpler, for single timer) o Callback function (multiple timers applications) The host is responsible for reading the port number from each packet it receives to decide which program should receive that chunk of data. o If you like, you can think of the packets as letters sent via snail mail, the IP address like the street address. Then the port is the apartment or suite numberas a general (but far from absolute) rule each computer only has one Internet address. However, computers often need to communicate with more than one host at a time. For example, there may be multiple ftp sessions, a few web connections, and a chat program all running at the same time. o To make this possible the computer's network interface is logically subdivided into 65,536 different ports. This is an abstraction. A port does not represent anything physical like a serial or parallel port. However as data traverses the Internet in CS 351 Week 15 Page 4
packets, each packet carries not only the address of the host but also the port on that host to which it's aimed. o As a general (but far from absolute) rule each computer only has one Internet address. However, computers often need to communicate with more than one host at a time. For example, there may be multiple ftp sessions, a few web connections, and a chat program all running at the same time. o To make this possible the computer's network interface is logically subdivided into 65,536 different ports. This is an abstraction. A port does not represent anything physical like a serial or parallel port. However as data traverses the Internet in packets, each packet carries not only the address of the host but also the port on that host to which it's aimed Sockets: - Socket is an abstraction for an end point of communication that can be manipulated with a file descriptor. - It is an abstract object from which messages are sent and received. - Sockets are created within a communication domain just as files are created within a file system. - A communication domain is an abstraction introduced to bundle common properties of processes communicating through sockets. Example: UNIX domain, internet domain. Interprocess Communication - Typical applications today consist of many cooperating processes either on the same host or on different hosts. - For example, consider a client-server application. How to share (large amounts of) data? - How to share files and avoid contention issues? - We want a general mechanism that will work for processes irrespective of their location. IPC: Purpose - Data Transfer - Sharing Data - Event Notification - Process Control IPC at different Levels: - Process Level- Pipes - OS Level- Sockets - Plain Language Level- Distriubuted System Methods Socket Address: CS 351 Week 15 Page 5
Socket address is nothing but the combination of IP address and the port number. The format of the socket address is something like the one given below: Socket address= <ip address>:<port address> Eg: 192.168.1.1:8902 Socket Functions: - Creating a Socket o socket system call create a socket on demand. o int socket(int domain, int type, int protocol) Domain: PF_INET (Internet), PF_UNIX (local) Type: SOCK_STREAM, SOCK_DGRAM, SOCK_RAW Protocol: 0 usually for IP (see /etc/protocols for details) - Binding a Socket o A socket is created without any association to local or destination address. It is possible to bind the socket to a specific host and in it a specific port number. o int bind(int socket struct sockaddr *addr int addr_len) - Closing a socket o close (socid); closes the specified socket. This is done by a process or thread when it no longer needs the socket connection. o close(int socket) - Connecting o A socket is created in an unconnected state, which means that the socket is not associated with any destination. o An application program should call connect to establish a connection before it can transfer data thru reliable stream socket. For datagrams connect is not required but recommended. o int connect(int socket, struct sockaddr *addr, int addr_len) - Sending o Five different system calls : send, sendto, sendmsg, write, writev o send, write and writev work only with connected sockets. No parameter for destination address. Prior connect should be present for communication. o int send(int socket, char *buf, int blen, int flags) - Receiving o Five different calls are available: read, readv, recv, recvfrom, recvmsg o read, readv, and recv are for connection-oriented comm. o int recv(int socket, char *buf, int blen, int flags) CS 351 Week 15 Page 6
- Listening o To avoid having protocols reject incoming request, a server may have to specify how many messages need to be queued until it has time to process them. o Example: listen(socket,length); - Accepting o Wait for the call. o int accept(int socket, struct sockaddr *addr, int addr_len) Threads lightweight processes o A sequence of instructions being executed when there is no external intervention. o Sometimes we want to share data as well as code. o Easier to create than a process. o They provide a nice encapsulation of a problem within a process rather than multiple processes. o Easier to switch between threads than between processes. o Typical uses splitting work across processors (shared memory multiprocessor) added responsiveness (handle user input while still finishing another function) controlling and monitoring other threads server applications can help program abstraction Thread implementation o User-level The OS only sees one thread per process. The process constructs other threads by user-level library calls or by hand. User-level control over starting and stopping threads. Usually a request is made to the OS to interrupt the process regularly (an alarm clock) so that the process can schedule another thread. The state of threads in the library code does not correspond to the state of the process. o System-level The OS knows about multiple threads per process. Threads are constructed and controlled by system calls. The system knows the state of each thread. User-level thread advantages Works even if the OS doesn't support threads. Some implementations of Java have user-level threads because the underlying OS doesn t. Ruby provides threads even on DOS. Easier to create - no system call. Just a normal library procedure call. CS 351 Week 15 Page 7
No switch into kernel mode (this saves time). Control can be application specific. Sometimes the OS doesn t give the type of control an application needs. Example: precise priority levels, changing scheduling decisions according to state changes Easier to switch between - saves two processor mode changes. System-level thread advantages o Each thread can be treated separately. o Rather than using the timeslice of one process over many threads. o Should a process with 100 threads get 100 times the CPU time of a process with 1 thread? o A thread blocking in the kernel doesn't stop all other threads in the same process. o With the user-level threads if one thread blocks for IO the OS sees the process as blocked for IO. o On a multiprocessor different threads can be scheduled on different processors. o This can only be done if the OS knows about the threads. Jacketing: o The major problem with user-level threads is the blocking of all threads within a process when one blocks. o A possible solution is known as jacketing. o A blocking system call has a user-level jacket. o The jacket checks to see if the resource is available. Example: A device when it is free. o If not another thread is started. o When the calling thread is scheduled again (by the thread library) it once again checks the state of the device. o So there has to be some way of determining if resources are available to accept requests immediately. Pipes: - Provides interprocess communication channel - Processes must be on the same machine - Used mostly for filters - A filter is a process that reads from stdin and writes to stdout - Pipe is a communication channel abstraction Process A can write to one end using write system call Process B can read from the other end using read system call - System call - int pipe( int fd[2] ); return 0 upon success 1 upon failure fd[0] is open for reading fd[1] is open for writing. CS 351 Week 15 Page 8