Difference between revisions of "Operating Systems 2019W Lecture 6"

From Soma-notes
Jump to navigation Jump to search
Line 6: Line 6:


Topics for today
Topics for today
* environment variables
* signal handlers
* signal handlers
* I/O redirection
* I/O redirection
* environment variables


===In Class===
===In Class===

Revision as of 22:27, 23 January 2019

Video

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

Notes

Topics for today

  • signal handlers
  • I/O redirection
  • environment variables

In Class

Lecture 6
---------

Topics
 - signal handlers
 - I/O redirection
 - environment variables & command line arguments


Key ideas for 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

* 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

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;
}