All about programming in GNU/LINUX

A Simple Program to programatically invoke your favorite executables using FORK and EXEC System calls in GNU/LINUX

#include<unistd.h>
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<wait.h>
void printerror(void);
int main()
{
    int pid,choice;
    int *wai;
    printf(“\nThe process id is:%d “,getpid());
    sleep(2);
    if((pid=fork())==-1)
    {
        printf(“\nerror Forking “);
        exit(0);

    }
    else if(pid==0)
    {
        printf(“\n\nChild process in action,the process Id of the child is %d\n”,getpid());
        printf(“\n\nParent process Id is %d”,getppid());
        sleep(3);
        printf(“\n1.Shell\n2.Firefox\n3.VLC\n4.EXIT\n”);/*just list the path of the executables you want to open*/

        printf(“\t\tMake the choice: “);
        scanf(“%d”,&choice);
        switch(choice)
        {
            case 1:execv(“/bin/bash”,0);//exec the executables
                   printerror();

            case 2:execv(“/usr/bin/firefox”,0);/*this wont work in case the executables doesnt exist*/
                   printerror();
            case 3:execv(“/usr/bin/vlc”,0);
                   printerror();
            default:exit(1);
        }
    }
    else
    {
        wait(wai);
        printf(“\n\nParent process is in action “);
        printf(“\nchild returnds=%d\n\n\n”,*wai);
        
        
    }

}
void printerror()
{
    printf(“\nError opening the executable , check if this exists”);
    exit(0);
}
Image

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s