Sunday, August 3, 2014

Unix Prog: Unix System Overview - Signal

1. Signal Quick Overview:
signal is sent to the process to notify some condition has occured.
There are 3 choices for process to handle the signal:
1) Ignore the signal, this is not recommended for hardware exceptions, whose result is undefined
2) Let the default behavior occur
3) Provide a handler function when signal occur

2. Conditions generating signal
1) Interrupt key: like DELETE, or Ctrl C, Ctrl Backslash etc.
2) kill command: send a signal to specified process

3. Setup the signal handler
myshell.c:
 #include<stdio.h>  
 #include<stdlib.h>  
 #include<unistd.h>  
 #include<string.h>  
 #include<sys/wait.h>  
   
 #define MAXLINE 1000  
   
 void err_ret(const char* msg)  
 {  
  printf("%s\n", msg);  
  exit(127);  
 }  
   
 void err_sys(const char* msg)  
 {  
  printf("%s\n", msg);  
  exit(1);  
 }  
   
 void sig_int(int signo)  
 {  
  printf("interrupt: %d\n%% ",signo);  
 }  
   
 int main(void)  
 {  
  char buf[MAXLINE];  
  pid_t pid;  
  int status;  
   
  // Setup the signal handler function, whenever SIGINT is received  
  // it will run the function sig_int  
  if(signal(SIGINT, sig_int) == SIG_ERR)  
   err_sys("signal error");  
   
  printf("%% ");  
  while(fgets(buf, MAXLINE, stdin) != NULL)  
   {  
    if(buf[strlen(buf)-1] == '\n')  
     buf[strlen(buf)-1] = 0; // Replace new line with null  
   
    if((pid = fork()) < 0)  
     {  
      err_sys("fork error");  
     }  
     else if(pid == 0)  
      {  
       execlp(buf, buf, (char*)0);  
       err_ret("could not execute the buffered command");  
      }  
   
     if((pid = waitpid(pid, &status, 0)) < 0)  
      err_sys("waitpid error");  
   
     printf("%% ");  
   }  
   
  exit(0);  
 }  

shell:
1) Running the myshell.out
2) type in command "date", it will execute the command in unix shell
3) type in "Ctrl C" to send SIGINT signal to this process, it will output "interrupt" and signal number.
4) type in command "pwd", it will execute the command in unix shell, which means the process is still running.@ip-172-31-23-227:~$ ./myshell.out  
 % date  
 Sun Aug 3 22:51:42 UTC 2014  
 % ^Cinterrupt: 2  
 pwd  
 /home/ubuntu  
 % %  

Explanation:
signal function is defined at /usr/include/signal.h. By default it is using the BSD semantics
 ubuntu@ip-172-31-23-227:~$ less /usr/include/signal.h  
 ......  
 /* Set the handler for the signal SIG to HANDLER, returning the old  
   handler, or SIG_ERR on error.  
   By default `signal' has the BSD semantic. */  
 __BEGIN_NAMESPACE_STD  
 #ifdef __USE_BSD  
 extern __sighandler_t signal (int __sig, __sighandler_t __handler)  
    __THROW;  
 #else  
 /* Make sure the used `signal' implementation is the SVID version. */  
 # ifdef __REDIRECT_NTH  
 extern __sighandler_t __REDIRECT_NTH (signal,  
                    (int __sig, __sighandler_t __handler),  
                    __sysv_signal);  
 # else  
 # define signal __sysv_signal  
 # endif  
 #endif  
 __END_NAMESPACE_STD  
 ......  

4. Others - Unix Time Values
Unix system maintains 2 kinds of time values
1) Calendar time: counts the number of seconds since the epoch 00:00:00 January 1st, 1970. Data type: primitive system data type: time_t
2) Process time: also called CPU time, used to measure the central processor resources used by a process. Data type: primitive system data type: clock_t

Execution Time of a process:
1) clock time: also called the wall clock time, is the amount of time taken by the process to run
2) User CPU time: CPU time attributed to user instructions
3) System CPU time: CPU time attributed to kernel when it executes on behalf of the process.

5. System Calls and Library Functions
system call: well-defined limited number of entry points directly into the kernel called system call.

Other Older Operating System: system call, kernel entry points is defined with assembly language of the machine.

Unix System: each system call has a c library function with the same name

System Call can't be replaced, but c library function can be replaced by system calls if necessary.

malloc and sbrk: malloc is the c library function which allocate memory space. sbrk is the system call which can increase or decrease the address space of the process by specified number of bytes. We can replace malloc with our own implementations using sbrk system call.

system call usually provide very simple interface but c library function usually provide more elaborate interfaces. For example, unix's system call only can provide the number of seconds since 00:00:00 Jan 1, 1970 epoch, any interpretation is left to library functions.

No comments:

Post a Comment