/
Unix System Calls and Posix Threads Unix System Calls and Posix Threads

Unix System Calls and Posix Threads - PowerPoint Presentation

stefany-barnette
stefany-barnette . @stefany-barnette
Follow
344 views
Uploaded On 2019-11-22

Unix System Calls and Posix Threads - PPT Presentation

Unix System Calls and Posix Threads Overview Processrelated Unix system calls Posix threads 2 Processrelated Unix System Calls A process in U nix consists of an address space and one thread Unix provides several processrelated system calls ID: 767025

parent process wait system process parent system wait pthread child thread call code fork mutex amp exit threads space

Share:

Link:

Embed:

Download Presentation from below link

Download Presentation The PPT/PDF document "Unix System Calls and Posix Threads" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.


Presentation Transcript

Unix System Calls and Posix Threads

Overview Process-related Unix system calls Posix threads 2

Process-related Unix System Calls A process in Unix consists of an address space and one threadUnix provides several process-related system calls: getpid (), getppid()Get unique id for process, for parent processPid identifies both address space and threadfork(), execv()Create new processesexit(), wait()Terminate processes and synchronize with terminating processkill(), sigaction()Communicate with another process via signals 3

Fork System Call fork() system call creates a new processThe original process is called the parent process The new process is called the child of the parent process Child process is an identical copy of the parent 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

Fork After fork system call returns: Both parent and child process start executing the instruction after forkTo distinguish the two processes, fork returns PID of child in parent process, and 0 in child process Parent and child run code and modify data completely independently, how? What does this code print? 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; }printf(“n = %d\n”, n); 5

Crazy Fork What output does this code produce? 6 int n = 5; int pid = fork(); if ( pid == 0) { n = n + 1; if (fork()) { n = n + 1; }} else { n = n - 1; if (fork()) { n = n - 1; }} printf("n = %d\n", n);

Execv System Call execv() call allows running a new programFork 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, i.e., current process exits and new program starts 7 stack text data Address space SP PC After execve stack text data Address space SP PC Before execve

Execv On execv system call:New program is loaded from executable file on disk into current program’s address space Code and data regions are copied from diskStack is initialized with activation frame of main()Processor registers are reinitializedNew program’s process ID is the same as old program’s process IDexecv() 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 8

Exit System Call A process can terminate itself by calling the exit() system callOn exit( retval ) system call: Address space of the process is destroyed (memory for code, data, etc., regions is reclaimed)Process-specific OS state, e.g., open files, is destroyedretval is saved, can be returned to parent processWhy is this useful?Thread state is destroyedWhen is this done? 9

Wait System Call A parent process can wait for a child process to terminate by calling the wait() system callWhen child issues exit(retval ), wait() returns retval What happens if child exits before parent issues wait?What happens if parent never issues wait()? 10

Wait Wait needs to handle 4 cases Parent issues wait, before or after child’s exit (case 1, 2)Parent doesn’t issue wait, exits after or before child’s exit (case 3, 4) W: wait starts, C: wait continues, E: exit starts, D: exit done 11 Parent Child E E D Parent Child W C E D Parent Child W CE D Parent Child EED

Kill and Sigaction System CallsUnix 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 When the Receiver process runs the next time, instead of performing its normal execution, it starts executing a function called a signal handlerWhen signal handler finishes, normal execution continuesThe sigaction() system call allows a process to setup the signal handler functionWhen no handler is setup, receiver process is forced to exitReceiver process exits when it is scheduled to run next, why? 12

Code Examples Read the man pages of the Unix system calls if you have trouble understanding their semanticsOn google: man fork Some code examples are available on the web site shell.c A simple shell program in Unixunix-signal.cA program that shows how signals are used in Unix 13

Posix Threads Recall that a Unix process consists of an address space with one thread Within main(), this thread is running POSIX Threads or the pthreads system calls allow creating additional threads in a Unix process 14

Posix Threads API pthread_create (thread, attr , start_routine , arg)Returns new thread ID in threadExecutes function specified by start_routine(arg) pthread_exit (status) Terminates current thread, returns status to a joining threadpthread_join(thread_id, status)Blocks thread until thread specified by thread_id terminatesReturn value from pthread_exit is passed in statuspthread_yield()Thread gives up processor and enters the run queue 15

Posix Threads Synchronization API Pthreads provides mutex, semaphores, and condition variables Mutex Semaphores 16 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);

Posix Monitor Example Say a thread wishes to wait until x > y Another thread signals when x > y 17 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);

Posix Code Examples Some posix code examples are available on the web site pthread-example.cSeveral threads are created and run without synchronizationpthread-example-sync.c Several threads are created and run with synchronization 18

Summary Unix OS provides various system calls for managing processes fork creates a new clone processexecv loads a new program in an existing processexit ends a process wait allows a parent process to wait for a child’s exit kill and sigaction are used to send signals to processes, similar to how device controllers interrupt the CPUPthreads is a standardized API for creating and managing threads in Unix OSes19

Think Time What happens when the various system calls we have discussed fail (due to some error)? 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? 20