#include <pthread.h>
int pthread_join (pthread_t thread, void **status_addr);
pthread_join , || , , . ϳ|| , || status_addr, , , thread || || , pthread_exit(). , , NULL.
0 . | | ( ||, !), , <errno.h>. errno .
struct char_print_params
{
char character;
int count;
};
void *char_print(void *params)
{
struct char_print_params *p = (struct char_print_params *) params;
int i;
for (i = 0; i < p->count; i++)
fputc(p->character, stderr);
return NULL;
}
int main(int argc, char *argv[])
{
pthread_t thread1_id, thread2_id;
struct char_print_params thread1_args, thread2_args;
thread1_args.character = 'x';
thread1_args.count = 2000;
pthread_create(&thread1_id, NULL, &char_print, &thread1_args);
thread2_args.character = 'o';
thread2_args.count = 2000;
pthread_create(&thread2_id, NULL, &char_print, &thread2_args);
pthread_join(thread1_id, NULL);
pthread_join(thread2_id, NULL);
fprintf(stderr, "\n");
return 0;
}
䳿
Unix/Linux || 䳿 . (pipe, - ||) || |,| ||. , , . , ||, , || -, ' -, || . |,| , || ||, , .
Unix/Linux | - . || || .
|',| || 䳿 ' - . :
int pipe(int fd[2]);|| |',| . , || . fd[0] |',| || , fd[1] - . , || , , , . , || , - || . , , , || , || || , || . , , , - . ϳ|| || || :
|
|
- , - ', || || , , - . ( -1 - ).
|,| ||. ||, || || || . || ||, '|| '|| , .
:
int close(int pipe_fd);, , , . , , , "" , read.
Linux
Linux ( UNIX || FIFO) || 䳿 || || . ', . , . :
int mknod(char *name, int mode, int dev);|| || , . name |',| , || ' (' |,|). mode . 7 ( : S_IFIFO, 9 "rwx" ( ), ( ), ( ). , , , || , mode S_IFIFO|0x140, , -- - S_IFIFO|0x1B6. (, "x " .) || 0.
|
|
|| :
int open(int *name, int oflag); int read(int pipe_fd, void *area, int cnt); int write(int pipe_fd, void *area, int cnt); int close(int pipe_fd);, || - ||, O_NDELAY. || || , , || , || , |,| , || , - read write || .
|',| ', || , , - :
int unlink(char *name);