1. 程式人生 > >程序控制篇程式碼示例(複習)

程序控制篇程式碼示例(複習)

/* 建立一個程序 */

#include "Process.h"  /* 自己寫的標頭檔案 */

int main()
{
	pid_t pid;

	printf("Fork pid:%d\n",getpid());
	pid = fork();
	if(-1 == pid)
	{
		perror("fork");
		exit(1);
	}
	else if(0 == pid)
	{
		printf("Child Process!\n");
		printf("Child Pid:%d \n",getpid());
	}
	else
	{
		printf("Parent Process!\n");
		printf("Parent Pid:%d \n",getpid());
		waitpid(pid,NULL,0);    //確保父程序後結束
	}

	return 0;
}



/* 1.在以上的程式中經過多次執行,有時候會出現父程序
	先執行,造成子程序變成孤兒程序?該如何解決?  */

/* 解決方法:使用 waitpid(); 函式,確保父程序後結束 */


/* 2.進過多次執行,可以發現 子程序號 與 父程序號 是連在一起的.
	且子程序號 比 父程序號 大1      */ 









/* fork2.c */
/* fork 建立的子程序 與 父程序 到底有什麼關係呢 ?  */

#include "Process.h"  //自己寫的標頭檔案

int main()
{
	pid_t pid;
	int count = 0;

	pid = fork();
	if(-1 == pid)   //返回值為-1,判斷出錯
	{
		perror("fork");
		exit(1);
	}
	else if(0 == pid)   //返回值為0,判斷為子程序
	{
		count++;
		printf("Child  count :%d \n",count);
	}
	else   //否則,為父程序
	{
		count++;
		printf("Parent  count :%d \n",count);
		waitpid(pid,NULL,0);
	}

	return 0;
}


/* 結論: 1.子程序 完全複製 父程序 的資源;
         2. "寫時複製" 
		 3.子程序 與 父程序 佔用不同的虛擬記憶體空間 
*/









/* vfork 建立程序 */

/* 1.vfork 子程序 與 父程序 又有什麼關係呢?  
   2.vfork 與 fork 的區別與聯絡
*/

#include "Process.h"

int main()
{
	pid_t pid;
	int count = 0;

	pid = vfork();
	if(-1 == pid)
	{
		perror("vfork");
		exit(1);
	}
	else if(0 == pid)
	{
		printf("Child Process! \n");
		count++;
		printf("Child count :%d \n",count);
		exit(2);      //子程序需手動退出
	}
	else
	{
		printf("Parent Process!\n");
		count++;
		printf("Parent count :%d \n",count);
	}

	return 0;
}


/* vfork的特點:
	1.子程序需要手動退出 (與fork不同)
	2.每次都是子程序先執行, 父程序後執行  (與fork不同)
	3.建立的 子程序 與 父程序 共享空間    (與fork不同)
	4.當然,與fork 的相同點是: 都可以建立程序
*/















/* vfork的案例應用:子程序中另起一個新程序 */

#include "Process.h"

int main()
{
	pid_t pid;

	pid = vfork();
	if(-1 == pid)
	{
		perror("vfork");
		exit(1);
	}
	else if(0 == pid)
	{
		printf("Child Pid:%d \n",getpid());
		execl("/mnt/hgfs/share/example/Linux/process/fork","./fork",NULL);   //另起的程序./fork
	}
	else
	{
		printf("Parent Process!\n");
	}

	return 0;
}


/* vfork 中 子程序另起的程序與子程序 有相同的程序號  */












/* 父程序寫 子程序讀 */
/* waitpid.c 案例應用 */

#include "Process.h"

void ChildRead()
{
	int fd,ret;
	char buf[20] = {0};

	fd = open("hello.txt",O_RDONLY);
	if(-1 == fd)
	{
		perror("open1");
		exit(2);
	}

	ret = read(fd,buf,sizeof(buf));
	if(-1 == ret)
	{
		perror("read");
		exit(3);
	}
	else
	{
		printf("Read from txt:%s\n",buf);
		memset(buf,0,sizeof(buf));
	}
	
	close(fd);
}


void ParentWrite()
{
	int fd;
	int ret;
	char buf[20] = {0};
 
	fd = open("hello.txt",O_CREAT | O_WRONLY);
	if(-1 == fd)
	{
		perror("open2");
		exit(4);
	}

	printf("Please input the string:\n");
	scanf("%s",buf);

	ret = write(fd,buf,strlen(buf));
	if(-1 == ret)
	{
		perror("write");
		exit(5);
	}
	
	close(fd);
}


int main()
{
	pid_t pid;

	pid = fork();
	if(-1 == pid)
	{
		perror("fork");
		exit(1);
	}
	else if(0 == pid)
	{
		sleep(4);
		ChildRead();
	}
	else
	{
		ParentWrite();
		waitpid(pid,NULL,0);
	}

	return 0;
}