Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 311 - Lecture 15 Outline Process Management System calls – exec() – chdir() – system() – nice() – Accessing User and Group IDs – Redirection Lecture.

Similar presentations


Presentation on theme: "CS 311 - Lecture 15 Outline Process Management System calls – exec() – chdir() – system() – nice() – Accessing User and Group IDs – Redirection Lecture."— Presentation transcript:

1 CS 311 - Lecture 15 Outline Process Management System calls – exec() – chdir() – system() – nice() – Accessing User and Group IDs – Redirection Lecture 151CS 311 - Operating Systems I

2 Differentiating a process: exec() exec() system call replaces the code of a process with another process. Prototype of exec() int execl (const char* cmd_path, const char* arg0, const char* arg1,..., const char* argn, NULL) int execv (const char* cmd_path, const char* argv[ ]) Uses the absolute or relative pathname to commands. int execlp (const char* cmd_path, const char* arg0, const char* arg1,..., const char* argn, NULL) int execvp (const char* cmd_path, const char* argv[ ]) Uses $PATH variable to find commands Lecture 152CS 311 - Operating Systems I

3 Examples for exec() To execute a command say “ls –l –a directory” execl (“/bin/ls”, “ls”, “-l”, “-a”, “directory”, NULL); char *argv []= {“ls”, “-l”, “-a”, “directory”, NULL}; execv (“/bin/ls”, argv); uses $PATH variable to find ls command execlp (“ls”, “ls”, “-l”, “-a”, “directory”, NULL); execvp (argv[0], argv); Returns -1 if failure. Lecture 15CS 311 - Operating Systems I3

4 Change directory: chdir() chdir() changes directory A child inherits current working directory from its parent. Syntax: int chdir (const char* pathname) chdir () returns 0 if successful; otherwise, it returns -1 Lecture 15CS 311 - Operating Systems I4

5 system() You can use the system() function to execute UNIX/Linux commands from a C program. Syntax: system(“command”) Example: system(“pwd”) Lecture 15CS 311 - Operating Systems I5

6 Change process priority: nice() Every process has a priority value between -20 and +19. Lower the priority faster the process gets scheduled. nice() changes the priority value of the process. Syntax: int nice(int delta) Delta is added to the current priority value of the process. Lecture 15CS 311 - Operating Systems I6

7 Accessing user and group IDs Getting user and group IDs uid_t getuid () – get real user ID uid_t geteuid () – get effective user ID gid_t getgid () gid_t getegid () Setting user and group IDs int setuid (uid_t id) int seteuid (uid_t id) int setgid (gid_t id) int setegid (gid_t id) Lecture 15CS 311 - Operating Systems I7

8 Redirection Steps that occur in background when executing ls > ls.out – The parent shell forks and then waits for the child shell to terminate. – The child shell opens the file "ls.out," creating it or truncating it as necessary. – The child shell then duplicates the file descriptor of "ls.out" to the standard output file descriptor, number 1, and then closes the original descriptor of "ls.out". – The child shell then exec's the ls utility. – When the child shell terminates, the parent resumes. The parent's file descriptors are unaffected by the child's actions. Lecture 15CS 311 - Operating Systems I8


Download ppt "CS 311 - Lecture 15 Outline Process Management System calls – exec() – chdir() – system() – nice() – Accessing User and Group IDs – Redirection Lecture."

Similar presentations


Ads by Google