// simpfork.c
#include <stdio.h>
#include <unistd.h>
main()
{
int i;
printf("simpfork: pid = %d\n", getpid());
i = fork();
printf("Did a fork. It returned %d. getpid = %d. getppid = %d\n",
i, getpid(), getppid());
}
$ gcc -o simpfork simpfork.c -Wall
$ ./simpfork
simpfork: pid = 914
Did a fork. It returned 915. getpid = 914. getppid = 381
Did a fork. It returned 0. getpid = 915. getppid = 914
เป็นฟังก์ชัน system call ที่จะสั่งให้โปรเซสสิ้นสุดการทำงาน โดยค่า returnCode จะถูกส่งกลับไปยังโปรเซสแม่ ในกรณีที่โปรเซสแม่กำลังคอยให้การทำงานของโปรเซสลูกเสร็จสมบูรณ์
ฟังก์ชัน int wait(int *returnCode);
เป็นฟังก์ชัน system call ที่จะส่งผลให้โปรเซสที่เรียกใช้ system call ตัวนี้ จะต้องทำการรอจนกระทั่งแต่ละโปรเซสที่ถูกสร้างโดยโปรเซสตัวนี้ทำงานให้เสร็จสิ้นทั้งหมดเสียก่อน โดยค่าที่ system call ตัวนี้จะส่งกลับมาจะเป็นค่าของหมายเลขโปรเซสที่เสร็จสิ้นการทำงาน และรหัสที่ส่งค่ากลับมาจะถูกเก็บอยู่ในตัวแปรพอยท์เตอร์ returnCode
// simpfork5.c
#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/wait.h>
int main() {
pid_t pid1, pid2, pid3, pid4;
printf("Parent[A] of all: %d\n", getpid());
pid1 = fork();
if (pid1 == 0) { // Child Process, lets say B.
printf("Child[B] with id: %d and its Parent id: %d \n", getpid(),
getppid());
pid2 = fork();
if (pid2 == 0) { // Child process, lets say D.
printf("Child[D] with id: %d and its Parent id: %d \n", getpid(),
getppid());
}
}
if (pid1 > 0) {
pid3 = fork();
if (pid3 == 0) { // Child process, lets say C.
printf("Child[C] with id: %d and its Parent id: %d \n", getpid(),
getppid());
pid4 = fork();
if (pid4 == 0) { // Child process, lets say E.
printf("Child[E] with id: %d and its Parent id: %d \n",
getpid(), getppid());
}
}
}
for (int i = 0; i < 3; i++)
wait(NULL);
}
$ gcc -o simpfork5 simpfork5.c -Wall
$ ./simpfork5
Parent[A] of all: 27385
Child[B] with id: 27386 and its Parent id: 27385
Child[C] with id: 27387 and its Parent id: 27385
Child[D] with id: 27388 and its Parent id: 27386
Child[E] with id: 27389 and its Parent id: 27387
// simpfork7.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
void main() {
int n, m, status;
n = fork();
if (n == -1)
exit(1);
else {
if (n != 0) /* Parent Process */
{
printf("-p-- value of fork %d PID = %d \n", n, getpid());
printf("-p-- Parent PID = %d \n", getppid());
m = wait(&status);
printf("-p-- first exit status = %d \n", status);
status = status >> 8;
printf("-p-- exit status of child process = %d\n", status);
printf("-p-- child PID that just stop = %d\n", m);
fflush(stdout);
m = execl("com", "com", NULL);
printf(" Return to parent process ** ,m=%d \n", m);
exit(0);
} else {
printf("\n");
printf("\n");
printf("-c-- value of fork = %d \n", n);
printf("-c-- this PID = %d and Parent PID = %d\n "\
, getpid(),
getppid());
sleep(4);
fflush(stdout);
m = execl("com", "com", NULL);
exit(2);
}
}
}
$ gcc -o simpfork7 simpfork7.c -Wall
$ ./simpfork7
-p-- value of fork 4463 PID = 4462
-p-- Parent PID = 2646
.
.
.
-c-- value of fork = 0
-c-- this PID = 4463 and Parent PID = 4462
-p-- first exit status = 512
-p-- exit status of child process = 2
-p-- child PID that just stop = 4463
Return to parent process ** ,m=-1
ตัวอย่างที่ 8
แสดงตัวอย่างการเขียนโปรแกรมในลักษณะ shell อย่างง่ายเพื่อรับคำสั่งที่ป้อนเข้ามา (command line) แล้วทำการสร้างโปรเซสลูกในการดำเนินการคำสั่งนั้น โดยทั้งสองตัวอย่างข้างล่างจะขียนในรูปแบบภาษา C และภาษา C++
// simpshell.c
#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
/* a simple define for convenience */
#define TRUE 1
int main() {
/* C declaration before any function call */
int pid, i = 0;
char tab[256], *s;
/* the main loop of our shell */
while (TRUE) {
/* the printf to print strings and numbers */
printf("prompt %d> ", i);
/* "flush" the standard output */
fflush(stdout);
/* read the command line */
s = gets(tab);
if (s == NULL) { /* <Control+D> pressed */
fprintf(stderr, "Bye bye\n");
exit(0);
}
/* one process running */
pid = fork();
/* 2 processes running */
printf("I'm running\n");
switch (pid) /* where are we ? */
{
case 0: /* in the child! */
printf("In the child\n");
/* exec command */
execlp(tab, tab, NULL);
/* executed only if the exec command fails */
perror("exec");
exit(0);
break;
case -1: /* in... the parent: fork has failed no wait!*/
perror("fork");
break;
default: /* in the parent, the fork worked! */
printf("In the parent.. wait\n");
wait(0);
i++; /* for the next command */
}
}
/* end of the loop */
return 0;
}
$ gcc -o simpshell simpshell.c -Wall
$ ./simpshell
prompt 0> ls
I'm running
In the parent.. wait
I'm running
In the child
pipe1.c pipe3.c programA signal simpfork7 simpshell Other_Multi-Socket.zip chat_socket simpshell.c simpshell.cc
5287 done
prompt 1> date
I'm running
In the parent.. wait
I'm running
In the child
Fri Sep 12 07:42:28 ICT 2014
5288 done
prompt 2> pwd
I'm running
In the parent.. wait
I'm running
In the child
/home/wiroon/os
5289 done
ตัวอย่างโปรแกรมในรูปแบบภาษา C++
// simpshell.cc
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <iostream>
using namespace std;
/* a simple define for convenience */
#define TRUE 1
int main() {
int pid, i = 0;
char tab[1024];
/* the main loop of our shell */
while (TRUE) {
/* print strings and numbers */
cout << "prompt " << i << "> ";
/* read the command line */
cin >> tab;
if (tab[0] == 0) { /* <Control+D> pressed */
cerr << "Bye bye\n";
exit(0);
}
/* one process running */
pid = fork();
/* 2 processes running */
cout << "I'm running\n";
switch (pid) /* where are we ? */
{
case 0: /* in the child! */
cout << "In the child\n";
/* exec command */
execlp(tab, tab, NULL);
/* executed only if the exec command fails */
perror("exec");
exit(0);
break;
case -1: /* in... the parent: fork has failed no wait!*/
perror("fork");
break;
default: /* in the parent, the fork worked! */
cout << "In the parent.. wait\n";
cout << wait(0) << " done\n";
i++; /* for the next command */
}
}
/* end of the loop */
return 0;
}
$ g++ -o simpshell simpshell.cc -Wall
$ ./simpshell
prompt 0> ls
I'm running
In the parent.. wait
I'm running
In the child
pipe1.c pipe3.c programA signal simpfork7 simpshell Other_Multi-Socket.zip chat_socket simpshell.c simpshell.cc
5287 done
prompt 1> date
I'm running
In the parent.. wait
I'm running
In the child
Fri Sep 12 07:42:28 ICT 2014
5288 done
prompt 2> pwd
I'm running
In the parent.. wait
I'm running
In the child
/home/wiroon/os
5289 done