When a program is run from the shell using ./a.out, the shell uses fork() to create a child process. The child process executes ./a.out while the parent shell process waits to collect the return status.
During a write system call, data is first copied from the user space into a temporary kernel space buffer. The kernel then copies the data from the buffer to the file located in disk space. This buffering helps synchronize the faster CPU with slower disk write speeds.
At the kernel level, processes, files, and other resources are identified by numeric identifiers. Data structures like the file descriptor table and inode table map these numeric IDs to the corresponding objects and metadata.
1 of 3
Download to read offline
More Related Content
Concepts unix process
1. 1) What happens when ./a.out is called from shell ?
It is fact that a process is always invoked by process itself(via fork() )
Initially we are in shell prompt.As we invoke ./a.out then what happens is :
shell internally forks implicitly.And child process is now responsible to
execute ./a.out
Parent process that is nothing but shell is waiting to collect return status
of child process.
So,What happens is the executable(./a.out) is copied (via exec ) in process
address space of child process.
./a.out
shell -> fork()
if( pid == 0)
{
// child process execute ./a.out
}
else
{
Wait(0);
// Parent is waiting for child to complete the process and collecting the
return status.
}
2) What Acutually happens in write system call ?
Here we have three working space :
=> User-Space
=> Kernel-Space
=> Disk-Space
so Assume scenario: char str[15] = "Hello";
fd = open("filename",o_WRONLY,S_IRWX);
write(fd,str,sizeof(str),1); // jst check syntax
So, Exact scene is like that :
================================================================================
================
User Space char str[15]
="HEllo";
|
====================================================================|
============================
# All system calls are invoked in kernel space only |
V-------------------
| |
| Temperory Buffer | |
|
| |-------------------|
|
2. |
Kernel Space Write call here
|
=====================================================================|
============================
|
finally written in file with fd descriptor:
V
Disk Space ( All permanent
storage like files
are
here only )
================================================================================
==================
So, str is first copied into temperory buffer in kernel space after that write
system call invoked then it copies that buffer
into file which is in Disk Space. ( Temperory Buffer is required because cpu
processing speed is quite faster that disk operation like writing and reading ..
so in order to synchronise disk opeartion speed the data is temperorily copied
in buffer.
################################################################################
###############################################3
Note:
Kernel keeps or recognize each and every entity whether process,thread,files,etc
by numeric number.
So,At kernal level we have only numeric information. So,There must be some
mapping data structures:
Exactly true: Data Structures at Kernel Level are :=>
===> File Descriptor Table or Proces Control Block(PCB)
Having information about file decriptor and corresponding file pointers
====> V-table
===> Inode-Table
Having complete information about file with it's inode number.
====> Directory Inode Table( Directory table)
Having information about file and it's inode number
e===>(1)
################################################################################
################################################
================================================================================
================================================
Note===>(2)
Process should not enter into address space of each other, otherwise it can
corrupt each -other segment. It can be deliberately allowed in case of shared
memory scenario.
3. The child process copies the exact copy of parent adresss space which was there
before fork(). After that both works in there independent address space. Copy of
address space include complete copies ( all stack,data segment,heap etc, file
descriptors,..)
================================================================================
================================================