Week 40 Process and Signals
Week 40 Process and Signals
Introduction
1
Process
• What is process?
• Structure
2
A process consists of the executable
program, its data and stack, variables(occupying
system memory), open files(file descrip-
tor) and an environment.
1. ps command
ps prints out the information about
the active processes. without options
it only prints out the information about
process associated with the control-
ling terminal.
$ ps
PID TTY TIME CMD
25089 pts/1 00:00:00 tcsh
25115 pts/1 00:00:00 ps
$ps -au
USER PID %CPU %MEM VSZ RSS TTY
lchang 25089 0.0 0.5 2860 1380 pts/1
lchang 25116 0.0 0.3 2856 896 pts/1
$ pstree
init-+-atd
|-3*[automount]
|-bdflush
|-cannaserver
|-crond
|-dhcpcd
|-esd
|-geyes_applet
|-gmc
|-gnome-name-serv
|-gnome-smproxy
|-gnome-terminal-+-gnome-pty-helpe
| |-tcsh
| ‘-tcsh---pstree
|-identd---identd---3*[identd]
|-jserver
|-keventd
|-khubd
|-klogd
|-kreclaimd
|-kswapd
|-kupdated
|-lockd
|-login---tcsh---startx---xinit-+-.xini
| ‘-X
|-magicdev
|-mdrecoveryd
|-5*[mingetty]
|-panel
|-portmap
|-rpc.statd
|-rpciod
|-sawfish
|-sshd
|-syslogd
|-tasklist_applet
|-xemacs
|-xfs
|-xinetd
|-xscreensaver
‘-ypbind---ypbind---2*[ypbind]
#include <sys/types.h>
#include <unistd.h>
pid_t fork(void)
pid_t new_pid;
new_pid = fork();
switch(new_pid){
case -1: /* erro */
break;
case 0: /*we are child*/
break;
default: /*we are parent*/
break;
}
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
int main()
{
pid_t pid;
char *message;
int n;
switch(pid){
case -1:
fprintf(stderr, "fork failed");
exit(1);
case 0:
n=3;
message = "Child";
break;
default:
n=4;
message = "Parent";
break;
}
for(; n>0 ; n--){
printf("%s %d\n", message, n);
sleep(1);
}
exit(0);
}
#include <sys/types.h>
#include <sys/wait.h>
pid_t wait(int *stat_loc);
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
int main()
{
pid_t pid;
char *message;
int n;
int stat_val;
switch(pid){
case -1:
fprintf(stderr, "fork failed");
exit(1);
case 0:
n=3;
message = "Child";
break;
default:
n=2;
message = "Parent";
break;
}
for(; n>0 ; n--){
printf("%s %d\n", message, pid);
sleep(1);
}
if(pid !=0){
pid_t child_pid;
child_pid = wait(&stat_val);
printf("Child %d has finished\n",
child_pid);
if(WIFEXITED(stat_val))
printf("Child exit with code %d\n",
WEXITSTATUS(stat_val));
else
printf("Child terminate abnormally\n");
}
exit(WEXITSTATUS(stat_val));
}
Signals
• What is signal?
1. signal()
#include <signal.h>
void (*signal(int sig, void(*func)(int)));
2. A program CtrlC
#include <signal.h>
#include <stdio.h>
#include <unistd.h>
#include <signal.h>
int sigaction(int sig, const struct
sigaction *act,
sigaction *oact);
void sa_handler/*function,SIG_DFL,SIG_IGN*/
sigset_t sa_mask /*signals to be blocked */
int sa_flags /*signal action modifier*/
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
void question()
{
printf("continue or quit?\n");
sleep(5);
}
int main()
{
struct sigaction act;
act.sa_handler = question;
sigemptyset(&act.sa_mask);
act.sa_flags=0;
sigaction(SIGINT, &act,0);
while(1){
printf("current time is:\n");
system("date");
sleep(1);
}
}
• Sending Signals
1. kill()
#include <sys/types.h>
#include <signal.h>
int kill(pid_t pid, int sig);
#include <unistd.h>
unsigned int alarm(unsigned int seconds);
#include <signal.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
THE END