0

I am writing a mini-shell and I encounter a problem in job control.
I cannot retrieve the data in the main function and even in the bottom of run_command.
I would like to know how i can store and successfully retrieve the information in the main.

typedef struct job{
    int num;
    pid_t pid[SIZE];
    char* name[SIZE];
    int occupied;
}
job jobs; <- the global variable

int run_command(........){
.    .......
.    .......
.    result = fork(); // generate child process
.    if ( result == 0 ){ //child process
.    .    if ( 2-pipe function is detected){ // 2 pipe
.    .    .    .......
.    .    .    .......
.    .    .    pid01 = fork(); // fork the first child ( for the first "cat" in case of "cat | cat | cat")
.    .    .    if (pid 01 == 0){
.    .    .        .....
.    .    .    }
.    .    .    else{
.    .    .    .    pid02 = fork(); // fork the second child
.    .    .    .    if (pid02 == 0){
.    .    .    .        .....
.    .    .    .    }
.    .    .    .    else{
.    .    .    .    .    pid03 = fork(); // fork the third child
.    .    .    .    .    if (pid03 == 0){
.    .    .    .    .        ....
.    .    .    .    .    }
.    .    .    .    .    else{
.    .    .    .    .    .     ....
.    .    .    .    .    .     waitpid(pid01,&status1, WUNTRACED);
.    .    .    .    .    .     waitpid(pid02,&status2, WUNTRACED);
.    .    .    .    .    .     waitpid(pid03,&status3, WUNTRACED);
.    .    .    .    .    .     if (WIFSTOPPED(status1)){
.    .    .    .    .    .         jobs.occupied = 1;
.    .    .    .    .    .         jobs.num = 3;
.    .    .    .    .    .         for () {} (a for loop to store the job, i.e. " cat | cat | cat ")
.    .    .    .    .    .         jobs.pid[0] = pid01;
.    .    .    .    .    .     }
.    .    .    .    .    .     if (WIFSTOPPED(status2)){
.    .    .    .    .    .         jobs.pid[1] = pid02;
.    .    .    .    .    .     }
.    .    .    .    .    .     if (WIFSTOPPED(status3)){
.    .    .    .    .    .         jobs.pid[2] = pid03;
.    .    .    .    .    .     }
.    .    .    .    .    }
.    .    .    .    }
.    .    .    }
.    .    }
.    .    exit(-1);
.    }
.    waitpid( result, &status, WUNTRACED);
.    if (WIDSTOPPED(status)){
.    ....
.    }
.    ( cannot retrieve jobs here already, like the variable "jobs" has been initialized again)
.    return 0;
}

int main(){
.....
.....
.....
run_command(........);
(jobs hasn't been modified after I have press ctrl-z)
return 0;
}
  • 1
    All of your assignments to the global variable are done in the first child. Of course they are not available in the parent. – William Pursell Oct 14 '13 at 12:00
  • If your code is so deeply indented that you think you need lines of dots to show the indents, then you need to look hard at why you don't have functions in your code so it is not so deeply indented. And 7 levels of indent is quite high — not outrageous, but not good. Linux kernel style requires 8-space tabs for indents; this encourages using functions because 7 levels of indent means the code starts in column 56. – Jonathan Leffler Oct 14 '13 at 15:30

1 Answers1

0

Variables are not shared between parent and child processes created via fork. The only ways to communicate between processes include:

  • Using a Unix domain socket to exchange data
  • Create a pipe before the fork and use it to write/read data
  • Use of the exit status of a process (limited data size)
  • Use of signals (very limited data size, actually only SIGUSR1 and SIGUSR2 can be used safely)
LostBoy
  • 948
  • 2
  • 9
  • 21