help with unix command exec1 in C


 
Thread Tools Search this Thread
Top Forums Programming help with unix command exec1 in C
# 8  
Old 03-13-2011
i think theres something wrong with this code
since in the child process, it runs execl, so the child process gets replaced by ls, ls command process, so when it finishes where does the return number go to? does it go to the parent process (the fd[0] end of the pipe)? And how does Wexitstatus get the return code of the command?
# 9  
Old 03-14-2011
Have you actually tried any of this code?

The return code goes to wait (or waitpid) as part of the status variable. The kernel actually keeps the process around in "zombie" form until you wait for it, which is how it can get the statistics even after it's dead. It's only part of the status variable which is why you need WEXITSTATUS to extract it.

Last edited by Corona688; 03-14-2011 at 12:53 PM..
# 10  
Old 03-14-2011
Ok it sort of works now, but theres a problem, here what i did
The output is sorta correct, i gave the input command to be ls, the output was
my_prog.c
my_prog
abc
ñà·
EÉ÷·8É÷·*à·
0

The zero come up since ls was successfull, but i dont understand why the files in the directory came up line by line and not all in one line like how normally doing ls would result in, also theres some gibberish lines after the ls command results, why did that show up? Am I missing a \0 anywhere or something?

Also i tried giving a command that would fail like cd ffg, and the WEXITSTATUS number came out to be 2, so what really are the possible outcomes for WEXITSTATUS? i only know so far 0 and 2, shouldnt it be -1 instead of 2?

Last edited by omega666; 03-15-2011 at 04:38 PM..
# 11  
Old 03-14-2011
Quote:
Originally Posted by omega666
The zero come up since ls was successfull, but i dont understand why the files in the directory came up line by line and not all in one line like how normally doing ls would result in
ls can tell the difference between a terminal and a pipe. ls | cat will do that too. In other words -- that's normal.
Quote:
also theres some gibberish lines after the ls command results, why did that show up? Am I missing a \0 anywhere or something?
Remember that read() doesn't null-terminate for you -- it's a general purpose system call that can be used for anything, not just strings. So you're not only missing a NULL -- you're mistaken in that you even need NULLs.

Also, you shouldn't do printf(string); like that, since that could cause crashes if your string ends up having formatting specifiers like %s in it -- printf will try to read more parameters off the stack and crash since there aren't any. Let's use write() instead -- it needs no NULLs and can't crash in that manner.

I have no idea why you're duplicating pipes in the parent's code when you don't ever use the duplicate. Don't just throw dup2() around, only use it when you know exactly what you want to do with it why.

Furthermore, I repeat: fileno(stdin) is redundant. Use STDIN_FILENO.

Further furthermore: read first, then wait(). And keep reading until it's done, otherwise you might be throwing away tons of output.

Further further furthermore: You're still not closing your pipes before you wait(). This can cause a deadlock if the child process thinks it's waiting for you because it'll wait until the pipe closes. You could even end up waiting for yourself when you're reading from one end and still have the other end open.

Code:
int sys_new(char *comm) {
    char pipeout[512];
    int fd[2], status;
    pipe(fd);
    pid_t pid = fork();
    if (pid == 0) { 
        dup2(fd[1], STDOUT_FILENO);
        close(fd[0]); close(fd[1]);
        execl("/bin/sh", "/bin/sh", "-c", comm, NULL);
        // Need to exit() down here in case execl fails
        perror("couldn't execl");
        exit(1);
    }
    else if (pid > 0) {
        ssize_t bytes;
        close(fd[1]); // So we don't wait for ourselves forever.
     
        bytes=read(fd[0], pipeout, 512);
        while(bytes > 0) // happens when child's pipe closes.
        {
            write(STDOUT_FILENO, pipeout, bytes);            
            bytes=read(fd[0], pipeout, 512);
        }
        close(fd[0]);

        wait(&status);
    }
    else {
        perror("fork"); exit(1);
    }
    return 0;
}

Quote:
Also i tried giving a command that would fail like cd ffg, and the WEXITSTATUS number came out to be 2, so what really are the possible outcomes for WEXITSTATUS? i only know so far 0 and 2, shouldnt it be -1 instead of 2?
Possible return values are 0 through 255, it can't return negative numbers. 0 means success, anything else means error, what the return values mean specifically depends on the application.
This User Gave Thanks to Corona688 For This Post:
# 12  
Old 03-14-2011
when I run it using ls, i get

my_prog.c
my_prog
abc
my_prog.c
my_prog
abc
Ù·Eið·8ið·ÊØ·
0

Why is this "Ù·Eið·8ið·ÊØ·" coming up when i do printf, I dont understand what you mean by adding NULL's. How can i printf it while making the output of what i want to only come up, and not that gibberish stuff?
# 13  
Old 03-14-2011
Quote:
Originally Posted by omega666
when I run it using ls, i get

my_prog.c
my_prog
abc
my_prog.c
my_prog
abc
Ù·Eið·8ið·ÊØ·
0
Now I know you don't try the code I post for you, my version doesn't do that.

It's because, as stated above, read() doesn't add NULLs for you. This causes printf() to write more data than you actually read, because it doesn't know when to end.

Also as above, read() shouldn't add NULLs for you -- NULLs only make sense in C text strings, which program output might not always be. So you should write exactly the bytes you got, and exactly as many bytes as you read, nothing more, nothing less. This will work for any kind of data, and is what the code I posted above does.
This User Gave Thanks to Corona688 For This Post:
# 14  
Old 03-14-2011
i dont see where in your version are u recording the amount of bytes written, and then using that number to read that amount...

this line reads

bytes=read(fd[0], pipeout, 512);
and it reads 512 bytes which i think is more than needed, how do i know the amount written from execl?
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

UNIX command to copy files from Windows to UNIX box

Hi Folks, I have a file name abc.xml in my windows machine at the location c:\ytr\abc.xml which I want to place at the unix box machine inside cde directory.. at the following location that is /opt/app/cde/ now the credentials of unix box are abc345 -->(dummyid) ftyiu88--->(dummy passwd) ... (4 Replies)
Discussion started by: punpun66
4 Replies

2. Shell Programming and Scripting

Specify an entire UNIX command as a command line argument

I'm trying to write a bash script called YN that looks like the following YN "Specify a question" "doThis" "doThat" where "doThis" will be executed if the answer is "y", otherwise "doThat". For example YN "Do you want to list the file dog?" "ls -al dog" "" Here's my attempt... (3 Replies)
Discussion started by: LeoKSimon
3 Replies

3. UNIX Desktop Questions & Answers

Can Unix access Windows' File through Command Prompt in Unix

Hi all, I wish to know whether Unix can access window's file in Unix's terminal? Apart from that, how to copy files or share files between Window and Unix? I get to know of secure copy, however, my company's Unix does not support the feature of secure copy? Any other method for me to share/... (5 Replies)
Discussion started by: jessy83
5 Replies

4. UNIX for Dummies Questions & Answers

New to Unix command line and have a question about the "sort" command

I am going through the Unix Made Easy second edition book by John Muster. So far it's been very informative and I can tell it may be a bit out of date. In one of the exercises it talks about the "sort" command and using it to sort column's of data etc. The "sort" command has changed a bit and... (1 Reply)
Discussion started by: budfoxcat
1 Replies

5. UNIX for Advanced & Expert Users

unix command : how to insert text at the cursor location via command line?

Hi, Well my title isn't very clear I think. So to understand my goal: I have a script "test1" #!/bin/bash xvkbd -text blabla with xbindkeys, I bind F5 key in order it runs my test1 script So when I press F5, test1 runs. I'm under Emacs/Vi and I press F5 in order to have "blabla" be... (0 Replies)
Discussion started by: xib.be
0 Replies

6. Shell Programming and Scripting

assign a command line argument and a unix command to awk variables

Hi , I have a piece of code ...wherein I need to assign the following ... 1) A command line argument to a variable e.g origCount=ARGV 2) A unix command to a variable e.g result=`wc -l testFile.txt` in my awk shell script When I do this : print "origCount" origCount --> I get the... (0 Replies)
Discussion started by: sweta_doshi
0 Replies

7. HP-UX

How to use more than one MPE command STREAM with Unix command in a single shell?

Hello, I have problem in writing the shell script involving MPE command STREAM related to HP-UX and Unix command. Script is sh "nlshCMD 'STREAM <job name1>' | 'SHOWJOB' | grep $HPJOBNUM" sh "nlshCMD 'STREAM <job name2>' | 'SHOWJOB' | grep $HPJOBNUM" sh "nlshCMD 'STREAM <job name3>' |... (1 Reply)
Discussion started by: bosskr
1 Replies

8. Shell Programming and Scripting

How to use more than one MPE command STREAM with Unix command in a single shell?

Hello, I have problem in writing the shell script involving MPE command STREAM related to HP-UX and Unix command. Script is sh "nlshCMD 'STREAM <job name1>' | 'SHOWJOB' | grep $HPJOBNUM" sh "nlshCMD 'STREAM <job name2>' | 'SHOWJOB' | grep $HPJOBNUM" sh "nlshCMD 'STREAM <job name3>' |... (0 Replies)
Discussion started by: bosskr
0 Replies

9. UNIX for Dummies Questions & Answers

wc command help.... and other unix command....... urgent

hi all. thanks for looking i am doing some homework. one question is that when type wc and then how to tell the program that we have finished entering data? also why do some operating systems report 22 as the number of bytes in the file above, while others only 20? thanks so much,... (1 Reply)
Discussion started by: dashi2k
1 Replies
Login or Register to Ask a Question