IO进程线程 day5 work

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

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

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

#include
#include
#include
#include
#include
#include
#include

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
#include
#include
#include
#include
#include

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
#include
#include
#include
#include
#include
#include

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
#include
#include
#include
#include
#include
#include

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

你可能感兴趣的:(ubuntu,linux,运维,c语言)