免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1984 | 回复: 5

[网络] 进程线程的问题 [复制链接]

论坛徽章:
0
发表于 2012-09-27 19:50 |显示全部楼层
现在有一个父进程和一个子进程,在父进程和子进程中又简单定义了线程。
#include<stdio.h>
#include<unistd.h>
#include<pthread.h>

void thread_one()
{
        int i = 0;
        for (i = 0; i < 3; i++)
        {
                printf("This is the first thread\n");
        }
}

void thread_two()
{
        int i = 0;
        for (i = 0; i < 3; i++)
        {
                printf("This is the second thread\n");
        }
}

int main()
{
        pid_t pid;
        pthread_t id;
        int ret = 0;
        int i = 0;
        pid = fork();

        if (pid > 0)
        {
                printf("This is the parent,pid = %d\n", pid);
                ret = pthread_create(&id, NULL, (void *)thread_one, NULL);
                printf("~~~~~~~~parent~~~~~~~~~~~~~~~~`\n");
                if (ret != 0) {
                        printf("thread err!\n");
                        return -1;
                }
                for (i = 0; i < 5; i++)
                {
                        printf("This is the parent main!\n");
                }
                pthread_join(id, NULL);
        }
        else if (pid == 0)
                {
                        printf("This is the child,pid = %d\n", pid);
                        ret = pthread_create(&id, NULL, (void *)thread_two, NULL);
                        printf("!!!!!!!!!!child!!!!!!!!!!!!!!!\n");
                        if (ret != 0) {
                                printf("thread err!\n");
                                return -1;
                        }
                for (i = 0; i < 5; i++)
                {
                        printf("This is the child main!\n");
                }
                pthread_join(id, NULL);
                }
        else {
                printf("fork err!\n");
                return -1;
        }
        printf("~~~~~~~~~~~over~~~~~~~~~~~~~\n");
        return 0;
}
但是执行后 结果却是多样的
root@brook-desktop:~/Desktop/ceshi# ./fock
This is the parent,pid = 703
~~~~~~~~parent~~~~~~~~~~~~~~~~`
This is the child,pid = 0
This is the parent main!
This is the parent main!
This is the parent main!
This is the parent main!
This is the parent main!
This is the first thread
This is the first thread
This is the first thread
~~~~~~~~~~~over~~~~~~~~~~~~~
!!!!!!!!!!child!!!!!!!!!!!!!!!
This is the child main!
This is the child main!
This is the child main!
This is the child main!
This is the child main!
This is the second thread
This is the second thread
This is the second thread
~~~~~~~~~~~over~~~~~~~~~~~~~





This is the parent,pid = 710
This is the child,pid = 0
~~~~~~~~parent~~~~~~~~~~~~~~~~`
This is the parent main!
This is the parent main!
This is the parent main!
This is the parent main!
This is the parent main!
This is the first thread
This is the first thread
This is the first thread
~~~~~~~~~~~over~~~~~~~~~~~~~
!!!!!!!!!!child!!!!!!!!!!!!!!!
This is the child main!
This is the child main!
This is the child main!
This is the child main!
This is the child main!
This is the second thread
This is the second thread
This is the second thread
~~~~~~~~~~~over~~~~~~~~~~~~~



在执行父进程的线程时会被子进程打断么?进程和线程是不是有优先级啊? 这个log该怎么理解呢?
如果我想老老实实的父进程-》线程  子进程-》线程  是不是要上锁?
有一段log还是这样的
root@brook-desktop:~/Desktop/ceshi# ./fock
This is the parent,pid = 736
~~~~~~~~parent~~~~~~~~~~~~~~~~`
This is the parent main!
This is the parent main!
This is the parent main!
This is the parent main!
This is the parent main!
This is the first thread
This is the first thread
This is the first thread
~~~~~~~~~~~over~~~~~~~~~~~~~
root@brook-desktop:~/Desktop/ceshi# This is the child,pid = 0
!!!!!!!!!!child!!!!!!!!!!!!!!!
This is the child main!
This is the child main!
This is the child main!
This is the child main!
This is the child main!
This is the second thread
This is the second thread
This is the second thread
~~~~~~~~~~~over~~~~~~~~~~~~
怎么回事呀?


论坛徽章:
0
发表于 2012-09-27 19:57 |显示全部楼层
有谁知道的讲解一下啊

论坛徽章:
0
发表于 2012-09-28 16:11 |显示全部楼层
进程(线程)启动都需要时间, 你的循环次数太少了,执行的太快了。改成几百个循环的次数试试。

不过,到可以研究下, 当fork()后,先调度的哪个进程(是父还是子)...

论坛徽章:
0
发表于 2012-10-07 17:39 |显示全部楼层
一般情况下不做处理的话,无论是进程还是线程,执行次序都是不确定的。。fork后到底是先执行父进程还是先执行子进程,天晓得,呵呵!

论坛徽章:
0
发表于 2012-10-08 09:21 |显示全部楼层
子进程父进程哪个先调度都有可能的

论坛徽章:
0
发表于 2012-12-28 11:21 |显示全部楼层
线程并发执行,貌似顺序不确定啊。。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP