This commit is contained in:
2021-03-01 16:39:46 +01:00
parent 0453269f24
commit 5b651516d2
35 changed files with 1013 additions and 14 deletions

View File

@@ -0,0 +1,16 @@
all: p1 p2 p3 p4
p1:
cc -o p1 p1.c
p2:
cc -o p2 p2.c
p3:
cc -o p3 p3.c
p4:
cc -o p4 p4.c
clean:
rm -f p1 p2 p3 p4

View File

@@ -0,0 +1,11 @@
#include <stdio.h>
#include <unistd.h>
int main()
{
printf("%d: code before forking - executed once only\n", getpid());
fork();
printf("%d: code after forking - executed by each process\n", getpid());
}

View File

@@ -0,0 +1,20 @@
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
int main()
{
if (fork() == 0)
{ // child proc
printf("%d: child created by parent %d; executing 'p4'...\n", getpid(), getppid());
execl("p4", "p4", NULL);
printf("system call execl(): no success\n");
}
else
{ // parent proc
printf("%d: parent process\n", getpid());
}
wait(NULL);
}

View File

@@ -0,0 +1,22 @@
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
int main()
{
if (fork() == 0)
{ // child proc
printf("%d: child created by parent %d; executing 'p4'...\n", getpid(), getppid());
execl("p4", "p4", NULL);
}
else
{ // parent proc
printf("%d: parent process\n", getpid());
}
fork();
printf("%d: terminating\n", getpid());
wait(NULL);
}

View File

@@ -0,0 +1,7 @@
#include <stdio.h>
#include <unistd.h>
int main()
{
printf("%d: p4 running\n", getpid());
}

View File

@@ -0,0 +1,13 @@
all: p5 p6 p7
p5:
gcc -o p5 p5.c
p6:
gcc -o p6 p6.c
p7:
gcc -o p7 p7.c
clean:
rm -f p5 p6 p7

View File

@@ -0,0 +1,23 @@
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
int main()
{
int pid;
pid = fork();
if (pid == 0)
{ // child proc looping endlessly
printf("%d: child ...\n", getpid());
for(;10;);
}
else
{ // parent proc
printf("%d: parent; child pid: %d \n", getpid(), pid);
wait(NULL); // for child termination
printf("%d: parent after wait ... terminating\n", getpid());
}
}

View File

@@ -0,0 +1,7 @@
#include <stdio.h>
int main()
{
printf("p6: calling getchar()\n");
getchar();
}

View File

@@ -0,0 +1,8 @@
#include <stdio.h>
#include <unistd.h>
int main()
{
printf("p7: calling sleep()\n");
sleep(100);
}