void cgi_run(const char* filename)
{
char buffer[1024] = { 0 };
int len;
int pfd[2];
int status;
pid_t pid;
/* create pipe */
if (pipe(pfd)<0)
return -1;
/* fork to execute external program or scripts */
pid = fork();
if (pid<0) {
return 0;
} else if (pid==0) { /* child process */
dup2(pfd[1], STDOUT_FILENO);
close(pfd[0]);
/* execute CGI */
execlp(filename, filename, NULL);
exit(0);
} else { /* parent process */
close(pfd[1]);
/* print output from CGI */
while((len=read(pfd[0], buffer, 1023))>0) {
buffer[len] = '\0';
printf("%s\n", buffer);
}
/* waiting for CGI */
waitpid((pid_t)pid, &status, 0);
}
}
2011年4月29日 星期五
2011年4月28日 星期四
TCP_NODELAY
How do I use TCP_NODELAY?
First off, be sure you really want to use it in the first place. It
will disable the Nagle algorithm (see ``2.11 How can I force a socket
to send the data in its buffer?''), which will cause network traffic
to increase, with smaller than needed packets wasting bandwidth.
Also, from what I have been able to tell, the speed increase is very
small, so you should probably do it without TCP_NODELAY first, and
only turn it on if there is a problem.
Here is a code example, with a warning about using it from Andrew
Gierth:
int flag = 1;
int result = setsockopt(sock, /* socket affected */
IPPROTO_TCP, /* set option at TCP level */
TCP_NODELAY, /* name of option */
(char *) &flag, /* the cast is historical
cruft */
sizeof(int)); /* length of option value */
if (result < 0)
... handle the error ...
TCP_NODELAY is for a specific purpose; to disable the Nagle buffering
algorithm. It should only be set for applications that send frequent
small bursts of information without getting an immediate response,
where timely delivery of data is required (the canonical example is
mouse movements).
2011年4月26日 星期二
get IP
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netdb.h>
#include <ifaddrs.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(){
struct ifaddrs *ifaddr, *ifa;
int family, s;
char host[NI_MAXHOST];
if (getifaddrs(&ifaddr) == -1){
perror("getifaddrs");
exit(EXIT_FAILURE);
}
for(ifa = ifaddr; ifa!=NULL; ifa = ifa->ifa_next){
family = ifa->ifa_addr->sa_family;
if(family == AF_INET){
s = getnameinfo(ifa->ifa_addr, sizeof(struct sockaddr_in), host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
if(s != 0){
printf("getnameinfo() failed: %s\n", gai_strerror(s));
exit(EXIT_FAILURE);
}
printf("Interface=%s\t Address=%s\n", ifa->ifa_name, host);
}
}
return 0;
}
#include <sys/socket.h>
#include <netdb.h>
#include <ifaddrs.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(){
struct ifaddrs *ifaddr, *ifa;
int family, s;
char host[NI_MAXHOST];
if (getifaddrs(&ifaddr) == -1){
perror("getifaddrs");
exit(EXIT_FAILURE);
}
for(ifa = ifaddr; ifa!=NULL; ifa = ifa->ifa_next){
family = ifa->ifa_addr->sa_family;
if(family == AF_INET){
s = getnameinfo(ifa->ifa_addr, sizeof(struct sockaddr_in), host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
if(s != 0){
printf("getnameinfo() failed: %s\n", gai_strerror(s));
exit(EXIT_FAILURE);
}
printf("Interface=%s\t Address=%s\n", ifa->ifa_name, host);
}
}
return 0;
}
2011年4月20日 星期三
2011年4月15日 星期五
Makefile
| $?: | 代表已被更新的 dependencies 的值。 也就是 dependencies 中,比 targets 還新的值。 |
| $@: | 代表 targets 的值。 |
| $<: | 代表第一個 dependencies 的值。 |
| $* : | 代表 targets 所指定的檔案,但不包含副檔名。 |
2011年4月13日 星期三
GOOD MACRO
#define install_log(f, a...) \
do { \
printf("INSTALL LOG %s:%d:%s:\r\n", \
__FILE__, __LINE__, __func__); \
printf(f, ##a); \
} while (0)
do { \
printf("INSTALL LOG %s:%d:%s:\r\n", \
__FILE__, __LINE__, __func__); \
printf(f, ##a); \
} while (0)
2011年4月1日 星期五
crt
crt1.o
crt1.o
crti.o
crtbegin.o
crtend.o
crtn.o
這五個檔案分別是 startup, initialization, constructor, destructor, finalization
它們通常會被自動地連結( link )到我們的程式。經由這些檔案,我們程式的 main() 才能被呼叫。
當我們告訴編繹器不要使用標準的連結( LDFLAGS = -nostdlib -L${TARGET_PREFIX}/lib ),那我們必須明確地指定這些檔案到編譯參數中。例如
crti.o
crtbegin.o
crtend.o
crtn.o
這五個檔案分別是 startup, initialization, constructor, destructor, finalization
它們通常會被自動地連結( link )到我們的程式。經由這些檔案,我們程式的 main() 才能被呼叫。
當我們告訴編繹器不要使用標準的連結( LDFLAGS = -nostdlib -L${TARGET_PREFIX}/lib ),那我們必須明確地指定這些檔案到編譯參數中。例如
STARTUP_FILES = ${TARGET_PREFIX}/lib/crt1.o \
${TARGET_PREFIX}/lib/crti.o \
${PREFIX}/lib/gcc-lib/${TARGET}/2.95.3/crtbegin.o
END_FILES = ${PREFIX}/lib/gcc-lib/${TARGET}/2.95.3/crtend. \
${TARGET_PREFIX}/lib/crtn.o
LIBS = -lc
OBJS = daemon.o
LINKED_FILES = ${STARTUP_FILES} ${OBJS} ${LIBS} ${END_FILES}
...
daemon: ${OBJS}
$(CC) -o $(EXEC_NAME) ${LINKED_FILES} $(LDFLAGS)
Linker
http://www.cs.utah.edu/dept/old/texinfo/as/gprof.html
http://linux.about.com/library/cmd/blcmdl1_ld.htm
main.c
#include <stdio.h>
int main()
{
printf("Hello World\r\n");
}
gcc -c main.c
ld -dynamic-linker /lib/ld-linux-x86-64.so.2 /usr/lib/crt1.o /usr/lib/crti.o -lc main.o /usr/lib/crtn.o
http://linux.about.com/library/cmd/blcmdl1_ld.htm
main.c
#include <stdio.h>
int main()
{
printf("Hello World\r\n");
}
gcc -c main.c
ld -dynamic-linker /lib/ld-linux-x86-64.so.2 /usr/lib/crt1.o /usr/lib/crti.o -lc main.o /usr/lib/crtn.o
訂閱:
文章 (Atom)