⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 posix threads programming.htm

📁 可以自学操作系统全部课程
💻 HTM
📖 第 1 页 / 共 5 页
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- saved from url=(0077)http://www.llnl.gov/computing/tutorials/workshops/workshop/pthreads/MAIN.html -->
<HTML><HEAD><TITLE>POSIX Threads Programming</TITLE>
<META http-equiv=Content-Type content="text/html; charset=gb2312">
<SCRIPT language=JavaScript1.2 
src="POSIX Threads Programming.files/jslib_llnl01.js"></SCRIPT>
<LINK href="POSIX Threads Programming.files/workshop.css" type=text/css 
rel=StyleSheet>
<META content="MSHTML 6.00.2800.1400" name=GENERATOR></HEAD>
<BODY><BASEFONT><!-- default font size --></FONT>
<FORM><!-- required for interactive buttons --><A name=top></A>
<SCRIPT language=JavaScript1.2>addNavigation()</SCRIPT>

<P><BR><IMG height=107 src="POSIX Threads Programming.files/title.gif" width=346 
border=0> 
<P><BR><A name=TOC></A><SPAN class=heading2>Table of Contents </SPAN>
<P>
<OL>
  <P>
  <LI><A 
  href="http://www.llnl.gov/computing/tutorials/workshops/workshop/pthreads/MAIN.html#Overview">Pthreads 
  Overview</A> 
  <OL>
    <LI><A 
    href="http://www.llnl.gov/computing/tutorials/workshops/workshop/pthreads/MAIN.html#Thread">What 
    is a Thread?</A> 
    <LI><A 
    href="http://www.llnl.gov/computing/tutorials/workshops/workshop/pthreads/MAIN.html#Pthread">What 
    are Pthreads?</A> 
    <LI><A 
    href="http://www.llnl.gov/computing/tutorials/workshops/workshop/pthreads/MAIN.html#WhyPthreads">Why 
    Pthreads?</A> 
    <LI><A 
    href="http://www.llnl.gov/computing/tutorials/workshops/workshop/pthreads/MAIN.html#Designing">Designing 
    Threaded Programs</A> </LI></OL>
  <LI><A 
  href="http://www.llnl.gov/computing/tutorials/workshops/workshop/pthreads/MAIN.html#PthreadsAPI">The 
  Pthreads API</A> 
  <LI><A 
  href="http://www.llnl.gov/computing/tutorials/workshops/workshop/pthreads/MAIN.html#Management">Thread 
  Management</A> 
  <OL>
    <LI><A 
    href="http://www.llnl.gov/computing/tutorials/workshops/workshop/pthreads/MAIN.html#CreatingThreads">Creating 
    Threads</A> 
    <LI><A 
    href="http://www.llnl.gov/computing/tutorials/workshops/workshop/pthreads/MAIN.html#TerminatingThreads">Terminating 
    Thread Execution</A> 
    <LI><A 
    href="http://www.llnl.gov/computing/tutorials/workshops/workshop/pthreads/MAIN.html#CreateExample">Example: 
    Pthread Creation and Termination</A> 
    <LI><A 
    href="http://www.llnl.gov/computing/tutorials/workshops/workshop/pthreads/MAIN.html#PassingArguments">Passing 
    Arguments to Threads</A> 
    <LI><A 
    href="http://www.llnl.gov/computing/tutorials/workshops/workshop/pthreads/MAIN.html#ThreadIdentifiers">Thread 
    Identifiers</A> 
    <LI><A 
    href="http://www.llnl.gov/computing/tutorials/workshops/workshop/pthreads/MAIN.html#Joining">Joining 
    Threads</A> 
    <LI><A 
    href="http://www.llnl.gov/computing/tutorials/workshops/workshop/pthreads/MAIN.html#Detaching">Detaching 
    / Joining Threads</A> 
    <LI><A 
    href="http://www.llnl.gov/computing/tutorials/workshops/workshop/pthreads/MAIN.html#JoiningExample">Example: 
    Joining Threads</A> </LI></OL>
  <LI><A 
  href="http://www.llnl.gov/computing/tutorials/workshops/workshop/pthreads/MAIN.html#Mutexes">Mutex 
  Variables</A> 
  <OL>
    <LI><A 
    href="http://www.llnl.gov/computing/tutorials/workshops/workshop/pthreads/MAIN.html#MutexOverview">Mutex 
    Variables Overview</A> 
    <LI><A 
    href="http://www.llnl.gov/computing/tutorials/workshops/workshop/pthreads/MAIN.html#MutexCreation">Creating 
    / Destroying Mutexes</A> 
    <LI><A 
    href="http://www.llnl.gov/computing/tutorials/workshops/workshop/pthreads/MAIN.html#MutexLocking">Locking 
    / Unlocking Mutexes</A> 
    <LI><A 
    href="http://www.llnl.gov/computing/tutorials/workshops/workshop/pthreads/MAIN.html#MutexExample">Example: 
    Using Mutexes</A> </LI></OL>
  <LI><A 
  href="http://www.llnl.gov/computing/tutorials/workshops/workshop/pthreads/MAIN.html#ConditionVariables">Condition 
  Variables</A> 
  <OL>
    <LI><A 
    href="http://www.llnl.gov/computing/tutorials/workshops/workshop/pthreads/MAIN.html#ConVarOverview">Condition 
    Variables Overview</A> 
    <LI><A 
    href="http://www.llnl.gov/computing/tutorials/workshops/workshop/pthreads/MAIN.html#ConVarCreation">Creating/Destroying 
    Condition Variables</A> 
    <LI><A 
    href="http://www.llnl.gov/computing/tutorials/workshops/workshop/pthreads/MAIN.html#ConVarSignal">Waiting 
    / Signalling on Condition Variables</A> 
    <LI><A 
    href="http://www.llnl.gov/computing/tutorials/workshops/workshop/pthreads/MAIN.html#ConVarExample">Example: 
    Using Condition Variables</A> </LI></OL>
  <LI><A 
  href="http://www.llnl.gov/computing/tutorials/workshops/workshop/pthreads/MAIN.html#LLNL">LLNL 
  Specific Information and Recommendations</A> 
  <LI><A 
  href="http://www.llnl.gov/computing/tutorials/workshops/workshop/pthreads/MAIN.html#Routines">Pthread 
  Library Routines Reference</A> 
  <LI><A 
  href="http://www.llnl.gov/computing/tutorials/workshops/workshop/pthreads/MAIN.html#References">References 
  and More Information</A> 
  <LI><A 
  href="http://www.llnl.gov/computing/tutorials/workshops/workshop/pthreads/exercise.html">Exercise</A> 

  <LI><A onclick="resetTop('../index.html');" href="javascript://">Workshop 
  Home</A> </LI></OL><!---------------------------------------------------------------------------><A 
name=Overview><BR><BR></A><A name=Thread></A>
<TABLE cellSpacing=0 cellPadding=5 width="100%" border=1>
  <TBODY>
  <TR>
    <TD background="POSIX Threads Programming.files/bg2.gif" 
      bgColor=navy><SPAN class=heading1>Pthreads Overview 
  </SPAN></TD></TD></TR></TBODY></TABLE>
<P><SPAN class=heading2>What is a Thread? </SPAN>
<P><BR>
<UL>
  <P>
  <LI>Technically, a thread is defined as an independent stream of instructions 
  that can be scheduled to run as such by the operating system. But what does 
  this mean? 
  <P></P>
  <LI>In the UNIX environment a thread: 
  <UL>
    <LI>Exists within a process and uses the process resources 
    <LI>Has its own independent flow of control as long as its parent process 
    exists and the OS supports it 
    <LI>May share the process resources with other threads that act equally 
    independently (and dependently) 
    <LI>Dies if the parent process dies - or something similar </LI></UL>
  <P></P>
  <LI>To the software developer, the concept of a "procedure" that runs 
  independently from its main program may best describe a thread. 
  <P></P>
  <LI>Understanding what a thread means knowing the relationship between a 
  process and a thread. A process is created by the operating system. Processes 
  contain information about program resources and program execution state, 
  including: 
  <UL>
    <LI>Process ID, process group ID, user ID, and group ID 
    <LI>Environment 
    <LI>Working directory. 
    <LI>Program instructions 
    <LI>Registers 
    <LI>Stack 
    <LI>Heap 
    <LI>File descriptors 
    <LI>Signal actions 
    <LI>Shared libraries 
    <LI>Inter-process communication tools (such as message queues, pipes, 
    semaphores, or shared memory). </LI></UL>
  <P>
  <CENTER><IMG height=406 src="POSIX Threads Programming.files/process.gif" 
  width=450 border=0></CENTER>
  <P></P>
  <LI>Threads use and exist within these process resources, yet are able to be 
  scheduled by the operating system and run as independent entities within a 
  process. 
  <P></P>
  <LI>A thread can possess an independent flow of control and be schedulable 
  because it maintains its own: 
  <UL>
    <LI>Stack pointer 
    <LI>Registers 
    <LI>Scheduling properties (such as policy or priority) 
    <LI>Set of pending and blocked signals 
    <LI>Thread specific data. </LI></UL>
  <P>
  <CENTER><IMG height=398 src="POSIX Threads Programming.files/thread.gif" 
  width=450 border=0></CENTER>
  <P></P>
  <LI>A process can have multiple threads, all of which share the resources 
  within a process and all of which execute within the same address space. 
  Within a multi-threaded program, there are at any time multiple points of 
  execution. 
  <P></P>
  <LI>Because threads within the same process share resources: 
  <UL>
    <LI>Changes made by one thread to shared system resources (such as closing a 
    file) will be seen by all other threads. 
    <LI>Two pointers having the same value point to the same data. 
    <LI>Reading and writing to the same memory locations is possible, and 
    therefore requires explicit synchronization by the programmer. 
</LI></UL></LI></UL><!---------------------------------------------------------------------------><A 
name=Pthread><BR><BR></A>
<TABLE cellSpacing=0 cellPadding=5 width="100%" border=1>
  <TBODY>
  <TR>
    <TD background="POSIX Threads Programming.files/bg2.gif" 
      bgColor=navy><SPAN class=heading1>Pthreads Overview 
  </SPAN></TD></TD></TR></TBODY></TABLE>
<P><SPAN class=heading2>What are Pthreads? </SPAN>
<P><BR>
<UL>
  <P>
  <LI>Historically, hardware vendors have implemented their own proprietary 
  versions of threads. These implementations differed substantially from each 
  other making it difficult for programmers to develop portable threaded 
  applications. 
  <P></P>
  <LI>In order to take full advantage of the capabilities provided by threads, a 
  standardized programming interface was required. For UNIX systems, this 
  interface has been specified by the IEEE POSIX 1003.1c standard (1995). 
  Implementations which adhere to this standard are referred to as POSIX 
  threads, or Pthreads. Most hardware vendors now offer Pthreads in addition to 
  their proprietary API's. 
  <P></P>
  <LI>Pthreads are defined as a set of C language programming types and 
  procedure calls, implemented with a <TT>pthread.h</TT> header/include file and 
  a thread library - though the this library may be part of another library, 
  such as <TT>libc</TT>. 
  <P></P>
  <LI>There are several drafts of the POSIX threads standard. It is important to 
  be aware of the draft number of a given implementation, because there are 
  differences between drafts that can cause problems. </LI></UL><!---------------------------------------------------------------------------><A 
name=WhyPthreads><BR><BR></A>
<TABLE cellSpacing=0 cellPadding=5 width="100%" border=1>
  <TBODY>
  <TR>
    <TD background="POSIX Threads Programming.files/bg2.gif" 
      bgColor=navy><SPAN class=heading1>Pthreads Overview 
  </SPAN></TD></TD></TR></TBODY></TABLE>
<P><SPAN class=heading2>Why Pthreads? </SPAN>
<P><BR>
<UL>
  <P>
  <LI>The primary motivation for using Pthreads is to realize potential program 
  performance gains. 
  <P></P>
  <LI>When compared to the cost of creating and managing a process, a thread can 
  be created with much less operating system overhead. Managing threads requires 
  fewer system resources than managing processes. 
  <P>For example, the following table compares timing results for the 
  <TT><B>fork()</B></TT> subroutine and the <TT><B>pthreads_create()</B></TT> 
  subroutine. Timings reflect 50,000 process/thread creations, were performed 
  with the <TT>time</TT> utility, and units are in seconds. 
  <P>
  <TABLE cellSpacing=0 cellPadding=5 width="90%" border=1>
    <TBODY>
    <TR>
      <TH bgColor=#eeeeee rowSpan=2><SPAN class=heading3>Platform </SPAN>
      <TH bgColor=#eeeeee colSpan=3><SPAN class=heading3><TT>fork() 
        </TT></SPAN>
      <TH bgColor=#eeeeee colSpan=3><SPAN class=heading3><TT>pthread_create() 
        </TT></SPAN>
    <TR>
      <TH bgColor=#eeeeee><SPAN class=heading3>real </SPAN>
      <TH bgColor=#eeeeee><SPAN class=heading3>user </SPAN>
      <TH bgColor=#eeeeee><SPAN class=heading3>sys </SPAN>
      <TH bgColor=#eeeeee><SPAN class=heading3>real </SPAN>
      <TH bgColor=#eeeeee><SPAN class=heading3>user </SPAN>
      <TH bgColor=#eeeeee><SPAN class=heading3>sys </SPAN>
    <TR vAlign=top align=right>
      <TD align=left><B><NOBR>IBM 332 MHz 604e</NOBR></B> <BR><NOBR>4 
        CPUs/node</NOBR> <BR><NOBR>512 MB Memory</NOBR> <BR><NOBR>AIX 4.3</NOBR> 

      <TD>92.4 
      <TD>2.7 
      <TD>105.3 
      <TD>8.7 
      <TD>4.9 
      <TD>3.9 
    <TR vAlign=top align=right>
      <TD align=left><B><NOBR>IBM 375 MHz POWER3</NOBR></B> <BR><NOBR>16 
        CPUs/node</NOBR> <BR><NOBR>16 GB Memory</NOBR> <BR><NOBR>AIX 5.1</NOBR> 
      <TD>173.6 
      <TD>13.9 
      <TD>172.1 
      <TD>9.6 
      <TD>3.8 
      <TD>6.7 
    <TR vAlign=top align=right>
      <TD align=left><B><NOBR>INTEL 2.2 GHz Xeon</NOBR></B> <BR><NOBR>2 
        CPU/node</NOBR> <BR><NOBR>2 GB Memory</NOBR> <BR><NOBR>RedHat Linux 
        7.3</NOBR> 
      <TD>17.4 
      <TD>3.9 
      <TD>13.5 
      <TD>5.9 
      <TD>0.8 
      <TD>5.3 </TR></TBODY></TABLE><A onclick=blur() 
  href="http://www.llnl.gov/computing/tutorials/workshops/workshop/pthreads/fork_vs_thread.txt" 
  target=W2><IMG height=20 alt="View source code" 
  src="POSIX Threads Programming.files/source1.gif" width=65 border=0> 
  fork_vs_thread.txt</A> 
  <P></P>
  <LI>All threads within a process share the same address space. Inter-thread 
  communication is more efficient and in many cases, easier to use than 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -