Today - PowerPoint PPT Presentation

About This Presentation
Title:

Today

Description:

Todays topic – PowerPoint PPT presentation

Number of Views:26
Avg rating:3.0/5.0
Slides: 15
Provided by: wfa
Learn more at: http://www.cs.fsu.edu
Category:
Tags: options | sscanf | today

less

Transcript and Presenter's Notes

Title: Today


1
Todays topic
  • Environment variables
  • Signal

2
  • The list of environment variables
  • try env
  • Environment variables can be defined in shell
  • setenv DISPLAY L1030.0
  • DISPLAYL1030.0
  • These variables can be accessed in the program
  • Accessing environmental variables
  • extern char environ

3
  • This figure shows an example how the environment
    list is organized.
  • See env1.c for how to use environ.

4
  • ANSI C also specifies a routine for accessing
    environment variables
  • char getenv(char name)
  • See env2.c.

5
  • IPC mechanism Signal
  • Tells a process that some event occurs. It occurs
  • In the kill command.
  • Try kill l
  • kill s INT (pid
  • when Ctrl-C is typed (SIGINT).
  • When a child exits (SIGCHLD to parent).
  • A form of inter-process communication.

6
  • When a process receives a signal, it performs one
    of the following three options
  • Ignore the signal
  • Perform the default operation
  • Catch the signal (perform a user defined
    operation).
  • Some commonly used signals
  • SIGABRT, SIGALRM, SIGCHLD, SIGHUP, SIGINT,
    SIGUSR1, SIGUSR2, SIGTERM, SIGKILL, SIGSTOP,
    SIGSEGV, SIGILL
  • All defined in signal.h

7
  • Processing signals
  • similar to interrupt (software interrupt)
  • when a process receives a signal
  • stop execution
  • call the signal handler routine
  • continue
  • Signal can be received at any point in the
    program.
  • Most default signal handlers will exit the
    program.
  • You can change the way your program responses to
    signals.
  • E.g Make Ctrl-C have no effect.

8
  • ANSI C signal function to change the signal
    handler
  • syntax
  • include ltsignal.hgt
  • void (signal(int sig, void (disp)(int)))(int)
  • semantic
  • sig -- signal (defined in signal.h)
  • disp SIG_IGN, SIG_DFL or the address of a signal
    handler.
  • Handler may be erased after one invocation.
  • How to get continuous coverage?
  • Still have problems may lose signals
  • See example1.c

9
  • Block/unblock signals
  • Manipulate signal sets
  • include ltsignal.hgt
  • Int sigemptyset(sigset_t set)
  • Int sigfillset(sigset_t set)
  • Int sigaddset(sigset_t set, int signo)
  • Int sigdelset(sigset_t set, int signo)
  • Int sigismember(const sigset_t set, int signo)
  • Manipulate signal mask of a process
  • Int sigprocmask(int how, const sigset_t set,
    sigset_t oset)
  • How SIG_BLOCK, SIG_UNBLOCK, SIG_SETMASK
  • See example2.c

10
  • For a critical region where you dont want
    certain signal to come, the program will look
    like
  • sigprocmask(SIG_BLOCK, newmask, oldmask)
  • . / critical region /
  • sigprocmask(SIG_SETMASK, oldmask, NULL)

11
  • sigaction
  • Supersedes the signal function
  • include ltsignal.hgt
  • int sigaction(int signo, const struct sigaction
    act, struct sigaction oact)
  • struct sigaction
  • void (sa_handler)() / signal handler /
  • sigset_t sa_mask /additional signal to be
    block /
  • int sa_flags / various options for
    handling signal /
  • See example4.c

12
  • Kill
  • Send a signal to a process
  • include ltsignal.hgt
  • include ltsys/types.hgt
  • Int kill(pid_t pid, int signo)
  • Pid gt 0, normal
  • Pid 0, all processes whose group ID is the
    current processs group ID.
  • Pid lt0, all processes whose group ID pid
  • See example5.c

13
  • Signalling example keeping track of number of
    child processes in a shell when a process exits,
    it sends a SIGCHLD to its parent.

14
  • int numofchild 0
  • void sigchildhandler() numofchild --
  • main()
  • char cmd1000, buf1000, argv2 struct
    sigaction abc
  • abc.sa_handler sigchildhandler
    sigemptyset(abc.sa_mask)
  • abc.sa_flags 0 sigaction(SIGCHLD, abc,
    NULL)
  • while(1)
  • printf(ltdgt, numofchild) fflush(stdout)
  • while(fgets(buf, 100, stdin) NULL)
  • sscanf(buf, s, cmd)
  • if (strcmp(cmd, quit) 0)
  • if (numofchild 0) exit(0)
  • else printf(Cannot exit, there are
    still d children.\n, numofchild)
  • if ((pid fork()) 0)
  • argv0 cmd, argv1 NULL
  • execv(argv0, argv) exit(0)
  • if (pid ! -1) numofchild
Write a Comment
User Comments (0)
About PowerShow.com