进程和线程编程之一

【代码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 file
	lock.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);
#else
	max_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;
}

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

百里杨

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值