追答:q1012164262,楼主说的是系统调用,不是标准文件流,谭浩强的是标准文件流,推荐文件流方式读写文件比较合理,避免多频率使用系统资源(内核态)
追答:写了个简单点的,可以运行(在ubuntu下运行的):
ps. 程序中没有用系统调用read,仅用了write写;
#include
#include
#include
#include
#include
#include
#include
#include
#define NUM_THREADS 4
void *thread_function(void *arg)
{
int fd;
int thread_number = (int)arg;
char *buff;
buff = (char *)malloc(sizeof(char) * 128);
if(!buff)
{
exit(EXIT_FAILURE);
}
fd = open("test.c", O_RDWR | O_CREAT, 0777);
if(fd < 0)
{
fprintf(stderr, "Open test.c failed.");
exit(EXIT_FAILURE);
}
if(thread_number == 0)
{
sprintf(buff,"pthread_number_0");
printf("thread_0 running. Argument was %d\n",thread_number);
lseek(fd, 0, SEEK_SET);
write(fd, buff, strlen(buff));
buff = NULL;
}
#if 1
if(thread_number == 1)
{
sprintf(buff,"pthread_number_1");
printf("thread_1 is running. Argument was %d\n",thread_number);
lseek(fd, 0, SEEK_END);
write(fd,buff, strlen(buff));
buff = NULL;
}
if(thread_number == 2)
{
sprintf(buff, "pthread_number_2");
printf("thread_2 is running. Argument was %d\n", thread_number);
lseek(fd, 0, SEEK_END);
write(fd, buff, strlen(buff));
buff = NULL;
}
if(thread_number == 3)
{
sprintf(buff, "thread_number_3");
printf("thread_3 is running. Argument was %d\n", thread_number);
lseek(fd, 0, SEEK_END);
write(fd, buff, strlen(buff));
buff = NULL;
}
#endif
close(fd);
}
int main()
{
pthread_t a_thread[NUM_THREADS];
int res;
int lots_of_threads;
for(lots_of_threads = 0; lots_of_threads < NUM_THREADS; lots_of_threads++) {
res = pthread_create(&(a_thread[lots_of_threads]), NULL, thread_function,(void *)lots_of_threads);
if(res != 0) {
fprintf(stderr, "Thread creation failed.");
exit(EXIT_FAILURE);
}
usleep(1000 * 2000);
}
printf("All thread finish......\n");
}
追答:头文件给挂了?
#include
#include
#include
#include
#include
#include
#include
#include
#define NUM_THREADS 4
void *thread_function(void *arg)
{
int fd;
int thread_number = (int)arg;
char *buff;
buff = (char *)malloc(sizeof(char) * 128);
if(!buff)
{
exit(EXIT_FAILURE);
}
fd = open("test.c", O_RDWR | O_CREAT, 0777);
if(fd < 0)
{
fprintf(stderr, "Open test.c failed.");
exit(EXIT_FAILURE);
}
if(thread_number == 0)
{
sprintf(buff,"pthread_number_0");
printf("thread_0 running. Argument was %d\n",thread_number);
lseek(fd, 0, SEEK_SET);
write(fd, buff, strlen(buff));
buff = NULL;
}
#if 1
if(thread_number == 1)
{
sprintf(buff,"pthread_number_1");
printf("thread_1 is running. Argument was %d\n",thread_number);
lseek(fd, 0, SEEK_END);
write(fd,buff, strlen(buff));
buff = NULL;
}
if(thread_number == 2)
{
sprintf(buff, "pthread_number_2");
printf("thread_2 is running. Argument was %d\n", thread_number);
lseek(fd, 0, SEEK_END);
write(fd, buff, strlen(buff));
buff = NULL;
}
if(thread_number == 3)
{
sprintf(buff, "thread_number_3");
printf("thread_3 is running. Argument was %d\n", thread_number);
lseek(fd, 0, SEEK_END);
write(fd, buff, strlen(buff));
buff = NULL;
}
#endif
close(fd);
}
int main()
{
pthread_t a_thread[NUM_THREADS];
int res;
int lots_of_threads;
for(lots_of_threads = 0; lots_of_threads < NUM_THREADS; lots_of_threads++) {
res = pthread_create(&(a_thread[lots_of_threads]), NULL, thread_function,(void *)lots_of_threads);
if(res != 0) {
fprintf(stderr, "Thread creation failed.");
exit(EXIT_FAILURE);
}
usleep(1000 * 2000);
}
printf("All thread finish......\n");
}
追答:头文件有:
unistd.h stdlib.h stdio.h fcntl.h sys/types.h sys/stat.h pthread.h string.h
编译:
gcc -o pthread pthread.c -lpthread