Operating Systems 2019W Lecture 6: Difference between revisions

From Soma-notes
Line 9: Line 9:
* When the kernel delivers a signal to a process, it runs the specified handler
* When the kernel delivers a signal to a process, it runs the specified handler
* The C library defines default handlers for all signals (except STOP and KILL)
* The C library defines default handlers for all signals (except STOP and KILL)
* When a process gets a signal, current execution is interrupted and the handler
* When a process gets a signal, current execution is interrupted and the handler is invoked.  When the handler terminates, the process continues where it was
  is invoked.  When the handler terminates, the process continues where it was
* Interrupted system calls return with an error EINTR.
* Interrupted system calls return with an error EINTR.
* If the process was blocked on a system call, the system call is interrupted
* If the process was blocked on a system call, the system call is interrupted and the handler is run.
  and the handler is run.
* The standard library can do different things with interrupted system calls.  The SA_RESTART flag says to run the system call again.  It only applies to a few system calls, primarily read-like ones.  (So, when a read is interrupted, it is tried again.)
* The standard library can do different things with interrupted system calls.  The SA_RESTART flag says to run the system call again.  It only applies to a few system calls, primarily read-like ones.  (So, when a read is interrupted, it is tried again.)


Line 23: Line 21:
* They are both stored in the memory of the process.
* They are both stored in the memory of the process.
* They are put there by execve, as part of loading a new executable into a process (and erasing everything that was in the process's address space).
* They are put there by execve, as part of loading a new executable into a process (and erasing everything that was in the process's address space).
* execve doesn't necessarily change other aspects of process's state.
* execve doesn't necessarily change other aspects of process's state, thus they should be initialized by something else (normally, the C library)
* In particular, the data structures associated with file descriptors (i.e., open files) are preserved across execve.


==Code==
==Code==

Revision as of 03:29, 24 January 2019

Video

Video from the lecture given on January 23, 2019 is now available.

Notes

Signals

  • processes can register signal handler functions for specific signals
  • When the kernel delivers a signal to a process, it runs the specified handler
  • The C library defines default handlers for all signals (except STOP and KILL)
  • When a process gets a signal, current execution is interrupted and the handler is invoked. When the handler terminates, the process continues where it was
  • Interrupted system calls return with an error EINTR.
  • If the process was blocked on a system call, the system call is interrupted and the handler is run.
  • The standard library can do different things with interrupted system calls. The SA_RESTART flag says to run the system call again. It only applies to a few system calls, primarily read-like ones. (So, when a read is interrupted, it is tried again.)


I/O redirection

Command-line arguments and environment variables

  • They are both stored in the memory of the process.
  • They are put there by execve, as part of loading a new executable into a process (and erasing everything that was in the process's address space).
  • execve doesn't necessarily change other aspects of process's state, thus they should be initialized by something else (normally, the C library)
  • In particular, the data structures associated with file descriptors (i.e., open files) are preserved across execve.

Code

reactive.c

#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <string.h>

void signal_handler(int the_signal)
{
        if (the_signal == SIGUSR1) {
                fprintf(stderr, "Ouch!\n");
                return;
        }
}

int main(int argc, char *argv[], char *envp[])
{
        int i = 1;
        
        struct sigaction signal_handler_struct;

        memset(&signal_handler_struct, 0, sizeof(signal_handler_struct));
        signal_handler_struct.sa_handler = signal_handler;
        signal_handler_struct.sa_flags = SA_RESTART;

        if (sigaction(SIGUSR1, &signal_handler_struct, NULL)) {
                fprintf(stderr, "Couldn't register SIGUSR1 handler.\n");
        }

        printf("Hello!\n");
        printf("Environment variables at %lx\n", (unsigned long) envp);
        printf("Argument variables at %lx\n", (unsigned long) argv);
        printf("Sitting around doing nothing...\n");

        while (1) {
                sleep(i);
                i++;
        }
        
        return 0;
}