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

Advanced synchronization, Synchronization serves two purposes: 1) to ensure...

Synchronization serves two purposes: 1) to ensure safety for updates on shared data (e.g. to avoid races conditions), and 2) to coordinate and order actions taken by threads (e.g.

What are the issues in designing a network, Normal 0 false fa...

Normal 0 false false false EN-US X-NONE X-NONE MicrosoftInternetExplorer4

Explai basic concepts of demand paging, Basic concepts When a process i...

Basic concepts When a process is to be changed in, the pager guesses which pages will be used before the process is changed out again. Instead of swapping in a entire process,

Crank-nicolson timestepping schemes, For the heat conduction problem, inves...

For the heat conduction problem, investigate the effects on the numerical solution of the following aspects: 1. non-uniform meshes with re?nement at both ends versus a uniform m

Cpu, what is cpu

what is cpu

Define lru page replacement policy, Define ‘LRU’ page replacement policy ...

Define ‘LRU’ page replacement policy LRU is Least Recently Used page replacement policy.

How free-space is managed using bit vector implementation, How free-space i...

How free-space is managed using bit vector implementation? The free-space list is implemented as a bit map or bit vector. Each block is shown by 1 bit. If the block is free, th

User threads control dispatching, Consider a particular system that does no...

Consider a particular system that does not have an interrupting clock.  The only way a thread can lose the processor is to voluntarily surrender it.  Additionally, the operating sy

Load sharing , Load Sharing : Processes are not given to a particular proc...

Load Sharing : Processes are not given to a particular processor. A global queue of threads is used to maintain. Each processor, when idle, picks a thread from this queue. Note th

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