Göm menyn
IT-Programmet, Tema 1 i termin 4:

TTIT61 Processprogrammering och Operativ System

/Concurrent Programming and Operating Systems/


Vinjett 2

Kritiska sektioner


I ett multitrådat program används strtok från flera trådar, och problem uppstår ibland.

Följande är ett utdrag ur den text man får med man strtok:


C Library Functions                                    string(3C)

NAME
     string, strcasecmp, strncasecmp,  strcat,  strncat,  strchr,
     strrchr,  strcmp, strncmp, strcpy, strncpy, strcspn, strspn,
     strdup, strlen, strpbrk, strstr, strtok, strtok_r,  - string
     operations

SYNOPSIS
     #include <strings.h>
     #include <string.h>

     /...snipp - bort med saker som inte berör strtok.../

     char *strtok(char *s1, const char *s2);

     char *strtok_r(char *s1, const char *s2, char **lasts);

DESCRIPTION
     The arguments s, s1, and s2  point  to  strings  (arrays  of
     characters  terminated  by  a null character). The strcat(),
     strncat(), strcpy(),  strncpy(),  strtok(),  and  strtok_r()
     functions all alter their first argument. These functions do
     not check for overflow of the array pointed to by the  first
     argument.

     /...snipp - bort med lite mer.../

  strtok()
     The strtok() function  can  be  used  to  break  the  string
     pointed to by s1 into a sequence of tokens, each of which is
     delimited by one or more characters from the string  pointed
     to  by  s2. The strtok() function considers the string s1 to
     consist of a sequence of zero or more text tokens  separated
     by spans of one or more characters from the separator string
     s2. The first call (with pointer  s1  specified)  returns  a
     pointer  to the first character of the first token, and will
     have written a null character into s1 immediately  following
     the returned token. The function keeps track of its position
     in the string between separate  calls,  so  that  subsequent
     calls  (which  must  be made with the first argument being a
     null pointer) will work through the  string  s1  immediately
     following that token. In this way subsequent calls will work
     through the string s1 until no tokens remain. The  separator
     string  s2 may be different from call to call. When no token
     remains in s1, a null pointer is returned.

  strtok_r()
     The  strtok_r()  function  has  the  same  functionality  as
     strtok() except that a pointer to a string placeholder lasts
     must be supplied by the caller.  The  lasts  pointer  is  to
     keep  track of the next substring in which to search for the
     next token.

ATTRIBUTES
     See attributes(5) for descriptions of the  following  attri-
     butes:
     ____________________________________________________________
    |       ATTRIBUTE TYPE        |       ATTRIBUTE VALUE       |
    |_____________________________|_____________________________|
    | MT-Level                    | See NOTES below.            |
    |_____________________________|_____________________________|

SEE ALSO
     malloc(3C), setlocale(3C), strxfrm(3C), attributes(5)

NOTES
     The strtok_r() function is as proposed in the POSIX.4a Draft
     #6 document, and is subject to change to be compliant to the
     standard when it is accepted.

     When compiling multithreaded applications,  the   _REENTRANT
     flag  must be defined on the compile line.  This flag should
     only be used in multithreaded applications.

     All of these functions assume the default locale ``C.''  For
     some  locales,  strxfrm()  should  be applied to the strings
     before they are passed to the functions.

     The strcasecmp(), strcat(),  strchr(),  strcmp(),  strcpy(),
     strcspn(),      strdup(),      strlen(),      strncasecmp(),
     strncat(),strncmp(),     strncpy(),     strpbrk(),strrchr(),
     strspn(),   and  strstr()  functions  are  MT-Safe  in  mul-
     tithreaded applications.

     The strtok() function is Unsafe  in  multithreaded  applica-
     tions.  The strtok_r() function should be used instead.

C-koden som används i trådarna för att initialisera en tabell av strängar ser (i princip) ut så här:

  char* words = "stina olle kalle bertil janne kajsa elin nisse anna";
  char* table[64];
  char* token = strtok(words, " ");
  int i = 0;
  while (token && i<64) {
     table[i++] = token;
     token = strtok(null, " ");
  }

TTIT61
Temamål
Temaplan
Schema
Examination
Referenslitteratur
Personal
Register for labs

Föreläsningarna
Programexempel
Forum
Labresultat

Schemaläggning
Kritiska sektioner
Processorstöd för operativsystem
Sekundärminne
UNIX, WinNT
Säkerhet

Intro: C/make
Intro: installation
Threads and synchronisation
System calls
Execution of user programs
File system

Lesson 1
Lesson 2
Lesson 3

C/C++ OH
C/C++ tutorial
C pointers tutorial
Pintos documentation
Memory Issues in Pintos
Pintos on-line documentation
The gnu DDD documentation
DDD tutorial
Debugging topics
Programing with threads

Guidelines for writine and changing source code
Pintos source code

Sidansvarig: Sergiu Rafiliu
Senast uppdaterad: 2011-09-12