Skip to content

Commit 4398572

Browse files
author
Anton Yarkov
committed
Reworked Common part: definitions mobed to separate Defs.h, only useful common methods are in Common.h. Added error handlers to have proper error validation (TODO work more on it).
1 parent 5f74ed9 commit 4398572

18 files changed

+1076
-190
lines changed
Lines changed: 48 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,49 @@
1-
#include <stdio.h>
2-
#include <stdlib.h>
3-
#include <fcntl.h>
4-
#include <unistd.h>
5-
#include <sys/wait.h> // For wait, waitpid
6-
7-
#include "Common.h"
8-
9-
#define PORTNUM 1500 // Port > 1024 because program will not work not as root.
10-
#define NUMBER_OF_PROCESSES 2
11-
12-
// Compilation:
13-
// gcc -std=gnu99 "00 - Fork Processes.c" -o forkProcesses
14-
15-
// Task:
16-
// Use fork to create a child process.
17-
18-
int main(int argc, char *argv[])
19-
{
20-
printf("Enter to main!\n");
21-
printf("Creating %d of child processes...\n", NUMBER_OF_PROCESSES);
22-
for (int i = 0; i < NUMBER_OF_PROCESSES; i++)
23-
{
24-
pid_t pid;
25-
int status;
26-
switch(pid=fork())
27-
{
28-
case -1:
29-
printf("PROC %d:", i);
30-
perror("1 Error of calling fork");
31-
exit(EXIT_FAILURE);
32-
case 0:
33-
printf("PROC %d: 1 CHILD: Child process %d!\n", i, getpid());
34-
printf("PROC %d: 2 CHILD: Parent PID %d!\n", i, getppid());
35-
printf("PROC %d: 3 CHILD: Wait 4 seconds...\n", i);
36-
sleep(4);
37-
printf("PROC %d: 4 CHILD: Exit!\n", i);
38-
exit(EXIT_SUCCESS);
39-
default:
40-
printf("PROC %d: 1 PARENT: Parent process %d!\n", i, getpid());
41-
printf("PROC %d: 2 PARENT: My child PID %d\n", i, pid);
42-
printf("PROC %d: 3 PARENT: Wait until child calls exit()...\n", i);
43-
waitpid(pid, &status, 0);
44-
printf("PROC %d: 4 PARENT: Child exit code: %d\n", i, WEXITSTATUS(status));
45-
printf("PROC %d: 5 PARENT: Exit!\n", i);
46-
}
47-
}
48-
49-
printf("Exit main!\n");
50-
exit(EXIT_SUCCESS);
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <fcntl.h>
4+
#include <unistd.h>
5+
6+
#include "Common.h"
7+
8+
#define NUMBER_OF_PROCESSES 2
9+
10+
// Compilation:
11+
// gcc -std=gnu99 "00 - Fork N Processes.c" -o forkProcesses
12+
13+
// Task:
14+
// Use fork to create a child process.
15+
16+
int main(int argc, char *argv[])
17+
{
18+
printf("Enter to main!\n");
19+
printf("Creating %d of child processes...\n", NUMBER_OF_PROCESSES);
20+
for (int i = 0; i < NUMBER_OF_PROCESSES; i++)
21+
{
22+
pid_t pid;
23+
int status;
24+
switch(pid=fork())
25+
{
26+
case -1:
27+
printf("PROC %d:", i);
28+
perror("1 Error of calling fork");
29+
exit(EXIT_FAILURE);
30+
case 0:
31+
printf("PROC %d: 1 CHILD: Child process %d!\n", i, getpid());
32+
printf("PROC %d: 2 CHILD: Parent PID %d!\n", i, getppid());
33+
printf("PROC %d: 3 CHILD: Wait 4 seconds...\n", i);
34+
sleep(4);
35+
printf("PROC %d: 4 CHILD: Exit!\n", i);
36+
exit(EXIT_SUCCESS);
37+
default:
38+
printf("PROC %d: 1 PARENT: Parent process %d!\n", i, getpid());
39+
printf("PROC %d: 2 PARENT: My child PID %d\n", i, pid);
40+
printf("PROC %d: 3 PARENT: Wait until child calls exit()...\n", i);
41+
waitpid(pid, &status, 0);
42+
printf("PROC %d: 4 PARENT: Child exit code: %d\n", i, WEXITSTATUS(status));
43+
printf("PROC %d: 5 PARENT: Exit!\n", i);
44+
}
45+
}
46+
47+
printf("Exit main!\n");
48+
exit(EXIT_SUCCESS);
5149
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <fcntl.h>
4+
#include <unistd.h>
5+
6+
#include "Common.h"
7+
8+
// Compilation:
9+
// gcc -std=gnu99 "01 - Fork Processes And End Differently.c" -o forkEndProcesses
10+
11+
// Task:
12+
// Use fork to create a child processes, which stop working with different reasons.
13+
14+
void display_status(pid_t pid, int status)
15+
{
16+
if (pid != 0)
17+
printf("Process %d: ", (long)pid);
18+
if (WIFEXITED(status))
19+
{
20+
printf("Exit code: %d\n", WEXITSTATUS(status));
21+
}
22+
else
23+
{
24+
char *desc;
25+
char *signame = errsymbol("signal", WTERMSIG(status), &desc); // 301
26+
27+
if (desc != NULL && desc[0] == '?')
28+
desc = signame;
29+
30+
if (signame != NULL && signame[0] == '?')
31+
printf("Signal #%d", WTERMSIG(status));
32+
else
33+
printf("%s", desc);
34+
35+
if (WCOREDUMP(status))
36+
printf(" - memory dump reset");
37+
38+
if (WIFSTOPPED(status))
39+
printf(" (paused)");
40+
41+
printf("\n");
42+
}
43+
}
44+
45+
static bool wait_and_display(void)
46+
{
47+
pid_t wpid;
48+
int status;
49+
ec_neg1( wpid = waitpid(-1, &status, 0));
50+
display_status(wpid, status);
51+
return true;
52+
53+
EC_CLEANUP_BGN
54+
return false;
55+
EC_CLEANUP_END
56+
}
57+
58+
int main(int argc, char *argv[])
59+
{
60+
printf("Enter to main!\n");
61+
pid_t pid;
62+
63+
// Case 1: explicit call _exit.
64+
if (fork() == 0)
65+
{
66+
_exit(123); // Child.
67+
}
68+
ec_false(wait_and_display()); // Parent.
69+
70+
// Case 2: fail stop.
71+
if (fork() == 0)
72+
{
73+
int a, b = 0; // Child.
74+
a = 1 / b; // Divide by zero
75+
_exit(EXIT_SUCCESS);
76+
}
77+
ec_false(wait_and_display()); // Parent.
78+
79+
// Case 3: signal.
80+
if ((pid=fork()) == 0)
81+
{
82+
sleep(100);
83+
_exit(EXIT_SUCCESS);
84+
}
85+
ec_neg1(kill(pid, SIGHUP));
86+
ec_false(wait_and_display()); // Parent
87+
88+
printf("Exit main!\n");
89+
exit(EXIT_SUCCESS);
90+
91+
EC_CLEANUP_BGN
92+
exit(EXIT_FAILURE);
93+
EC_CLEANUP_END
94+
}

multithreading/01 - Env Vars.c renamed to multithreading/02 - Env Vars.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ extern char ** environ; // Global variable availble to any process for read/writ
99
#define OVERWRITE 1
1010

1111
// Compilation:
12-
// gcc -std=gnu99 "01 - Env Vars.c" -o envVars
12+
// gcc -std=gnu99 "02 - Env Vars.c" -o envVars
1313

1414
// Task:
1515
// Read, write environment variables.

multithreading/02 - Parent Process Die.c

Lines changed: 0 additions & 67 deletions
This file was deleted.
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <fcntl.h>
4+
#include <unistd.h>
5+
6+
#include "Common.h"
7+
8+
// Compilation:
9+
// gcc -std=gnu99 "03 - Parent Process Die.c" -o parentDie
10+
11+
// Task:
12+
// Use fork to create a child process. Emulate parent process died and Parent PID should become Init (1) or some other?
13+
14+
const char* get_process_name_by_pid(const int pid)
15+
{
16+
char* name = (char*)calloc(1024,sizeof(char));
17+
if(name)
18+
{
19+
sprintf(name, "/proc/%d/cmdline",pid);
20+
FILE* f = fopen(name,"r");
21+
if(f)
22+
{
23+
size_t size;
24+
size = fread(name, sizeof(char), 1024, f);
25+
if(size>0)
26+
{
27+
if('\n'==name[size-1])
28+
{
29+
name[size-1]='\0';
30+
}
31+
}
32+
fclose(f);
33+
}
34+
}
35+
return name;
36+
}
37+
38+
int main(int argc, char *argv[])
39+
{
40+
printf("Enter to main!\n");
41+
printf("Create 1 child process...\n");
42+
pid_t pid;
43+
int status;
44+
switch(pid=fork())
45+
{
46+
case -1:
47+
perror("1 Error of calling fork");
48+
exit(EXIT_FAILURE);
49+
case 0:
50+
printf("1 CHILD: Child process %d!\n", getpid());
51+
printf("2 CHILD: Parent PID %d!\n", getppid());
52+
printf("3 CHILD: Wait 3 seconds...\n");
53+
sleep(3);
54+
pid_t ppid = getppid();
55+
printf("4 CHILD: Looks like PARENT died! What is my parent pid? %d\n", ppid); // TODO Find Parent Process Name.
56+
printf("5 CHILD: My parent now is: %s\n", get_process_name_by_pid(ppid)); // TODO Find Parent Process Name.
57+
printf("6 CHILD: Wow! Cool! Exit!\n");
58+
exit(EXIT_SUCCESS);
59+
default:
60+
printf("1 PARENT: Parent process %d!\n", getpid());
61+
printf("2 PARENT: Child PID %d\n", pid);
62+
printf("3 PARENT: Wait 2 seconds...\n");
63+
sleep(2);
64+
printf("4 PARENT: Dying... bye-bye child process!\n");
65+
}
66+
}

0 commit comments

Comments
 (0)