(추후 수정)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | #include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <unistd.h> #include <sys/wait.h> int main(int argc, char *argv[], char *env[]) { pid_t my_pid, parent_pid, child_pid; int status; // get and print my pid and my parent's pid. my_pid = getpid(); parent_pid = getppid(); printf("\nParent : My PID is %d.\n\n", my_pid); printf("Parent : My parent's PID is %d.\n\n", parent_pid); // print error message if fork() fails if((child_pid = fork()) < 0) { perror("fork failure"); exit(1); } // fork() == 0 for child process if(child_pid == 0) { printf("\nChild : I am a new-born process!\n\n"); my_pid = getpid(); parent_pid = getppid(); printf("Child : My PID is %d.\n\n", my_pid); printf("Child : My parent's PID is %d.\n\n", parent_pid); printf("Child : I will sleep 3 seconds and then execute - date - command.\n\n"); sleep(3); printf("Child : Now, I woke up and I'm executing date command.\n\n"); execl("/bin/date", "date", 0, 0); perror("execl() failure!\n\n"); printf("This print is after execl() and should not have been executed if execl were successful!\n\n"); sleep(5); _exit(1); } // parent process else { printf("\nParent : I created a child process.\n\n"); printf("Parent : My child's PID is %d.\n\n", child_pid); system("ps -acefl | grep ercal"); printf("\n\n"); wait(&status); // can use wait(NULL) since exit status frum child is not used. printf("\nParent : My child is dead. I am going to leave.\n\n"); } return 0; } | cs |
'System > Network' 카테고리의 다른 글
간단한 TCP 서버 / TCP 클라이언트 만들어보기 (0) | 2019.04.24 |
---|---|
Linux에서 스레드 생성 - pthread_create() 예제 (0) | 2019.04.01 |