linux多线程读写文件夹,Linux   C语言多线程实例读写文件,求代码及思想

追答: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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值