Finishing bootstrap program.
authorRafał Długołęcki <rafal@dlugolecki.net.pl>
Fri, 20 Jun 2014 22:14:28 +0000 (00:14 +0200)
committerRafał Długołęcki <rafal@dlugolecki.net.pl>
Fri, 20 Jun 2014 22:14:28 +0000 (00:14 +0200)
src/bootstrap.c
src/process1.c
src/process2.c
src/process3.c

index 1df77c16c8852aef5f11adee20876e234725a52a..c8ae5bcd92953985d778f22a5a664cdc309f600a 100644 (file)
@@ -20,9 +20,16 @@ char * processes[PROCESS_NUMBER] = {
  * 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;
+
+       /** Index of the current process to spawn. */
        int i = 0;
+
+       /** Error (theoretically) returned by execl. */
        int err = 0;
 
        for (; i < PROCESS_NUMBER; i++) {
@@ -31,7 +38,14 @@ int main(void) {
 
                /* 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
@@ -42,9 +56,14 @@ int main(void) {
                                        "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. */
-       
+
        return 0;
 }
\ No newline at end of file
index d0b8e19135969cf98be9cc36f7c305c781107ac8..d514b03e38daa000c379cc21e9c43212576eb2c6 100644 (file)
@@ -40,7 +40,9 @@ int main(void) {
        mkfifo(write_pipe, 0666);
 
        file_descriptor = open(write_pipe, O_WRONLY);
-       
+
+       fprintf(stderr, "[%s] Init!\n", "process1");
+
        do {
                c = fgetc(stdin);
 
index 4db50e07f72c1ad7593bbfd6f8ef7aad9a568088..b6dff4b8d2fa0703405cf9ee49e97793ee01d5e3 100644 (file)
@@ -45,7 +45,9 @@ int main(void) {
        write_descriptor = open(write_pipe, O_WRONLY);
 
        int number_of_characters = 0;
-       
+
+       fprintf(stderr, "[%s] Init!\n", "process2");
+
        while(1) {
                /* Read data from input pipe */
                count = read(read_descriptor, buffer, BUFFER_STEP);
index dfa30dbe389d56ab4cc24f5bf3206fde813c0d94..4568d97d1e3c4ee1280221fbf7f23ef1cd7f57e2 100644 (file)
@@ -23,6 +23,8 @@ int main(void) {
        /* Reading from process2 */
        read_descriptor = open(read_pipe, O_RDONLY);
 
+       fprintf(stderr, "[%s] Init!\n", "process3");
+
        while(1) {
                /* Read data from input pipe */
                count = read(read_descriptor, &buffer, sizeof(int));