Page 1 of 1

Shell in C

Posted: Sun May 09, 2004 11:22 am
by chris
I'm having trouble when it comes to the execution part of my shell. Single commands work fine but I've been testing my pipes using two piped commands ("ls | sort") and things havn't been going to well.

Once fork()ed each child calls my execute_task() function and a bunch of debug info is printed. Then there are if()s to setup the pipes and/or redirection (disregard redirection for now), and finaly a call to execvp(). I have printf()s along the way but only the child process for "sort" seems to print them. I have no idea what happens to "ls", although I know it does terminate (shown below). "sort" just hangs. I think this may be some sort of pipe problem. Is an EOF sent to the reader of a pipe if the writer process closes? ...I hope I've made sense

I've attached the entire file.

Code: Select all

[Aurora:~/dev/unix] chris% rm shell ; gcc -Wall -o shell shell.c ; ./shell
shell% ls | sort
* Debug
argv[0]: "ls"
argv[1]: "(null)"
argv[2]: "(null)"
argv[3]: "(null)"
pipes[0]: 3
pipes[1]: 4
redirect[0]: -1
redirect[1]: -1
flags: 16
P_WRITE

argv[0]: "sort"
argv[1]: "(null)"
argv[2]: "(null)"
argv[3]: "(null)"
pipes[0]: -1
pipes[1]: -1
redirect[0]: -1
redirect[1]: -1
flags: 1
P_READ

1304 pstdin: 3
caught: 1303
P_* are flags set so I know how to setup the pipes.

"1304 pstdin: 3" says that the second process ("sort") has dup2()ed read pipe descriptor (3) onto it's stdin (the pipe is stored in the structure of the first process in the pipe).

there is no "1303 pstdout: 4" saying that the process for "ls" did the opposite.

"caught: 1303" shows that the first process (for "ls") has terminated.

Re:Shell in C

Posted: Sun May 09, 2004 1:53 pm
by Candy
the tasks terminate when their stdin returns eof + the tasks don't terminate -> their stdin doesn't return eof

stdin returns eof when the pipe is closed + stdin doesn't return eof -> the pipe is not closed

when you fork after pipe()-ing, you get the pipes both in the parent as well as the child + you close the pipe in the child -> you didn't close the pipe in the parent.

;)

Re:Shell in C

Posted: Sun May 09, 2004 2:18 pm
by chris
Thanks alot Candy, that was it :)