初学者关于多线程写文件的问题

sayaza 2006-03-23 08:50:35
初学多线程,写一个小程序想以多线程复制文件,但是不行,请教原因。


#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <winbase.h>
#include <process.h>
#include <sys/types.h>
#include <sys/stat.h>

typedef struct
{
FILE *filein;
FILE *fileout;
int pos;
int size;
}
PARAMS,*PPARAMS;

void copythread(PVOID pvoid);
CRITICAL_SECTION cs;

int main(void)
{
int i,ret;
int filesize,partsize,position,redundant;
FILE *input,*output;
struct _stat statbuf;
static PARAMS params;
unsigned long hthread;



input=fopen("aaa.txt","rb");
if(input==NULL)
return 1;

output=fopen("bbb.txt","wb");
if(input==NULL)
return 1;

ret=_fstat(_fileno(input),&statbuf);
if(ret!=0)
return 1;

filesize=statbuf.st_size;
partsize=filesize/5;
redundant=filesize%5;

InitializeCriticalSection(&cs);
for(i=0;i<5;i++)
{

position=partsize*i;
params.filein=input;
params.fileout=output;
params.pos=position;
if(i==4)
params.size=partsize+redundant;
else
params.size=partsize;

hthread=_beginthread(copythread,0,¶ms);
}

DeleteCriticalSection(&cs);
fclose(input);
fclose(output);

return 0;
}


void copythread(PVOID pvoid)
{
volatile PPARAMS pparams;
char *buf;
int pos,size,ret;
FILE *input,*output;


EnterCriticalSection(&cs);
pparams=(PPARAMS)pvoid;


size=pparams->size;
pos=pparams->pos;
input=pparams->filein;
output=pparams->fileout;
LeaveCriticalSection(&cs);

buf=(char *)malloc(size);
memset(buf,0,sizeof(buf));

ret=fseek(output,pos,SEEK_SET);

fread(buf,size,1,input);
fwrite(buf,size,1,output);


free(buf);
_endthread();
}

...全文
353 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
sayaza 2006-03-26
  • 打赏
  • 举报
回复
谢谢,我运行了代码并用了WaitForMultiObject(),但仍然有问题。写出的文件每次都不同,时好时坏。问题在哪里呢?

#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <winbase.h>
#include <process.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <process.h>

FILE *filein;
FILE *fileout;
int n_thread = 0;
int partsize = 0;
int redundant = 0;
void copythread(PVOID pvoid);
CRITICAL_SECTION cs;


int main(void)
{
int i, ret;
struct _stat statbuf;
HANDLE threadid[5];

if((filein = fopen("aaa.txt","rb")) == NULL)
return 1;

if((fileout = fopen("bbb.txt","wb")) == NULL)
return 1;

ret=_fstat( _fileno(filein), &statbuf);
if(ret!=0)
return 1;

int filesize = statbuf.st_size;
partsize = filesize / 5;
redundant = filesize % 5;

InitializeCriticalSection(&cs);
for(i=0; i<5 ;i++)
{
printf("%d\n", threadid[i]=(HANDLE)_beginthread(copythread, 0, 0));
}

WaitForMultipleObjects(5,threadid,true,INFINITE);

DeleteCriticalSection(&cs);
fclose(filein);
fclose(fileout);

return 0;
}


void copythread(PVOID pvoid)
{
char *buf;
int pos,size,ret;
FILE *input,*output;

input = filein;
output = fileout;

EnterCriticalSection(&cs);
if(n_thread == 4) size = partsize + redundant;
else size = partsize;
pos = n_thread * partsize;
n_thread++;
LeaveCriticalSection(&cs);

printf("size = %d, pos = %d\n", size, pos);

buf = (char *)malloc(size);
memset(buf,0,sizeof(buf));
ret = fseek(output,pos,SEEK_SET);
fread(buf,size,1,input);
fwrite(buf,size,1,output);


free(buf);
_endthread();

}
zengwujun 2006-03-25
  • 打赏
  • 举报
回复
主线程等待你要这个函数:WaitForMultipleObjectsEx.

我只是简单实现你的代码,你自己看看了
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <winbase.h>
#include <process.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <process.h>

FILE *filein;
FILE *fileout;
int n_thread = 0;
int partsize = 0;
int redundant = 0;
void copythread(PVOID pvoid);
CRITICAL_SECTION cs;


int main(void)
{
int i, ret;
struct _stat statbuf;
// unsigned long hthread;

if((filein = fopen("aaa.txt","rb")) == NULL)
return 1;

if((fileout = fopen("bbb.txt","wb")) == NULL)
return 1;

ret=_fstat( _fileno(filein), &statbuf);
if(ret!=0)
return 1;

int filesize = statbuf.st_size;
partsize = filesize / 5;
redundant = filesize % 5;

InitializeCriticalSection(&cs);
for(i=0; i<5 ;i++)
{
printf("%d\n", _beginthread(copythread, 0, 0));
}

Sleep(5000);
DeleteCriticalSection(&cs);
fclose(filein);
fclose(fileout);

return 0;
}


void copythread(PVOID pvoid)
{
char *buf;
int pos,size,ret;
FILE *input,*output;

input = filein;
output = fileout;

EnterCriticalSection(&cs);
if(n_thread == 4) size = partsize + redundant;
else size = partsize;
pos = n_thread * size;
n_thread++;
LeaveCriticalSection(&cs);

printf("size = %d, pos = %d\n", size, pos);

buf = (char *)malloc(size);
memset(buf,0,sizeof(buf));
ret = fseek(output,pos,SEEK_SET);
fread(buf,size,1,input);
fwrite(buf,size,1,output);


free(buf);
_endthread();

}


zez 2006-03-25
  • 打赏
  • 举报
回复
自己看一下 同步与 互斥 相关的知识
sayaza 2006-03-24
  • 打赏
  • 举报
回复
十分感谢,想问怎么做能让所有子线程都得到正确的参数呢?
zengwujun 2006-03-24
  • 打赏
  • 举报
回复
你的程序存在几个问题:

(1)你开了6个线程,一个主线程,5个子线程.

但你没有线程的同步机制,你没法保证子线程能够得到运行的机会.
也许你的主线程结束了,然而没有一个子线程得到运行的机会,而子线程
随着主线程的结束而结束了,这样文件bbb就没有写入.
不知道你的子线程有几个可以得到调度运行,而你的主线程是如此快,子线程
一个也得不到运行的机会很大.

(2)你没法保证子线程的params参数能得到5个不同的值,也许在主线程的i=3时,
子线程的好几个依次得到了调度,那么他们的param的值都是一样的.

要解决几个问题,首先你要让主线程等待5个子线程全部结束,其次是子线程怎么样
才能得到正确的参数
sayaza 2006-03-24
  • 打赏
  • 举报
回复
anybody helps?

69,373

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧