/*
* 1、编写一个孤儿进程,这个孤儿进程可以同时创建100个僵死进程。
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <unistd.h>
int main()
{
pid_t pid;
pid = fork();
if(pid == 0)
{
int i;
for(i = 1; i <= 100; i++ )
{
pid_t Pid;
Pid = fork();
if(Pid == 0)
{
exit(0);
}
}
}
else
{
exit(0);
}
return 0;
}
/*
* 2、编写两个不同的可执行程序,名称分别为a和b,b为a的子进程。在a程序中
* 调用open函数打开a.txt文件。在b程序不可以调用open或者fopen,只允
* 许 调用read函数来实现读取a.txt文件。
* (a程序中可以使用 fork与execve函数创建子进程)。
*/
//a.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
int main()
{
int fd = open("a.txt",O_RDONLY);
char Fd[10];
memset(Fd,0,sizeof(Fd));
sprintf(Fd,"%d",fd);
pid_t pid = fork();
if(pid == 0)
{
char * args[] = {"pro_b",Fd,NULL};
execve("pro_b",args, NULL);
}
else
{
exit(0);
}
return 0;
}
//b.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
int main(int argc , char * argv[])
{
if(argc < 2)
{
printf("arguments error\n");
}
char buf[20];
memset(buf,0,sizeof(buf));
int fd = atoi(argv[1]);
while(read(fd,buf,sizeof(buf) - 1) > 0)
{
printf("%s",buf);
memset(buf,0,sizeof(buf));
}
return 0;
}
备份地址: 【Linux进程控制demo】