16.05.2015 Views

Working with the Unix OS

Working with the Unix OS

Working with the Unix OS

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Processes I<br />

process is <strong>the</strong> child process.<br />

main()<br />

{<br />

int childpid;<br />

if ((childpid = fork()) == -1) {<br />

fprintf(stderr, "can't fork\n"); exit(1);<br />

} else if (childpid == 0) { /* child process */<br />

printf("child: childpid=%d, parentpid=%d\n",<br />

getpid(), getppid()); exit(0);<br />

} else { /* parent process */<br />

printf("parent: childpid=%d, parentpid=%d\n",<br />

childpid, getpid()); exit(0);}<br />

}<br />

fork operation:<br />

parent process<br />

fork<br />

child process<br />

child process<br />

- Text segment can be shared.<br />

- Child's copy of <strong>the</strong> data segment is a copy of <strong>the</strong> parent's data segment, not <strong>the</strong> program's disk file<br />

1. Process makes a copy of itself<br />

- one copy can handle an operation while o<strong>the</strong>r copy does ano<strong>the</strong>r task<br />

- typical of network servers<br />

2. Process executes ano<strong>the</strong>r program<br />

- fork to make a copy of itself<br />

- issue exec to execute new program<br />

exit System Call<br />

- Process terminates<br />

- exit status 0 to 255 (nonzero indicates error)<br />

- _exit function avoids any standard I/O cleanup<br />

exec System Call<br />

- Replaces current process <strong>with</strong> new program .<br />

- There are 6 version of exec<br />

int execlp(char*file, char *arg, ..., NULL);<br />

int execvp(char *file, char **argv);<br />

int execl(char *path, char *arg, ..., NULL);<br />

int execv{char *path, char **argv);<br />

int execle{char *path, char *arg, ..., NULL, char **envp);<br />

int execve{char *path, char **argv, char **envp);<br />

- Exec process inherits attributes: process ID, parent process ID, process group ID, terminal group ID, time left<br />

until an alarm clock signal, root directory, current working directory, first mode creation mask, file locks, real<br />

user ID, real group ID<br />

- Attributes that can change: effective user ID, effective group ID<br />

- If <strong>the</strong> set-user-ID bit is set <strong>the</strong>n effective user ID is changed to <strong>the</strong> user ID of <strong>the</strong> owner of <strong>the</strong> program<br />

wait System Call<br />

- A process can wait for a child process to finish<br />

- Wait returns a process ID when a child process<br />

- calls exit or<br />

- is terminated by a signal or<br />

- is being traced and <strong>the</strong> process stops<br />

- Steps taken by kernel when a child process exits<br />

if parent process has called a wait, <strong>the</strong>n <strong>the</strong> parent is notified else <strong>the</strong> terminating process is marked as a<br />

zombie process (kernel releases resources but keeps its exit status)<br />

- If parent process terminates before child process <strong>the</strong>n parent process ID is set to 1.<br />

118

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!