0

I've the following code:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <sys/types.h>
#include <sys/wait.h>
#define CHILD_NO 2
#define THREAD_NO 2
int counter = 0;
void *func(void *thread_no) {
    counter = counter + 1;
    printf("Func:  Proces ID = %d Parent process ID = %d Thread no = %d, Counter = %d \n", getpid(), getppid(), thread_no, counter);
    pthread_exit(NULL);
}

int main(void) {
    pthread_t threads[THREAD_NO];
    int childs[CHILD_NO];
    int rc, i, j;
    int f = 1;
    printf("Main: Proces ID = %d Parent process ID: %d\n", getpid(), getppid());
    for (i = 0; i < CHILD_NO; i++) {
        if (f > 0)
            f = fork();
        if (f == 0)
            break;
        else
            childs[i] = f;
    }
    if (f == 0) {
        printf("Child: Proces ID: %d Parent process ID: %d counter:%d\n" , getpid(), getppid(), counter);
        /*for(j = 0; j < THREAD_NO; j++)
            rc = pthread_create(&threads[j], NULL, func, (void *)j);*/
    }
    else{
        for (i = 0; i < CHILD_NO; i++)
            printf("Main: Child[%d] ID: %d created.\n", i, childs[i]);
        wait(NULL);
    }
    pthread_exit(0);
    return 0;
}

If I run it with thread part commented out I get the following output:

Main: Proces ID = 31138 Parent process ID: 28446
Child: Proces ID: 31139 Parent process ID: 31138 counter:0
Child: Proces ID: 31140 Parent process ID: 31138 counter:0
Main: Child[0] ID: 31139 created.
Main: Child[1] ID: 31140 created.

Which is expected. Parent calls fork, child process hits break and exits loop.This happens for 2 times. So we have a parent process with 2 childs. That's all. So if parent process has PID, a child will have PID+1 and another will have PID+2.

If I remove the comments and run the code I get the following:

Main: Proces ID = 31664 Parent process ID: 28446
Child: Proces ID: 31665 Parent process ID: 31664 counter:0
Child: Proces ID: 31668 Parent process ID: 31664 counter:0
Main: Child[0] ID: 31665 created.
Main: Child[1] ID: 31668 created.
Func:  Proces ID = 31665 Parent process ID = 31664 Thread no = 1, Counter = 1 
Func:  Proces ID = 31665 Parent process ID = 31664 Thread no = 0, Counter = 2 
Func:  Proces ID = 31668 Parent process ID = 31664 Thread no = 1, Counter = 1 
Func:  Proces ID = 31668 Parent process ID = 31664 Thread no = 0, Counter = 2 

Now I understand the way how threads behave. It is not their outputs that confuse me.

How and why PID of child processes differ by 3? Shouldn't the tree hierarchy of processes stay the same?

SpiderRico
  • 1,890
  • 8
  • 28
  • 48
  • 3
    In Linux there's no real difference between a thread and a process, both are (ultimately) created using the [`clone`](http://man7.org/linux/man-pages/man2/clone.2.html) system call. – Some programmer dude Apr 03 '15 at 14:25
  • @JoachimPileborg So then I assume it works like this: Main creates first child process(PID+1) it then calls 2 threads (PID+2, PID+3) then back to main and create the other child process(PID+4). – SpiderRico Apr 03 '15 at 14:29
  • @JoachimPileborg Nevertheless, the tree for processes stay the same, rite? – SpiderRico Apr 03 '15 at 14:31
  • Basically yes, but you're not guaranteed to get consecutive pid values, as you may be preemted and another process may create new processes/threads inbetween. And yes, the process tree will be the same. – Some programmer dude Apr 03 '15 at 14:31
  • Yeh rite, I was trying to understand this particular output. – SpiderRico Apr 03 '15 at 14:33
  • @JoachimPileborg: There are very big differences between threads and processes. Your information is outdated. What would be accurate to say is that thread ids and process ids both exist in the same (numeric) namespace. But they're not the same. – R.. GitHub STOP HELPING ICE Apr 03 '15 at 15:02
  • Here's an answer I wrote on the topic: http://stackoverflow.com/questions/9154671/distinction-between-processes-and-threads-in-linux/9154725#9154725 – R.. GitHub STOP HELPING ICE Apr 03 '15 at 15:03
  • @R.. there are no threads and processes in linux kernel. Both are represented by the same structure `task_struct`. The only difference is that threads belonging to the same process share some resources, which depend on the `clone(2)` system call. But in the regular case (with posix threads, for example), they share memory mapping, file descriptors and many more. – holgac Apr 03 '15 at 15:45
  • @holgac: Yes there are. A process is what the kernel calls a "thread group" and it consists of all tasks (threads) sharing the same tgid. – R.. GitHub STOP HELPING ICE Apr 03 '15 at 16:14

0 Answers0