04.进程和线程编程之一

来源:互联网 发布:淘宝开充话费 编辑:程序博客网 时间:2024/05/20 04:30

【代码1】fun.c

#include <stdio.h>#include <string.h>#include <errno.h>#include <stdlib.h>int main(int argc, char *argv[]){char *p;if (!(p = getenv("TT")))fprintf(stderr, "getenv : cannot found the enviroment variable!\n");printf("%s\n", p);exit(0);}

【代码2】test_daemon.c

#include <stdio.h>#include <stdlib.h>#include <string.h>#include <errno.h>#include <fcntl.h>#include <sys/types.h>#include <sys/stat.h>#include <unistd.h>#include <sys/wait.h>#include <syslog.h>#define err_exit(function) \do {\fprintf(stderr, "in %s at %s line : %d : %s : %s\n", __FUNCTION__, __FILE__, __LINE__ - 1, function, strerror(errno)); \exit(EXIT_FAILURE);\}while(0)int main(int argc, char *argv[]){pid_t pid;int  maxfd;int counter = 0;if(0 > (pid = fork()))err_exit("fork");else if (0 < pid)exit(0);if ( 0 > setsid())err_exit("setsid");if (0 > chdir("/"))err_exit("chdir");if (0 > umask(0))err_exit("umask");if (0 > (maxfd = sysconf(_SC_OPEN_MAX))) /* or sysconf(OPEN_MAX) */err_exit("sysconf");int i;for(i = 0; i < maxfd; i++)close(i);openlog("test_daemon", LOG_CONS | LOG_PID, LOG_DAEMON); /* connect to syslogd!*/while(1){syslog(LOG_DAEMON, "this is a test for test_deamon program! counter = %d", counter++); /* write to /var/log/syslog */sleep(1);}exit(0);}

【代码3】test_exec.c

#include <unistd.h>#include <stdio.h>#include <stdlib.h>#include <errno.h>#include <string.h>int main(int argc, char *argv[]){char *a[] = {"./fun", NULL};char *e[] = {"TT=kk", NULL};if (0 > execve("./fun", a, e)) {fprintf(stderr, "execv : %s\n", strerror(errno));}printf("hello world!\n");exit(0);}

【代码4】test_fork.c

#include <stdio.h>#include <stdlib.h>#include <string.h>#include <errno.h>#include <unistd.h>#include <sys/types.h>#include <sys/wait.h>int main(int argc, char *argv[]){pid_t pid = -1;int a  = 20;if (0 > (pid = fork())) {fprintf(stderr, "fork : %s\n", strerror(errno));exit(1);} else if (0 == pid) {printf("in child, pid is %d\n", getpid());printf("in child, a = %d\n", a);} else {a = 100;printf("in parent, child is %d, a = %d\n", pid, a);}exit(0);}

【代码5】test_heap_stack.c

#include <stdio.h>#include <stdlib.h>#include <string.h>#include <errno.h>void fun2(void);void fun3(void);void fun1(void){int a;printf("addr in fun1 is %p\n", &a);fun2();}void fun2(void){int a;printf("addr in fun2 is %p\n", &a);fun3();}void fun3(void){int a;printf("addr in fun3 is %p\n", &a);}int k;int main(void){int *i1 = (int *)malloc(sizeof(int));printf("i1 in heap is %p\n", i1);int *i2 = (int *)malloc(sizeof(int));printf("i2 in heap is %p\n", i2);int *i3 = (int *)malloc(sizeof(int));printf("i3 in heap is %p\n", i3);fun1();return 0;}

【代码6】test_text_data_bss.c

#include <stdio.h>#include <stdlib.h>#include <string.h>#include <errno.h>int global1 = 0;int global2 = 0;//int global3;int main(void){//static int static_int = 0;printf("hello world!\n");return 0;}

【代码7】test_waitpid1.c

#include <stdio.h>#include <stdlib.h>#include <string.h>#include <errno.h>#include <unistd.h>#include <sys/types.h>#include <sys/wait.h>int main(int argc, char *argv[]){pid_t pid = -1;if ((pid = fork()) == -1) {fprintf(stderr, "fork : %s\n", strerror(errno));exit(1);} else if (pid == 0) {sleep(5);printf("child process exit...\n");exit(88);} else {int status;int ret;while(!(ret = waitpid(pid, &status, WNOHANG)))sleep(1);if (pid == ret)printf("exit code of child process is %d\n", WEXITSTATUS(status));}exit(0);}

daemon测试:

【代码1】daemon.c

#include "daemon.h"extern void debug_wait(int debug);void test_to_locked(void){int fd;struct flock lock;char *buf = "abdcefghijklmnopqrstuvwxyz";//begin to test file lock   fd = open("/tmp/mylock.lock", O_WRONLY | O_CREAT, 0777);if (fd < 0) {fprintf(stderr, "lock file open : %s\n", strerror(errno));exit(1);}if (0 > write(fd, buf, strlen (buf))) {fprintf(stderr, "write : %s\n", strerror(errno));exit(1);}fsync (fd);//file be locked?lock.l_whence = SEEK_SET;lock.l_start = 0;lock.l_len = 10;lock.l_type = F_WRLCK;lock.l_pid = -1;if (fcntl (fd, F_GETLK, &lock) < 0) {fprintf(stderr, "fcntl : %s\n", strerror(errno));exit(1);}        //if(lock.l_type == F_WRLCK)if (lock.l_type != F_UNLCK) {fprintf (stderr, "have a daemon. so quit!\n");exit (1);}//locked filelock.l_type = F_WRLCK;if (fcntl (fd, F_SETLKW, &lock) < 0) {fprintf(stderr, "(Lock file failed: type = %d)fcntl : %s\n", lock.l_type, strerror(errno));exit (1);}printf ("daemon start...file have locked!\n");}int daemon_init (void){pid_t pid;int fd0, fd1, fd2, max_fd, i;struct rlimit rl;struct sigaction sa;umask (0);if ((pid = fork ()) < 0) {fprintf(stderr, "fork : %s\n", strerror(errno));exit (1);} else if (pid != 0) {exit (0);}if (setsid () < 0)exit (1);sa.sa_handler = SIG_IGN;sigaction(SIGHUP, &sa, NULL);if ((pid = fork ()) < 0) {fprintf(stderr, "fork : %s\n", strerror(errno));exit (1);} else if (pid != 0) {exit (0);}//test_to_locked();if (0 > chdir("/")) {fprintf(stderr, "chdir : %s\n", strerror(errno));exit(1);}//umask(0);if (0 > setpgrp()) {fprintf(stderr, "setpgrp : %s\n", strerror(errno));exit(1);}#if 0// Get maximum number of file descriptors.if (getrlimit (RLIMIT_NOFILE, &rl) < 0) {fprintf (stderr, "can't get file limit");}// Close all open file descriptors.if (rl.rlim_max == RLIM_INFINITY)rl.rlim_max = 1024;for (i = 0; i < rl.rlim_max; i++)close (i);#elsemax_fd = sysconf(_SC_OPEN_MAX);for (i = max_fd; i >= 0; i--)close (i);#endif//test_to_locked();//attach file descriptors 0, 1, and 2 to /dev/null.fd0 = open ("/dev/null", O_RDWR);fd1 = dup (0);fd2 = dup (0);test_to_locked();//Initialize the log file.openlog ("daemon_test", LOG_CONS | LOG_PID, LOG_DAEMON);if (fd0 != 0 || fd1 != 1 || fd2 != 2) {syslog(LOG_ERR, "unexpected file descriptors %d %d %d", fd0, fd1, fd2);exit (1);}return 0;}

【代码2】daemon.h

#include <string.h>#include <unistd.h>#include <sys/stat.h>#include <syslog.h>#include <sys/types.h>#include <stdlib.h>#include <errno.h>#include <sys/file.h>#include <stdio.h>#include <sys/resource.h>#include <signal.h>void test_to_locked ();int daemon_init (void);

【代码3】main.c

#include <unistd.h>#include "daemon.h"int main (void){daemon_init ();int i = 0;while (1) {syslog (LOG_DAEMON, "this is a syslog test for daemon! i = %d", i++);sleep (1);}return 0;}

原创粉丝点击