Notes for Advanced Linux Programming 4. Threads
4. Threads
To use the POSIX standard thread API (pthreads), link libpthread.so
to your program.
4.1. Thread Creation
Each thread in a process is identified by a thread ID,
pthread_t.
The pthread_self function returns the thread ID of the current
thread.
This thread IDs can be compared with the pthread_equal
function.
if (!pthread_equal (pthread_self (),
other_thread))
pthread_join
(other_thread, NULL);
each thread executes a thread function:
void * function(void *)
The pthread_create function creates a new thread.
A pointer to a pthread_t variable: store the thread ID of
the new thread.
A pointer to a thread attribute object. You can pass NULL
to use the default attributes.
A pointer to the thread function. void* (*) (void*)
A thread argument value of type void*.
Compile and link this program:
% cc -o thread-create thread-create.c
–lpthread
A thread exits in two ways.
return from the thread function. The function return value
is the thread return value.
explicitly call pthread_exit. The argument to pthread_exit
is the thread’s return value.
Create a Thread
#include <pthread.h>
#include <stdio.h>
/* Prints x’s to stderr. The parameter is
unused. Does not return. */
void* print_xs (void* unused)
{
while (1)
fputc (‘x’, stderr);
return NULL;
}
/* The main program. */
int main ()
{
pthread_t thread_id;
/* Create a new thread. The new thread will run the print_xs function.
*/
pthread_create (&thread_id, NULL, &print_xs, NULL);
/* Print o’s continuously to stderr. */
while (1)
fputc (‘o’, stderr);
return 0;
}
4.1.1. Passing Data to Threads
Listing 4.2 (thread-create2) Create Two
Threads
#include <pthread.h&g
Ïà¹ØÎĵµ£º
ÐÞ¸Ä/etc/fstab, /etc/rc.d/rc.sysinitµÈϵͳÆô¶¯ÎļþʱÎó²Ù×÷¾Í»áÔì³ÉlinuxÎÞ·¨½øÈ룬ÓÐrescueÅÌ¿ÉÒÔÈÝÒ×ÐÞ¸´£¬Õâ¸ö·½·¨ÊʺÏÓÚûÓÐrescueÅÌʱÐÞ¸´ÏµÍ³¡£
grub²Ëµ¥ÖÐÑ¡Ôñlinux,°´e,e,½øÈë±à¼Ä£Ê½£¬kernel (hd0,0)/vmlinuz root=/dev/hda2.....ÕâÒ»ÐÐ×îºó¼ÓÉÏinit=/bin/bash,Ð޸ĺúó°´enter,bÒýµ¼Ð޸ĺóµÄÒýµ¼ÐÅÏ¢ÀàËÆÈçÏ ......
Linux LVM µÄʹÓÃÏê½â
ÕªÒª£º LinuxÓû§°²×°Linux²Ù×÷ϵͳʱÓöµ½µÄÒ»¸ö×î³£¼ûµÄÄÑÒÔ¾ö¶¨µÄÎÊÌâ¾ÍÊÇÈçºÎÕýÈ·µØ¸øÆÀ¹À¸÷·ÖÇø´óС£¬ÒÔ·ÖÅäºÏÊʵÄÓ²Å̿ռ䡣¶øÓöµ½³öÏÖij¸ö·ÖÇø¿Õ¼äºÄ¾¡Ê±£¬½â¾öµÄ·½·¨Í¨³£ÊÇʹÓ÷ûºÅÁ´½Ó£¬»òÕßʹÓõ÷Õû·ÖÇø´óСµÄ¹¤¾ß(±ÈÈçPatition MagicµÈ)£¬µ«Õâ¶¼Ö»ÊÇÔÝʱ½â¾ö°ì·¨£¬Ã»Óиù±¾½â¾öÎÊÌâ¡£Ëæ×ÅL ......
FreeBSDÊÇÒ»¸öÍêÕûµÄ²Ù×÷ϵͳ£¬°üº¬ÁË´Ó¿ª·¢¹¤¾ßµ½¸÷ÖÖ¸÷ÑùµÄÓ¦ÓóÌÐò¡£
ĿǰÈËÃÇÈÏΪFreeBSDÔÚÎȶ¨ÐÔºÍÍøÂçÔË×÷ÉϵÄÐÔÄÜÒªÓÅÓÚLinux¡£
ËüÓÉÒ»¸öÈí¼þ¿ª·¢µÄºËÐÄÍŶÓÀ´Î¬»¤£¬Õû¸öÔʼ³ÌÐò´úÂë»áÓÐ×éÖ¯µØ½øÐиüУ¬ËùÒÔ³ÌÐò´úÂë±È½ÏÓÐÒ»ÖÂÐÔ¡£
ÓÉÓÚÈËÃǶÔFreeBSDµÄÈÏʶ±È½ÏÉÙ£¬Ê¹Ó÷¶Î§Ò²±È½ÏС£¬µ¼ÖÂÁËËüÔÚ¶ÔһЩвúÆ· ......
“Sudo”ÊÇUnix/Linuxƽ̨ÉϵÄÒ»¸ö·Ç³£ÓÐÓõŤ¾ß£¬ËüÔÊÐíϵͳ¹ÜÀíÔ±·ÖÅ䏸ÆÕͨÓû§Ò»Ð©ºÏÀíµÄ“ȨÀû”£¬ÈÃËûÃÇÖ´ÐÐһЩֻÓг¬¼¶Óû§»òÆäËûÌØÐíÓû§²ÅÄÜÍê³ÉµÄÈÎÎñ£¬±ÈÈ磺ÔËÐÐһЩÏñmount£¬halt£¬suÖ®ÀàµÄÃüÁ»òÕß±à¼Ò»Ð©ÏµÍ³ÅäÖÃÎļþ£¬Ïñ/etc/mtab£¬/etc/samba/smb.confµÈ¡£ÕâÑùÒÔÀ´£¬¾Í²»½ö¼õÉÙÁ ......