0

I have a parent process that receives user-input that can create child processes and then with further input, manipulate individual child processes.

What should be printing is the following:

New Locker Created: 1

New Locker Created: 2

New Locker Created: 3

New Locker Created: 4

New Locker Created: 5

But instead I receive the following:

New Locker Created: 1

New Locker Created: 2

New Locker Created: 3

New Locker Created: 4

New Locker Created: 5

New Locker Created: 5

New Locker Created: 4

New Locker Created: 5

New Locker Created: 5

New Locker Created: 3

New Locker Created: 4

New Locker Created: 5

And this continues for a few more rows but that's the idea. From what I understand this is because the child process continues to execute as parent has been waiting for the other child processes to finish while receiving new input. Is this correct?

Here's my code:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#include <string.h>
#include "locker.h"
#include <signal.h>
#include <stdarg.h>

#define P1_READ 0
#define P2_WRITE 1
#define P2_READ 2
#define P1_WRITE 3

#define NUM_PIPES 2

pid_t this_pid;

int main(){

char buffer[100];

int locker_count = 0;
int fd[2*NUM_PIPES];
int len = 0, i;
pid_t pid_list[100], wpid;

memset(pid_list,0,100);

pid_t this_pid;
struct locker_t new_locker = { .id = 0, .user_id = 0, .locked = 1, .owned = 0};

fflush(stdout);

while(1){
    scanf("%s", buffer);

    fflush(stdout);

    if(strcmp(buffer, "CREATE") == 0){

        for(i = 0; i < NUM_PIPES; ++i){
            if(pipe(fd+(i*2)) < 0){
              perror("Failed to allocate pipes");
              exit(EXIT_FAILURE);
            }
        }

        fflush(stdout);

        this_pid = fork();

        if(this_pid == 0){

            //close descriptors we don't need in this process
            close(fd[P1_READ]);
            close(fd[P1_WRITE]);

            len = read(fd[P2_READ], &locker_count, sizeof(locker_count));

            //increase by 1
            locker_count++;

            //create locker
            new_locker.id = locker_count;

            printf("New Locker Created: %d\n", locker_count);
            printf("\n");

            write(fd[P2_WRITE], &locker_count, sizeof(locker_count));

            //finished. close remaning descriptors
            close(fd[P2_READ]);
            close(fd[P2_WRITE]);

            //need to stop this process for further executing

        }
        if(this_pid > 0){

            //close unneeded descriptors
            close(fd[P2_READ]);
            close(fd[P2_WRITE]);

            write(fd[P1_WRITE], &locker_count, sizeof(locker_count));

            //now wait for response
            len = read(fd[P1_READ], &locker_count, sizeof(locker_count));

            //pid_list[(locker_count-1)] = this_pid;

            close(fd[P1_READ]);
            close(fd[P1_WRITE]);            

            fflush(stdout);

            while ((wpid = waitpid(this_pid, NULL, 0)) > 0); 

        }
    }

    if(strcmp(buffer, "QUIT") == 0){
        break;
        exit(0);
    }
}
}

Thanks for your help :)

Hews
  • 569
  • 6
  • 19
  • 1
    You have a comment saying `need to stop this process`, but comments don't stop anything. What happens when the child keeps running through the loop? What did you see when you ran this under `strace -f` or added logging which included the `pid`? – Useless May 10 '18 at 08:39
  • The comments are just for my own referencing. When I run strace -f I get this: execve("./locker.c", ["./locker.c"], 0x7ffc7b71eda8 /* 15 vars */) = -1 EACCES (Permission denied) fstat(2, {st_mode=S_IFCHR|0620, st_rdev=makedev(136, 0), ...}) = 0 write(2, "strace: exec: Permission denied\n", 32strace: exec: Permission denied ) = 32 getpid() = 51 exit_group(1) = ? +++ exited with 1 +++ – Hews May 10 '18 at 08:53
  • You're trying to strace running the source file. You're supposed to run the program, which apparently you were doing fine before. And the point about the comment is that if you want to stop the process, you should `exit` or return from main. – Useless May 10 '18 at 10:42

0 Answers0