Unix process api, Operating System

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.

Posted Date: 3/12/2013 4:12:42 AM | Location : United States







Related Discussions:- Unix process api, Assignment Help, Ask Question on Unix process api, Get Answer, Expert's Help, Unix process api Discussions

Write discussion on Unix process api
Your posts are moderated
Related Questions
SSTF stands for Abbreviation of SSTF is Shortest-Seek-time-first scheduling

. A paper mill produces two grades of paper viz., X and Y. Because of raw material restrictions, it cannot produce more than 400 tons of grade X paper and 300 tons of grade Y paper

What is parsing Parsing is the procedure of analyzing a text, made of a sequence of tokens, to define its grammatical structure regarding to a given formal grammar. Parsing is

how to find ncr value in shell script

Differentiate between sequential access and direct access method?

when demand is 24000 units/year, production rate is 48000 units/year, setup cost is rs 200 per setup, carring cost is rs 20 per units/year, and economic batch quantity is 692.8203

Essential Properties of Operating System 1. Batch: Jobs with similar needs are batched together and run through the computer as a group by an operator or automatic job sequen

The dynamic memory allocator is a layer between the application and the OS, managing heap objects. When a program requests memory from the allocator (via malloc(), for instance), t

Explain the Network protocols There are a wide variety of network protocols to choose from traditionally client - server developers were required to choose a communications pro

What are the methods for handling deadlocks? The deadlock problem can be dealt with in one of the three ways: a. Use a protocol to prevent or avoid deadlocks, ensuring that