Unix process api, Operating System

Assignment Help:

Unix process API

The two most important function calls to use when programming with several processes are fork and exec:

 fork() creates a copy of current process. It gives a different return value to each process and works based on Copy On Write;

 exec() replaces a process with an executable.

(The Windows CreateProcess(...), taking ten arguments, is analogous.)

Notice that fork() implies that each process descends from another process. In fact, in Unix everything descends from a single process called init: basically, init forks a process and then "replaces its code" with, say, the code of bash, using exec().

Example of how to use fork:
#include
#include
#include
int parentid = getpid();
char program_name[1024];
gets(program_name); // reads the name of program we want to start
int cid = fork();
if (cid==0) { // i'm the child
execlp(program_name, program_name, 0); // loads the program and runs it
printf("if the above worked, this line will never be reached\n");
}
else { // i'm the parent
sleep (1); // give my child time to start
waitpid(cid, 0, 0); // waits for my child to terminate
print("program %s finished\n", program_name);
}
Is the sleep(1) call necessary to allow the child process to start? The answer is no, it is not at all necessary. In general, if you think you need to sleep in a program, you are probably doing something wrong, and just slowing down your program. The call to waitpid() is a blocking wait, and will ?rst wait to let the child process start (if it hasn't already), then will wait until it ends.


Related Discussions:- Unix process api

What is a latency time, What is a Latency Time Latency Time is the time...

What is a Latency Time Latency Time is the time required through a sector to reach below read/write head.

Explain fixed partitioning techniques, FIXED PARTITIONING In a fixed p...

FIXED PARTITIONING In a fixed partitioning, the operating system inhabiting some fixed portion of main memory is available for use of multiple processes -Using fixed partit

Define instance, Define Instance. An instance is an administrative unit...

Define Instance. An instance is an administrative unit in which components of R/3 systems giving one or more services are grouped together.  The services offered by a case are

What are the allocation methods of a disk space, What are the allocation me...

What are the allocation methods of a disk space? Methods of allocating disk space which are widely in use are a. Contiguous allocation b. Linked allocation c. Indexed

Reference counting, The idea of reference counting is to maintain, for ever...

The idea of reference counting is to maintain, for every object, the total number of references to that object, i.e. the number of "incoming" pointers. Whenever the number of refer

What is the basic approach of page replacement, What is the basic approach ...

What is the basic approach of page replacement? If no frame is free is available, find one that is not presently being used and free it. A frame can be freed by writing its con

What is critical section problem, What is critical section problem? Con...

What is critical section problem? Consider a system consists of 'n' processes. Every process has segment of code called a critical section, in which the process might be changi

Implementation of modern memory management system , Central to implementati...

Central to implementation of a modern memory management system is the page replacement algorithm. Modern virtual memory systems break memory up into pages and map (via a page table

Choice of operating systems to boot from, Q. How could a system be designed...

Q. How could a system be designed to allow a choice of operating systems to boot from? What would the bootstrap program need to do? Answer: Delieve a system that would like to

Financial objective of corporation, The primary financial objective of corp...

The primary financial objective of corporation is usually taken to be the maximization of shareholder wealth. Discuss:- i. Ways in which the shareholder of a company can encour

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd