lab4 Process
  • Introduction
  • Knowing about process
    • Checking System Process
    • Process Identification
  • Let's Fork! Process Creation
    • Background of Process Creation
    • fork system call
    • Distinguish Parent and Child Process
    • Exercises
  • Process Execution
    • First exec example
    • Environment Variables
    • exec Family
    • exec Family: execlp
    • exec Family: execle
    • exec Family: execv
    • exec Family: execvp
    • exec Family: execve
    • exec error handling
  • Wait!
    • Problem 1: Without Wait?
    • Sleep
    • Problem 2: Zombies!
    • Wait System Call
    • waitpid
Powered by GitBook
On this page

Was this helpful?

  1. Process Execution

exec Family: execve

/* Exec/execve.c */
#include <stdio.h>
#include <unistd.h>
int main(int argc,char *argv[]){ 
    char *env[] = {"LS_COLORS=fi=04;33;44",NULL}; 
    char *arg[] = {"ls","-l","--color",NULL}; 
    printf("Using *execve* to exec ls -l\n");  
    execve("/bin/ls",arg,env); 
    printf("Program Terminated\n");
    return 0;
}

execve() uses filename, argument array and provided ENV to execute the program.

execve("/bin/ls",arg,env);

It only takes a filename to invoke the program, and it is searched in paths specified in $PATH in sequence.

The arguments are listed in an array of char* and passed to the function.

The new sets of environment variables are declared in an array of char* and passed to the program in the function. They can be used to change the behavior of the new program, for example, ls in this example.

Previousexec Family: execvpNextexec error handling

Last updated 4 months ago

Was this helpful?