- 论坛徽章:
- 0
|
验证了一下,还是那个仁兄的方法高明啊~~哈哈
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <pthread.h>
#include <errno.h>
#include <unistd.h>
#include <stdio.h>
char *file[] = {"./a","./b","./c","./d"};
void*thread_func(void*arg)
{
int num, fd[4], i;
char buf[4];
num = *(int*)arg;
free(arg);
memset(buf, 0, sizeof(buf));
sprintf(buf, "%d", num+1);
for (i=0; i<4; i++) {
fd[i] = open(file[i],O_WRONLY);
lseek(fd, (num+(4-i))%4, SEEK_CUR);
write(fd[i], (void*)buf, strlen(buf));
}
while (1) {
for (i=0; i<4; i++) {
lseek(fd[i], 3, SEEK_CUR);
write(fd[i], (void*)buf, strlen(buf));
}
}
pthread_exit(0);
}
int main()
{
int i, *num, fd;
for (i=0; i<4; i++) {
fd = open(file[i], O_CREAT | O_WRONLY, S_IRUSR|S_IWUSR);
close(fd);
}
for (i=0; i<4; i++) {
num =(int*)malloc(sizeof(int));
*num = i;
pthread_create(NULL, NULL, thread_func, num);
}
pause();
} |
[ 本帖最后由 wuxiangzhi 于 2007-10-27 11:05 编辑 ] |
|