原创 fork系统调用

2024-6-12 16:12 234 2 2 分类: 软件与OS 文集: Linux
代码:

/*
 * main.c
 *
 *  Created on: 2024-6-11
 *      Author: root
 */
#include
#include
#include
#include
#include
#include
#include
#include
#include


int main()
{
	char buf[100]={0};
	pid_t cld_pid;
	int fd;
	int status;




	if((fd=open("temp",O_CREAT|O_RDWR|O_TRUNC,0664))==-1)
	{
		perror("create file");
		exit(1);
	}
	strcpy(buf,"parent process data");
	if((cld_pid=fork())==0)
	{
		strcpy(buf,"chid process data");
		puts(" child process is working!");
		printf("child process PID is %d\n",getpid());
		printf("parent process PID is %d\n",getppid());
		write(fd,buf,strlen(buf));
		close(fd);
		exit(0);

	}

	else
	{
		puts(" parent process is working!");
		printf("parent process PID is %d\n",getpid());
		printf("child process PID is %d\n",cld_pid);
		write(fd,buf,strlen(buf));
		close(fd);
	}
	wait(&status);


	return 0;
}
运行

PARTNER CONTENT

文章评论0条评论)

登录后参与讨论
EE直播间
更多
我要评论
0
2
关闭 站长推荐上一条 /3 下一条