Project 2 - Deadline: 12 Noon Oct 28 ================================================== Signals - Review - Signals are generated in the kernel and sent to a process: * All signals have a default action. Most can be set to a different action > Actions: Block, Catch, Ignore, Default + A blocked signal will eventually be unblocked and at that time can be caught, ignored, or allowed to act by default > Signals are handled by default in the following manners: Terminate, ignore List: https://dsa.cs.tsinghua.edu.cn/oj/static/unix_signal.html - signal() sets up a signal handler for a particular signal * Example signal(SIGINT, removeQ) sets up the program to catch the signal and call removeQ, which is a handler. > A signal handler can have only one parameter, and that can only be an int for the signal #. sigset_t - bitmap where each of the 32 bits corresponds to one signal. - Used to create signal masks for blocking/unblocking desired signals. - sigset below is a sigset_t - A signal mask is a bit string with each element representing one signal. * sigprocmask is used to set selected signals to block or unblock the signals whose corresponding bits are set in the sigmask, which is of type sigset_t. These are used for working with a signal mask for blocking/unblocking signals: sigemptyset(&sigset); // Clear the sigset_t ; all signals unset sigfillset(&sigmask); // Set all bits in the sigset_t sigaddset(&sigset,signalNum); // Set the bit for signalNum sigdelset(&sigset,signalNum); // Unset the bit for signalNum sigprocmask(SIG_BLOCK,&sigset,&oldset);// For all signals with bit set, the action on that signal will be to block it. sigprocmask(SIG_UNBLOCK,&sigset,&oldset);// For all signals with bit set, the action on that signal will be to unblock it. sigpending(&pendingset); // Get all signals that are currently blocked and waiting to be active pending set is an export parameter - has sigset_t when it returns sigismember(&pendingset,i) // Returns 0 or 1 depending on whether signal i is in pendingset sigaction() and signal() are used to designate a signal handler ============================================================= Sockets Two types: Stream - TCP-IP - TCP-IP stands for Transmission control protocol, internet protocol Datagram - UDP - UDP stands for Unconnected data protocol TCP-IP Notes - C/C++: The server services clients by forking child processes - Java : The server services clients within threads, i.e. lightweight processes Processes - Server * Allocate socket file descriptor socket() * Claim port # bind() > Only the server needs to allocate a specific port, because its port for handling service requests must be 'well-known' * Set up listening queue size listen() * Wait for connect requests accept() - fires when a connect request comes in from a client - Client * Allocate socket file descriptor socket() * Make connection request connect() Both client and server must populate a sockaddr struct: - Server: Specify who can connect and port number. bind() executes - Client: Specify server IP address and port. connect executes. Connecting - connect() in client causes accept() in server to fire. Some machines are big-endian and some are little-endian. To insure that data transferred over the internet is not corrupted, hton and ntoh are used to convert to and from network format. They are suffixed with an s for short or an n for long. Notes: - write() and read() can be used if no flags need to be specified * send() and recv() provide an argument to specify a flag > See peekser for an example. - See sersig for an example of catching SIGCHLD * EINTR is the signal received when a system call is interrupted. * In sersig, accept() is NOT re-entrant. This means we must restart it manually - Datagram client must use sendto() * Otherwise, system has no way to route content - Return address of client * In accept() for stream * In recvfrom() for dgram > Datagram server can use read or recv if no return message or ack will be sent.