Sign in to follow this  
law_order

Questions about Processes and Files

Recommended Posts

law_order    122
Hi people! well,i have some questions regarding processes and files... it would be great if you could help me... 1)when we say that a process opens a file,we mean that the operating system finds the file on the disk,brings it to main memory and maps it into the process virtual address space? if yes,where is the file being "put"?(stack,heap etc) if the above is true,what happens when a second process wants to open the same file? my guess is,that the operating system sees that a copy of that file is already in memory and maps it also to the second process' adress space?is that true? 2)when we create a new process using the forc() system call,what parts of the data are being shared between the two processes? as far as i know,each process has its own stack and heap.what about global and static data? i'm looking forward to any replies. thanks a lot.

Share this post


Link to post
Share on other sites
Roboguy    794
Pretty much all of those depend on the OS, but most modern OSes are like this:
1) yes
1b) Heap, probably(heap is random access, while the stack is FIFO, IIRC)
1c) Probably, but, again, that depends on the OS

2) IIRC, no information is shared, fork creates a copy of the parent process(with a few minor differences, but they aren't big, for example, it'll have a new PID, of course). The way you'd use fork() is like this:


int main() {
int ret = fork();
if (ret == 0) {
// Child process code goes here
} else if (ret != -1) {
// Parent process code goes here
} else {
// Error has occured, which is stored in errno
}
}

Share this post


Link to post
Share on other sites
law_order    122
Thanks a lot roboguy.
you've been very helpful.

i'd like to ask some more questions.

ok.processes don't share anything.but what about threads?
ok...let me guess again..
threads share the same code,global and static data,but each one has its own stack and heap..
hm...right?

also...does each thread have its own virtual address space?

if threads share the same virtual address space and each one has its own stack,each stack is just a sub-space of the process'
stack?

sorry guys but i'm confused...

Share this post


Link to post
Share on other sites
Roboguy    794
1) yes, you also store the CPU state(or use setjmp and longjmp, which, I believe, essientally just set and load the CPU state too):
From the GeekOS code(GeekOS, GeekOS code on ViewCVS)

struct TSS {
// Link to nested task. For example, if an interrupt is handled
// by a task gate, the link field will contain the selector for
// the TSS of the interrupted task.
unsigned short link;
unsigned short reserved1;

// Stacks for privilege levels. esp0/ss0 specifies the kernel stack.
unsigned long esp0;
unsigned short ss0;
unsigned short reserved2;
unsigned long esp1;
unsigned short ss1;
unsigned short reserved3;
unsigned long esp2;
unsigned short ss2;
unsigned short reserved4;

// Page directory register.
unsigned long cr3;

// General processor registers.
unsigned long eip;
unsigned long eflags;
unsigned long eax;
unsigned long ecx;
unsigned long edx;
unsigned long ebx;
unsigned long esp;
unsigned long ebp;
unsigned long esi;
unsigned long edi;

// Segment registers and padding.
unsigned short es;
unsigned short reserved5;
unsigned short cs;
unsigned short reserved6;
unsigned short ss;
unsigned short reserved7;
unsigned short ds;
unsigned short reserved8;
unsigned short fs;
unsigned short reserved9;
unsigned short gs;
unsigned short reserved10;

// GDT selector for the LDT descriptor.
unsigned short ldt;
unsigned short reserved11;

// The debug trap bit causes a debug exception upon a switch
// to the task specified by this TSS.
unsigned int debugTrap : 1;
unsigned int reserved12 : 15;

// Offset in the TSS specifying where the io map is located.
unsigned short ioMapBase;
};








weather or not it has it's own virtual address space is probably OS dependent

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this