Open-Source Software Project Development Instructor: Dr. T.Y. Wong
Week 10, Part 2 Writing Realistic Server Software (1)
Servers? • We sometimes call them daemons. – Apache; MySQL; Samba; etc. – Many of them are open source software. – You may have a chance to write them.
Spring semester 2008-2009
CSC4140 – Open-Source Software Project Development
Page 2
Realistic Server? • Properties?
No STDOUT nor STDERR?
I/O Redirection?
Spring semester 2008-2009
(Usually) Run in background
(Usually) Only one execution instance is allowed. How?
CSC4140 – Open-Source Software Project Development
Page 3
Background process revisited… • Using fork(). Filename: bg.c
int pid = fork(); if(pid == 0) { while(1) This is the most easy part sleep(1); } else { printf(“Created a bg process: %d\n”, pid); exit(0); }
Spring semester 2008-2009
CSC4140 – Open-Source Software Project Development
Page 4
Background process revisited… • How about STDOUT and STDERR? Filename: bg_with_print.c int pid = fork(); if(pid == 0) { while(1) { fprintf(stdout, “STDOUT: I’m alive\n”); fprintf(stderr, “STDERR: I’m alive\n”); sleep(1); } } else { printf(“Created a bg process: %d\n”, pid); exit(0); $ ./bg_with_print } Created a bg process: 1234 $ STDOUT: I’m alive Very irritating…you’ll STDERR: I’m alive STDOUT: I’m alive never find this kind of STDERR: I’m alive servers running… ......
Spring semester 2008-2009
CSC4140 – Open-Source Software Project Development
Page 5
Background process revisited… • How about STDOUT and STDERR? Filename: bg_with_print.c int pid = fork(); if(pid == 0) { while(1) { fprintf(stdout, “STDOUT: I’m alive\n”); fprintf(stderr, “STDERR: I’m alive\n”); sleep(1); } } else { printf(“Created a bg process: %d\n”, pid); exit(0); $ ./bg_with_print } Created a bg process: 1234 $ STDOUT: I’m alive Very irritating…you’ll STDERR: I’m alive STDOUT: I’m alive never find this kind of STDERR: I’m alive servers running… ......
Spring semester 2008-2009
CSC4140 – Open-Source Software Project Development
Page 6
I/O redirection revisited… • How about STDOUT and STDERR? Filename: bg_with_redirect.c int pid = fork(); char buf[1024]; if(pid == 0) { sprintf(buf, “log/%d.stdout”, getpid()); YEAH! I can have a process redirect(stdout, buf); sprintf(buf, “log/%d.stderr”, getpid()); redirect(stderr, buf);
running silently, giving outputs at the same time.
while(1) { fprintf(stdout, “STDOUT: I’m alive\n”);*in_fp, char *buf) { void redirect(FILE fprintf(stderr, “STDERR:int I’mfd alive\n”); = fileno(in_fp); sleep(1); FILE *fp = fopen(buf, “w”); } if(fp == NULL) { ...... }; // kill yourself } close(fd); else { dup2(fileno(fp), fd); printf(“Created a bg process: %d\n”, pid); fclose(fp); exit(0); } } Spring semester 2008-2009
CSC4140 – Open-Source Software Project Development
Page 7
One-instance only? • Using a technique called pidfile. NO…
1st instance
Step (1) check if exist?
Apache
Step (2) create and write
1234
t?
2nd instance
ki
p Ste
c he c )
(1
f
is ex
/var/run/apache2.pid
s…
Ye
Apache Step (2) Kill himself!
Spring semester 2008-2009
CSC4140 – Open-Source Software Project Development
Page 8
One-instance only? • Using a technique called pidfile. 1st instance Step (1) remove file.
Apache I wanna quit…
1234 5678 Step (2) Kill himself! t?
ki
3rd instance
p Ste
(1
c he c )
f
is ex
/var/run/apache2.pid
… No
d write
Apache
reate an c ) 2 ( p e St
Filename: bg_with_pidfile.c Spring semester 2008-2009
CSC4140 – Open-Source Software Project Development
The story goes on… Page 9
To be continued… Spring semester 2008-2009
CSC4140 – Open-Source Software Project Development
Page 10