IO进程线程 day5 work

该文探讨了如何使用文件IO函数在父子进程中协同拷贝图片,避免使用sleep函数。同时,通过示例代码展示了waitpid非阻塞形式下子进程是否可能成为僵尸进程的情况,以及如何创建孤儿进程和僵尸进程的场景。
摘要由CSDN通过智能技术生成

作业
1. 使用文件IO函数,拷贝一张图片,父进程拷贝前半部分,子进程拷贝后半部分。不能使用sleep函数。

提示:1)一个进程拷贝完毕后再开始另外一个进程。

           2)父子进程各自开各自的文件。

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>

int main(int argc, const char *argv[])
{
	int fd = open("/home/ubuntu/Pictures/picture.png",O_RDONLY);
	if(fd<0)
	{
		perror("open");
		return -1;
	}	
	off_t a = lseek(fd,0,SEEK_END);
	lseek(fd,0,SEEK_SET);
	pid_t pid = fork();
	char c;	

	if(pid>0)
	{
		int fd1 = open("./kb.png",O_RDWR|O_CREAT|O_TRUNC,0775);
		if(fd1<0)
		{
			perror("open1");
			return -1;
		}

		int i=0;

		while(i<(a/2))
		{
			
			ssize_t res = read(fd,&c,1);
			if(res<0)
			{
				perror("read");
				return -1;
			}
			ssize_t res1 = write(fd1,&c,res);
			if(res1<0)
			{
				perror("write");
				return -1;
			}

			i++;
		}
		close(fd1);
	}
	else if(pid==0)
	{
		while(1)
		{
			off_t b = lseek(fd,0,SEEK_CUR);
			if((a/2)==b)
				break;
		}
			int fd1 = open("./kb.png",O_RDWR);	
			lseek(fd1,0,SEEK_END);
			if(fd1<0)
			{
				perror("open1");
				return -1;
			}
			
			while(1)
			{
			ssize_t res = read(fd,&c,1);
			if(res<0)
			{
				perror("read");
				return -1;
			}
			if(res == 0)
				break;
			ssize_t res1 = write(fd1,&c,1);
			if(res1<0)
			{
				perror("write");
				return -1;
			}
			}
			close(fd1);
	}
	
	else
	{
		perror("fork");
		return -1;
	}

	fflush(stdout);
	close(fd);

	return 0;
}

2. 验证运行到waitpid非阻塞形式时,若子进程没退出,则子进程会不会变成僵尸进程?

ubuntu@ubuntu:~/32/exercise$ cat waitpid.c
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/types.h>
#include<unistd.h>
#include<sys/wait.h>

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

	pid_t pid = fork();
	printf("child pid =%d  line = %d\n",pid,__LINE__);

	int wstatus;
	if(pid>0)
	{
		pid_t a = waitpid(-1,&wstatus,WNOHANG);
		printf("a = %d\n",a);
	}

	else if(0==pid)
	{
		printf("hello\n");
		printf("child pid = %d",getpid());
		sleep(3);
		exit(0);
	}
	else
	{
		perror("fork");
		return -1;
	}

while(1)
	sleep(1);

	return 0;
}
ubuntu@ubuntu:~/32/exercise$ gcc waitpid.c
ubuntu@ubuntu:~/32/exercise$ ./a.out
child pid =22651  line = 12
a = 0
child pid =0  line = 12
hello
child pid = 22641


3. 创建孤儿进程和僵尸进程

僵尸进程:僵尸进程是当子进程比父进程先结束,而父进程又没有回收子进程,释放子进程占用的资源,此时子进程将成为一个僵尸进程。

ubuntu@ubuntu:~/32/work/io/day5$ cat father.c
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/types.h>
#include<unistd.h>
#include<sys/wait.h>
#include<unistd.h>

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

	pid_t pid = fork();
	printf("child pid =%d  line = %d\n",pid,__LINE__);

	if(pid>0)
	{
		printf("hello\n");
	}

	else if(0==pid)
	{
		printf("world\n");
		exit(0);
	}
	else
	{
		perror("fork");
		return -1;
	}

while(1)
	sleep(1);

	return 0;
}

 孤儿进程:指的是在其父进程执行完成或被终止后仍继续运行的一类进程。

ubuntu@ubuntu:~/32/work/io/day5$ cat child.c
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/types.h>
#include<unistd.h>
#include<sys/wait.h>
#include<unistd.h>

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

	pid_t pid = fork();
	printf("child pid =%d  line = %d\n",pid,__LINE__);

	if(pid>0)
	{
		printf("hello\n");
	}

	else if(0==pid)
	{
		sleep(3);
		printf("父pid = %d\n",getppid());
		printf("world\n");
	}
	else
	{
		perror("fork");
		return -1;
	}

	return 0;
}

ubuntu@ubuntu:~/32/work/io/day5$ gcc child.c
ubuntu@ubuntu:~/32/work/io/day5$ ./a.out
child pid =23052  line = 13
hello
ubuntu@ubuntu:~/32/work/io/day5$ child pid =0  line = 13
父pid = 1
world
^C
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值