dlugolecki.net.pl
Dziennik
Polecane
Software
projects
/
wsti_so.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
Added initial inter-process communication: Sharing pids between processes.
[wsti_so.git]
/
src
/
bootstrap.c
diff --git
a/src/bootstrap.c
b/src/bootstrap.c
index 1df77c16c8852aef5f11adee20876e234725a52a..bca693c25076765134806010d76b6f5331d4dbee 100644
(file)
--- a/
src/bootstrap.c
+++ b/
src/bootstrap.c
@@
-10,6
+10,9
@@
/** Number of processes to spawn */
#define PROCESS_NUMBER 3
/** Number of processes to spawn */
#define PROCESS_NUMBER 3
+/**
+ * Processes to spawn (programs to be execl'ed)
+ */
char * processes[PROCESS_NUMBER] = {
"process1",
"process2",
char * processes[PROCESS_NUMBER] = {
"process1",
"process2",
@@
-20,9
+23,16
@@
char * processes[PROCESS_NUMBER] = {
* Bootstrap program used for starting all three main processes.
*/
int main(void) {
* Bootstrap program used for starting all three main processes.
*/
int main(void) {
-
+ /**
+ * Variable storing Process ID. In our case we need only to check if
+ * it's equal 0, to exec new program.
+ */
pid_t pid;
pid_t pid;
+
+ /** Index of the current process to spawn. */
int i = 0;
int i = 0;
+
+ /** Error (theoretically) returned by execl. */
int err = 0;
for (; i < PROCESS_NUMBER; i++) {
int err = 0;
for (; i < PROCESS_NUMBER; i++) {
@@
-31,7
+41,14
@@
int main(void) {
/* If it is child fork */
if (pid == 0) {
/* If it is child fork */
if (pid == 0) {
- err = execl(processes[i], processes[i], NULL);
+ fprintf(stderr, "[%s] Forked process %d has id: %d\n", "bootstrap", i, getpid());
+
+ /*
+ * Create new session for this process, so it won't close
+ * after the parent process exit.
+ */
+ setsid();
+ err = execl(processes[i], NULL);
/*
* According to manual, this will only occur
/*
* According to manual, this will only occur
@@
-42,9
+59,14
@@
int main(void) {
"bootstrap", processes[i], strerror(errno));
}
}
"bootstrap", processes[i], strerror(errno));
}
}
+ /* There was an error when forking */
+ else if (pid < 0) {
+ fprintf(stderr, "[%s] Something went wrong when forking %s. Error: %s\n",
+ "bootstrap", processes[i], strerror(errno));
+ }
}
/* All processes should be now spawned. Close bootstrap program. */
}
/* All processes should be now spawned. Close bootstrap program. */
-
+
return 0;
}
\ No newline at end of file
return 0;
}
\ No newline at end of file