How to write the shell using c


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to write the shell using c
# 1  
Old 02-26-2011
How to write the shell using c

Hi, guys:

I am working on my own shell using c. When I fork a child process and make it run in background. I hope when this process exits, my shell can print a line such as "this process exits". Does anyone know how to implement it?

Thanks
# 2  
Old 02-26-2011
Code:
# include       <stdio.h>

main()
{
        int child;

        child=fork();
        if (child == 0) { /* child process */
                system("ls");
                }
        else { /* parent process */
                while (wait() != child) sleep(1);
                printf("\n%d child ended\n",child);
                }
}

# 3  
Old 02-26-2011
Thanks for your reply. But I want the child process can run in background, not in foreground




Quote:
Originally Posted by kshji
Code:
# include       <stdio.h>

main()
{
        int child;

        child=fork();
        if (child == 0) { /* child process */
                system("ls");
                }
        else { /* parent process */
                while (wait() != child) sleep(1);
                printf("\n%d child ended\n",child);
                }
}

# 4  
Old 02-26-2011
parent is foreground, child is background.
Just like in some shell:
Code:
ls &
pid=$!
wait $pid

# 5  
Old 02-26-2011
Quote:
Originally Posted by tomlee
Thanks for your reply. But I want the child process can run in background, not in foreground
It is in the background, independent. Your program only wait()'s because you tell it to.
# 6  
Old 02-27-2011
waitpid supports the WNOHANG option to check a child process is complete without blocking.

Typically you would check for child processes just before printing the shell prompt and report them exiting there. This means the process could be finished for quite a while but user is only notified with then hit return.

This simple example spawns 1 child process, checks it is still running before each prompt is printed and when it is complete prints the exist status.

Code:
#include<stdio.h>
#include <sys/wait.h>
#include <string.h>
int main()
{
    pid_t child;
    child=fork();
    if (child == 0) { /* child process */
        system("sleep 5");
        return 15;
    }
    else { /* parent process */
        int child_stat;
        char cmd[128];
        while(1) {
            if (child != -1) {
                if(waitpid(child, &child_stat, WNOHANG) &&
                   WIFEXITED(child_stat)) {
                    printf("Child process exited with: %d\n", WEXITSTATUS(child_stat));
                    child=-1;
                }
            }
            printf("myShell> ");
            gets(cmd);
            if (!stricmp(cmd, "quit")) return 0;
        }
    }
}

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Please help me to write the shell script

Please help me to write the shell script AC|NAME|STATE|MAXVALUE|MINVALUE---------heading 111|UMA|ODISHA|123,00.00|54.00 111|UMA|ODISHA|124,00.00|25.00 111|UMA|ODISHA|114,00.00|58.00 111|UMA|ODISHA|104,00.00|00.00 111|UMA|ODISHA|194,00.00|19.00 111|UMA|ODISHA|184,00.00|64.00... (5 Replies)
Discussion started by: alokjyotibal
5 Replies

2. Shell Programming and Scripting

How to write config shell script to pass variables in master shell script?

Dear Unix gurus, We have a config shell script file which has 30 variables which needs to be passed to master unix shell script that invokes oracle database sessions. So those 30 variables need to go through the database sessions (They are inputs) via a shell script. one of the variable name... (1 Reply)
Discussion started by: dba1981
1 Replies

3. UNIX for Dummies Questions & Answers

How to write Config shell script to pass variables in master shell script?

Dear Unix gurus, We have a config shell script file which has 30 variables which needs to be passed to master unix shell script that invokes oracle database sessions. So those 30 variables need to go through the database sessions (They are inputs) via a shell script. one of the variable name... (1 Reply)
Discussion started by: dba1981
1 Replies

4. Shell Programming and Scripting

Need to write a shell script

Hi, I need some help to write a script i need to write script that will count the no of logs generated for the day and specify the logfiles names which is not generated for the day. Default no of log generate per day is 29 if less than 29 logs generated it should specify the perticular logfile... (7 Replies)
Discussion started by: mail.chiranjit
7 Replies

5. Shell Programming and Scripting

how to write shell script

Hi , i need to write a script like this In 1.sh ./test.sh wait(5sec) send ctrl+C to server ./testxxxx.sh first i need invoke test.sh from 1.sh , after wating for some time i need to close the test.sh script & i need to start new script i tried to invoke script... (2 Replies)
Discussion started by: pvr_satya
2 Replies

6. IP Networking

read/write,write/write lock with smbclient fails

Hi, We have smb client running on two of the linux boxes and smb server on another linux system. During a backup operation which uses smb, read of a file was allowed while write to the same file was going on.Also simultaneous writes to the same file were allowed.Following are the settings in the... (1 Reply)
Discussion started by: swatidas11
1 Replies

7. Shell Programming and Scripting

Need to Write Shell Script based off of this shell command

I'm trying to read a bunch of log files and output the lines that contain particular strings. To accomplish this, I've been running the following from the command line: find . -name "*" | xargs grep " " | grep " " > output.txt Two grep statements are needed in case I'm looking for a... (3 Replies)
Discussion started by: Rally_Point
3 Replies

8. Shell Programming and Scripting

I need to write a shell script.

Hi Guys, I need to write a shell script, to which I have getting results from CPU and Memory Utilization. This is to generate automatically through Shell script by running cron job. Is it possible to write it. I am new to this shell scripting. Can you please help me on this ASAP. ... (6 Replies)
Discussion started by: lakshmanrk
6 Replies

9. Shell Programming and Scripting

need help to write shell script

. I wrote shell script with help to extract data and generate report . I need help to modify that in sub section . Currently I am generating the report in this format Version Name Host Total Number of Fails 10 Animator 45 10 Krachel ... (0 Replies)
Discussion started by: getdpg
0 Replies

10. Shell Programming and Scripting

how to write shell prog

how to write shell program. EX : to check whether a string is a palindrome or not. (9 Replies)
Discussion started by: raram
9 Replies
Login or Register to Ask a Question