2012-02-14 16:18:16 +01:00
|
|
|
/*
|
|
|
|
* \brief Simple fork test
|
|
|
|
* \author Norman Feske
|
|
|
|
* \date 2012-02-14
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
2012-02-18 01:46:54 +01:00
|
|
|
enum { MAX_COUNT = 100 };
|
|
|
|
|
2012-02-14 16:18:16 +01:00
|
|
|
int main(int, char **)
|
|
|
|
{
|
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
pid_t fork_ret = fork();
|
|
|
|
if (fork_ret < 0) {
|
|
|
|
printf("Error: fork returned %d, errno=%d\n", fork_ret, errno);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("pid %d: fork returned %d\n", getpid(), fork_ret);
|
|
|
|
|
|
|
|
if (fork_ret == 0) {
|
|
|
|
printf("pid %d: child says hello\n", getpid());
|
2012-02-18 01:46:54 +01:00
|
|
|
for (int j = 0; j < MAX_COUNT; j++) {
|
2012-02-14 16:18:16 +01:00
|
|
|
printf("pid %d: child i = %d\n", getpid(), i);
|
|
|
|
}
|
2012-02-18 01:46:54 +01:00
|
|
|
return 0;
|
2012-02-14 16:18:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
printf("pid %d: parent received child pid %d, starts counting...\n",
|
|
|
|
getpid(), fork_ret);
|
|
|
|
|
2012-02-18 01:46:54 +01:00
|
|
|
for (; i < MAX_COUNT; ) {
|
2012-02-14 16:18:16 +01:00
|
|
|
printf("pid %d: parent i = %d\n", getpid(), i++);
|
|
|
|
}
|
|
|
|
|
2012-02-18 01:46:54 +01:00
|
|
|
for (;;);
|
2012-02-14 16:18:16 +01:00
|
|
|
return 0;
|
|
|
|
}
|