您的位置:首页 > 其它

fork系统调用

2015-12-24 22:09 295 查看
fork()学习, 理解

例1: (独立的上下文)

点击(此处)折叠或打开

#include <stdio.h>

#include <errno.h>

#include <stdlib.h>

#include <unistd.h>

#include <sys/types.h>

#include <fcntl.h>

#define ERROR(flag) \

if(flag) \

{ \

printf("%d: ",__LINE__); \

fflush(stdout); \

perror("error"); \

exit(errno); \

}

int num = 999;

int main(int argc,char *argv[])

{

pid_t pid;

char *s = NULL;

printf("hello, my pid is %d\n\n",getpid());

pid = fork();

ERROR(pid == -1);

if(pid == 0)

{

int i = 2;

while(i--)

{

printf("child process, pid = %d, ppid = %d\n\n",

getpid(),getppid());

s = "child process";

sleep(1);

}

}

else

{

int i = 2;

while(i--)

{

// getchar();

printf("parent process, pid = %d, child pid = %d\n\n",

getpid(),pid);

s = "parent process";

sleep(1);

}

}

printf("i'm %s, byebye!!!\n",s);

return 0;

}

编译链接成功后运行, 父进程输出当前及子进程号, 子进程输出当前及父进程号. 结果如下图:



例2: (独立的文件读写)
code:1

点击(此处)折叠或打开

#include <stdio.h>

#include <errno.h>

#include <stdlib.h>

#include <unistd.h>

#include <sys/types.h>

#include <fcntl.h>

#define ERROR(flag) \

if(flag) \

{ \

printf("%d: ",__LINE__); \

fflush(stdout); \

perror("error"); \

exit(errno); \

}

int main(int argc,char *argv[])

{

#if 0

int fd = open(argv[1],O_RDONLY);

ERROR(fd == -1);

#endif

pid_t pid = fork();

ERROR(pid == -1);

#if 1

int fd = open(argv[1],O_RDONLY);

ERROR(fd == -1);

#endif

if(pid == 0)

{

char ch;

int ret = read(fd,&ch,1);

ERROR(ret < 0);

printf("child process got %c\n",ch);

}

else

{

char ch;

int ret = read(fd,&ch,1);

ERROR(ret < 0);

printf("parent process got %c\n",ch);

}

return 0;

}

编译链接成功后运行, 父子进程输出从文件中文件读出的内容. 结果如下图:



例3: 孤儿进程

点击(此处)折叠或打开

#include <stdio.h>

#include <errno.h>

#include <stdlib.h>

#include <unistd.h>

#include <sys/types.h>

#include <fcntl.h>

#define ERROR(flag) \

if(flag) \

{ \

printf("%d: ",__LINE__); \

fflush(stdout); \

perror("error"); \

exit(errno); \

}

int main(int argc,char *argv[])

{

pid_t pid;

pid = fork();

ERROR(pid == -1);

if(pid == 0)

{

int i = 1000;

while(i--)

{

printf("child process, pid = %d, ppid = %d\n",getpid(),getppid());

sleep(5);

}

}

else

{

printf("parent process, byebye!!!\n");

sleep(3);

_exit(0);

}

return 0;

}

编译链接成功后运行, 父子进程输出内容. 结果如下图:



在另一个终端查看的进程间关系如下图:



例4: 父进程取得子进程的返回值

点击(此处)折叠或打开

#include <stdio.h>

#include <errno.h>

#include <stdlib.h>

#include <unistd.h>

#include <sys/types.h>

#include <fcntl.h>

#include <sys/wait.h>

#define ERROR(flag) \

if(flag) \

{ \

printf("%d: ",__LINE__); \

fflush(stdout); \

perror("error"); \

exit(errno); \

}

int main(int argc,char *argv[])

{

pid_t pid;

pid = fork();

ERROR(pid == -1);

if(pid == 0)

{

int i = 3;

while(i--)

{

printf("child process, pid = %d, ppid = %d\n\n",

getpid(),getppid());

sleep(1);

}

printf("chile process exit, byebye!!!\n\n");

_exit(99);

}

#if 1

int stat;

pid_t child_pid;

child_pid = wait(&stat);

printf("child process has exited, pid = %d\n\n",child_pid);

if(WIFEXITED(stat))

printf("child exited with code %d\n\n",WEXITSTATUS(stat));

else

printf("child exit abnormally\n\n");

#endif

printf("parent process, pid = %d\n",getpid());

getchar();

return 0;

}

编译链接成功后运行, 父子进程输出内容. 结果如下图:



例5: 进程退出

点击(此处)折叠或打开

#include <stdio.h>

#include <stdlib.h>

#include <time.h>

#include <sys/stat.h>

#include <unistd.h>

#include <sys/types.h>

#include <errno.h>

int main(int argc, char *argv[])

{

printf("hello");

_exit(0);

return 0;

}

编译链接成功后运行, 无输出内容. 结果如下图:



注释掉_exit()函数后,如下

点击(此处)折叠或打开

#include <stdio.h>

#include <stdlib.h>

#include <time.h>

#include <sys/stat.h>

#include <unistd.h>

#include <sys/types.h>

#include <errno.h>

int main(int argc, char *argv[])

{

printf("hello");

//_exit(0);

return 0;

}

编译链接成功后运行, 输出内容如下图:



_exit()使得进程在字符输出到显示设备之前退出.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: