Execution Environment: Linux ubuntu 4.4.0-31-generic #50-ubuntu SMP Wed Jul 00:07:12 UTC x86_64 x86_64 x86_64 gnu/linux
1. Test code: A.C
1#include <fcntl.h>2#include <unistd.h>3#include <stdio.h>4#include <pthread.h>5#include <string.h>6 7 pthread_t Ntid;8 9 void* THR_FN (void*Arg)Ten { One int* fp = (int*) arg; A Char* str ="Hello the world!\n"; - - intLen = Write (*Fp,str,strlen (str)); the if(len) - { -printf"Thread Write a string to the file by the descriptor the CreateProcess created!\n"); - } + -Close (*FP); + return((void*)0); A } at - intMainvoid) - { - interr; - intFD; - inFD = open ("Log.txt", o_creat|o_wronly); - if(-1==FD) to { +printf"Failed to open file!\n"); - } the *Err = Pthread_create (&ntid,NULL,thr_fn,&FD); $ if(Err! =0)Panax Notoginseng { -printf"can ' t create thread, errno =%d\n", err); the } +Sleep3); A return 0; the}
2. If the system does not have a corresponding Pthread library, execute:
1 sudo apt-get install glibc-doc
2 sudo apt-get install Manpages-posix-dev
Compile run:
1 gcc a.c-lpthread
Unix thread sharing Create process Open file resource (1)