Linux 变量未维护

Linux Variable not Maintaining

我正在编写这个程序,它创建一个 parent 和一个 child,我必须验证用户名。 但它似乎不起作用,因为我有一个全局变量集,所以我可以跳过一些代码块直接跳到我想评估用户名的部分。 但似乎我拥有的变量 logEval 没有保持我在第一个 child 中给它的值,所以它 returns 0 在 parent.

这是代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <fcntl.h>
//sys variables

char Input[100], commandLogin[100], Output[100], Username[50], possibleUsername[50];
int readDescriptor, logEval;
pid_t childPid;

//

int commandCatcher(){

    //define socket
    int socketOne[2], socketTwo[2];

    if(socketpair(AF_UNIX, SOCK_STREAM, 0, socketOne) < 0){
        perror("socket - err");
        exit(0);
    }

    if(logEval == 0)
    switch(fork()){//first child
        case -1:
            perror("fork - err");
            exit(1);

        case 0:
            childPid=getpid();
            readDescriptor = read(socketOne[0], commandLogin, sizeof(commandLogin));

            if (strcmp(commandLogin, "login") == 0 ){
                logEval = logEval + 1;
                printf("Log eval from first:%d\n", logEval);
                write(socketOne[0], "ok", strlen("ok") + 1);
                //something to do here so that the child knows it was given the username

                exit(1);
            }

            else {
                printf("Try again.\n");
                write(socketOne[0], "none", strlen("none")+1);
                exit(1);
            }
    }

    if(logEval == 1){

        switch(fork()){//verify username
            case -1:
                perror("fork - err");
                exit(2);

            case 0:
                readDescriptor = read(socketTwo[0], possibleUsername, sizeof(possibleUsername));
                printf("Username from second child:%s\n", possibleUsername);
                printf("logEval is : %d", logEval);
        }
        exit(2);
    }

    //parent

    //getting initial value
    scanf("%s", Input);
 //writing initial value
    write(socketOne[1], Input, strlen(Input)+1);

    readDescriptor = read(socketOne[1], Output, sizeof(Output));
    //printf("output    %s\n", Output);

//verify if the command was correctly given
    if(strcmp(Output, "ok") == 0) {
        printf("Command was accepted. Insert your username: %d\n", logEval);

        if(socketpair(AF_UNIX, SOCK_STREAM, 0, socketTwo) < 0){//second socket
            perror("sockettwo - err");
            exit(2);
        }

        scanf("%s", Username);
        write(socketTwo[1], Username, strlen(Username)+1);
        printf("%s\n", Username);

        printf("%d\n", logEval);

        commandCatcher();
    }
    else if(strcmp(Output, "none") == 0) {
        printf("Command was denied. Please try again:\n");

        commandCatcher();//recursive call
    }

wait(&childPid);
printf("execution finished");
}

int main(){
    printf("Welcome to Sys v1.0. To start off, please insert your command. \n");
    commandCatcher();
    return 0;
}

第二次编辑:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <fcntl.h>
//sys variables

char Input[100], commandLogin[100], Output[100], Username[50], possibleUsername[50];
int readDescriptor, logEval;
int readDescriptor2;
pid_t childPid;

//

int commandCatcher(){

//define socket
    int socketOne[2], socketTwo[2];

    if(socketpair(AF_UNIX, SOCK_STREAM, 0, socketOne) < 0){
        perror("socket - err"); 
        exit(0);
    }

    if(logEval == 0)
    switch(fork()){//first child
        case -1:
            perror("fork - err");
            exit(1);

        case 0:
            childPid=getpid();
            readDescriptor = read(socketOne[0], commandLogin, sizeof(commandLogin));

            if (strcmp(commandLogin, "login") == 0 ){

                printf("Log eval from first:%d\n", logEval);
                write(socketOne[0], "ok", strlen("ok") + 1);
                //something to do here so that the child knows it was given the username

                exit(logEval);
            }

            else {
                printf("Try again.\n");
                write(socketOne[0], "none", strlen("none")+1);
                exit(1);
            }
    }

    if(logEval == 1){

        switch(fork()){//verify username
            case -1:
                perror("fork - err");
                exit(2);

            case 0:
                readDescriptor2 = read(socketTwo[0], possibleUsername, sizeof(possibleUsername));
                printf("Username from second child:%s  ---\n", possibleUsername);
                printf("logEval is : %d\n", logEval);
}
                exit(2);
        }


    //parent

    //getting initial value
    scanf("%s", Input);
 //writing initial value
    write(socketOne[1], Input, strlen(Input)+1);

    readDescriptor = read(socketOne[1], Output, sizeof(Output));
    //printf("output    %s\n", Output);


//verify if the command was correctly given
    if(strcmp(Output, "ok") == 0) {
        printf("Command was accepted. Insert your username: %d\n", logEval);

        if(socketpair(AF_UNIX, SOCK_STREAM, 0, socketTwo) < 0){//second socket
            perror("sockettwo - err");
            exit(2);
        }

        scanf("%s", Username);
        write(socketTwo[1], Username, strlen(Username)+1);

        logEval = 1;

        commandCatcher();
        }
    else if(strcmp(Output, "none") == 0) {
        printf("Command was denied. Please try again:\n");

        commandCatcher();//recursive call
    }

wait(&logEval);

printf("execution finished");
}

int main(){
    printf("Welcome to Sys v1.0. To start off, please insert your command. \n");
    commandCatcher();
    return 0;
}

当您 fork() 一个进程时,它的子进程无法访问父进程的地址 space。它将拥有它的完整副本,因此在 fork() 之前设置的任何内容在子进程中都是相同的,但是如果您在子进程中更新任何内容,父进程将看不到它。您将需要为此使用某种形式的进程间通信 (IPC),例如共享内存或管道。