lecture
in color
fork.
pid = fork();
if (pid!=0) {
... // parent
} else {
... //child
}
main() { while (1) fork(); }
main() { while (1) { if (fork()) exit(0); else fork(); } }
if (fork()) if I'm a parent
exit(0) end execution so control-C's won't propogate to children
else fork() and once you're free of parent, fork again!
exec
int pid,status;
if (pid=fork()) {
waitpid(pid,&status,0); // wait for command to finish
} else {
// whatever you want
execl("/path/to/some/command", "arg1", "arg2", ...);
}
cd /g; tar cf - 150NET | (cd /loc/adm; tar xfBp -)this'll have the effect of copying the whole 150NET hierarchy from
/g to /loc/adm. This'll take awhile!cd /g; tar cf - 150NET | (cd /loc/adm; tar xfBp -) &The trailing
& tells the shell to run this process in the background.
I can continue doing other things.
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#define MAIL "/g/150NET/public_html/notes/processes/mail"
main() {
struct stat buf;
time_t lastmod;
stat(MAIL,&buf);
lastmod = buf.st_mtime;
while (1) {
sleep (10);
stat(MAIL,&buf);
if (buf.st_mtime>lastmod) {
printf("you've got mail!");
exit(0);
}
}
}
stat(file,&buf): find out about a file, place the
results into buf.stat.st_mtime: the seconds between the time the
file was last modified and Jan 1 1970 00:00:00 GMT (the birthday
of UNIX).
blackhole{couch}129: touch mail
blackhole{couch}130: spy &
[1] 16248
... time passes ...
blackhole{couch}131: touch mail
blackhole{couch}132: you've got mail!
[1] Done spy
blackhole{couch}132:
touch mail: make mail have the current time as
modification time (as if mail arrived).
.cshrc to be
started every time they logged in.
%1 is job 1.
kill %1kills job number 1.
/etc/services gives names to these ports.
#! /local/bin/perl
use Socket;
use Carp;
BEGIN { $ENV{PATH} = '/usr/ucb:/bin' }
$EOL = "\015\012"; # carriage return/linefeed
# how to print out a log message with time and date
sub logmsg { print "$0 $$: @_ at ", scalar localtime, "\n" }
my $port = 2345; # unique identifier for daemon
my $proto = getprotobyname('tcp');
# create a 'socket': a mechanism to accept connections from outside
socket(Server, PF_INET, SOCK_STREAM, $proto)
|| die "socket: $!";
setsockopt(Server, SOL_SOCKET, SO_REUSEADDR, pack("l", 1))
|| die "setsockopt: $!";
# bind this socket to the port number 2345
bind(Server, sockaddr_in($port, INADDR_ANY))
|| die "bind: $!" ;
# start listening for connections from outside
listen(Server,SOMAXCONN)
|| die "listen: $!";
logmsg "server started on port $port";
my $paddr;
# main loop: accept and process connections
for ( ; $paddr = accept(Client,Server); close Client) {
my($port,$iaddr) = sockaddr_in($paddr);
my $name = gethostbyaddr($iaddr,AF_INET);
logmsg "connection from $name [",
inet_ntoa($iaddr), "]
at port $port";
print Client "Hello there, $name, it's now ",
scalar localtime, $EOL;
}
foo.eecs.tufts.edu,
and then from another host, say:
telnet foo.eecs.tufts.edu 2345then the response will be something like:
blackhole{couch}51: telnet foo.eecs.tufts.edu 2345
Trying 127.0.0.1...
Connected to foo.eecs.tufts.edu.
Escape character is '^]'.
Hello there, foo.eecs.tufts.edu, it's now Thu Feb 8 14:56:32 2001
Connection closed by foreign host.
blackhole{couch}52:
ps -l
ps -efl | grep couch | morewhere you subsitute yourself for
couch.
couch 15742 15740 0 11:29:45 pts/9 0:01 /usr/bin/tcsh
couch 11217 1 0 Jan 22 ? 0:00 /bin/ksh /usr/dt/bin/sdtvolcheck -d -z 5 cdrom
couch 14030 13996 0 Jan 24 ? 0:00 /bin/cat /tmp/.removable/notify8
couch 10112 1 0 Jan 21 ? 0:00 /bin/ksh /usr/dt/bin/sdtvolcheck -d -z 5 cdrom
couch 13996 1 0 Jan 24 ? 0:00 /bin/ksh /usr/dt/bin/sdtvolcheck -d -z 5 cdrom
couch 6596 1 0 Jan 20 ? 0:00 /bin/ksh /usr/dt/bin/sdtvolcheck -d -z 5 cdrom
couch 1236 1 0 Jan 18 ? 0:00 /bin/ksh /usr/dt/bin/sdtvolcheck -d -z 5 cdrom
couch 15531 15506 0 10:14:15 pts/4 0:01 /usr/bin/tcsh
couch 15676 15505 0 10:49:49 ?? 0:01 /usr/dt/bin/dtterm
couch 2901 2865 0 Jan 18 ? 0:00 /bin/cat /tmp/.removable/notify0
couch 15648 15505 1 10:34:12 ?? 0:05 /usr/dt/bin/dtterm
couch 15499 15480 0 10:13:49 pts/2 0:01 /usr/dt/bin/dtsession
couch 10150 10112 0 Jan 21 ? 0:00 /bin/cat /tmp/.removable/notify4
couch 1273 1236 0 Jan 18 ? 0:00 /bin/cat /tmp/.removable/notify2
couch 371 1 0 Jan 17 ? 0:00 /bin/ksh /usr/dt/bin/sdtvolcheck -d -z 5 cdrom
couch 15514 1 0 10:14:10 ? 0:00 /bin/ksh /usr/dt/bin/sdtvolcheck -d -z 5 cdrom
couch 15691 15690 0 10:51:31 pts/8 0:01 (dns helper)
couch 414 371 0 Jan 17 ? 0:00 /bin/cat /tmp/.removable/notify1
couch 15478 1 0 10:13:40 ? 0:00 /usr/dt/bin/dsdm
couch 16146 15650 0 16:01:00 pts/7 0:00 grep couch
couch 15480 15477 0 10:13:40 pts/2 0:00 tcsh -c unsetenv _ PWD; source /u/f/couch/.login; unsetenv DT;
couch 15498 1 0 10:13:48 pts/2 0:01 /usr/dt/bin/ttsession
couch 15425 14791 0 10:13:31 ? 0:01 /bin/ksh /usr/dt/bin/Xsession
couch 15740 15505 0 11:29:41 ?? 0:20 /usr/dt/bin/dtterm
couch 15477 15425 0 10:13:39 pts/2 0:01 /usr/dt/bin/sdt_shell -c unsetenv _ PWD; source /u/f/couch/.login;
couch 9175 1 0 Jan 21 ? 0:00 /bin/ksh /usr/dt/bin/sdtvolcheck -d -z 5 cdrom
couch 15550 15511 0 10:14:18 ? 0:00 /bin/cat /tmp/.removable/notify10
couch 14790 249 10 19:12:09 ? 10:17 /usr/openwin/bin/Xsun :0 -nobanner -auth /var/dt/A:0-tRaiFa
couch 6634 6596 0 Jan 20 ? 0:00 /bin/cat /tmp/.removable/notify3
couch 9214 9175 0 Jan 21 ? 0:00 /bin/cat /tmp/.removable/notify5
couch 15551 15514 0 10:14:18 ? 0:00 /bin/cat /tmp/.removable/notify9
couch 13796 13756 0 Jan 24 ? 0:00 /bin/cat /tmp/.removable/notify6
couch 15435 15425 0 10:13:32 ? 0:00 /usr/openwin/bin/fbconsole
couch 2865 1 0 Jan 18 ? 0:00 /bin/ksh /usr/dt/bin/sdtvolcheck -d -z 5 cdrom
couch 11264 11217 0 Jan 22 ? 0:00 /bin/cat /tmp/.removable/notify7
couch 15524 15506 0 10:14:14 pts/3 0:01 /usr/bin/tcsh
couch 13756 1 0 Jan 24 ? 0:00 /bin/ksh /usr/dt/bin/sdtvolcheck -d -z 5 cdrom
couch 15511 1 0 10:14:09 ? 0:00 /bin/ksh /usr/dt/bin/sdtvolcheck -d -z 5 cdrom
couch 15505 15499 0 10:13:52 ? 0:26 dtwm
couch 15566 15506 0 10:14:22 pts/6 0:00 /usr/bin/tcsh
couch 15557 15506 0 10:14:20 pts/5 0:00 /usr/bin/tcsh
couch 15506 15499 0 10:14:09 ?? 2:18 dtterm -session dtBia4Ra -sdtserver
couch 15508 15499 0 10:14:09 ? 0:06 sdtperfmeter -f -H -t cpu -t disk -s 1 -name fpperfmeter
couch 15650 15648 0 10:34:13 pts/7 0:02 /usr/bin/tcsh
couch 15678 15676 0 10:49:50 pts/8 0:01 /usr/bin/tcsh
couch 15690 15678 5 10:51:26 pts/8 2:44 netscape http://conbrio/g/150NET/register.html
#define SIGHUP 1 /* hangup */ #define SIGINT 2 /* interrupt (rubout) */ #define SIGQUIT 3 /* quit (ASCII FS) */ #define SIGILL 4 /* illegal instruction (not reset when caught) */ #define SIGTRAP 5 /* trace trap (not reset when caught) */ #define SIGIOT 6 /* IOT instruction */ #define SIGABRT 6 /* used by abort, replace SIGIOT in the future */ #define SIGEMT 7 /* EMT instruction */ #define SIGFPE 8 /* floating point exception */ #define SIGKILL 9 /* kill (cannot be caught or ignored) */ #define SIGBUS 10 /* bus error */ #define SIGSEGV 11 /* segmentation violation */ #define SIGSYS 12 /* bad argument to system call */ #define SIGPIPE 13 /* write on a pipe with no one to read it */ #define SIGALRM 14 /* alarm clock */ #define SIGTERM 15 /* software termination signal from kill */ #define SIGUSR1 16 /* user defined signal 1 */ #define SIGUSR2 17 /* user defined signal 2 */ #define SIGCLD 18 /* child status change */ #define SIGCHLD 18 /* child status change alias (POSIX) */ #define SIGPWR 19 /* power-fail restart */ #define SIGWINCH 20 /* window size change */ #define SIGURG 21 /* urgent socket condition */ #define SIGPOLL 22 /* pollable event occured */ #define SIGIO SIGPOLL /* socket I/O possible (SIGPOLL alias) */ #define SIGSTOP 23 /* stop (cannot be caught or ignored) */ #define SIGTSTP 24 /* user stop requested from tty */ #define SIGCONT 25 /* stopped process has been continued */ #define SIGTTIN 26 /* background tty read attempted */ #define SIGTTOU 27 /* background tty write attempted */ #define SIGVTALRM 28 /* virtual timer expired */ #define SIGPROF 29 /* profiling timer expired */ #define SIGXCPU 30 /* exceeded cpu limit */ #define SIGXFSZ 31 /* exceeded file size limit */ #define SIGWAITING 32 /* process's lwps are blocked */ #define SIGLWP 33 /* special signal used by thread library */ #define SIGFREEZE 34 /* special signal used by CPR */ #define SIGTHAW 35 /* special signal used by CPR */ #define SIGCANCEL 36 /* thread cancellation signal used by libthread */ #define SIGLOST 37 /* resource lost (eg, record-lock lost) */
kill
command (which does a lot more than just kill processes).
kill really does is to send a specified signal to a process.
conbrio{couch}172: stty -a
speed 9600 baud;
rows = 30; columns = 80; ypixels = 400; xpixels = 570;
eucw 1:0:0:0, scrw 1:0:0:0
intr = ^c; quit = ^\; erase = ^?; kill = ^u;
eof = ^d; eol = <undef>; eol2 = <undef>; swtch = <undef>;
start = ^q; stop = ^s; susp = ^z; dsusp = ^y;
rprnt = ^r; flush = ^o; werase = ^w; lnext = ^v;
-parenb -parodd cs8 -cstopb -hupcl cread -clocal -loblk -crtscts -crtsxoff -parext
-ignbrk brkint -ignpar -parmrk -inpck -istrip -inlcr -igncr icrnl -iuclc
ixon -ixany -ixoff imaxbel
isig icanon -xcase echo echoe echok -echonl -noflsh
-tostop echoctl -echoprt echoke -defecho -flusho -pendin iexten
opost -olcuc onlcr -ocrnl -onocr -onlret -ofill -ofdel tab3
kill -INT 300or
kill -2 300
kill -23 300if one was connected to process 300 in a shell?
kill 15566would end one of my shells (that I might be typing within at the time). The default is to send SIGTERM (15).
kill -9 15566or
kill -KILL 15566This gives the process no choice; it can't block or ignore the request (as it can with the above signal).
kill -HUP 15566sends a HANGUP signal to a process. This means the connection hung up.
% ps -ef | grep couch
couch 15742 15740 0 11:29:45 pts/9 0:01 /usr/bin/tcsh
couch 11217 1 0 Jan 22 ? 0:00 /bin/ksh /usr/dt/bin/sdtvolcheck -d -z 5 cdrom
couch 14030 13996 0 Jan 24 ? 0:00 /bin/cat /tmp/.removable/notify8
couch 10112 1 0 Jan 21 ? 0:00 /bin/ksh /usr/dt/bin/sdtvolcheck -d -z 5 cdrom
couch 13996 1 0 Jan 24 ? 0:00 /bin/ksh /usr/dt/bin/sdtvolcheck -d -z 5 cdrom
couch 6596 1 0 Jan 20 ? 0:00 /bin/ksh /usr/dt/bin/sdtvolcheck -d -z 5 cdrom
couch 1236 1 0 Jan 18 ? 0:00 /bin/ksh /usr/dt/bin/sdtvolcheck -d -z 5 cdrom
couch 15531 15506 0 10:14:15 pts/4 0:01 /usr/bin/tcsh
couch 15676 15505 0 10:49:49 ?? 0:01 /usr/dt/bin/dtterm
couch 2901 2865 0 Jan 18 ? 0:00 /bin/cat /tmp/.removable/notify0
couch 15648 15505 1 10:34:12 ?? 0:05 /usr/dt/bin/dtterm
couch 15499 15480 0 10:13:49 pts/2 0:01 /usr/dt/bin/dtsession
couch 10150 10112 0 Jan 21 ? 0:00 /bin/cat /tmp/.removable/notify4
couch 1273 1236 0 Jan 18 ? 0:00 /bin/cat /tmp/.removable/notify2
couch 371 1 0 Jan 17 ? 0:00 /bin/ksh /usr/dt/bin/sdtvolcheck -d -z 5 cdrom
couch 15514 1 0 10:14:10 ? 0:00 /bin/ksh /usr/dt/bin/sdtvolcheck -d -z 5 cdrom
couch 15691 15690 0 10:51:31 pts/8 0:01 (dns helper)
couch 414 371 0 Jan 17 ? 0:00 /bin/cat /tmp/.removable/notify1
couch 15478 1 0 10:13:40 ? 0:00 /usr/dt/bin/dsdm
couch 16146 15650 0 16:01:00 pts/7 0:00 grep couch
couch 15480 15477 0 10:13:40 pts/2 0:00 tcsh -c unsetenv _ PWD; source /u/f/couch/.login; unsetenv DT;
couch 15498 1 0 10:13:48 pts/2 0:01 /usr/dt/bin/ttsession
couch 15425 14791 0 10:13:31 ? 0:01 /bin/ksh /usr/dt/bin/Xsession
couch 15740 15505 0 11:29:41 ?? 0:20 /usr/dt/bin/dtterm
couch 15477 15425 0 10:13:39 pts/2 0:01 /usr/dt/bin/sdt_shell -c unsetenv _ PWD; source /u/f/couch/.login;
couch 9175 1 0 Jan 21 ? 0:00 /bin/ksh /usr/dt/bin/sdtvolcheck -d -z 5 cdrom
couch 15550 15511 0 10:14:18 ? 0:00 /bin/cat /tmp/.removable/notify10
couch 14790 249 10 19:12:09 ? 10:17 /usr/openwin/bin/Xsun :0 -nobanner -auth /var/dt/A:0-tRaiFa
couch 6634 6596 0 Jan 20 ? 0:00 /bin/cat /tmp/.removable/notify3
couch 9214 9175 0 Jan 21 ? 0:00 /bin/cat /tmp/.removable/notify5
couch 15551 15514 0 10:14:18 ? 0:00 /bin/cat /tmp/.removable/notify9
couch 13796 13756 0 Jan 24 ? 0:00 /bin/cat /tmp/.removable/notify6
couch 15435 15425 0 10:13:32 ? 0:00 /usr/openwin/bin/fbconsole
couch 2865 1 0 Jan 18 ? 0:00 /bin/ksh /usr/dt/bin/sdtvolcheck -d -z 5 cdrom
couch 11264 11217 0 Jan 22 ? 0:00 /bin/cat /tmp/.removable/notify7
couch 15524 15506 0 10:14:14 pts/3 0:01 /usr/bin/tcsh
couch 13756 1 0 Jan 24 ? 0:00 /bin/ksh /usr/dt/bin/sdtvolcheck -d -z 5 cdrom
couch 15511 1 0 10:14:09 ? 0:00 /bin/ksh /usr/dt/bin/sdtvolcheck -d -z 5 cdrom
couch 15505 15499 0 10:13:52 ? 0:26 dtwm
couch 15566 15506 0 10:14:22 pts/6 0:00 /usr/bin/tcsh
couch 15557 15506 0 10:14:20 pts/5 0:00 /usr/bin/tcsh
couch 15506 15499 0 10:14:09 ?? 2:18 dtterm -session dtBia4Ra -sdtserver
couch 15508 15499 0 10:14:09 ? 0:06 sdtperfmeter -f -H -t cpu -t disk -s 1 -name fpperfmeter
couch 15650 15648 0 10:34:13 pts/7 0:02 /usr/bin/tcsh
couch 15678 15676 0 10:49:50 pts/8 0:01 /usr/bin/tcsh
couch 15690 15678 5 10:51:26 pts/8 2:44 netscape http://conbrio/g/150NET/register.html
% kill -9 15742 11217 14030 10112 13996 6596 1236 15531 15676 2901 15648 \ 15499 10150 1273 371 15514 15691 414 15478 16146 15480 15498 15425 15740 \ 15477 9175 15550 14790 6634 9214 15551 13796 15435 2865 11264 15524 13756 \ 15511 15505 15566 15557 15506 15508 15650 15678 15690
kill -9 `ps -ef | awk '$1=="couch" { print $2 }'`
`ps -ef | awk '$1=="couch" { print $2 }'`
executes the command
ps -ef | awk '$1=="couch" { print $2 }'
and substitutes its output inline!
This is the process list.
ps -ef generates the list of all processes, while
awk '$1=="couch" { print $2 }'
selects only the lines with couch in the first
field, and then prints the second field (the process ID).
0x00000000 |---TEXT----|--GLOB--|----DATA---->| ..... |<---STACK-----| 0xffffffff
read heap: blank! subroutine
only malloc and arguments
program new manage and variables
malloc and free, new and delete
lecture
in color