– The child process is a duplicate of the parent process (it has the same program and data as the parent) Process Creation (Cont.) • UNIX examples – fork system call creates new process – execve system call used after a fork to replace the process memory space with a new program.
int main()
{
pid_t pid; /* C Program Forking Separate Process
/* fork another process */
pid = fork();
if (pid < 0) { /* error occurred */
fprintf(stderr, "Fork Failed");
exit(-1);
}
else if (pid == 0) { /* child process */
execlp("/bin/ls", "ls", NULL);
}
else { /* parent process */
/* parent will wait for the child to complete */
wait (NULL);
printf ("Child Complete");
exit(0);
}
}
When does a process get terminated? Process Termination - Process executes last statement and asks the operating system to delete it (exit)
- Output data from child to parent (via wait)
- Process’ resources are de-allocated by operating system
- Parent may terminate execution of children processes (abort)
Threads - Process creation is heavy-weight while thread creation is light-weight
– Threads run within application - Multiple tasks with the application can be implemented by separate threads
- Update display
- Fetch data
- Spell checking
- Answer a network request
- Can simplify code, increase efficiency
- Kernels are generally multithreaded.
Do'stlaringiz bilan baham: |