0% found this document useful (0 votes)
97 views10 pages

Software Project Development Spring Semester

This document discusses some key properties of realistic server software, including running servers as background processes, redirecting standard output and error, and ensuring only one instance runs at a time. It provides code examples to illustrate how to fork a background process, redirect stdout and stderr to log files, and use a pidfile technique to check if another instance is already running and kill additional instances. The document is part of a lecture on writing open-source server software.

Uploaded by

lamkakaka
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
97 views10 pages

Software Project Development Spring Semester

This document discusses some key properties of realistic server software, including running servers as background processes, redirecting standard output and error, and ensuring only one instance runs at a time. It provides code examples to illustrate how to fork a background process, redirect stdout and stderr to log files, and use a pidfile technique to check if another instance is already running and kill additional instances. The document is part of a lecture on writing open-source server software.

Uploaded by

lamkakaka
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 10

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 CSC4140 – Open-Source Page 2


2008-2009 Software Project Development
Realistic Server?
• Properties?

(Usually)
Run in background
No STDOUT nor STDERR?

(Usually)
I/O Redirection? Only one execution instance
is allowed. How?

Spring semester CSC4140 – Open-Source Page 3


2008-2009 Software Project Development
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 CSC4140 – Open-Source Page 4


2008-2009 Software Project Development
Background process revisited…
• How about STDOUT and STDERR?
int pid = fork(); Filename: bg_with_print.c
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
never find this kind of STDOUT: I’m alive
servers running… STDERR: I’m alive
......
Spring semester CSC4140 – Open-Source Page 5
2008-2009 Software Project Development
Background process revisited…
• How about STDOUT and STDERR?
int pid = fork(); Filename: bg_with_print.c
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
never find this kind of STDOUT: I’m alive
servers running… STDERR: I’m alive
......
Spring semester CSC4140 – Open-Source Page 6
2008-2009 Software Project Development
I/O redirection revisited…
• How about STDOUT and STDERR?
int pid = fork(); Filename: bg_with_redirect.c
char buf[1024];
if(pid == 0) {
sprintf(buf, “log/%d.stdout”, getpid());
YEAH! I can have a process
redirect(stdout, buf);
running silently, giving
sprintf(buf, “log/%d.stderr”, getpid()); outputs at the same time.
redirect(stderr, buf);

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 CSC4140 – Open-Source Page 7


2008-2009 Software Project Development
One-instance only?
• Using a technique called pidfile.
NO…
1st instance
Step (1) check if exist?
Apache 1234
Step (2) create and write

ist?
f ex /var/run/apache2.pid
cki
2nd instance che
(1)
p
Ste Ye
s…

Apache
Step (2) Kill himself!

Spring semester CSC4140 – Open-Source Page 8


2008-2009 Software Project Development
One-instance only?
• Using a technique called pidfile.

1st instance
Step (1) remove file.
Apache 1234
5678
Step (2) Kill himself!
I wanna quit…
ist?
f ex /var/run/apache2.pid
cki
3rd instance che
(1)
p
Ste No

Apache d write
p (2 ) c reate an
St e

The story goes on…


Filename: bg_with_pidfile.c
Spring semester CSC4140 – Open-Source Page 9
2008-2009 Software Project Development
To be continued…
Spring semester CSC4140 – Open-Source Page 10
2008-2009 Software Project Development

You might also like