执着的贝壳 发表于 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~~~~~~~~~~~~
怎么回事呀?


执着的贝壳 发表于 2012-09-27 19:57

有谁知道的讲解一下啊:'(

junze_tianjian 发表于 2012-09-28 16:11

进程(线程)启动都需要时间, 你的循环次数太少了,执行的太快了。改成几百个循环的次数试试。

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

will122 发表于 2012-10-07 17:39

一般情况下不做处理的话,无论是进程还是线程,执行次序都是不确定的。。fork后到底是先执行父进程还是先执行子进程,天晓得,呵呵!

_Rayx 发表于 2012-10-08 09:21

子进程父进程哪个先调度都有可能的

梦醒潇湘love 发表于 2012-12-28 11:21

线程并发执行,貌似顺序不确定啊。。
页: [1]
查看完整版本: 进程线程的问题