linux编程|fork函数讲解
来源:互联网 发布:linux c select 编辑:程序博客网 时间:2024/05/02 00:50
[root@slave bdkyr]# cat fork_test.c
/*
* create by bdkyr
* date 2015-1-22
*/
#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <stdarg.h>
#include <string.h>
#define MAXLINE 4096 /* max line length */
int glob = 6;
char buf[]= "a write to stdout\n";
void err_sys(const char *fmt, ...);
static void err_doit(int, int, const char *, va_list);
int main(void){
int var;
pid_t pid;
var = 88;
if (write (STDOUT_FILENO, buf, sizeof(buf) -1) != sizeof(buf)-1)
err_sys("write error");
printf("before fork\n");
if((pid = fork()) < 0){
err_sys("fork error");
}else if(pid == 0){
glob++;
var++;
}else{
sleep(2);
}
printf("pid=%d, glob = %d, var = %d\n", getpid(),glob, var);
exit(0);
}
void err_sys(const char *fmt, ...){
va_list ap;
va_start(ap, fmt);
err_doit(1, errno, fmt, ap);
va_end(ap);
exit(1);
}
static void err_doit(int errnoflag, int error, const char *fmt, va_list ap){
vsnprintf(buf, MAXLINE, fmt, ap);
if(errnoflag)
snprintf(buf+strlen(buf), MAXLINE-strlen(buf), ": %s", strerror(error));
strcat(buf, "\n");
fflush(stdout);
fputs(buf, stderr);
fflush(NULL);
}
[root@slave bdkyr]# gcc fork_test.c -o fork_test
[root@slave bdkyr]# ./fork_test
a write to stdout
before fork
pid=5209, glob = 7, var = 89
pid=5208, glob = 6, var = 88
0 0
- linux编程|fork函数讲解
- linux编程|fork函数讲解
- Linux Fork函数讲解
- linux下fork()函数讲解
- Linux/UNIX的fork()函数讲解
- Linux中fork()函数讲解(转载)
- Linux中fork()函数实例讲解
- (转)Linux中fork()函数讲解
- fork函数讲解
- 【Linux编程】进程标识符与fork函数
- fork()函数与Linux中的多线程编程
- fork() 函数与 Linux 中的多线程编程
- fork()函数与Linux中的多线程编程
- 【Linux系统编程】fork() 函数详解
- linux中fork()函数详解(原创!!实例讲解)
- linux中fork()函数详解(原创!!实例讲解)
- linux中fork()函数详解(原创!!实例讲解)
- linux中fork()函数详解(原创!!实例讲解)
- Java 动态代理
- Codeforces Round #285 (Div. 2) C. Misha and Forest
- iOS常用尺寸大小说明
- ssh自动登录工具
- MFC List Control滚动条
- linux编程|fork函数讲解
- 产品经理之原型工具
- tabBar视图互相切换时弹出登陆页效果问题
- InnoDB undo log解析(一)
- keychain
- 动态内存管理详解:malloc/free/new/delete/brk/mmap
- Liferay6.2开发之使用Search-Container
- Sales_item.h (C++ Primer 第五版)
- 自定义控件其实很简单7/12