Title: Ch4%20Linux%20System%20Programming%20%20%20%20
1Ch4 Linux System Programming Process
IPC
- Jianjian SONG
- Software Institute, Nanjing University
- Oct, 2004
2Content
- Process process environment
- Process Control
- Process identifier, fork, exec
- Process relationship
- Signal
- Inter-process communication (IPC)
- Pipe, FIFO
- semaphore, shared memory, message queue
- Daemon
- Thread
31. Process Process Environment
- What is a process?
- Program and process
- Process an address space with one or more
threads executing within that address space, and
the required system resources for those threads.
(SUSv3)
4The startup of a process
- System call fork
- Process resources
- struct task_struct
- System space stack
-
- System call exec
- The entry of C programs
5System stack
6The entry of C programs
- crt0.o
- cc/ld
- main function
- function prototype
- int main(int argc, char argv)
7The termination of a process
- Five ways of terminating a process
- Normal termination
- Return from main function
- Call exit function
- Call _exit function
- Abnormal termination
- Call abort function
- Terminated by a signal
8exit _exit functions
- Function prototype
- include ltstdlib.hgt
- void exit(int status)
- include ltunistd.hgt
- void _exit(int status)
- Exit status
- Difference
- _exit is corresponding to a system call, while
exit is a library function. - _exit terminate a process immediately.
9Exit handler
- atexit function
- Register a function to be called at normal
program termination. - Prototype
- include ltstdlib.hgt
- int atexit(void (function)(void))
- on_exit function
- Example
10(No Transcript)
11The memory map of a C program
- Text segment (code segment)
- Data segment
- Initialized data
- Uninitialized data
- Heap
- Stack
12(No Transcript)
13Command line arguments
- main function
- int main(int argc, char argv)
- Example
- The implementation of echo(1)
- Command line option
- Standard usage
- getopt function
14getopt function
- Prototype
- int getopt(int argc, char const argv, const
char optstring) - extern char optarg
- extern int optind, opterr, optopt
- Question
- getopt(argc, argv, iflr)
- Program example (P104, in BLP)
15Environment variables
- Environment table
- Environment pointer
- extern char environ
16putenv getenv functions
- Get, set or unset an environment variable
- include ltstdlib.hgt
- char getenv(const char name)
- int putenv(char string)
- int setenv(const char name, const char value,
int overwirte) - void unsetenv(const char name)
17Shared object
- Shared object
- Dynamic link
- Advantages and disadvantages
- Example
- How to create a static and shared library?
- How to use (link) a static or shared library?
18Memory allocation
- Allocate and free dynamic memory.
- include ltstdlib.hgt
- void malloc(size_t size)
- void calloc(size_t nmemb, size_t size)
- void free(void ptr)
- void realloc(void ptr, size_t size)
192. Process control
- Process identifier
- System call fork
- The simple synchronization between parent and
child process - The exec function family
- Example
- The implementation of a simple shell
20Process identifier
21fork
- fork create a child process
- include ltsys/types.hgt
- include ltunistd.hgt
- pid_t fork(void)
- returned value
- pid of child (in the current (parent) process),
- 0 (in child process),
- -1 (failure)
22A simple example
include ltstdio.hgt include ltsys/types.hgt include
ltunistd.hgt / fork??????????(??????)/ void
main() printf(Hello, world!\n) fork() pri
ntf(bye\n)
23The usage of fork
- Code example
- if ( (pidfork()) lt 0)
- / error handling /
- else if (pid 0)
- / child /
- else
- / parent /
-
- Program example
- Program8-1 in APUE
24File sharing
- ?????????????????????????????????????????????
25(No Transcript)
26vfork clone functions
- vfork
- include ltsys/types.hgt
- include ltunistd.hgt
- pid_t vfork(void)
- clone
- include ltsched.hgt
- int clone(int (fn)(void ), void child_stack,
int flag, void arg)
27The simple synchronization between parent and
child process
- The relationship between parent and child process
- wait and waitpid functions
28The relationship between parent and child process
- The parent terminates before the child
- Orphan process
- The child terminates before the parent
- SIGCHLD signal
- Handled by wait/waitpid in parent
- Not handled by wait/wait in parent -gt zombie
29wait waitpid functions
- Wait for process termination
- Prototype
- include ltsys/types.hgt
- include ltsys/wait.hgt
- pid_t wait(int status)
- pid_t waitpid(pid_t pid, int status, int
options) - Example
30Race condition
- int main(void)
- pid_t pid
-
- if ( (pid fork()) lt 0)
- err_sys("fork error")
- else if (pid 0)
- charatatime("output cccccccccccc from
child\n") - else
- charatatime("output pppppppppp from
parent\n") -
- exit(0)
31An improvement
- int main(void)
- pid_t pid
- TELL_WAIT()
- if ( (pid fork()) lt 0)
- err_sys("fork error")
- else if (pid 0)
- WAIT_PARENT() / parent goes first /
- charatatime("output cccccccccccc from
child\n") - else
- charatatime("output pppppppppp from
parent\n") - TELL_CHILD(pid)
-
- exit(0)
32The exec family of functions
- Replace the current process image with a new
process image. - ???????????????????
- pid, ppid, uid, gid, working directory, root
directory - euid/egid?
- ????????
33Functions prototypes
- include ltunistd.hgt
- extern char environ
- int execl(const char path, const char arg,
...) - int execlp(const char file, const char arg,
...) - int execle(const char path, const char arg,
..., char const envp) - int execv(const char path, char const argv)
- int execvp(const char file, char const
argv) - include ltunistd.hgt
- int execve(const char filename, char const
argv, char const envp)
34(No Transcript)
35(No Transcript)
36exec and opened file descriptor
- close-on-exec bit of a file descriptor
- Set by fcntl function
- fcntl(fd, F_SETFD, 0) /????, ???????? exec
???? / - fcntl(fd, F_SETFD, 1) /???????? exec ??? /
37Using fork and exec together
- Two ways of using fork
- The parent process duplicates itself, and then
two different pieces of codes are executed in
parent process and child process. - A process want to execute another program.
- Example
- The implementation of system function
- int system(const charcmdstring)
38Example a simple shell
- printf(" ") / print prompt /
- while (fgets(buf, MAXLINE, stdin) ! NULL)
- bufstrlen(buf) - 1 0 / replace
newline with null / - if ( (pid fork()) lt 0 )
- err_sys(fork error)
- else if ( pid 0 ) / child /
- execlp(buf, buf, (char ) 0)
- fprintf(stderr, "couldn't execute s",
buf) - exit(127)
-
- if ( (pid waitpid(pid, status, 0)) lt 0 )
/ parent / - err_sys(waitpid error)
- printf(" ")
393. Process relationship
40Startup login (1)
41(No Transcript)
42Startup login (2)
43Process group session
- Process group
- The set of one or more process(es).
- getpgrp/setpgid functions
- Session
- The set of one or more process group(s).
- setsid function
- Controlling terminal
- Why are they introduced?
- Job control
44Process group session (contd)
45Process group session (contd)
464. Signal
- The concept of signals
- The signal function
- Send a signal
- kill, raise
- The alarm and pause functions
- Reliable signal mechanism
47The concept of signals
- Signal
- Software interrupt
- Mechanism for handling asynchronous events
- Having a name (beginning with SIG)
- Defined as a positive integer (in ltsignal.hgt)
- How to produce a signal
- ????,????,kill(2)??,kill(1)??,????,...
48Signals in Linux/UNIX
?? ??
SIGABRT ??????(??abort???????)
SIGALRM ??(alarm)
SIGFPE ??????(??0,?????)
SIGHUP ????
SIGILL ??????
SIGINT ?????(Clt-C)
SIGKILL ??(????????)
SIGPIPE ????????????
SIGQUIT ?????(Clt-\)
SIGTERM ??(?kill?????????????)
SIGUSR1 ??????
SIGUSR2 ??????
49Signals in Linux/UNIX (contd)
?? ??
SIGSEGV ??????(???)
SIGCHLD ????????
SIGCONT ???????
SIGSTOP ??(????????)
SIGTSTP ?????(Clt-Z)
SIGTTIN ????????????
SIGTTOUT ????????????
50Signal handling
- ????
- ???????
- SIGKILL, SIGSTOP
- ????????
- ????????
- ????
51The signal function
- Installs a new signal handler for the signal with
number signum. - include ltsignal.hgt
- typedef void (sighandler_t)(int)
- sighandler_t signal(int signum, sighandler_t
handler) - (Returned Value the previous handler if
success, SIG_ERR if error) - The handler parameter
- a user specified function, or
- SIG_DEF, or
- SIG_IGN
52The signal function (contd)
- Program example
- static void sig_usr(int)
- int main(void)
-
- if (signal(SIGUSR1, sig_usr) SIG_ERR)
- err_sys("can't catch SIGUSR1")
- if (signal(SIGUSR2, sig_usr) SIG_ERR)
- err_sys("can't catch SIGUSR2")
- for ( )
- pause()
53Send a signal
- kill(2) send signal to a process
- include ltsys/types.hgt
- include ltsignal.hgt
- int kill(pid_t pid, int sig)
- (Returned Value 0 if success, -1 if failure)
- raise(3) send a signal to the current process
- include ltsignal.hgt
- int raise(int sig)
- (Returned Value 0 if success, -1 if failure)
54alarm pause functions
- alarm set an alarm clock for delivery of a
signal - include ltunistd.hgt
- unsigned int alarm(unsigned int seconds)
- (Returned value 0, or the number of seconds
remaining of previous alarm) - pause wait for a signal
- include ltunistd.hgt
- int pause(void)
- (Returned value -1, errno is set to be EINTR)
55alarm pause functions (contd)
- Program example
- The implementation of the sleep function
- unsigned int sleep1(unsigned int nsecs)
-
- if ( signal(SIGALRM, sig_alrm) SIG_ERR)
- return(nsecs)
- alarm(nsecs) / start the timer /
- pause() /next caught signal wakes us
up/ - return(alarm(0) ) /turn off timer, return
unslept time /
56Possible problems
- Problems related to time
- Race condition
- Interrupted system calls
- Reentrancy
57Reliable signal mechanism
- Weakness of the signal function
- Signal block
- signal mask
- Signal set
- sigset_t data type
- Signal handling functions using signal set
- sigprocmask, sigaction, sigpending, sigsuspend
58signal set operations
- include ltsignal.hgt
- int sigemptyset(sigset_t set)
- int sigfillset(sigset_t set)
- int sigaddset(sigset_t set, int signum)
- int sigdelset(sigset_t set, int signum)
- (Return value 0 if success, -1
if error) - int sigismember(const sigset_t set, int signum)
- (Return value 1 if true, 0 if
false)
59sigprocmask function
- ?????(???)???????
- include ltsignal.hgt
- sigprocmask(int how, const sigset_t set,
sigset_t oldset) - (Return Value 0 is success, -1 if failure)
- ??how??????????
- SIG_BLOCK ?set???????????(??)
- SIG_UNBLOCK ????????set????(??)
- SIG_SETMASK ????????set????
- ?? SIGKILL, SIGSTOP
60sigpending function
- ??????????
- include ltsignal.hgt
- sigpending(sigset_t set)
- (Returned Value 0 is success, -1 if failure)
- Example
- critical.c (Prog10-11 in APUE)
61sigaction function
- ?????(???)????????????
- include ltsignal.hgt
- sigaction(int signum, const struct sigaction
act, struct sigaction oldact) - (Returned Value 0 is success, -1 if failure)
- struct sigaction????????
- handler_t sa_handler / addr of signal handler,
or SIG_IGN, or SIG_DEL / - sigset_t sa_mask / additional signals to block
/ - int sa_flags / signal options /
62(No Transcript)
63sigsuspend function
- ?sigmask????????,???????????????????,?????
- include ltsignal.hgt
- sigsuspend(const sigset sigmask)
- (Returned value -1, errno is set to be EINTR)
- sigsuspend?pause
64signal function review
- ?sigaction??signal??
- Sigfunc signal(int signo, handler_t func)
- struct sigaction act, oact
- act.sa_handler func
- sigemptyset(act.sa_mask)
- act.sa_flags 0
- if (signo SIGALRM)
- ifdef SA_INTERRUPT
- act.sa_flags SA_INTERRUPT / SunOS /
- endif
- else
- ifdef SA_RESTART
- act.sa_flags SA_RESTART / SVR4, 44BSD /
- endif
-
- if (sigaction(signo, act, oact) lt 0)
- return(SIG_ERR)
- return(oact.sa_handler)
65Example solution of race condition
- int main(void)
- pid_t pid
- TELL_WAIT()
- if ( (pid fork()) lt 0)
- err_sys("fork error")
- else if (pid 0)
- WAIT_PARENT() / parent goes first /
- charatatime("output cccccccccccc from
child\n") - else
- charatatime("output pppppppppp from
parent\n") - TELL_CHILD(pid)
-
- exit(0)
66- static sigset_t newmask, oldmask, zeromask
- static void sig_usr(int signo) / one handler
for SIGUSR1, SIGUSR2 / - sigflag 1 return
-
- void TELL_WAIT()
- if (signal(SIGUSR1, sig_usr) SIG_ERR)
- err_sys("signal(SIGINT) error")
- if (signal(SIGUSR2, sig_usr) SIG_ERR)
- err_sys("signal(SIGQUIT) error")
- sigemptyset(zeromask)
- sigemptyset(newmask)
- sigaddset(newmask, SIGUSR1)
- sigaddset(newmask, SIGUSR2)
- / block SIGUSR1 and SIGUSR2, and save current
signal mask / - if (sigprocmask(SIG_BLOCK, newmask, oldmask) lt
0) - err_sys("SIG_BLOCK error")
67- void WAIT_PARENT(void)
- while (sigflag 0)
- sigsuspend(zeromask) / and wait for parent
/ - sigflag 0
- / reset signal mask to original value /
- if (sigprocmask(SIG_SETMASK, oldmask, NULL) lt
0) - err_sys("SIG_SETMASK error")
-
- void TELL_CHILD(pid_t pid)
- kill(pid, SIGUSR1) / tell child we're done
/
685. Inter-process communication
- IPC Inter-Process Communication
- IPC mechanisms
- shared file
- signal
- pipe, FIFO (named pipe), message queue,
semaphore, shared memory - socket
69IPC illustrations
70Simple Client-Server or IPC model
71The concept of pipe
- Pipe
- Pipe mechanism in a shell
- e.g. cmd1 cmd2
- Pipe is half-duplex
- ???????????????
- ??????
- ????(FIFO)
72The pipe function
- The pipe function create a pipe
- include ltunistd.hgt
- int pipe(int filedes2)
- (Returned value 0 if success, -1 if failure)
- A pipe First In, First Out
- filedes0read, filedes1 write
73A pipe in a single process
74A pipe between the parent child
75The coordinationof pipe read write
- ????,??PIPE_BUF??????????????
- ????????,
- ????,??????????????,read??0,?????????
- ????,????????SIGPIPE
76Examples
77?????????????
- ??shell????
- cmd1 cmd2
- ??? cmd gt file
- ????
- ??cmd1?
- if (fd1 ! STDOUT_FILENO)
- if (dup2(fd1, STDOUT_FILENO) ! STDOUT_FILENO)
- err_sys(dup2 error to stdout)
-
- ??cmd2?
- if (fd0 ! STDIN_FILENO)
- if (dup2(fd0, STDIN_FILENO) ! STDIN_FILENO)
- err_sys(dup2 error to stdin)
-
78(No Transcript)
79Examples
80pipe Application(1)solution of race condition
- int main(void)
- pid_t pid
- TELL_WAIT()
- if ( (pid fork()) lt 0)
- err_sys("fork error")
- else if (pid 0)
- WAIT_PARENT() / parent goes first /
- charatatime("output cccccccccccc from
child\n") - else
- charatatime("output pppppppppp from
parent\n") - TELL_CHILD(pid)
-
- exit(0)
81- static int pfd2
- void TELL_WAIT()
- if (pipe(pfd) lt 0)
- err_sys("pipe error")
-
- void WAIT_PARENT(void)
- char c
- if (read(pfd0, c, 1) ! 1)
- err_sys("read error")
- if (c ! 'p')
- err_quit("WAIT_PARENT incorrect data")
-
- void TELL_CHILD(pid_t pid)
- if (write(pfd1, "p", 1) ! 1)
- err_sys("write error")
82pipe Application(2) shell
83popen pclose functions
- popen, pclose process I/O
- include ltstdio.hgt
- FILE popen(const char command, const char
type) - int pclose(FILE stream)
84Applications of popen pclose
85Home work popen???
86FIFO named pipe
- ???????
- ???
- ???
- ?????
- ?????????
- mkfifo(1), mkfifo(3), mknod(1), mknod(2)
87??FIFO
- mkfifo make a FIFO special file (a named pipe)
- include ltsys/types.hgt
- include ltsys/stat.hgt
- int mkfifo(const char pathname, mode_t mode)
- (Returned value 0 if success, -1 if failure)
- Examples
- fifo1.c (Ch12 in BLP)
- list a fifo (ls lF)
88?open????FIFO
- Review open system call
- int open(const char pathname, int flags)
- flags parameter
- ?????????
- O_RDONLY, O_WRONLY, O_RDWR
- O_NONBLOCK
- consideration
- ??/??
- ??
89FIFO??????
- ??FIFO????
- ?????(????O_NONBLOCK),???????????????????FIFO???,
??????FIFO????????????????? - ?????O_NONBLOCK,??????????????????,??????????????
?FIFO,??open????? -1,errno??ENXIO? - ??FIFO????
- same as pipe
90????
- shell???fifo???
- cat lt my_fifo
- echo a string to fifo gt my_fifo
- fifo2.c (Ch12 in BLP)
91FIFO???(1)
92FIFO???(2)
- C/S????
- ?client.c, server.c
93System V IPC
- IPC objects
- ???(semaphore set)
- ????(message queue)
- ????(shared memory)
- shell??
- ipcs, ipcrm
94System V IPC?????
- ???????
- ??IPC?????
- ??IPC????????(key_t key)
- key????????IPC_PRIVATEftok??
- ????????????
- ?????
- ?????
- struct ipc_perm
95SV IPC System Calls Overview
?? ???? ??? ????
????IPC??,???IPC??? msgget semget shmget
IPC?? ??/????,?????,??/?????? msgsnd/ msgrcv semop shmat/ shmdt
IPC????/??????,??IPC msgctl semctl shmctl
96Semaphore
97PV??????
- procedure p(var ssamephore)
- s.values.value-1
- if (s.valuelt0) asleep(s.queue)
-
- procedure v(var ssamephore)
- s.values.value1
- if (s.valuelt0) wakeup(s.queue)
-
98Linux/UNIX??????
- semaphore set
- struct semid_ds
- struct ipc_perm sem_perm
- struct sem sem_base / ptr to first sem in
set / - time_t sem_otime / last operation time /
- time_t sem_ctime / last change time /
- ushort sem_nsems / count of sems in set /
99semaphore set system calls
- include ltsys/types.hgt
- include ltsys/ipc.hgt
- include ltsys/sem.hgt
- int semget(key_t key, int nsems, int semflg)
- int semop(int semid, struct sembuf sops,
unsigned nsops) - int semctl(int semid, int semnum, int cmd, ...)
100semget get a semaphore set identifier
- prototype
- int semget(key_t key, int nsems, int semflg)
- key parameter
- ?????IPC_PRIVATE
- ??????
- ftok??
- semflg parameter
- ??????(?9?)
- IPC_CREAT, IPC_EXCL ???
- examples
- sem1.c
101semop semaphore operations
- prototype
- int semop(int semid, struct sembuf sops,
unsigned nsops) - sops parameter
- struct sembuf
- unsigned short int sem_num / semaphore
number / - short int sem_op / semaphore
operation / - short int sem_flg / operation
flag / -
- examples
- PV?????
102semctl semaphore control operations
- prototype
- int semctl(int semid, int semnum, int cmd, ...)
- the 4th argument
- union semun
- int val
- struct semid_ds buf
- unsigned short array
- arg
- cmd parameter
103semctl semaphore control operations (contd)
- cmd parameter
- IPC_STAT ???????????arg.semid_ds???????
- IPC_SET ????????????,??????????????arg.semid_ds??
???? - IPC_RMID ???????
- SETVAL ?????????semnum??????????(??arg.val)
- examples
- set_semvalue(???), del_semvalue(??)
104Examples
105Home work
106Message queue
- ????
- ??????????,??????????????????
- First in, first out
- message type ???
- ???
- struct msqid_ds
107message queue system calls
- include ltsys/types.hgt
- include ltsys/ipc.hgt
- include ltsys/msg.hgt
- int msgget(key_t key, int size, int flag)
- int msgsnd(int msqid, const void ptr, size_t
nbytes, int flag) - int msgrcv(int msqid, void ptr, size_t nbytes,
long type, int flag) - int msgctl(int msqid, int cmd, struct shmid_ds
buf)
108msgctl message control operations
- prototype
- int msgctl(int msqid, int cmd, struct shmid_ds
buf) - cmd parameter
- IPC_STAT ?msqid_ds??????????????????
- IPC_SET ????????????,?????????????msqid_ds??????
- IPC_RMID ??????
109Example
- A C/S application
- One server, several clients only one queue is
required. - Compared with FIFO implementation
110 Shared memory
- ????
- ????????????????????,????(attach)????????,????????
??????? - ??????????
- ?????????
111shared memory system calls
- include ltsys/types.hgt
- include ltsys/ipc.hgt
- include ltsys/shm.hgt
- int semget(key_t key, int size, int flag)
- void shmat(int shmid, void addr, int flag)
- int shmdt(void addr)
- int shmctl(int shmid, int cmd, struct shmid_ds
buf)
112mmap/munmap system calls
- mmap/munmap map or unmap files or devices into
memory
1136. Daemon process
- ???daemon??
- daemon???????
- ????
- ????
114What is a daemon?
115What is a daemon? (contd)
- daemon
- ?????????
- ????, ????????? Shell ???
- why daemon?
116Concepts related to job control
- Process group session
- Controlling terminal
- jobs, fg, bg comands
- SIGINT, SIGQUIT, SIGTSTP, SIGHUP signals
- A solution the problem
- nohup command gtgt logfile
117daemon?????(1)
- ????
- ????fork,??????exit
- ??setsid?????????
- ??????????????
- ???umask??0
- ???????????
118Example
- int daemon_init(void)
- pid_t pid
- if ( (pid fork()) lt 0)
- return(-1)
- else if (pid ! 0)
- exit(0) / parent goes bye-bye /
- / child continues /
- setsid() / become session leader /
- chdir("/") / change working directory /
- umask(0) / clear our file mode creation mask
/ - return(0)
119daemon?????(2)
1207. Threads
- What is a thread
- Basic pthread functions
- Thread synchronization
- Thread attributes
- Thread cancellation
121What is a thread?
- Thread
- A single flow of control within a process.
(susv3) - Process and thread
- New process when a process executes a fork call,
a new copy of the process is created with its own
variables and its own PID. - New thread When we create a new thread in a
process, the new thread of execution gets its own
stack (and hence local variables) but shares
global variables, file descriptors, signal
handlers, and its current directory state with
the process that created it.
122Process and thread
123POSIX thread
- POSIX1003.1c
- pthread library
- /usr/lib/libpthread.so, /usr/lib/libpthread.a
- pthread.h header file
- /usr/include/pthread.h
- Compiler options
- gcc thread.c o thread -lpthread
124Basic thread functions(1)
- Create a new thread
- include ltpthread.hgt
- int pthread_create(pthread_t thread,
pthread_attr_t attr, void (start_routine)(void
), void arg) - Terminate the calling thread
- void pthread_exit(void retval)
125Basic thread functions(2)
- Wait for termination of another thread
- include ltpthread.hgt
- int pthread_join(pthread_t th, void
thread_return) - Put a running thread in the detached state
- int pthread_detach(pthread_t th)
126Thread synchronization
- ????????
- ????????
- ?????????
127POSIX4 semaphore
- include ltsemaphore.hgt
- int sem_init(sem_t sem, int pshared, unsigned
int value) - int sem_wait(sem_t sem)
- int sem_post(sem_t sem)
- int sem_destroy(sem_t sem)
- int sem_trywait(sem_t sem)
- int sem_getvalue(sem_t sem, int sval)
128Example
- Producer-consumer problem
- thread4.c, thread4a.c (in Ch11, BLP)
129Mutex (Mutual Exclusion)
- include ltpthread.hgt
- int pthread_mutex_init(pthread_mutex_t mutex,
const pthread_mutexattr_t mutexattr) - int pthread_mutex_lock(pthread_mutex_t mutex)
- int pthread_mutex_unlock(pthread_mutex_t mutex)
- int pthread_mutex_destroy(pthread_mutex_t
mutex) - int pthread_mutex_trylock(pthread_mutex_t mutex)
130Example
131Thread attributes
- ??????
- pthread_attr_t
- ???
- include ltpthread.hgt
- int pthread_attr_init(pthread_attr_t attr)
- get/set???
132Get/Set thread attributes
- include ltpthread.hgt
- int pthread_attr_setdetachstate(pthread_attr_t
attr, int detachstate) - int pthread_attr_getdetachstate(const
pthread_attr_t attr, int detachstate) - int pthread_attr_setschedpolicy(pthread_attr_t
attr, int policy) - int pthread_attr_getschedpolicy(const
pthread_attr_t attr, int policy) - int pthread_attr_setschedparam(pthread_attr_t
attr, int param) - int pthread_attr_getschedparam(const
pthread_attr_t attr, int param)
133Example(1) ????-????
- detachstate??
- Values
- PTHREAD_CREATE_JOIN / PTHREAD_CREATE_DETACHED
- thread6.c
134Example(2) ????-??
- schedpolicy??
- ????
- Values
- SCHED_OTHER/SCHED_RR/SCHED_FIFO
- schedparam??
- ????,??????
- thread7a.c
135Thread cancellation
- include ltpthread.hgt
- int pthread_cancel(pthread_t thread)
- int pthread_setcancelstate(int state, int
oldstate) - int pthread_setcanceltype(int type, int oldtype)
136Multithread program
- Examples
- thread9.c, thread9a.c (in BLP)
137Review