Domanda

How and when is the elements of 'task_struct' (process descriptor) initalized. Are they get initlized automatically after the fork() system call.

È stato utile?

Soluzione

Yes, "task_struct" gets initialized by the kernel once fork() system gets completed for the child process.

When program calls fork() system calls, kernel starts executing its internal main routine "do_fork". You can find the complete source code(fork.c) here

long do_fork(unsigned long clone_flags,
unsigned long stack_start,
struct pt_regs *regs,
unsigned long stack_size,
int __user *parent_tidptr,
int __user *child_tidptr)

Internally "do_fork" does executes the "copy_process" routine which is responsible for the initializing the "task_struct" of the child process. Its prototype has been defined as

static struct task_struct *copy_process(unsigned long clone_flags,
                                       unsigned long stack_start,
                                        unsigned long stack_size,
                                        int __user *child_tidptr,
                                        struct pid *pid,
                                        int trace)

It basically make a copy of the old task_struct and starts updating various attributes in it. So once fork() system calls gets completed, there would be a new "task_sturct" for the child process which stores all important information about a process which gets used by kernel at the various times while executing the process.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top