Demo entry 6749447

childProcessExample

   

Submitted by anonymous on Jun 13, 2018 at 03:45
Language: C. Code size: 536 Bytes.

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>

int main(void)
{
	pid_t pid; //pid_t 선언

	pid = fork(); //fork 발생
	if(pid == -1) { //-1 이면 fork생성 에러
		printf("can't fork, erro\n");
		exit(0);
	}

	if(pid == 0) { //0이면 자식 프로세스
		int j;
		for(j = 0; j < 10; j++) {
			printf("child: %d\n", j);
			sleep(1);
		}
		exit(0);
	} else { //부모프로세스
		int i;
		for(i = 0; i < 10; i++) {
			printf("parent : %d\n", i);
			sleep(1);
		}
		exit(0);
	}
	return 0;
}

This snippet took 0.00 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).