免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 45445 | 回复: 99
打印 上一主题 下一主题

新鲜出炉Google面试题一道(多线程) [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-10-26 14:53 |只看该作者 |倒序浏览
有四个线程1,2,3,4,线程1的功能就是输出1,线程2的功能就是输出2,以此类推.........
现在有四个文件.ABCD.初始都为空.
现要让四个文件呈如下格式:
A:  1 2 3 4 1 2....
B:  2 3 4 1 2 3....
C:  3 4 1 2 3 4....
D:  4 1 2 3 4 1....

设计程序.

论坛徽章:
0
2 [报告]
发表于 2007-10-26 14:55 |只看该作者
简单实现的话,很容易编码
考虑效率的话,就要话点心思设计调度算法

[ 本帖最后由 wuxiangzhi 于 2007-10-26 14:56 编辑 ]

论坛徽章:
0
3 [报告]
发表于 2007-10-26 14:56 |只看该作者
原帖由 wuxiangzhi 于 2007-10-26 14:55 发表
加锁
if(具备条件)
   写之


虽然这题不是很难,不过这个基本等于没答...

论坛徽章:
0
4 [报告]
发表于 2007-10-26 14:59 |只看该作者
我想这个题应该不能让线程有阻塞。

论坛徽章:
0
5 [报告]
发表于 2007-10-26 16:23 |只看该作者
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <pthread.h>
#include <errno.h>
#include <unistd.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>

typedef struct file_node{
        int             r_pipe;
        int             w_pipe;
        int             fd;
}file_node;

char *file[] = {"./a","./b","./c","./d"};

file_node  node_list[4];

void *thread_func(void *arg);

int main()
{
        int     *num, i;
        int     sockpair[2];

        memset(node_list, 0, sizeof(node_list));

        
//打开文件,构建线程间管道

        for (i=0; i<4; i++) {

                if (socketpair(AF_UNIX,SOCK_STREAM, 0, sockpair)) {
                        printf("socketpair error !\n");
                        exit(1);
                }

                node_list[i].w_pipe = sockpair[0];

                if (i != 3)
                        node_list[i+1].r_pipe = sockpair[1];
                else
                        node_list[0].r_pipe = sockpair[1];

                node_list[i].fd = open(file[i], O_CREAT | O_WRONLY| O_APPEND, S_IRUSR|S_IWUSR);
                if (node_list[i].fd < 0) {
                        printf("open fd error , exit !\n");
                        exit(1);
                }
        }
        
//创建线程      

        for(i=0;i<4; i++) {
                num =(int*)malloc(sizeof(int));
                *num = i;
                pthread_create(NULL, NULL, thread_func, num);
        }

        pause();
}

void *thread_func(void *arg)
{
        pthread_t          pid;
        char               buf[100];
        int                w_fd, num;

        num = *(int*)arg;
        free(arg);
        pid = pthread_self();
        pthread_detach(pid);

        sprintf(buf, "%d", num+1);
        w_fd = node_list[num].fd;


        for (; ; )
        {
                        
//写文件

                if (write(w_fd, (void*)buf, strlen(buf)) != strlen(buf)) {
                        printf("write something to file error !\n");
                        break;
                }
               
//通知下一个线程写      

                if (write(node_list[num].w_pipe, (void*)&w_fd, sizeof(int)) != sizeof(int)) {
                        printf("write fd to pipe error !\n");
                        break;
                }
                if (read(node_list[num].r_pipe, &w_fd, sizeof(int)) != sizeof(int)) {
                        printf("read fd from pipe error !\n");
                        break;
                }
        }

        pthread_exit(0);
}


[ 本帖最后由 wuxiangzhi 于 2007-10-31 14:31 编辑 ]

论坛徽章:
0
6 [报告]
发表于 2007-10-26 16:26 |只看该作者
随便写了,效率可能不高,需要优化

论坛徽章:
0
7 [报告]
发表于 2007-10-26 17:17 |只看该作者
赞一下,看来google对算法的要求还是很高的

论坛徽章:
0
8 [报告]
发表于 2007-10-26 19:57 |只看该作者
原帖由 xiaozhu2007 于 2007-10-26 17:17 发表
赞一下,看来google对算法的要求还是很高的

那是相当高
DaYuTou 该用户已被删除
9 [报告]
发表于 2007-10-26 20:26 |只看该作者
提示: 作者被禁止或删除 内容自动屏蔽

论坛徽章:
0
10 [报告]
发表于 2007-10-26 20:30 |只看该作者
原帖由 DaYuTou 于 2007-10-26 20:26 发表
大家有没有想到lseek,就是每次写完文件后用lseek跳到3个字节之后再写文件,这样就不需要线程间的同步

貌似不能往lseek形成的空洞里写东西的吧~~
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP