Unix processes and threads � Processes � fork() CSCE 515: � wait() & waitpid() Computer Network Programming ------ Processes vs. Threads � Threads � threads vs. processes Wenyuan Xu � synchronization Department of Computer Science and Engineering University of South Carolina CSCE515 – Computer Network Programming What is a process? Process A � A program in execution � context (the information/data) Code maintained for an executing program. Global Processes Variables � What makes up a process? � program code Stack � machine registers � global data � stack � open files (file descriptors) � an environment (environment Heap variables; credentials for security) CSCE515 – Computer Network Programming 5 State Model – More realistic Some of the Context Information dispatch admit Process ID ( pid ) � getpid() New Ready Running Process A � unique integer time-out � Parent process ID ( ppid ) getppid() Real User ID Code � event release wait ID of user/process which started this process � � Effective User ID Global � ID of user who wrote the process’ program Blocked Current directory � Exit Variables � File descriptor table � Environment VAR=VALUE pairs � � New : The process is being created. Pointer to program code � Stack Pointer to data � � Running : Instructions being executed. � Memory for global vars � Pointer to stack � Blocked (waiting): Must wait for some event to occur. � Memory for local vars � Ready : Runnable but waiting to be assigned to a processor. Pointer to heap � � Dynamically allocated � Exit (terminate): The process has finished execution. � Execution priority Heap � Signal information CSCE515 – Computer Network Programming CSCE515 – Computer Network Programming
Create a new process - fork() Unix processes � Creates a child process listenfd=socket(…) � The only way to create a new process in UNIX is bind(listenfd…) by making a copy of the to duplicate an existing process listen(listenfd,LISTENQ); parent process --- an For( ; ;) { exact duplicate. connfd = accept(listenfd, …); if ( (pid = fork())==0) { � Implicitly specifies code, � Parents create children; results in a tree of close(listendf); registers, stack, data, files doit(connfd); processes. close(connfd); � fork() is called once but exit(0); it returns twice } � Who is the ancestor of all processes? } close(connfd); � Return value: � 0: process scheduler ("swapper") system process � 0: return in the child � 1: init process, invoked after bootstrap, mother of all � Non-0: the PID of the newly processes. created process CSCE515 – Computer Network Programming CSCE515 – Computer Network Programming Terminate a process Special Exit Cases � 1) A child exits when its parent is not currently � exit() is called executing wait() � closes open files, sockets � the child becomes a zombie � status data about the child is stored until the parent � releases other resources does a wait() � saves resource usage statistics and exit status in proc structure � 2) A parent exits when 1 or more children are still running � wakeup parent � children are adopted by the system’s initialization � calls switch process ( /etc/init ) � it can then monitor/kill them � Process is in zombie state � Whenever we fork children, we must wait for them to prevent them from becoming zombies! CSCE515 – Computer Network Programming CSCE515 – Computer Network Programming wait Actions A server that collects child listenfd=socket( … ) #include <sys/wait.h> bind(listenfd … ) pid_t wait(int *stat) listen(listenfd,LISTENQ); For( ; ;) { � Return connfd = accept(listenfd, … ); Is the server still � the PID of the terminated child if ( (pid = fork())==0) { //child concurrent? close(listendf); � The termination status of the child doit(connfd); � A process that calls wait() can: close(connfd); exit(0); � suspend (block) if all of its children are still running, or } else { //parent wait( (int *)0 ); � return immediately with the termination status of a printf( “ child finished\n ” ); child, or } } � return immediately with an error if there are no child close(connfd); processes. CSCE515 – Computer Network Programming CSCE515 – Computer Network Programming
Handling SIGCHLD signals A better solution main(int argc, char **argv) void sig_chld(int signo) � When a child terminate, there will be a { listenfd=Socket(…) { SIGCHLD signal Bind(listenfd…) pid_t pid; Listen(listenfd,LISTENQ); int stat; Signal(SIGCHLD, sig_chld); � The parent process should catch For( ; ;) { pid = Wait(&stat); SIGCHLD connfd = accept(listenfd, if (WIFEXITED(stat)) …); printf(“child %d if ( (pid = Fork())==0) { terminated normally\n”, pid) � Within the signal handler, wait should be Close(listendf); doit(connfd); return; called Close(connfd); } exit(0); � To fetch the child exit status, use the } } macros: Close(connfd); } � WIFEXITED: the child exited normally Signal handler Main function � WIFSIGNALED: the child exited by a signal CSCE515 – Computer Network Programming CSCE515 – Computer Network Programming waitpid() waitpid() #include <sys/types.h> � options - #include <sys/wait.h> � WNOHANG pid_t waitpid( pid_t pid , int * status , int opts ) � Return immediately if no child has exited. � waitpid - can wait for a particular child � pid == -1 � Return value � Wait for any child process. � Same behavior which wait( ) exhibits. � The process ID of the child which exited. � pid == 0 � -1 on error; 0 if WNOHANG was used and no child � Wait for any child process whose process group ID is equal to was available. that of the calling process. � pid > 0 � Wait for the child whose process ID is equal to the value of pid . CSCE515 – Computer Network Programming CSCE515 – Computer Network Programming Handling SIGCHLD signals main(int argc, char **argv) void sig_chld(int signo) { listenfd=Socket(…) { Bind(listenfd…) pid_t pid; Listen(listenfd,LISTENQ); int stat; Signal(SIGCHLD, sig_chld); For( ; ;) { while( (pid = Waitpid(-1, Threads &stat, WNOHANG))> 0) connfd = Accept(listenfd, …); printf(“child %d terminated normally\n”, pid) if ( (pid = fork())==0) { Close(listendf); return; doit(connfd); } Close(connfd); exit(0); } } Close(connfd); } Signal handler Main function CSCE515 – Computer Network Programming
Threads vs. Processes fork() Process A Code fork() Process B Creation of a new process using fork is Global Code expensive (time & memory). Variables Global Stack Variables A thread (sometimes called a lightweight process ) does not require lots of memory or startup time. Stack Heap Heap CSCE515 – Computer Network Programming CSCE515 – Computer Network Programming pthread_create() Multiple Threads Multi Threaded Process Process A Each process can include Thread 1 many threads. Code pthread_create() Code Global All threads of a process Variables Global share: Variables Stack � memory (program code, Process A heap and global data) Thread 2 � open file/socket descriptors Stack Stack � signal handlers and signal Stack dispositions � working environment (current directory, user ID, Heap etc.) Heap CSCE515 – Computer Network Programming CSCE515 – Computer Network Programming Thread-Specific Resources POSIX Threads Each thread has it’s own: � Thread variants � Thread ID (integer) � POSIX (pthreads) � Stack, Registers, Program Counter � Sun threads (mostly obsolete) � errno (if not - errno would be useless!) � Java threads Threads within the same process can � We will focus on POSIX Threads - most communicate using shared memory. widely supported threads programming API. Must be done carefully! � Solaris - you need to link with “- lpthread ” CSCE515 – Computer Network Programming CSCE515 – Computer Network Programming
Recommend
More recommend