Download presentation
Presentation is loading. Please wait.
Published byGeorgiana Griffith Modified over 9 years ago
1
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Unix System Calls and Posix Threads
2
Overview Process-related Unix system calls Posix threads 2
3
Process-related Unix System Calls A process in Unix consists of an address space and one thread Unix provides several process-related system calls: o getpid(), getppid() Get unique id for process, for parent process Pid identifies both address space and thread o fork(), execv() Create new processes o exit(), wait() Terminate processes and synchronize with terminating process o kill(), sigaction() Communicate with another process via signals 3
4
Fork System Call fork() system call creates a new process o The original process is called the parent process o The new process is called the child of the parent process Child process is an identical copy of the parent o Thread state (registers) and address space are copied 4 stack text data Address space SP PC Child stack text data Address space SP PC Parent
5
Fork After fork system call returns: o Both parent and child process start executing the instruction after fork o To distinguish the two processes, fork returns PID of child in parent process, and 0 in child process o Parent and child run code and modify data completely independently, how? Why the weird call? int n = 5; int pid = fork(); if (pid == 0) { // run child code n = n + 1; } else { // pid value > 0 // run parent code n = n - 1; } 5
6
Execv System Call execv() call allows running a new program o Fork creates a new process that is a copy of its parent, but it doesn’t allow running a new program When process calls execv, new program replaces current process 6 stack text data Address space SP PC After execve stack text data Address space SP PC Before execve
7
Execv On execv system call: o New program is loaded from executable file on disk into current program’s address space o Code and data regions are copied from disk o Stack is initialized with activation frame of main() o Processor registers are reinitialized o New program’s process ID is the same as old program’s process ID o execv() does not return, why? char *cmd = “/bin/ls”; char *arg1 = “-l”; char *args[3]; args[1] = arg1; args[2] = NULL; execv(cmd, args); // code doesn’t execute 7
8
Exit System Call A process can terminate itself by calling the exit() system call On exit(retval) system call: o Address space of the process is destroyed (memory for code, data, etc., regions is reclaimed) o Process-specific OS state, e.g., open files, is destroyed o retval is saved, can be returned to parent process Why is this useful? o Thread state is destroyed When is this done? 8
9
Wait System Call A parent process can wait for a child process to terminate by calling the wait() system call o When child issues exit(retval), wait() returns retval o What happens if child exits before parent issues wait? o What happens if parent never issues wait()? 9
10
Wait Wait needs to handle 4 cases o Parent issues wait before or after child’s exit o Parent doesn’t issue wait, exits before or after child’s exit W: wait, C: continue, E: exit, D: destroy 10 ParentChild W CE D ParentChild W C E D ParentChild E E D ParentChild E E D
11
Kill and Sigaction System Calls Unix allows processes to send signals (or messages) to itself or to other processes by calling the kill() system call Receiver process handles signals similar to interrupts o Receiver process is immediately interrupted o It starts executing a function called a signal handler o When signal handler finishes, normal execution continues The sigaction() system call allows a process to setup the signal handler function o When no handler is setup, receiver process is forced to exit o Receiver process exits when it is scheduled to run next, why? 11
12
Code Examples Read the man pages of the Unix system calls if you have trouble understanding their semantics o On google: man fork Some code examples are available on the web site shell.c o A simple shell program in Unix unix-signal.c o A program that shows how signals are used in Unix 12
13
Posix Threads Recall that a Unix process consists of an address space with one thread o Within main(), this thread is running POSIX Threads or the pthreads system calls allow creating additional threads in a Unix process 13
14
Posix Threads API pthread_create(thread, attr, start_routine, arg) o Returns new thread ID in thread o Executes function specified by start_routine ( arg ) pthread_exit(status) o Terminates current thread, returns status to a joining thread pthread_join(thread_id, status) o Blocks thread until thread specified by thread_id terminates o Return value from pthread_exit is passed in status pthread_yield() o Thread gives up processor and enters the run queue 14
15
Posix Threads Synchronization API Pthreads provides mutex, semaphores, and condition variables Mutex Semaphores 15 sem_t sem_name; sem_init(&sem_name, 0, 0); /* 2nd arg is flag, 3rd arg is init value */ sem_wait(&sem_name); /* wait operation */ sem_post(&sem); /* signal operations */ pthread_mutex_t mut = PTHREAD_MUTEX_INITIALIZER; pthread_mutex_lock(&mut); pthread_mutex_unlock(&mut);
16
Posix Monitor Example Say a thread wishes to wait until x > y Another thread signals when x > y 16 pthread_mutex_lock(&mut); /* modify x and y */ if (x > y) pthread_cond_signal(&cond); pthread_mutex_unlock(&mut); int x,y; pthread_mutex_t mut = PTHREAD_MUTEX_INITIALIZER; pthread_cond_t cond = PTHREAD_COND_INITIALIZER; pthread_mutex_lock(&mut); while (x <= y) { pthread_cond_wait(&cond, &mut); } /* operate on x and y */ pthread_mutex_unlock(&mut);
17
Posix Code Examples Some posix code examples are available on the web site pthread-example.c o Several threads are created and run without synchronization pthread-example-sync.c o Several threads are created and run with synchronization 17
18
Summary Unix OS provides various system calls for managing processes o fork creates a new clone process o execv loads a new program in an existing process o exit ends a process o wait allows a parent process to wait for a child’s exit o kill and sigaction are used to send signals to processes, similar to how device controllers interrupt the CPU Pthreads is a standardized API for creating and managing threads in Unix OSes 18
19
Think Time What happens when the various system calls we have discussed fail (due to some error)? How would you write a Process A that waits for the exit of another arbitrary Process B (not just a child process), using the system calls we have discussed? Can Process A poll to check if Process B has exited? How will you ensure that Process A does not miss the exit event? Think about how the OS handles the various wait scenarios, i.e., how is the synchronization between the parent and the child implemented in the four cases? What are the steps involved in sending and receiving signals? 19
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.