Blogger news

Followers

Friday, July 25, 2014
/*C program that creates a child process to read commands from the standard input and execute them.You can assume that no arguments will be passed to the commands to be executed*/

#include<stdio.h>
#include<unistd.h>
int main()
{
    char i[10];
    int n;
    //int status;
    while(1)
    {
        printf("Enter your command\n");
        get(i);
        n=fork()
       
        if(n==0)
        {   /*it's for executes in new process execl("/bin/sh/","sh","-c",i,null)
              and add library unisd.h*/

            

             execl("/bin/sh/","sh","-c",i,null);
            exit(0);
        }
        else if(n>0)
        {    /*sometime wait() is not working then you change wait function to
            this following part:
                while(waitpid(n,status,0)<0)
                {
                    break;
                }
            */

            wait();
       
        }
        else
        {
            printf("error\n");
        }
    }
    return(0);
}

0 comments: