Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Processes and threads


Published on

This is the twelfth set of slightly updated slides from a Perl programming course that I held some years ago.
I want to share it with everyone looking for intransitive Perl-knowledge.
A table of content for all presentations can be found at
The source code for the examples and the presentations in ODP format are on

Published in: Technology, Education

Processes and threads

  1. 1. Perl Programming Course Processes and threadsKrassimir
  2. 2. Contents1. What is a process2. What is a thread3. Comparison4. Threads • Threaded Program Models • Creating and identifying Threads • Thread Management • Sharing Data • Synchronization and control
  3. 3. Contents5. Processes • fork • pipe • open • ...
  4. 4. What is a process• process • An instance of a running program • Two or more separate processes could be running the same program independently at the same time • A computer program itself is just a passive collection of instructions, while a process is the actual execution of those instructions
  5. 5. What is a thread• thread • a thread is a flow of control through a program with a single execution point • there can be several threads of execution within a process • multiple threads share the same program code, operating system resources and operating system permissions as the process they belong to
  6. 6. Comparison• Thread versus process
  7. 7. Comparison• Processes • run independently and do not share resources • the fork() system call in UNIX causes creation of a new process • on Windows it is emulated by using threads • The return value from fork() is used to distinguish the parent from the child • the parent receives the childs process id, but the child receives zero
  8. 8. Comparison• Processes (2) • The new process (child process) is an exact copy of the calling process (parent process) except for the following: • The child process has a unique process ID • The child process has a different parent process ID (i.e., the process ID of the parent process) • The child process has its own copy of the parents descriptors
  9. 9. Comparison• Threads • A thread is an entity within a process that consists of the schedulable part of the process • Creating new threads is faster • Thread creation induces a peer relationship between all the threads of a process
  10. 10. Comparison• Threads (2) • All threads can share • the parent process ID • the process memory • the process data • the process permissions • the Table with opened files
  11. 11. Comparison• Threads (2) • Every thread has its own • thread ID • separate point of execution • thread-local storage
  12. 12. Threaded Program Models• Three basic ways that you can structure a threaded program • Boss/Worker – one boss thread and one or more worker threads • Work Crew – several threads are created that do essentially the same thing to different pieces of data • Pipeline – a task is divided into a series of steps • the results of one step are passed to the thread processing the next step • Each thread does one thing to each piece of data
  13. 13. Creating and identifying Threads• Example BEGIN { use Config; $Config{useithreads} or die(Threads support needed.); } use strict;use warnings; use threads; $|++; while (1){ sleep 1; my $thread_id = threads->self()->tid; threads->create(&a_thread,$thread_id,[localtime]); #OR threads->new(&a_thread,$thread_id,[localtime]); } #...
  14. 14. Thread Management• Waiting For A Thread To Exit • join • waits for a thread to exit, • cleans up after it, • returns any data the thread may have produced while ($i<30){ sleep 1; my $odd = threads->create(&a_thread,[localtime]); print $odd->join(),$/; my $even = threads->new(&a_thread,[localtime]); print $even->join(),$/; $i++; } #...
  15. 15. Thread Management• Ignoring A Thread • detach • the thread runs until its finished • Perl will clean up after it automatically • may not be joined • any return data is lost while ($i<30){ sleep 1; my $odd = threads->create(&a_thread,[localtime]); $odd->detach(); my $even = threads->new(&a_thread,[localtime]); $even->detach(); $i++; } #...
  16. 16. Thread Management• Process and Thread Termination • an action that terminates a process will terminate all running threads. • perl does an exit() when the main thread exits my @threads = (); while ($i<30){ push @threads, threads->new(&a_thread,[localtime]); push @threads, threads->new(&a_thread,[localtime]); $i++; } #uncomment and run again #print $_->join foreach @threads; #...
  17. 17. Sharing Data• by default, no data is shared• all the data associated with the current thread is copied to the new thread, and is subsequently private to that new thread• all happens within the current process my @threads = (); while ($i<30){ push @threads, threads->new(&a_thread,[localtime]); push @threads, threads->new(&a_thread,[localtime]); $i++; } #uncomment and run again #print $_->join foreach @threads; #...
  18. 18. Sharing Data• by default, no data is shared• all the data associated with the current thread is copied to the new thread, and is subsequently private to that new thread• all happens within the current process• use threads::shared and the :shared attribute to share data• only simple values or references to shared variables are allowed
  19. 19. Sharing Data•Race conditions ● caused by unsynchronized access to shared data ● theres no way to be sure that nothing has happened to the shared data between the time you access it and the time you update it ● $a += 5 or $a++ are not guaranteed to be atomic
  20. 20. Synchronization and control• lock • takes a shared variable and puts a lock on it • no other thread may lock the variable until the variable is unlocked by the thread holding the lock • Unlocking happens automatically when the locking thread exits the block that contains the call to the lock() function • blocks the thread until the variable being locked is available • your thread can be sure that no other thread can lock that variable until the block containing the lock exits • does not prevent access to the variable, only lock attempts
  21. 21. Synchronization and control• lock – Example use threads; use threads::shared; my $a :shared = 4; my $b :shared = foo; my $thr1 = threads->create(sub { lock($a); sleep(2); lock($b); $a++; $b .= $a; })->join ; my $thr2 = threads->create(sub { lock($b); sleep(2); lock($a); $a++; $b .= $a; })->join ; print $thr1,$/,$thr2,$/;
  22. 22. Synchronization and control• Queues • A queue is a special thread-safe object that lets you put data in one end and take it out the other without having to worry about synchronization issues • add lists of scalars onto the end with enqueue() • pop scalars off the front of it with dequeue()
  23. 23. Synchronization and control• Semaphores • generic locking mechanism • behave very much like lockable scalars, except that they cant hold data • must be explicitly unlocked • by default, semaphores behave like locks • see also: perlthrtut/Advanced Semaphores
  24. 24. Processes• fork Does a fork(2) system call to create a new process running the same program at the same point • returns the child pid to the parent process, 0 to the child process, or undef if the fork is unsuccessful • file descriptors are shared, while everything else is copied • beginning with v5.6.0, Perl will attempt to flush all files opened for output before forking the child process
  25. 25. Processes• fork – Example #... sub REAPER { 1 until (-1 == waitpid(-1, WNOHANG)); $SIG{CHLD} = &REAPER; } $SIG{CHLD} = &REAPER; print "Server ($0) running on port $port...n"; while (my $connection = $server->accept) { if (my $pid = fork){ handle_connection($connection,$$); } } $server->close();
  26. 26. IPC• pipe READHANDLE,WRITEHANDLE • Opens a pair of connected pipes like the corresponding system call. • Perls pipes use IO buffering, so you may need to set $| to flush your WRITEHANDLE after each command, depending on the application. pipe (READ, WRITE); select WRITE; $| = 1; #...
  27. 27. IPC• system PROGRAM LIST • exactly the same thing as exec LIST , except that a fork is done first, and the parent process waits for the child process to complete • The return value is the exit status of the program as returned by the wait call • see perlfunc/system
  28. 28. IPC• open open(MAIL, "|/usr/lib/sendmail -oi -t") or die "cant fork sendmail: $!"; print MAIL <<EOF; From: $0 To: Subject: blah EOF close(MAIL)
  29. 29. Processes and threads• Ressources • Professional Perl Programming (Chapter 22 – Creating and Managing Processes) • perldoc perlthrtut • • • • perldoc perlipc • perldoc perlfork
  30. 30. Processes and threadsQuestions?